k8s1.26+containerd安装-kubeadm快速安装

Posted qq_36765991

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s1.26+containerd安装-kubeadm快速安装相关的知识,希望对你有一定的参考价值。

k8s1.26+containerd安装-单机master

1.机器

iphostname
192.168.137.133k8smaster
192.168.137.132k8snode1
192.168.137.134k8snode2

1.1机器初始化操作

每个机器设置对应的hostname,并查看

 hostnamectl set-hostname k8smaster
 hostname

在master机器配置host文件

 echo '''
192.168.137.131 k8smaster
192.168.137.129 k8snode1
192.168.137.131 k8snode2
''' >> /etc/hosts

每台机器都设置 转发 IPv4 并让 iptables 看到桥接流量

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

如果想要更好的网络性能就配置ipvs,自行在网上搜索。
每台机器都设置 时间同步

yum install chrony -y
systemctl start chronyd
systemctl enable chronyd
chronyc sources

每台机器 如果有防火墙关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

每台机器 关闭 swap

# 临时关闭;关闭swap主要是为了性能考虑
swapoff -a
# 可以通过这个命令查看swap是否关闭了
free
# 永久关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab

每台机器 禁用 SELinux

# 临时关闭
setenforce 0
# 永久禁用
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config

2.每台机器 安装containerd

# 添加docker源
curl -L -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安装containerd
yum install -y containerd.io
# 创建默认配置文件
containerd config default > /etc/containerd/config.toml
# 设置aliyun地址,不设置会连接不上
sed -i "s#registry.k8s.io/pause#registry.aliyuncs.com/google_containers/pause#g" /etc/containerd/config.toml
# 设置驱动为systemd
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
# 设置dicker地址为aliyun镜像地址
vi /etc/containerd/config.toml
# 文件内容为
   [plugins."io.containerd.grpc.v1.cri".registry]
     [plugins."io.containerd.grpc.v1.cri".registry.mirrors]
       [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
         endpoint = ["https://8aj710su.mirror.aliyuncs.com" ,"https://registry-1.docker.io"]

# 重启服务
systemctl daemon-reload
systemctl enable --now containerd
systemctl restart containerd

3.每台机器 安装kubelet kubeadm kubectl 和crictl工具

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
setenforce 0

# 安装crictl工具
yum install -y cri-tools
# 生成配置文件
crictl config runtime-endpoint
# 编辑配置文件
cat << EOF | tee /etc/crictl.yaml
runtime-endpoint: "unix:///run/containerd/containerd.sock"
image-endpoint: "unix:///run/containerd/containerd.sock"
timeout: 10
debug: false
pull-image-on-create: false
disable-pull-on-run: false
EOF

# 安装kub软件
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet

在master执行初始化

kubeadm init \\
  --apiserver-advertise-address=192.168.137.131 \\
  --image-repository registry.aliyuncs.com/google_containers \\
  --kubernetes-version v1.26.0 \\
  --service-cidr=10.96.0.0/12 \\
  --pod-network-cidr=10.244.0.0/16 \\
  --ignore-preflight-errors=all

如果kubelet启动失败查看启动文件

cat /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
cat /var/lib/kubelet/kubeadm-flags.env

如果初始化出错重置命令

kubeadm reset

rm -fr ~/.kube/  /etc/kubernetes/* var/lib/etcd/*

出现如图表示成功

然后执行第一个红框语句

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

在node节点机器执行第二个语句

kubeadm join 192.168.137.131:6443 --token cuyawq.2mrgrt9q6uxrhquw \\
        --discovery-token-ca-cert-hash sha256:dc878dc1667c7c927ebfae7875a699f96efd99a72147facaa93187a7b057ef6f

在master配置CNI

cat > kube-flannel.yaml << EOF
---
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:
  - 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.19.0 for ppc64le and mips64le (dockerhub limitations may apply)
        image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.0
        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.19.0 for ppc64le and mips64le (dockerhub limitations may apply)
        image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.0
        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
EOF
kubectl apply -f kube-flannel.yml

添加之后执行命令查看是否添加成功

kubectl get node
kubectl get pods -n kube-system

测试创建容器

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
# 查看
kubectl get pod,svc

使用任意一个node节点ip访问查询出来的端口

以上是关于k8s1.26+containerd安装-kubeadm快速安装的主要内容,如果未能解决你的问题,请参考以下文章

k8s二containerd的安装

k8s二containerd的安装

Ubuntu22.04使用kubeadm安装k8s 1.26版本高可用集群

Ubuntu22.04使用kubeadm安装k8s 1.26版本高可用集群

k8s系列-05-k8s集群搭建方案对比,以及containerd是什么,如何安装

k8s使用containerd作为容器运行时