(1)一台或多台机器,操作系统CentOS 7.x-86_x64(2)硬件配置:内存2GB或2G+,CPU 2核或CPU 2核+;(3)集群内各个机器之间能相互通信;(4)集群内各个机器可以访问外网,需要拉取镜像;(5)禁止swap分区;
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="3b00e4d7-975f-4f19-a41d-c109074968ff"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.75.133"
NETMASK="255.255.255.0"
GATEWAY="192.168.75.2"
DNS1="8.8.8.8"
service network restart
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config #永久setenforce 0 #临时
sed -ri 's/.*swap.*/#&/' /etc/fstab #永久swapoff -a #临时
cat >> /etc/hosts << EOF
192.168.75.133 k8smaster
192.168.75.134 k8snode1
192.168.75.135 k8snode2
EOF
hostnamectl set-hostname k8smaster
hostnamectl set-hostname k8snode1
hostnamectl set-hostname k8snode2
cat >> /etc/sysctl.d/k8s.conf <
yum install ntpdate -y
或者
sysetmctl start chronyd && systemctl enable chronyd
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubelet-1.23.0 kubeadm-1.23.0 kubectl-1.23.0然后执行(开机启动)
systemctl enable kubelet
kubeadm init \--apiserver-advertise-address=192.168.75.133 \--image-repository registry.aliyuncs.com/google_containers \--kubernetes-version v1.23.0 \--service-cidr=10.96.0.0/12 \--pod-network-cidr=10.244.0.0/16 \--ignore-preflight-errors=all
–apiserver-advertise-address #集群通告地址(master 机器IP)
–image-repository #由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址
–kubernetes-version #K8s版本,与上面安装的一致
–service-cidr #集群内部虚拟网络,Pod统一访问入口
–pod-network-cidr #Pod网络,与下面部署的CNI网络组件yaml中保持一致
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown ( i d − u ) : (id -u):(id−u):(id -g) $HOME/.kube/configexport KUBECONFIG=/etc/kubernetes/admin.conf
kubeadm join 192.168.75.133:6443 --token 7gqt13.kncw9hg5085iwclx \
--discovery-token-ca-cert-hash sha256:66fbfcf18649a5841474c2dc4b9ff90c02fc05de0798ed690e1754437be35a01
kubeadm token create --print-join-command
wget https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
找到如下位置,修改Newwork 与执行kubeadm init 一致。
net-conf.json: |
{
“Network”: “10.244.0.0/16”,
“Backend”: {
“Type”: “vxlan”
}
}
kubectl apply -f kube-flannel.yml
kubectl get pods --all-namespaces
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploymentlabels:app: nginx
spec:replicas: 3selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx
kubectl apply -f nginx.yml
apiVersion: v1
kind: Service
metadata:name: nginx-service
spec:selector:app: nginxtype: NodePortports:- protocol: TCPport: 80targetPort: 80
kubectl apply -f nginx-service.yml
kubectl get pod,svc