1. 程式人生 > 其它 >centos8 kubernetes k8s v1.22.1 搭配containerd搭建叢集

centos8 kubernetes k8s v1.22.1 搭配containerd搭建叢集

系統環境配置

#關閉防火牆
systemctl stop firewalld
systemctl disable firewalld

#關閉selinux
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0

#關閉swap
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

#同步伺服器時間
timedatectl set-timezone Asia/Shanghai
yum install chrony -y 
systemctl enable chronyd 
systemctl start chronyd 
chronyc sources

#配置網路轉發
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1

net.ipv4.ip_forward = 1
EOF

#載入配置生效
modprobe br_netfilter 
sysctl -p /etc/sysctl.d/k8s.conf 

安裝containerd

wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install containerd -y

#建立目錄並生成配置檔案
mkdir -p /etc/containerd 
containerd config default > /etc/containerd/config.toml 
# 替換配置檔案,設定阿里雲加速倉庫,啟用systemd管理cgroup
sed -i "s#k8s.gcr.io#registry.cn-hangzhou.aliyuncs.com/google_containers#g"  /etc/containerd/config.toml 
sed -i '/containerd.runtimes.runc.options/a\ \ \ \ \ \ \ \ \ \ \ \ SystemdCgroup = true' /etc/containerd/config.toml 
sed -i "s#https://registry-1.docker.io#https://bncakk5o.mirror.aliyuncs.com#g"  /etc/containerd/config.toml 

#啟動containerd
systemctl daemon-reload 
systemctl enable containerd 
systemctl restart containerd 

安裝k8s

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=1
repo_gpgcheck=1
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.22.1 kubeadm-1.22.1 kubectl-1.22.1

#設定執行時
crictl config runtime-endpoint /run/containerd/containerd.sock

#設定為開機啟動kubelet
systemctl daemon-reload 
systemctl enable kubelet && systemctl restart kubelet

#初始化叢集
kubeadm init \
--apiserver-advertise-address=192.168.56.108 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.22.1 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.50.0.0/16 \
--cri-socket=

#初始化完成後會再末尾生成提示,按照下列提示操作,將kubeadm join複製到其他node節點執行即可。

To start using your cluster, you need to run the following as a regular user: 
 
  mkdir -p $HOME/.kube 
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config 
  sudo chown $(id -u):$(id -g) $HOME/.kube/config 
 
Alternatively, if you are the root user, you can run: 
 
  export KUBECONFIG=/etc/kubernetes/admin.conf 
 
You should now deploy a pod network to the cluster. 
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: 
  https://kubernetes.io/docs/concepts/cluster-administration/addons/ 
 
Then you can join any number of worker nodes by running the following on each as root: 
 
kubeadm join 192.168.56.108:6443 --token v5u33x.o9x2p89x2e5x659n \
	--discovery-token-ca-cert-hash sha256:c704c9c42840b0bf27f1c91681cc9dabb4d0e67ba2a0ac557009b9a31961a87b

其他node節點除了不用初始化init,其他配置均一致。

節點加入後,會處於notready狀態,是因為沒有安裝網路外掛,這裡安裝calico,執行下列命令即可

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml