当前位置:   article > 正文

使用kubeadm基于ubuntu20.04部署K8S v1.29集群

使用kubeadm基于ubuntu20.04部署K8S v1.29集群

前言:根据目前CKA考试要求配置一个基础的练习环境。

           · Ubuntu20.04

           · Kubernetes v1.29.0

           · Containerd

一、硬件配置环境

下表是按照官方文档Kubernetes集群节点最低配置要求 。电脑资源允许可以适当调高配置。

RoleNameNumCPUMEM DISKSYSTEMIP
mastermaster12C2G20GUbuntu 20.04.6 LTS192.168.10.60
workernode0112C2G20GUbuntu 20.04.6 LTS192.168.10.61
workernode0212C2G20GUbuntu 20.04.6 LTS192.168.10.62

二、系统环境准备(此部分在集群所有节点操作)

1、系统基础设置

  1. sudo systemctl status ufw #查看防火墙状态
  2. sudo systemctl stop ufw #关闭防火墙
  3. sudo systemctl disable ufw #禁止防火墙开机启动
  4. sudo swapoff -a #关闭swap分区
  5. sed -ri 's/.*swap.*/#&/' /etc/fstab #禁用swap分区
  6. hostnamectl set-hostname master #设置master主机名
  7. hostnamectl set-hostname node01 #设置node01主机名
  8. hostnamectl set-hostname node02 #设置node02主机名
  9. sudo echo 192.168.10.60 master >> /etc/hosts #设置master的host解析
  10. sudo echo 192.168.10.61 node01 >> /etc/hosts #设置node01的host解析
  11. sudo echo 192.168.10.62 node02 >> /etc/hosts #设置node02的host解析
  12. sudo timedatectl set-timezone Asia/Shanghai #配置时区

 2、安装时间同步服务

  1. sudo apt-get update
  2. sudo apt-get install -y chrony
  3. sudo systemctl start chrony
  4. sudo systemctl status chrony
  5. sudo systemctl enable chrony
  6. sudo chronyc sourcestats -v

3、 允许 iptables 检查桥接流量设置

  1. #转发IPv4并让iptables看到桥接流量
  2. cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
  3. overlay
  4. br_netfilter
  5. EOF
  6. sudo modprobe overlay
  7. sudo modprobe br_netfilter
  8. #设置所需的sysctl参数,参数在重新启动后保持不变
  9. cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
  10. net.bridge.bridge-nf-call-iptables = 1
  11. net.bridge.bridge-nf-call-ip6tables = 1
  12. net.ipv4.ip_forward = 1
  13. EOF
  14. #应用sysctl参数而不重新启动
  15. sudo sysctl --system
  16. #检查确认br_netfilter和overlay模块被加载:
  17. lsmod | grep br_netfilter
  18. lsmod | grep overlay
  19. #检查sysctl是否成功应用:
  20. sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward

 4、安装容器运行时Containerd

  1. # 添加Docker'官方GPG key:
  2. sudo apt-get update
  3. sudo apt-get install ca-certificates curl gnupg
  4. sudo install -m 0755 -d /etc/apt/keyrings
  5. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  6. sudo chmod a+r /etc/apt/keyrings/docker.gpg
  7. # 添加repository到Apt源:
  8. echo \
  9. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  10. $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  11. sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  12. sudo apt-get update
  13. #安装containerd
  14. sudo apt-get install containerd.io
  15. #锁定containerd.io 版本
  16. sudo apt-mark hold containerd.io
  17. #生成containerd配置文件
  18. containerd config default > /etc/containerd/config.toml
  19. #修改/etc/containerd/config.toml
  20. #配置systemd cgroup驱动为true
  21. [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
  22. ...
  23. [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
  24. SystemdCgroup = true
  25. #使用阿里云沙箱(pause)镜像替换原有配置
  26. # registry.cn-shanghai.aliyuncs.com/kubesec/pause:3.9
  27. [plugins."io.containerd.grpc.v1.cri"]
  28. sandbox_image = "registry.k8s.io/pause:3.6"
  29. sudo systemctl restart containerd
  30. sudo systemctl enable containerd
  31. sudo systemctl status containerd

5、安装 kubeadm、kubelet 和 kubectl

  1. #更新 apt 包索引并安装使用 Kubernetes apt 仓库所需要的包:
  2. sudo apt-get update
  3. sudo apt-get install -y apt-transport-https ca-certificates curl gpg
  4. #下载用于 Kubernetes 软件包仓库的公共签名密钥。
  5. #如果 `/etc/apt/keyrings` 目录不存在,则应在 curl 命令之前创建它。
  6. # sudo mkdir -p -m 755 /etc/apt/keyrings
  7. curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
  8. #添加 Kubernetes apt 仓库。
  9. echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
  10. #更新 apt 包索引,安装 kubelet、kubeadm 和 kubectl,并锁定其版本:
  11. sudo apt-get update
  12. apt-cache madison kubeadm
  13. apt-get install -y kubelet=1.29.0-1.1 kubeadm=1.29.0-1.1 kubectl=1.29.0-1.1
  14. sudo apt-mark hold kubelet kubeadm kubectl

三、kubeadm创建集群 

从 v1.22 开始,在使用 kubeadm 创建集群时,如果用户没有在 KubeletConfiguration 下设置 cgroupDriver 字段,kubeadm 默认使用 systemd

1、初始化控制节点(只在master节点执行)

  1. kubeadm init --control-plane-endpoint="master" \
  2. --kubernetes-version=v1.29.0 \
  3. --pod-network-cidr=10.244.0.0/16 \
  4. --service-cidr=10.1.0.0/12 \
  5. --token-ttl=0 \
  6. --cri-socket unix:///run/containerd/containerd.sock \
  7. --image-repository registry.aliyuncs.com/google_containers \
  8. --upload-certs

2、根据输出结果执行以下命令(只在master节点执行)

  1. #要使非 root 用户可以运行 kubectl,请运行以下命令
  2. mkdir -p $HOME/.kube
  3. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  4. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  5. #root 用户,则可以运行:
  6. export KUBECONFIG=/etc/kubernetes/admin.conf

3、安装Pod网络插件Flannel(只在master节点执行)

  1. #添加raw.githubusercontent.com地址解析
  2. echo "185.199.109.133 raw.githubusercontent.com" >> /etc/hosts
  3. #下载flannel.yml文件并应用
  4. wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  5. kubectl apply -f kube-flannel.yml

4、安装集群资源监控服务kube-metrics-server(只在master节点执行)

  1. wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
  2. #将image: registry.k8s.io/metrics-server/metrics-server:v0.6.4
  3. #替换成阿里镜像源:registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.4
  4. mv components.yaml kube-metrics-server.yaml
  5. kubectl apply -f kube-metrics-server.yaml

 5、节点加入集群(node01和node02节点执行)

  1. #执行控制节点输入的加入命令并加上参数--cri-socket /run/containerd/containerd.sock
  2. kubeadm join master:6443 --token 3f6ssd.cj9ktdltvhu8myex \
  3. --discovery-token-ca-cert-hash sha256:aa4568ba9d4d77b8804f78b6f5441a2269b5363b5145efe30e5c7307ea2ec0cc \
  4. --cri-socket /run/containerd/containerd.sock

6、查看集群节点和Pod状态

  1. candidate@node01:~$ kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. master Ready control-plane 1h v1.29.0
  4. node01 Ready worker 1h v1.29.0
  5. node02 Ready worker 1h v1.29.0
  6. candidate@node01:~$ kubectl get pod --all-namespaces
  7. NAMESPACE NAME READY STATUS RESTARTS AGE
  8. kube-flannel kube-flannel-ds-2kcht 1/1 Running 0 1h
  9. kube-flannel kube-flannel-ds-gd9j4 1/1 Running 0 1h
  10. kube-flannel kube-flannel-ds-ngg46 1/1 Running 0 1h
  11. kube-system coredns-857d9ff4c9-bg6pn 1/1 Running 0 1h
  12. kube-system coredns-857d9ff4c9-rpr7b 1/1 Running 0 1h
  13. kube-system etcd-master 1/1 Running 0 1h
  14. kube-system kube-apiserver-master 1/1 Running 0 1h
  15. kube-system kube-controller-manager-master 1/1 Running 0 1h
  16. kube-system kube-proxy-5kwqj 1/1 Running 0 1h
  17. kube-system kube-proxy-7p48k 1/1 Running 0 1h
  18. kube-system kube-proxy-f4lgd 1/1 Running 0 1h
  19. kube-system kube-scheduler-master 1/1 Running 0 1h
  20. kube-system metrics-server-579956c978-wjftf 1/1 Running 0 1h
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/609739
推荐阅读
相关标签
  

闽ICP备14008679号