• kubeadm快速部署kubernetes集群(1.24.2版本)


    1.安装要求

    • 一台或多台机器,操作系统 CentOS7.x-86_x64
    • 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘30GB或更多
    • 集群中所有机器之间的网络互通
    • 交换禁用。您必须禁用交换才能使 kubelet 正常工作
    • 可以访问外网,需要拉取镜像;

    2.环境准备

    角色IP
    master-140

    192.168.100.140

    node-141192.168.100.141
    node-142192.168.100.142
    1. 1. 关闭防火墙:
    2. systemctl stop firewalld
    3. systemctl disable firewalld
    4. 2. 关闭selinux:
    5. sed -i 's/enforcing/disabled/'/etc/selinux/config # 永久 需重启
    6. setenforce 0# 临时
    7. 3. 关闭swap:
    8. swapoff -a # 临时
    9. vim /etc/fstab # 永久 将swap那一行注释
    10. 4.根据规划设置主机名
    11. 在master添加hosts:
    12. cat >>/etc/hosts << EOF
    13. 192.168.100.140 master-140
    14. 192.168.100.141 node-141
    15. 192.168.100.142 node-142
    16. EOF
    17. 5.修改linux的内核采纳数,添加网桥过滤和地址转发功能
    18. cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
    19. overlay
    20. br_netfilter
    21. EOF
    22. sudo modprobe overlay
    23. sudo modprobe br_netfilter
    24. cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
    25. net.bridge.bridge-nf-call-iptables = 1
    26. net.bridge.bridge-nf-call-ip6tables = 1
    27. net.ipv4.ip_forward = 1
    28. EOF
    29. sudo sysctl --system
    30. 6.时间同步:
    31. yum install ntpdate -y
    32. ntpdate cn.pool.ntp.org

    3.安装container

    1. 1.使用containerd 作为容器,下载 containerd 包
    2. # wget https://github.com/containerd/containerd/releases/download/v1.6.6/cri-containerd-cni-1.6.6-linux-amd64.tar.gz
    3. 这里需要制定解压目录为【/】,包自带结构。
    4. # tar zxvf cri-containerd-cni-1.6.6-linux-amd64.tar.gz -C /
    5. 2.创建容器目录
    6. # mkdir /etc/containerd
    7. 3.生成容器配置文件
    8. # containerd config default >> /etc/containerd/config.toml
    9. 4.配置systemdcgroup 驱动程序
    10. # vim /etc/containerd/config.toml
    11. [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
    12. ...
    13. [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
    14. SystemdCgroup = true
    15. 5.修改sandbox (pause) image地址
    16. # vim /etc/containerd/config.toml
    17. [plugins."io.containerd.grpc.v1.cri"]
    18. sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.2"
    19. 6.更新runc,因为cri-containerd-cni-1.6.6-linux-amd64.tar.gz的runc二进制文件有问题,最后说明。这一步很重要 ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
    20. # wget https://github.com/opencontainers/runc/releases/download/v1.1.3/runc.amd64
    21. # mv runc.amd64 /usr/local/sbin/runc
    22. mv:是否覆盖"/usr/local/sbin/runc"? y
    23. # chmod +x /usr/local/sbin/runc
    24. 7.启动containerd服务
    25. # systemctl start containerd
    26. # systemctl enable containerd

    4.安装kubeadm、kubelet、kubectl

    1. 1.添加阿里云YUM源
    2. # cat >/etc/yum.repos.d/kubernetes.repo << EOF
    3. [kubernetes]
    4. name=Kubernetes
    5. baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
    6. enabled=1
    7. gpgcheck=0
    8. repo_gpgcheck=0
    9. gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    10. EOF
    11. 2.指定版本安装软件
    12. # yum install kubelet-1.24.2 kubeadm-1.24.2 kubectl-1.24.2
    13. 3.配置kubelet的cgroup
    14. # vim /etc/sysconfig/kubelet, 添加下面的配置
    15. KUBELET_CGROUP_ARGS="--cgroup-driver=systemd"
    16. 3. kubelet设置开机自启
    17. # systemctl enable kubelet

    5.集群初始化

    1. 【此步骤只在master节点执行】
    2. # kubeadm init \
    3. --apiserver-advertise-address=192.168.100.140 \
    4. --image-repository=registry.aliyuncs.com/google_containers \
    5. --kubernetes-version=1.24.2 \
    6. --pod-network-cidr=10.244.0.0/16 \
    7. --service-cidr=10.96.0.0/12
    • --apiserver-advertise-address 集群通告地址
    • --image-repository 由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。
    • --kubernetes-version K8s版本,与上面安装的一致
    • --service-cidr 集群内部虚拟网络,Pod统一访问入口
    • --pod-network-cidr Pod网络,与下面部署的CNI网络组件yaml中保持一致
       
    1. 【下面为日志输出】
    2. ......
    3. [bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
    4. [kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
    5. [addons] Applied essential addon: CoreDNS
    6. [addons] Applied essential addon: kube-proxy
    7. Your Kubernetes control-plane has initialized successfully!
    8. To start using your cluster, you need to run the following as a regular user:
    9. mkdir -p $HOME/.kube
    10. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    11. sudo chown $(id -u):$(id -g) $HOME/.kube/config
    12. Alternatively, if you are the root user, you can run:
    13. export KUBECONFIG=/etc/kubernetes/admin.conf
    14. You should now deploy a pod network to the cluster.
    15. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
    16. https://kubernetes.io/docs/concepts/cluster-administration/addons/
    17. Then you can join any number of worker nodes by running the following on each as root:
    18. kubeadm join 192.168.100.140:6443 --token dirta5.mvlho7gqshh9hw6o \
    19. --discovery-token-ca-cert-hash sha256:fc2e5cf3feebbdf8fec37ca9ce7656431414ebf816f217b7d1c076dd89e9dadd

    根据输出日志操作

    1. # mkdir -p $HOME/.kube
    2. # sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    3. # sudo chown $(id -u):$(id -g) $HOME/.kube/config
    4. 查看node
    5. # kubectl get nodes
    6. NAME STATUS ROLES AGE VERSION
    7. master-140 Ready control-plane 2m16s v1.24.2

    6.Node节点加入集群

    1. 在node节点执行。向集群添加新节点,执行在kubeadm init输出的kubeadm join命令。
    2. # kubeadm join 192.168.100.140:6443 --token dirta5.mvlho7gqshh9hw6o --discovery-token-ca-cert-hash sha256:fc2e5cf3feebbdf8fec37ca9ce7656431414ebf816f217b7d1c076dd89e9dadd
    3. 查看node(上面我只在node-141执行了)
    4. kubectl get nodes
    5. NAME STATUS ROLES AGE VERSION
    6. master-140 Ready control-plane 2m16s v1.24.2
    7. node-141 Ready <none> 54s v1.24.2

    token默认有效期为24小时,过期后需要重新创建:

    1. 1.查看token
    2. # kubeadm token list
    3. TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
    4. dirta5.mvlho7gqshh9hw6o 23h 2022-06-27T05:01:40Z authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
    5. 2.创建token
    6. # kubeadm token create --print-join-command
    7. kubeadm join 192.168.100.140:6443 --token 81zsrm.jvjhbg0mwlsdzdb7 --discovery-token-ca-cert-hash sha256:fc2e5cf3feebbdf8fec37ca9ce7656431414ebf816f217b7d1c076dd89e9dadd
    8. 3.查看token
    9. # kubeadm token list
    10. TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
    11. 81zsrm.jvjhbg0mwlsdzdb7 23h 2022-06-27T05:11:20Z authentication,signing <none> system:bootstrappers:kubeadm:default-node-token
    12. dirta5.mvlho7gqshh9hw6o 23h 2022-06-27T05:01:40Z authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
    13. 4.查看discovery-token-ca-cert-hash
    14. # openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
    15. fc2e5cf3feebbdf8fec37ca9ce7656431414ebf816f217b7d1c076dd89e9dadd

    用新创建的token把node-142加入集群

    1. 【此操作在node-142执行】
    2. # kubeadm join 192.168.100.140:6443 --token 81zsrm.jvjhbg0mwlsdzdb7 --discovery-token-ca-cert-hash sha256:fc2e5cf3feebbdf8fec37ca9ce7656431414ebf816f217b7d1c076dd89e9dadd
    3. 【在master查看node】
    4. # kubectl get nodes
    5. NAME STATUS ROLES AGE VERSION
    6. master-140 Ready control-plane 16m v1.24.2
    7. node-141 Ready <none> 15m v1.24.2
    8. node-142 Ready <none> 35s v1.24.2

    7.部署网络插件

    解决容器跨主机网络通信,此cni网络插件使用calico

    参考地址:Quickstart for Calico on Kubernetes

    1. 1.查看kebe-system空间的pod
    2. # kubectl get pods -n kube-system
    3. NAME READY STATUS RESTARTS AGE
    4. coredns-74586cf9b6-5bfk7 0/1 ContainerCreating 0 22m
    5. coredns-74586cf9b6-d29mj 0/1 ContainerCreating 0 22m
    6. ...
    7. 查看到coredns的两个pod异常,是因为没有部署cni网络插件。
    8. 2.下载calico的yaml文件
    9. # wget https://projectcalico.docs.tigera.io/manifests/tigera-operator.yaml
    10. # wget https://projectcalico.docs.tigera.io/manifests/custom-resources.yaml
    11. 3.修改custom-resources.yaml
    12. ipPools:
    13. - blockSize: 26
    14. cidr: 10.244.0.0/16 # 此处修改为pod-network-cidr的范围,就是init集群时候写的。
    15. encapsulation: VXLANCrossSubnet
    16. natOutgoing: Enabled
    17. nodeSelector: all()
    18. 4.安装calico
    19. # kubectl apply -f tigera-operator.yaml
    20. # kubectl apply -f custom-resources.yaml
    21. 5.查看
    22. # kubectl get pods -n calico-system
    23. NAME READY STATUS RESTARTS AGE
    24. calico-kube-controllers-86dff98c45-jjflf 1/1 Running 0 2m20s
    25. calico-node-27zbg 1/1 Running 0 2m20s
    26. calico-node-kjphd 1/1 Running 0 2m20s
    27. calico-node-ntw22 1/1 Running 0 2m20s
    28. calico-typha-6c8778fdb7-bbpnh 1/1 Running 0 2m20s
    29. calico-typha-6c8778fdb7-lpmdl 1/1 Running 0 2m11s
    30. 6.查看coredns是否正常
    31. kubectl get pods -n kube-system
    32. NAME READY STATUS RESTARTS AGE
    33. coredns-74586cf9b6-5bfk7 1/1 Running 0 28m
    34. coredns-74586cf9b6-d29mj 1/1 Running 0 28m
    35. ...
    36. 查看已正常

    8.集群测试

    1. 1.部署一个deployment
    2. # kubectl create deployment deploy-nginx --image=nginx:1.18
    3. 2.部署的deploy默认是一个pod,现在扩容为3个
    4. # kubectl scale deployment deploy-nginx --replicas=3
    5. 3.暴露端口
    6. # kubectl expose deployment deploy-nginx --port=80 --target-port=8081 --type=NodePort
    7. 4.查看
    8. # kubectl get deployment,pods,svc -o wide
    9. NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
    10. deployment.apps/deploy-nginx 3/3 3 3 5m5s nginx nginx:1.18 app=deploy-nginx
    11. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    12. pod/deploy-nginx-74565bf758-8dsp7 1/1 Running 0 5m5s 10.244.65.194 node-141 <none> <none>
    13. pod/deploy-nginx-74565bf758-9kc74 1/1 Running 0 4m12s 10.244.56.3 node-142 <none> <none>
    14. pod/deploy-nginx-74565bf758-j7gs9 1/1 Running 0 4m12s 10.244.56.4 node-142 <none> <none>
    15. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
    16. service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 37m <none>
    17. service/svc-nginx NodePort 10.101.189.51 <none> 8081:31379/TCP 4s app=deploy-nginx
    18. 5.访问pod地址和svc地址
    19. ---pod地址
    20. # curl 10.244.65.194
    21. # curl 10.244.56.3
    22. # curl 10.244.56.4
    23. HTTP/1.1 200 OK
    24. Server: nginx/1.18.0
    25. Date: Sun, 26 Jun 2022 05:41:47 GMT
    26. Content-Type: text/html
    27. Content-Length: 612
    28. Last-Modified: Tue, 21 Apr 2020 14:09:01 GMT
    29. Connection: keep-alive
    30. ETag: "5e9efe7d-264"
    31. Accept-Ranges: bytes
    32. ---svc地址
    33. # curl -I 10.101.189.51:8081
    34. HTTP/1.1 200 OK
    35. Server: nginx/1.18.0
    36. Date: Sun, 26 Jun 2022 05:42:36 GMT
    37. Content-Type: text/html
    38. Content-Length: 612
    39. Last-Modified: Tue, 21 Apr 2020 14:09:01 GMT
    40. Connection: keep-alive
    41. ETag: "5e9efe7d-264"
    42. Accept-Ranges: bytes

    9.配置ipvs

    在Kubernetes中Service有两种带来模型,一种是基于iptables的,一种是基于ipvs的两者比较的话,ipvs的性能明显要高一些,但是如果要使用它,需要手动载入ipvs模块 。

    1. 1.安装ipset和ipvsadm
    2. # yum install ipset ipvsadm -y
    3. 2.添加需要加载的模块写入脚本文件
    4. # cat <<EOF> /etc/sysconfig/modules/ipvs.modules
    5. #!/bin/bash
    6. modprobe -- ip_vs
    7. modprobe -- ip_vs_rr
    8. modprobe -- ip_vs_wrr
    9. modprobe -- ip_vs_sh
    10. modprobe -- nf_conntrack_ipv4
    11. EOF
    12. 3.为脚本添加执行权限
    13. # chmod +x /etc/sysconfig/modules/ipvs.modules
    14. 4.执行脚本文件
    15. # /bin/bash /etc/sysconfig/modules/ipvs.modules
    16. 5.查看对应的模块是否加载成功
    17. # lsmod | grep -e ip_vs -e nf_conntrack_ipv4

     修改kube-proxy 的工作模式

    1. 1.在master节点执行
    2. # kubectl edit cm kube-proxy -n kube-system
    3. ...
    4. kind: KubeProxyConfiguration
    5. metricsBindAddress: ""
    6. mode: "ipvs" # 此处修改为ipvs,默认为空
    7. nodePortAddresses: null
    8. ...
    9. 2.查看当前的kube-proxy
    10. # kubectl get pods -n kube-system
    11. NAME READY STATUS RESTARTS AGE
    12. coredns-74586cf9b6-5bfk7 1/1 Running 0 75m
    13. coredns-74586cf9b6-d29mj 1/1 Running 0 75m
    14. etcd-master-140 1/1 Running 0 76m
    15. kube-apiserver-master-140 1/1 Running 0 76m
    16. kube-controller-manager-master-140 1/1 Running 0 76m
    17. kube-proxy-f7rcx 1/1 Running 0 74m
    18. kube-proxy-ggchx 1/1 Running 0 60m
    19. kube-proxy-hbt94 1/1 Running 0 75m
    20. kube-scheduler-master-140 1/1 Running 0 76m
    21. 3.删除当前的kube-proxy
    22. # kubectl delete pod kube-proxy-f7rcx kube-proxy-ggchx kube-proxy-hbt94 -n kube-system
    23. pod "kube-proxy-f7rcx" deleted
    24. pod "kube-proxy-ggchx" deleted
    25. pod "kube-proxy-hbt94" deleted
    26. 4.查看新自动创建的kube-proxy
    27. # kubectl get pods -n kube-system
    28. NAME READY STATUS RESTARTS AGE
    29. coredns-74586cf9b6-5bfk7 1/1 Running 0 77m
    30. coredns-74586cf9b6-d29mj 1/1 Running 0 77m
    31. etcd-master-140 1/1 Running 0 78m
    32. kube-apiserver-master-140 1/1 Running 0 78m
    33. kube-controller-manager-master-140 1/1 Running 0 78m
    34. kube-proxy-7859q 1/1 Running 0 44s
    35. kube-proxy-l4gqx 1/1 Running 0 43s
    36. kube-proxy-nnjr2 1/1 Running 0 43s
    37. kube-scheduler-master-140 1/1 Running 0 78m

     验证:

    1. 1.查看刚才创建的svc
    2. # kubectl get svc -o wide
    3. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
    4. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 80m <none>
    5. svc-nginx NodePort 10.101.189.51 <none> 8081:31379/TCP 42m app=deploy-nginx
    6. 2. 请求
    7. # curl -I 10.101.189.51:8081
    8. HTTP/1.1 200 OK
    9. Server: nginx/1.18.0
    10. Date: Sun, 26 Jun 2022 06:22:14 GMT
    11. Content-Type: text/html
    12. Content-Length: 612
    13. Last-Modified: Tue, 21 Apr 2020 14:09:01 GMT
    14. Connection: keep-alive
    15. ETag: "5e9efe7d-264"
    16. Accept-Ranges: bytes
    17. 3.查看ipvs规则
    18. # ipvsadm -ln
    19. IP Virtual Server version 1.2.1 (size=4096)
    20. Prot LocalAddress:Port Scheduler Flags
    21. -> RemoteAddress:Port Forward Weight ActiveConn InActConn
    22. ...
    23. TCP 10.101.148.59:443 rr
    24. -> 10.244.56.2:5443 Masq 1 0 0
    25. -> 10.244.65.193:5443 Masq 1 0 0
    26. (下面这个就是svc的ipvs规则链)
    27. TCP 10.101.189.51:8081 rr
    28. -> 10.244.56.3:80 Masq 1 0 0
    29. -> 10.244.56.4:80 Masq 1 0 0
    30. -> 10.244.65.194:80 Masq 1 0 1
    31. TCP 10.103.59.95:9094 rr
    32. -> 10.244.56.1:9094 Masq 1 0 0
    33. ...

    10.问题解决

    进行集群初始化时候遇到如下错误。是因为安装的containerd二进制包里面的runc有问题,从官网从新下载一个替换解决,不要问为什么,我也不知道。

    1. [wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
    2. [kubelet-check] Initial timeout of 40s passed.
    3. Unfortunately, an error has occurred:
    4. timed out waiting for the condition
    5. This error is likely caused by:
    6. - The kubelet is not running
    7. - The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)
    8. If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
    9. - 'systemctl status kubelet'
    10. - 'journalctl -xeu kubelet'
    11. Additionally, a control plane component may have crashed or exited when started by the container runtime.
    12. To troubleshoot, list all containers using your preferred container runtimes CLI.
    13. Here is one example how you may list all running Kubernetes containers by using crictl:
    14. - 'crictl --runtime-endpoint unix:///var/run/containerd/containerd.sock ps -a | grep kube | grep -v pause'
    15. Once you have found the failing container, you can inspect its logs with:
    16. - 'crictl --runtime-endpoint unix:///var/run/containerd/containerd.sock logs CONTAINERID'
    17. error execution phase wait-control-plane: couldn't initialize a Kubernetes cluster
    18. To see the stack trace of this error execute with --v=5 or higher

    如果以上内容有错误的地方,欢迎指正,谢谢!

  • 相关阅读:
    金三银四,风控建模面试高频问题大全
    自动驾驶学习笔记(十一)——高精地图
    Python 如何实现 Command(命令)模式?什么是 Command(命令)设计模式?
    图搜算算法分类
    git常用命令
    如何通过文件自动备份软件进行自动化备份?
    知乎转发最高的 Java 面试成神笔记,GitHub 已下载量已过百万
    【红日靶场】vulnstack3-完整渗透过程
    【设计模式】六大基本原则
    java设计模式---建造者模式
  • 原文地址:https://blog.csdn.net/qq_38619781/article/details/125465313