kubernetes部署 containerd版本

时间:2022-12-26 12:17:45

kubernetes部署 containerd版本

概述

kubernetes在1.24版本,彻底移除了docker-shim模块的代码,所以如果按照网上基于docker的部署文档进行部署就会失败,在1.24版本之后,运行kubeadm之前,需要在各个k8s节点先部署CRI,常见的CRI有:containerd、CRI-O、Docker Engine、Mirantis Container Runtime。本文以containerd为例子,并且是基于aarch64架构的CPU进行部署,针对x86架构的,需要将软件源、containerd等更换一下才行,请各位读者知悉。

部署k8s-master

关闭防火墙

systemctl stop firewalld
systemctl disable firewalld
iptables -F

关闭SElinux

sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
sed -i "s/^SELINUX=permissive/SELINUX=disabled/g" /etc/selinux/config
setenforce 0

禁用swap

swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
mount -a

配置时间同步

yum install ntpdate -y
ntpdate time.windows.com

配置k8s源仓库

cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-aarch64
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

部署容器运行时 containerd

根据containerd项目的指引,安装即可

containerd 项目

安装containerd二进制

# tar Cxzvf /usr/local containerd-1.6.14-linux-arm64.tar.gz 
bin/
bin/containerd-stress
bin/containerd-shim
bin/containerd-shim-runc-v1
bin/containerd-shim-runc-v2
bin/containerd
bin/ctr

安装runc二进制

install -m 755 runc.arm64 /usr/local/sbin/runc

安装服务启动项目

mkdir -p  /usr/local/lib/systemd/system
curl https://raw.githubusercontent.com/containerd/containerd/main/containerd.service -o /usr/local/lib/systemd/system/containerd.service
systemctl daemon-reload
systemctl enable --now containerd

配置systemd cgroup驱动

mkdir /etc/containerd/
containerd config default > /etc/containerd/config.toml
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml

修改pause容器为阿里云的地址

sed -i 's#sandbox_image = "registry.k8s.io/pause:3.6"#sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.7"#g' /etc/containerd/config.toml

重启containerd

systemctl restart containerd

使用kubeadm部署

kubeadm init --apiserver-advertise-address=192.168.137.10 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16
  • –apiserver-advertise-address 这个地址为控制平面的ip地址
  • –image-repository 由于默认源无法访问,需要修改下仓库源
  • –pod-network-cidr 这个为pod的ip分配地址,这里修改为10.244.0.0/16,是因为后面的flannel默认配置就是这个pod网段,可以不修改直接应用官方的yaml文件

配置kubectl

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

验证pod运行情况

kubectl get pod -A

运行结果

NAMESPACE     NAME                                               READY   STATUS    RESTARTS   AGE
kube-system   coredns-5bbd96d687-p2m46                           0/1     Pending   0          22m
kube-system   coredns-5bbd96d687-q4lwc                           0/1     Pending   0          22m
kube-system   etcd-wc-ctu-30-0001.novalocal                      1/1     Running   0          22m
kube-system   kube-apiserver-wc-ctu-30-0001.novalocal            1/1     Running   0          22m
kube-system   kube-controller-manager-wc-ctu-30-0001.novalocal   1/1     Running   0          22m
kube-system   kube-proxy-hhf9z                                   1/1     Running   0          22m
kube-system   kube-scheduler-wc-ctu-30-0001.novalocal            1/1     Running   0          22m

由于还没有安装flannel插件,所以coredns pod目前没有启动,处于pending状态,其余pod处于正常状态

部署flannel网络插件

官方的flannel文件内容

---
kind: Namespace
apiVersion: v1
metadata:
  name: kube-flannel
  labels:
    pod-security.kubernetes.io/enforce: privileged
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - nodes/status
  verbs:
  - patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: flannel
subjects:
- kind: ServiceAccount
  name: flannel
  namespace: kube-flannel
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: flannel
  namespace: kube-flannel
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: kube-flannel-cfg
  namespace: kube-flannel
  labels:
    tier: node
    app: flannel
data:
  cni-conf.json: |
    {
      "name": "cbr0",
      "cniVersion": "0.3.1",
      "plugins": [
        {
          "type": "flannel",
          "delegate": {
            "hairpinMode": true,
            "isDefaultGateway": true
          }
        },
        {
          "type": "portmap",
          "capabilities": {
            "portMappings": true
          }
        }
      ]
    }
  net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds
  namespace: kube-flannel
  labels:
    tier: node
    app: flannel
spec:
  selector:
    matchLabels:
      app: flannel
  template:
    metadata:
      labels:
        tier: node
        app: flannel
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/os
                operator: In
                values:
                - linux
      hostNetwork: true
      priorityClassName: system-node-critical
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni-plugin
       #image: flannelcni/flannel-cni-plugin:v1.1.0 for ppc64le and mips64le (dockerhub limitations may apply)
        image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
        command:
        - cp
        args:
        - -f
        - /flannel
        - /opt/cni/bin/flannel
        volumeMounts:
        - name: cni-plugin
          mountPath: /opt/cni/bin
      - name: install-cni
       #image: flannelcni/flannel:v0.20.2 for ppc64le and mips64le (dockerhub limitations may apply)
        image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.2
        command:
        - cp
        args:
        - -f
        - /etc/kube-flannel/cni-conf.json
        - /etc/cni/net.d/10-flannel.conflist
        volumeMounts:
        - name: cni
          mountPath: /etc/cni/net.d
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      containers:
      - name: kube-flannel
       #image: flannelcni/flannel:v0.20.2 for ppc64le and mips64le (dockerhub limitations may apply)
        image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.2
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        securityContext:
          privileged: false
          capabilities:
            add: ["NET_ADMIN", "NET_RAW"]
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: EVENT_QUEUE_DEPTH
          value: "5000"
        volumeMounts:
        - name: run
          mountPath: /run/flannel
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
        - name: xtables-lock
          mountPath: /run/xtables.lock
      volumes:
      - name: run
        hostPath:
          path: /run/flannel
      - name: cni-plugin
        hostPath:
          path: /opt/cni/bin
      - name: cni
        hostPath:
          path: /etc/cni/net.d
      - name: flannel-cfg
        configMap:
          name: kube-flannel-cfg
      - name: xtables-lock
        hostPath:
          path: /run/xtables.lock
          type: FileOrCreate

部署k8s服务器节点

部署过程同k8s的master节点类似,我这里做了一个简单的脚本,大家可以根据需要进行修改

#!/bin/sh

systemctl stop firewalld
systemctl disable firewalld
iptables -F
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
sed -i "s/^SELINUX=permissive/SELINUX=disabled/g" /etc/selinux/config
setenforce 0
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
mount -a
yum install ntpdate -y
ntpdate time.windows.com
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-aarch64
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


tar Cxzvf /usr/local containerd-1.6.14-linux-arm64.tar.gz 
install -m 755 runc.arm64 /usr/local/sbin/runc
mkdir -p  /usr/local/lib/systemd/system
cp  containerd.service /usr/local/lib/systemd/system/
systemctl daemon-reload
mkdir /etc/containerd/
cp config.toml /etc/containerd
systemctl enable --now containerd
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# 设置所需的 sysctl 参数,参数在重新启动后保持不变
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF

# 应用 sysctl 参数而不重新启动
sudo sysctl --system
yum install -y kubelet kubeadm kubectl
# 这里大家根据自己的master的join命令进行
# 打印join命令可以参考以下命令
# kubeadm token create --print-join-command
#kubeadm join 192.168.137.10:6443 --token ko4t79.0c5rqddlu5xykmxn --discovery-token-ca-cert-hash sha256:98398dea099a1e352d052fa6dbf4f7fa4025b831b486f613d3e98652190b6b9e

验证k8s集群节点状态

在master节点输入kubectl get nodes

# kubectl get nodes
NAME                       STATUS   ROLES           AGE    VERSION
wc-ctu-30-0001.novalocal   Ready    control-plane   2d7h   v1.26.0
wc-ctu-30-0002.novalocal   Ready    <none>          2d5h   v1.26.0
wc-ctu-30-0003.novalocal   Ready    <none>          2d5h   v1.26.0
wc-ctu-30-0004.novalocal   Ready    <none>          2d5h   v1.26.0

可以看到各个节点状态正常

总结

对于containerd版本安装,跟k8s 1.24版本之前docker安装的步骤类似,只不过把安装docker的过程换成了containerd而已,因为在k8s 1.24版本之后,移除了docker-shim,所以无法直接使用docker-engine,本质的部署过程也是一样的。