使用containerd從0搭建k8s(kubernetes)叢集

2023-06-12 21:00:11

準備環境

準備兩臺伺服器節點,如果需要安裝虛擬機器器,可以參考《wmware和centos安裝過程》

機器名 IP 角色 CPU 記憶體
centos01 192.168.109.130 master 4核 2G
centos02 192.168.109.131 node 4核 2G
設定主機名,所有節點都執行
vim /etc/hosts
#增加
192.168.109.130 centos01
192.168.109.131 centos02
關閉防火牆,所有節點都執行
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
vim /etc/selinux/config
#修改SELINUX的值
SELINUX=disabled
關閉swap記憶體,所有節點都執行
swapoff -a
vim /etc/fstab
# 將該行註釋掉
#/dev/mapper/cs-swap swap
設定網橋,所有節點都執行

1.修改引數

vim /etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables 	= 1
net.ipv4.ip_forward 				= 1
vm.swappiness 						= 0

2.然後,載入如下兩個模組,所有節點都執行

modprobe ip_vs_rr
modprobe br_netfilter

3.生效設定

[root@centos01 opt]# sysctl -p
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness = 0

安裝containerd

以下步驟所有節點都執行。

安裝
wget https://github.com/containerd/containerd/releases/download/v1.7.2/containerd-1.7.2-linux-amd64.tar.gz
tar Cxzvf /usr/local containerd-1.7.2-linux-amd64.tar.gz
修改設定
mkdir /etc/containerd
containerd config default > /etc/containerd/config.toml
vim /etc/containerd/config.toml
#SystemdCgroup的值改為true
SystemdCgroup = true
#由於國內下載不到registry.k8s.io的映象,修改sandbox_image的值為:
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"
啟動服務
mkdir -p /usr/local/lib/systemd/system
wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
mv containerd.service /usr/local/lib/systemd/system
systemctl daemon-reload
systemctl enable --now containerd
驗證安裝
[root@centos01 opt]# ctr version
Client:
  Version:  v1.7.2
  Revision: 0cae528dd6cb557f7201036e9f43420650207b58
  Go version: go1.20.4

Server:
  Version:  v1.7.2
  Revision: 0cae528dd6cb557f7201036e9f43420650207b58
  UUID: 747cbf1b-17d4-4124-987a-203d8c72de7c

安裝runc

以下步驟所有節點都執行。

準備檔案
wget https://github.com//opencontainers/runc/releases/download/v1.1.7/runc.amd64
chmod +x runc.amd64
查詢containerd安裝時已安裝的runc所在的位置
[root@centos01 opt]# which runc
/usr/bin/runc
替換上一步的結果檔案
mv -f runc.amd64 /usr/bin/runc
驗證安裝
[root@centos01 opt]# runc -v
runc version 1.1.7
commit: v1.1.7-0-g860f061b
spec: 1.0.2-dev
go: go1.20.3
libseccomp: 2.5.4

安裝kubernetes

新增阿里雲的kubernetes源,所有節點都執行
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[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 kubeadm kubelet kubectl
開機自啟動,所有節點都執行
systemctl enable kubelet
驗證安裝,所有節點都執行
[root@centos01 opt]# kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.1", GitCommit:"4c9411232e10168d7b050c49a1b59f6df9d7ea4b", GitTreeState:"clean", BuildDate:"2023-04-14T13:20:04Z", GoVersion:"go1.20.3", Compiler:"gc", Platform:"linux/amd64"}
初始化叢集,僅在master(centos01)上執行
[root@centos01 opt]# kubeadm init --apiserver-advertise-address 192.168.109.130 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.27.1 --pod-network-cidr=10.244.0.0/16

...
...
...

Your Kubernetes control-plane has initialized successfully!

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.109.130:6443 --token osh87v.zvo010kamsr8esmp \
	--discovery-token-ca-cert-hash sha256:ff4607c7c194e9f756b1eb509e64d2d926b5f8f9556a85c3c14a2d25add28230

其中,
–apiserver-advertise-address:通告偵聽地址
–image-repository:指定映象地址使用阿里雲的,預設會使用谷歌映象
–kubernetes-version:指定當前的kubernetes的版本
–pod-network-cidr=10.244.0.0/16:flannel網路的固定地址範圍

仔細閱讀kubeadm init執行的結果,根據提示,還需要進行3步操作

1.筆者用的是root使用者,僅在master節點執行

vim /etc/profile
#在最後一行增加
export KUBECONFIG=/etc/kubernetes/admin.conf

生效環境變數

source /etc/profile

2.安裝網路外掛,可以選擇calico或flannel,這裡選擇安裝flannel,僅在master節點執行

下載安裝檔案

wget https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
vim kube-flannel.yml
#和--pod-network-cidr的一樣
"Network": "10.244.0.0/16"

安裝網路外掛

kubectl apply -f kube-flannel.yml

3.其他節點加入叢集,非master節點都執行

[root@centos02 opt]# kubeadm join 192.168.109.130:6443 --token osh87v.zvo010kamsr8esmp --discovery-token-ca-cert-hash sha256:ff4607c7c194e9f756b1eb509e64d2d926b5f8f9556a85c3c14a2d25add28230
驗證安裝結果,僅在master節點執行
[root@centos01 opt]# kubectl get nodes
NAME       STATUS   ROLES           AGE    VERSION
centos01   Ready    control-plane   134m   v1.27.1
centos02   Ready    <none>          133m   v1.27.1
[root@centos01 opt]# kubectl get pods -n kube-system
NAME                               READY   STATUS    RESTARTS   AGE
coredns-7bdc4cb885-l4vs2           1/1     Running   0          9m3s
coredns-7bdc4cb885-wzc8x           1/1     Running   0          9m3s
etcd-centos01                      1/1     Running   0          9m18s
kube-apiserver-centos01            1/1     Running   0          9m18s
kube-controller-manager-centos01   1/1     Running   0          9m19s
kube-proxy-m92hr                   1/1     Running   0          28s
kube-proxy-pv4hw                   1/1     Running   0          9m3s
kube-scheduler-centos01            1/1     Running   0          9m18s

強烈建議重啟一遍所有伺服器節點,有可能會暴露出來隱藏的問題,再次驗證以上結果,仍然正常。

至此完成安裝,接下來可以部署應用了,推薦一個平臺:https://gitee.com/i512team/dhorse,是一個簡單易用、以應用為中心的雲原生DevOps系統,具有持續整合、持續部署、微服務治理等功能,主要特點:部署簡單、操作簡潔、功能快速。