0


【k8s】Kubernetes 1.29.4离线安装部署(总)

(一)kubernetes1.29.4离线部署之-安装文件准备
(二)kubernetes1.29.4离线部署之-镜像文件准备
(三)kubernetes1.29.4离线部署之-环境初始化
(四)kubernetes1.29.4离线部署之-组件安装
(五)kubernetes1.29.4离线部署之-初始化第一个控制平面
(六)kubernetes1.29.4离线部署之-加入Node节点
(七)kubernetes1.29.4离线部署之-网络插件
(八)kubernetes1.29.4离线部署之-测试验证

友情提示: 由于本文过长,不便于阅读,已经拆分为多个章节,可以点击上面的地址单独阅读

本文容器运行时采用的时Containerd

本文网络插件采用的时calico tigera-operator

本文涉及的所有脚本文件可以从如下地址获取:
https://gitee.com/qingplus/qingcloud-platform/tree/develop/qingcloud-deploy/service

前情提要:

本文不介绍K8S架构及细节,仅仅分享Kubernetes部署实施完整过程。
本文主要采用kubeadm方式安装部署。另外本文直接采用单控制平面方式快速完成版本部署,etcd的部署方式为堆叠方式,独立方式的切换后续完成文档说明。
高可用集群拓扑的两个选项介绍:
高可用集群拓扑的两个选项:

  • 使用堆叠(stacked)控制平面节点,其中 etcd 节点与控制平面节点共存
  • 使用外部 etcd 节点,其中 etcd 在与控制平面不同的节点上运行

本文主要采用第一种堆叠方式:

环境准备

硬件环境

服务器由6台ESXi虚拟化服务器组成,详情参考《ESXi网络配置与物理交换机VLAN ID规划》

本文为单控制平面部署,非高可用集群部署,并未用到规划中的所有节点。后续集群部署会继续使用。

主机名称ESXi 节点名角色IP地址资源配置安装组件itserver-master1xenserver01master10.0.0.108C/16G/200Gitserver-master2xenserver02master10.0.0.138C/16G/200Gitserver-node01xenserver01node10.0.0.1116C/64G/500Gitserver-node02xenserver01node10.0.0.1216C/64G/500Gitserver-node03xenserver02node10.0.0.1416C/64G/500Gitserver-node04xenserver02node10.0.0.1516C/64G/500G

物理服务器节点(安装ESXi)

三台物理服务器ESXi 节点名ip地址用户名备注xenserver01192.168.3.50rootxenserver02192.168.3.60rootxenserver03192.168.3.100root

网络分配

角色IP地址备注node网络10.0.0.0/24Service 网络10.96.0.0/16Pod网络172.16.0.0/16

网络部署图

企业基础网络结构拓扑.png

离线安装文件准备

需要准备如下几个必须的安装文件,可以离线下载后通过本地nginx代理供其他节点下载使用
  1. kubernetes_server

直接下载最新版二进制文件

  1. containerd

本文容器运行时采用containerd而非docker,如果容器运行时采用docker,需要另外的部署版本。本文直接下载三合一版本cri_containerd_cni

  1. cni_plugins
  2. crictl

命令行执行工具 。类似docker命令行

  1. runc

需要单独下载完整版,只是需要检查本地环境是否存在libcommp.so

  1. etcd

采用外部 etcd 节点部署时需要用到,如果使用的是堆叠的方式,直接用默认的etcd容器即可,可以不用下载。

完整的离线文件下载脚本:
  1. #!/bin/bashENV_CFG=./env.cfg
  2. if[-f${ENV_CFG}];thenchmod777${ENV_CFG}source${ENV_CFG}fi# Internet URLskernel_url="http://mirrors.tuna.tsinghua.edu.cn/elrepo/kernel/el7/x86_64/RPMS/${kernel_name}"cni_plugins_url="https://github.com/containernetworking/plugins/releases/download/${cni_plugins_version}/${cni_plugins_name}"cri_containerd_cni_url="https://github.com/containerd/containerd/releases/download/v${cri_containerd_cni_version}/${cri_containerd_cni_name}"crictl_url="https://github.com/kubernetes-sigs/cri-tools/releases/download/${crictl_version}/${crictl_name}"runc_url="https://github.com/opencontainers/runc/releases/download/v${runc_version}/${runc_name}"etcd_url="https://github.com/etcd-io/etcd/releases/download/${etcd_version}/${etcd_name}"kubernetes_server_url="https://storage.googleapis.com/kubernetes-release/release/${KUBERNETES_VERSION}/${kubernetes_server_name}"nginx_url="http://nginx.org/download/${nginx_name}"# Download packagespackages=($kernel_url$runc_url$cni_plugins_url$cri_containerd_cni_url$crictl_url$cri_dockerd_url$etcd_url$kubernetes_server_url)forpackage_urlin"${packages[@]}";dofilename=$(basename"$package_url")ifcurl-k-L-C - -o"$filename""$package_url";thenecho"Downloaded $filename"elseecho"Failed to download $filename"exit1fidone

离线镜像文件准备

注意:离线安装需要准备大量的镜像,这一步千万要仔细否则会出现各种意想不到的问题

需要准备的镜像文件
  1. kube-apiserver
  2. kube-controller-manager
  3. kube-scheduler
  4. kube-proxy
  5. kube-proxy
  6. coredns
  7. pause
  8. etcd

以上镜像文件可以从: registry.cn-hangzhou.aliyuncs.com/google_containers下载

  1. calico/node
  2. calico/kube-controllers
  3. calico/node
  4. calico/typha
  5. calico/node-driver-registrar
  6. calico/csi
  7. calico/cni
  8. calico/ctl
  9. calico/pod2daemon-flexvol
  10. calico/apiserver

以上可以直接从docker.io下载即可

注意: 以上所有的镜像文件务必准备到位。本文脚本中的几个版本便令注意修改到位:
KUBERNETES_VERSION=${KUBERNETES_VERSION:-“v1.29.4”}
COREDNS_VERSION=${COREDNS_VERSION:-‘v1.11.1’}
PAUSE_VERSION=${PAUSE_VERSION:-‘3.9’}
ETCD_VERSION=${ETCD_VERSION:-‘3.5.12-0’}
REGISTRY_VERSION=${REGISTRY_VERSION:-‘2.8.3’}
CALICO_VERSION=${CALICO_VERSION:-‘v3.27.3’}

完整的镜像下载脚本
  1. #!/bin/bashENV_CFG=./env.cfg
  2. if[-f${ENV_CFG}];thenchmod777${ENV_CFG}source${ENV_CFG}fiimage_list="${IMAGE_DOMAIN}/${IMAGE_NAMESPACE}/kube-apiserver:${KUBERNETES_VERSION}${IMAGE_DOMAIN}/${IMAGE_NAMESPACE}/kube-controller-manager:${KUBERNETES_VERSION}${IMAGE_DOMAIN}/${IMAGE_NAMESPACE}/kube-scheduler:${KUBERNETES_VERSION}${IMAGE_DOMAIN}/${IMAGE_NAMESPACE}/kube-proxy:${KUBERNETES_VERSION}${IMAGE_DOMAIN}/${IMAGE_NAMESPACE}/coredns:${COREDNS_VERSION}${IMAGE_DOMAIN}/${IMAGE_NAMESPACE}/pause:${PAUSE_VERSION}${IMAGE_DOMAIN}/${IMAGE_NAMESPACE}/etcd:${ETCD_VERSION}
  3. calico/node:${CALICO_VERSION}
  4. calico/kube-controllers:${CALICO_VERSION}
  5. calico/node:${CALICO_VERSION}
  6. calico/typha:${CALICO_VERSION}
  7. calico/node-driver-registrar:${CALICO_VERSION}
  8. calico/csi:${CALICO_VERSION}
  9. calico/cni:${CALICO_VERSION}
  10. calico/ctl:${CALICO_VERSION}
  11. calico/pod2daemon-flexvol:${CALICO_VERSION}
  12. calico/apiserver:${CALICO_VERSION}
  13. "#${IMAGE_DOMAIN}/${IMAGE_NAMESPACE}/registry:${REGISTRY_VERSION}newimage_list=()forimagein${image_list};dodocker pull "${image}"newimage=$(echo $image |sed-e"s/calico/${LOCAL_IMAGE_DOMAIN}\/calico/")newimage=$(echo $newimage |sed-e"s/${IMAGE_DOMAIN}\/${IMAGE_NAMESPACE}/${LOCAL_IMAGE_DOMAIN}\/${LOCAL_IMAGE_NAMESPACE}/")newimage_list+="${newimage} "docker tag $image$newimagedocker push $newimagedonedocker save -o qinghub-kube-"${VERSION}".tar ${newimage_list}
轻云官方下载

https:/qingplus.cn/pkg/kubernetes/v1.29.4/qinghub-kube-v1.29.4.tar
下载完成后再手动导入所有的镜像即可。详细过程不在细说。

环境初始化

检查步骤
  1. 关闭防火墙
  2. 关闭 swap partition permanently
  3. 配置检查时间同步
  4. 配置安装时间同步组件
  5. 配置检查 nfs-utils kubeadmin方式安装不需要检查
  6. 配置检查内核版本
  7. 配置检查资源情况
  8. 配置检查SSH
  9. 配置检查系统配置
  10. 配置检查转发 IPv4
  11. 配置检查Docker用户并添加ssh免密认证<authoirzed_keys> (建议手动执行)
  12. 配置检查Docker (容器运行时为Containerd时,不需要检查)
  13. 配置检查Docker用户权限 (容器运行时为Containerd时,不需要检查)
  14. 配置检查网络
完整的初始化脚本
  1. #!/bin/bash################################################ QingHub K8S Install 版本: $VERSION# 架构: $ARCH_TYPE 目前版本主要支持amd64,其他待敬请期待# 操作系统: $os_type# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################ENV_CFG=./env.cfg
  2. if[-f${ENV_CFG}];thenchmod777${ENV_CFG}source${ENV_CFG}fiexportCONSOLE=${CONSOLE:-false}os_type=$(cat /etc/os-release |grep"^ID="|awk-F='{print $2}'|tr-d[:punct:])os_version_id=$(cat /etc/os-release |grep"VERSION_ID="|awk-F='{print $2}'|tr-d[:punct:])if["$EUID"-ne0];thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${RED}[ERROR] 当前用户不是 root 用户,请切换到 root 用户执行该脚本.${NC}"exit1elseecho-e"${RED}[ERROR] Current user is not root user, please switch to root user to execute the script.${NC}"exit1fifiif[-z"$SSH_RSA"];thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${RED}[ERROR] 请设置环境变量 SSH_RSA, 该变量为 SSH 公钥.${NC}"exit1elseecho-e"${RED}[ERROR] Please set the environment variable SSH_RSA, the variable is SSH public key.${NC}"exit1fifi################################################ 新增ubuntu 用户# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functionadd_user_in_ubuntu(){useradd --create-home -s /bin/bash -g root "$1"echo"$1":"$2"| chpasswd
  3. if["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] 用户 $1 已经创建.${NC}"elseecho-e"${GREEN}[INFO] User $1 has been created.${NC}"fi}################################################ 新增redhat 用户# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functionadd_user_in_redhat(){
  4. adduser -g root "$1"echo"$1":"$2"| chpasswd
  5. if["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] 用户 $1 已经创建.${NC}"elseecho-e"${GREEN}[INFO] User $1 has been created.${NC}"fi}################################################ 描述: 检查并新增用户, 有些版本可以不用检查,请使用时根据# 情况自行注释掉# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioncheck_user(){if!grep-qdocker /etc/group;thengroupadd--forcedockerfiifid-u"${DOCKER_USER}">/dev/null 2>&1;thenif!id-nG"${DOCKER_USER}"|grep-qw"docker";then
  6. gpasswd -a"${DOCKER_USER}"dockerfiif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] 用户 ${DOCKER_USER} 已经存在.${NC}"elseecho-e"${GREEN}[INFO] User ${DOCKER_USER} already exists.${NC}"fielsecase$os_typein
  7. centos|redhat|euleros|fusionos|anolis|kylin|rhel|rocky|fedora|openEuler)
  8. add_user_in_redhat "${DOCKER_USER}""${DOCKER_PASS}";;
  9. ubuntu|debian)
  10. add_user_in_ubuntu "${DOCKER_USER}""${DOCKER_PASS}";;
  11. *)if["$LANG"=="zh_CN.UTF-8"];thenecho-e"${RED}[ERROR] 暂不支持 $os_type 操作系统.${NC}"exit1elseecho-e"${RED}[ERROR] The $os_type operating system is temporarily not supported.${NC}"exit1fi;;esacfi$CONSOLE$CONSOLE|| add_ssh_rsa "${DOCKER_USER}"}functionadd_ssh_rsa(){ifid-u"$user">/dev/null 2>&1;thenif[!-d"/home/$1/.ssh"];thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] 创建 /home/$1/.ssh 目录.${NC}"elseecho-e"${GREEN}[INFO] Create /home/$1/.ssh directory.${NC}"fimkdir-p /home/"$1"/.ssh
  12. fiif[-f"/home/$1/.ssh/authorized_keys"];thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] /home/$1/.ssh/authorized_keys 已经存在.${NC}"elseecho-e"${GREEN}[INFO] /home/$1/.ssh/authorized_keys already exists.${NC}"fichmod777 /home/"$1"/.ssh/authorized_keys
  13. if!< /home/"$1"/.ssh/authorized_keys grep-q"$SSH_RSA";thenecho"$SSH_RSA">> /home/"$1"/.ssh/authorized_keys
  14. fielseif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] 创建 /home/$1/.ssh/authorized_keys.${NC}"elseecho-e"${GREEN}[INFO] Create /home/$1/.ssh/authorized_keys.${NC}"fitouch /home/"$1"/.ssh/authorized_keys
  15. chmod777 /home/"$1"/.ssh/authorized_keys
  16. echo"$SSH_RSA"> /home/"$1"/.ssh/authorized_keys
  17. fiif< /home/"$1"/.ssh/authorized_keys grep-q"$SSH_RSA";thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] 成功将 SSH 公钥添加到 /home/$1/.ssh/authorized_keys.${NC}"elseecho-e"${GREEN}[INFO] Successfully added ssh public key to /home/$1/.ssh/authorized_keys.${NC}"fielseif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${RED}[ERROR] 将 SSH 公钥添加到 /home/$1/.ssh/authorized_keys 失败.${NC}"exit1elseecho-e"${RED}[ERROR] Add ssh public key to /home/$1/.ssh/authorized_keys failed.${NC}"exit1fifichmod600 /home/"$1"/.ssh/authorized_keys
  18. chown-R"$1":"$1" /home/"$1"/.ssh
  19. fi}functioncheck_user_permission(){ifsu${DOCKER_USER}-c"docker ps">/dev/null 2>&1;thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] Docker 用户有权限执行 docker 命令.${NC}"elseecho-e"${GREEN}[INFO] Docker users have the permission to execute docker commands.${NC}"fielseif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${RED}[ERROR] Docker 用户无权限执行 docker 命令, 请尝试重启docker 'systemctl restart docker'. 重启 docker 后, 再次执行该脚本.${NC}"exit1elseecho-e"${RED}[ERROR] Docker users have no permission to execute docker commands, Please try to restart docker 'systemctl restart docker'. After restarting docker, execute the script again.${NC}"exit1fifi}################################################ 描述: 关闭防火墙# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functiondisable_firewalld(){if systemctl status firewalld |grep Active |grep-q running >/dev/null 2>&1;then
  20. systemctl stop firewalld >/dev/null 2>&1
  21. systemctl disable firewalld >/dev/null 2>&1if["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] 检测到 Firewalld 服务已启动,正在将 Firewalld 服务关闭并禁用.${NC}"elseecho-e"${GREEN}[INFO] The Firewalld service has been started, Firewalld service is being turned off and disabled.${NC}"fielseif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] Firewalld 服务已经停止或未安装.${NC}"elseecho-e"${GREEN}[INFO] Firewalld service is not installed.${NC}"fifi}################################################ 描述: 关闭swap# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functiondisable_swap(){if swapoff -a;thensed-i'/swap/s/^/#/' /etc/fstab
  22. if["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] swap 已经禁用.${NC}"elseecho-e"${GREEN}[INFO] swap has been disabled.${NC}"fifi}functioncheck_time_sync(){if timedatectl status |grep"NTP synchronized"|grep-q"yes">/dev/null 2>&1|| timedatectl show |grep"NTPSynchronized=yes">/dev/null 2>&1;thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] NTP 时间同步已经启用.${NC}"elseecho-e"${GREEN}[INFO] NTP time synchronization has been enabled.${NC}"fielseif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${YELLOW}[WARN] NTP 时间同步未启用.${NC}"elseecho-e"${YELLOW}[WARN] NTP time synchronization is not enabled.${NC}"fifi}################################################ 描述: 安装时钟同步,请酌情修改并安装# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################install_chrony(){case$os_typein
  23. ubuntu|debian)if dpkg -l|grep-q chrony >/dev/null 2>&1;thenecho-e"${GREEN}[INFO] chrony 已经安装在主机上.${NC}"elseecho-e"${YELLOW}[WARN] chrony 未安装在主机上, 请执行命令安装 'apt -y install chrony'.${NC}"apt-yinstall chrony &> /dev/null;
  24. systemctl restart chronyd && systemctl enable--now chronyd &> /dev/null
  25. systemctl is-active chronyd &> /dev/null
  26. fi;;
  27. *)ifrpm-qa|grep-q chrony >/dev/null 2>&1;thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] chrony 已经安装在主机上.${NC}"elseecho-e"${GREEN}[INFO] chrony has been installed on the host.${NC}"fielseif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${YELLOW}[WARN] chrony 未安装在主机上, 请执行命令安装 'yum -y install chrony'.${NC}"elseecho-e"${YELLOW}[WARN] chrony is not installed on the host, please execute the command install 'yum -y install chrony'.${NC}"fi
  28. yum -yinstall chrony
  29. fi;;esacif["${CHRONY_TYPE}"=='server'];thensudobash-c'cat > /etc/chrony.conf << EOF
  30. pool ntp.aliyun.com iburst
  31. driftfile /var/lib/chrony/drift
  32. makestep 1.0 3
  33. rtcsync
  34. allow 10.0.0.0/24
  35. local stratum 10
  36. keyfile /etc/chrony.keys
  37. leapsectz right/UTC
  38. logdir /var/log/chrony
  39. EOF'elsesudobash-c'cat > /etc/chrony.conf << EOF
  40. pool ${CHRONY_SERVER} iburst
  41. driftfile /var/lib/chrony/drift
  42. makestep 1.0 3
  43. rtcsync
  44. keyfile /etc/chrony.keys
  45. leapsectz right/UTC
  46. logdir /var/log/chrony
  47. EOF'fi
  48. systemctl restart chronyd && systemctl enable--now chronyd &> /dev/null
  49. systemctl is-active chronyd &> /dev/null
  50. if["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] chrony 完成配置在主机上.${NC}"elseecho-e"${GREEN}[INFO] chrony has been configured on the host.${NC}"fi}################################################ 描述: 优化配置forwarding_ipv4# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioncheck_forwarding_ipv4(){sudobash-c'cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
  51. overlay
  52. br_netfilter
  53. EOF'sudo modprobe overlay
  54. sudo modprobe br_netfilter
  55. sudobash-c'cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
  56. net.bridge.bridge-nf-call-iptables = 1
  57. net.bridge.bridge-nf-call-ip6tables = 1
  58. net.ipv4.ip_forward = 1
  59. EOF'sudosysctl--system#加载ipvs模块sudobash-c'cat <<EOF | sudo tee /etc/modules-load.d/ipvs.conf <<EOF
  60. ip_vs
  61. ip_vs_rr
  62. ip_vs_wrr
  63. ip_vs_sh
  64. nf_conntrack
  65. ip_tables
  66. ip_set
  67. xt_set
  68. ipt_set
  69. ipt_rpfilter
  70. ipt_REJECT
  71. ipip
  72. EOF'
  73. systemctl restart systemd-modules-load.service
  74. }################################################ 描述: 检查服务器资源状况# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioncheck_resource(){cpu=$(grep-c'processor' /proc/cpuinfo)mem=$(free-g|awk'/^Mem/{print $2}')DISK_SPACE=$(df /|sed-n'2p'|awk'{print $2}')# check cpuif["${cpu}"-lt2];thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${YELLOW}[WARN] CPU核数建议至少为2核.${NC}"elseecho-e"${YELLOW}[WARN] The cpu is recommended to be at least 2C.${NC}"fifi# check memoryif["${mem}"-lt3];thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${YELLOW}[WARN] 内存建议至少为8G.${NC}"elseecho-e"${YELLOW}[WARN] The Memory is recommended to be at least 8G.${NC}"fifi# check disk spaceif["${DISK_SPACE}"-lt47185920];thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${YELLOW}[WARN] 根分区空间需大于 50G.${NC}"elseecho-e"${YELLOW}[WARN] The root partition space must be greater than 50G.${NC}"fifi}################################################ 描述: 检查内核版本# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioncheck_kernel(){kernel_version=$(uname-r|awk -F. '{print $1}')if["$kernel_version"-lt"4"];thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${YELLOW}[WARN] 内核版本必须高于4.0, 请尽快升级内核到4.0+.${NC}"elseecho-e"${YELLOW}[WARN] Kernel version must be higher than 4.0, Please upgrade the kernel to 4.0+ as soon as possible.${NC}"fifi}################################################ 描述: 检查 nfs是否安装,这里并未自动安装# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioncheck_nfscli(){case$os_typein
  75. ubuntu|debian)if dpkg -l|grep-q nfs-common >/dev/null 2>&1;thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] nfs-common 已经安装在主机上.${NC}"elseecho-e"${GREEN}[INFO] nfs-common has been installed on the host.${NC}"fielseif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${YELLOW}[WARN] nfs-common 未安装在主机上, 请执行命令安装 'apt -y install nfs-common'.${NC}"elseecho-e"${YELLOW}[WARN] nfs-common is not installed on the host, please execute the command install 'apt-get update && apt -y install nfs-common'.${NC}"fifi;;
  76. *)ifrpm-qa|grep-q nfs-utils >/dev/null 2>&1;thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] nfs-utils 已经安装在主机上.${NC}"elseecho-e"${GREEN}[INFO] nfs-utils has been installed on the host.${NC}"fielseif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${YELLOW}[WARN] nfs-utils 未安装在主机上, 请执行命令安装 'yum -y install nfs-utils'.${NC}"elseecho-e"${YELLOW}[WARN] nfs-utils is not installed on the host, please execute the command install 'yum -y install nfs-utils'.${NC}"fifi;;esac}functioncheck_openssh(){ifssh-V>/dev/null 2>&1;thenOPENSSH_VERSION=$(ssh-V|&awk -F[_.]'{print $2}')if["${OPENSSH_VERSION}"-lt"7"];thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${YELLOW}[WARN] Openssh 版本必须高于 7.0.${NC}"elseecho-e"${YELLOW}[WARN] Openssh version must be higher than 7.0 ${NC}"fifielseif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${RED}[ERROR] 需要安装 7.0+ 版本的openssh.${NC}"exit1elseecho-e"${RED}[ERROR] Need to install 7.0+ version of openssh.${NC}"exit1fifiifgrep-v"^\s*#" /etc/ssh/sshd_config |grep"AllowTcpForwarding yes">/dev/null 2>&1;thenif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${GREEN}[INFO] /etc/ssh/sshd_config 已经配置 AllowTcpForwarding yes.${NC}"elseecho-e"${GREEN}[INFO] /etc/ssh/sshd_config has been configured AllowTcpForwarding yes.${NC}"fielseifgrep"AllowTcpForwarding no" /etc/ssh/sshd_config >/dev/null 2>&1;thensed-i'/AllowTcpForwarding/s/^/#/' /etc/ssh/sshd_config
  77. sed-i'$a\AllowTcpForwarding yes' /etc/ssh/sshd_config
  78. elsesed-i'$a\AllowTcpForwarding yes' /etc/ssh/sshd_config
  79. fiif["$LANG"=="zh_CN.UTF-8"];thenecho-e"${YELLOW}[WARN] /etc/ssh/sshd_config 配置 AllowTcpForwarding yes 成功, 请执行命令重启 sshd 服务生效, 'systemctl restart sshd'.${NC}"elseecho-e"${YELLOW}[WARN] /etc/ssh/sshd_config AllowTcpForwarding yes is successfully configured, Run the following command to restart the sshd service to take effect, 'systemctl restart sshd'.${NC}"fifi}################################################ 描述: 优化参数# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functionoptimize_linux(){sudobash-c'cat > /etc/sysctl.conf << EOF
  80. net.bridge.bridge-nf-call-ip6tables=1
  81. net.bridge.bridge-nf-call-iptables=1
  82. net.ipv4.ip_forward=1
  83. net.ipv4.conf.all.forwarding=1
  84. net.ipv4.neigh.default.gc_thresh1=4096
  85. net.ipv4.neigh.default.gc_thresh2=6144
  86. net.ipv4.neigh.default.gc_thresh3=8192
  87. net.ipv4.neigh.default.gc_interval=60
  88. net.ipv4.neigh.default.gc_stale_time=120
  89. kernel.perf_event_paranoid=-1
  90. #sysctls for k8s node config
  91. net.ipv4.tcp_slow_start_after_idle=0
  92. net.core.rmem_max=16777216
  93. fs.inotify.max_user_watches=524288
  94. kernel.softlockup_all_cpu_backtrace=1
  95. kernel.softlockup_panic=0
  96. kernel.watchdog_thresh=30
  97. fs.file-max=2097152
  98. fs.inotify.max_user_instances=8192
  99. fs.inotify.max_queued_events=16384
  100. vm.max_map_count=262144
  101. fs.may_detach_mounts=1
  102. net.core.netdev_max_backlog=16384
  103. net.ipv4.tcp_wmem=4096 12582912 16777216
  104. net.core.wmem_max=16777216
  105. net.core.somaxconn=32768
  106. net.ipv4.ip_forward=1
  107. net.ipv4.tcp_max_syn_backlog=8096
  108. net.ipv4.tcp_rmem=4096 12582912 16777216
  109. net.ipv6.conf.all.disable_ipv6=1
  110. net.ipv6.conf.default.disable_ipv6=1
  111. net.ipv6.conf.lo.disable_ipv6=1
  112. kernel.yama.ptrace_scope=0
  113. vm.swappiness=0
  114. kernel.core_uses_pid=1
  115. # Do not accept source routing
  116. net.ipv4.conf.default.accept_source_route=0
  117. net.ipv4.conf.all.accept_source_route=0
  118. # Promote secondary addresses when the primary address is removed
  119. net.ipv4.conf.default.promote_secondaries=1
  120. net.ipv4.conf.all.promote_secondaries=1
  121. # Enable hard and soft link protection
  122. fs.protected_hardlinks=1
  123. fs.protected_symlinks=1
  124. net.ipv4.conf.all.rp_filter=0
  125. net.ipv4.conf.default.rp_filter=0
  126. net.ipv4.conf.default.arp_announce = 2
  127. net.ipv4.conf.lo.arp_announce=2
  128. net.ipv4.conf.all.arp_announce=2
  129. net.ipv4.tcp_max_tw_buckets=5000
  130. net.ipv4.tcp_syncookies=1
  131. net.ipv4.tcp_fin_timeout=30
  132. net.ipv4.tcp_synack_retries=2
  133. kernel.sysrq=1
  134. EOF'sudosysctl-p>/dev/null 2>&1echo-e"${GREEN}[INFO] 优化kernel参数成功${NC}"}functionoptimize_limits(){sudobash-c'cat >> /etc/security/limits.conf <<EOF
  135. * soft nofile 1024000
  136. * hard nofile 1024000
  137. EOF'echo-e"${GREEN}[INFO] 优化limits参数成功${NC}"}functioncheck_syscfg(){sudochmod777 /etc/sysctl.conf
  138. sudochmod777 /sbin/sysctl
  139. sudochmod777 /etc/security/limits.conf
  140. optimize_linux
  141. optimize_limits
  142. sudochmod644 /etc/sysctl.conf
  143. sudochmod755 /sbin/sysctl
  144. sudochmod644 /etc/security/limits.conf
  145. }################################################ 描述: calico 网络配置初始化# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioncheck_network(){sudobash-c'cat >> /etc/NetworkManager/conf.d/calico.conf << EOF
  146. [keyfile]
  147. unmanaged-devices=interface-name:cali*;interface-name:tunl*
  148. unmanaged-devices=interface-name:cali*;interface-name:tunl*;interface-name:vxlan.calico;interface-name:wireguard.cali
  149. EOF'
  150. systemctl restart NetworkManager
  151. }################################################ 描述: 主入口函数# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functionmain{echo-e"${GREEN}[INFO] ==========开始检查并配置初始化========= ${NC}"# 停止 friewalld
  152. disable_firewalld
  153. # 关闭 swap partition permanently
  154. disable_swap
  155. # 配置检查时间同步
  156. check_time_sync
  157. # 配置安装时间同步组件
  158. install_chrony
  159. # 配置检查 nfs-utils kubeadmin方式安装不需要检查#check_nfscli# 配置检查内核版本
  160. check_kernel
  161. # 配置检查资源情况
  162. check_resource
  163. # 配置检查SSH
  164. check_openssh
  165. # 配置检查系统配置
  166. check_syscfg
  167. # 转发 IPv4
  168. check_forwarding_ipv4
  169. # 配置检查Docker用户并添加ssh免密认证<authoirzed_keys>
  170. check_user
  171. # 配置检查Docker 容器运行时为Containerd时,不需要检查#check_docker# 配置检查Docker用户权限 容器运行时为Containerd时,不需要检查#check_user_permission# 配置检查网络
  172. check_network
  173. echo-e"${GREEN}[INFO] ==========成功完成检查并配置初始化========= ${NC}"}
  174. main

组件安装

需要安装的组件列表
  1. 安装containerd
  2. 部署containerd 配置文件
  3. 安装cni plugin
  4. 安装crictl
  5. 安装runc
  6. 安装部署kubeadm、kubelet、kubectl

特别关注:
再拷贝kubeadm、kubelet、kubectl文件后,务必记得要创建必须要的几个目录,否则会导致后面初始化控制平面时会出错,详情见完整的安装脚本
sudo mkdir -p /var/lib/kubelet
sudo mkdir -p /etc/kubernetes/manifests
yum 自动从网络安装不会出现该问题,会自动创建。

  1. 初始化集群第一控制平面节点
安装完整的脚本
  1. #!/bin/bash################################################ QingHub K8S Install 版本: $VERSION# 架构: $ARCH_TYPE 目前版本主要支持amd64,其他待敬请期待# 操作系统: $os_type# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################ENV_CFG=./env.cfg
  2. if[-f${ENV_CFG}];thenchmod777${ENV_CFG}source${ENV_CFG}fios_type=$(uname-s)if[$(arch)="x86_64"]||[$(arch)="amd64"];thenarch_type=amd64
  3. elif[$(arch)="aarch64"]||[$(arch)="arm64"];thenarch_type=arm64
  4. elif[$(arch)="i386"];thenarch_type=amd64
  5. echo-e"${YELLOW}[WAIN] 检测到 i386, 我们暂时把它当做 x86_64(amd64) ${NC}"elseecho-e"${RED}[ERROR] QingHub Studio 目前还不支持 $(arch) 架构 ${NC}"exit1fi################################################ 复制并安装cri_containerd_cni 包括:cri, containerd,cni三合一,后续会覆盖安装# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioninstall_cri_containerd_cni(){echo-e"${GREEN}[INFO] 下载并安装cri_containerd_cni二级制文件 ${cri_containerd_cni_name}${NC}"if[[-e /usr/bin/curl ]];then# 离线下载文件请修改TODOcurl-L${cri_containerd_cni_url}|sudotar-C / -xz||{echo-e"${RED}[ERROR] 下载并安装cri_containerd_cni二进制文件失败 ${NC}"exit1}elsewget-c"$cri_containerd_cni_url"|sudotar-C / -xz||{echo-e"${RED}[ERROR] 下载cri_containerd_cni二进制文件失败 ${NC}"exit1}fiecho-e"${GREEN}[INFO] 创建containerd.service 文件 ${NC}"sudo systemctl enable--now containerd
  6. echo-e"${GREEN}[INFO] 安装containerd成功${NC}"}################################################ 描述: 初始化containerd 配置文件,添加修改仓库信息,请酌情修改# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioninstall_containerd_config(){mkdir-p /etc/containerd
  7. sudo /usr/local/bin/containerd config default |tee /etc/containerd/config.toml
  8. # 修改Containerd的配置文件sudosed-i"s#SystemdCgroup\ \=\ false#SystemdCgroup\ \=\ true#g" /etc/containerd/config.toml
  9. cat /etc/containerd/config.toml |grep SystemdCgroup
  10. sudosed-i"s#registry.k8s.io\/pause:3.8#qinghub.net:5000\/qingcloudtech\/pause:3.9#g" /etc/containerd/config.toml
  11. cat /etc/containerd/config.toml |grep sandbox_image
  12. sudosed-i"s#config_path\ \=\ \"\"#config_path\ \=\ \"/etc/containerd/certs.d\"#g" /etc/containerd/config.toml
  13. cat /etc/containerd/config.toml |grep certs.d
  14. mkdir /etc/containerd/certs.d/qinghub.net:5000 -pvsudobash-c'cat > /etc/containerd/certs.d/qinghub.net:5000/hosts.toml << EOF
  15. server = "qinghub.net:5000"
  16. [host."qinghub.net:5000"]
  17. capabilities = ["pull", "resolve"]
  18. EOF'mkdir /etc/containerd/certs.d/docker.io -pvsudobash-c'cat > /etc/containerd/certs.d/docker.io/hosts.toml << EOF
  19. server = "https://docker.io"
  20. [host."https://qinghub.net:5000"]
  21. capabilities = ["pull", "resolve"]
  22. [host."https://dockerproxy.com"]
  23. capabilities = ["pull", "resolve"]
  24. [host."https://docker.m.daocloud.io"]
  25. capabilities = ["pull", "resolve"]
  26. [host."https://reg-mirror.qiniu.com"]
  27. capabilities = ["pull", "resolve"]
  28. [host."http://hub-mirror.c.163.com"]
  29. capabilities = ["pull", "resolve"]
  30. EOF'
  31. systemctl daemon-reload
  32. systemctl restart containerd.service
  33. }################################################ 描述: 安装cni文件# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioninstall_cni_plugins(){DEST="/opt/cni/bin"sudomkdir-p"$DEST"echo-e"${GREEN}[INFO] 下载并安装cni_plugins二级制文件 ${cni_plugins_name}${NC}"if[[-e /usr/bin/curl ]];then# 离线下载文件请修改TODOcurl-L${cni_plugins_url}|sudotar-C"$DEST"-xz||{echo-e"${RED}[ERROR] 下载cni_plugins二进制文件失败 ${NC}"exit1}elsewget-c"$cni_plugins_url"|sudotar-C"$DEST"-xz||{echo-e"${RED}[ERROR] 下载cni_plugins二进制文件失败 ${NC}"exit1}fiecho-e"${GREEN}[INFO] 安装cni_plugins成功${NC}"}################################################ 描述: 拷贝runc文件# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioninstall_runc(){DEST="/usr/bin"sudomkdir-p"$DEST"cd"$DEST"echo-e"${GREEN}[INFO] 下载并安装runc二级制文件 ${runc_name}${NC}"if[[-e /usr/bin/curl ]];then# 离线下载文件请修改TODOcurl-L-O${runc_url}||{echo-e"${RED}[ERROR] 下载并安装runc二进制文件失败 ${NC}"exit1}elsewget-c"$runc_url"||{echo-e"${RED}[ERROR] 下载runc二进制文件失败 ${NC}"exit1}fiif[-f$DEST/runc.amd64 ];thenchmod +x $DEST/runc.amd64
  34. mv$DEST/runc.amd64 $DEST/runc
  35. fiecho-e"${GREEN}[INFO] 安装runc成功${NC}"}################################################ 描述: 拷贝crictl工具文件# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioninstall_crictl(){DEST="/usr/local/bin"sudomkdir-p"$DEST"echo-e"${GREEN}[INFO] 下载并安装crictl二级制文件 ${crictl_name}${NC}"if[[-e /usr/bin/curl ]];then# 离线下载文件请修改TODOsudocurl-L${crictl_url}|sudotar-C$DEST-xz||{echo-e"${RED}[ERROR] 下载crictl二进制文件失败 ${NC}"exit1}elsesudowget-c"$crictl_url"|sudotar-C"$DEST"-xz||{echo-e"${RED}[ERROR] 下载crictl二进制文件失败 ${NC}"exit1}fiecho-e"${GREEN}[INFO] 安装crictl成功${NC}"#生成配置文件sudobash-c"cat > /etc/crictl.yaml <<EOF
  36. runtime-endpoint: unix:///run/containerd/containerd.sock
  37. image-endpoint: unix:///run/containerd/containerd.sock
  38. timeout: 10
  39. EOF"
  40. systemctl restart containerd
  41. }################################################ 描述: 安装kubeadmin,kubelet,kubectl,在每个节点都需要安装kubeadmin,kubelet,# kubectl根据情况在合适的控制平台节点安装,可以修改参数$QINGCLOUD_KUBECTL_FLAG=no关闭安装# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioninstall_kubeadm(){TEMP="/tmp"echo-e"${GREEN}[INFO] 下载Kubernetes二级制文件 ${KUBERNETES_VERSION}${NC}"if[[-e /usr/bin/curl ]];then# 离线下载文件请修改TODOsudocurl-L"${kubernetes_server_url}"|sudotar-C"$TEMP"-xz||{echo-e"${RED}[ERROR] 下载Kubernetes二进制文件失败 ${NC}"exit1}elsesudowget-c"$kubernetes_server_url"|sudotar-C"$TEMP"-xz||{echo-e"${RED}[ERROR] 下载Kubernetes二进制文件失败 ${NC}"exit1}fiDEST="/usr/bin"cd$DESTsudocp$TEMP/kubernetes/server/bin/{kubeadm,kubelet}.sudochmod +x {kubeadm,kubelet}echo-e"${GREEN}[INFO] 成功安装kubeadm,kubelet${DEST}目录 ${NC}"echo-e"${GREEN}[INFO] 创建kubelet.service 文件 ${NC}"sudobash-c"cat > /usr/lib/systemd/system/kubelet.service <<EOF
  42. [Unit]
  43. Description=kubelet: The Kubernetes Node Agent
  44. Documentation=https://kubernetes.io/docs/
  45. Wants=network-online.target
  46. After=network-online.target
  47. [Service]
  48. ExecStart=${DEST}/kubelet
  49. Restart=always
  50. StartLimitInterval=0
  51. RestartSec=10
  52. [Install]
  53. WantedBy=multi-user.target
  54. EOF"sudomkdir-p /usr/lib/systemd/system/kubelet.service.d
  55. echo-e"${GREEN}[INFO] 创建10-kubeadm.conf 文件 ${NC}"sudobash-c"cat > /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf <<EOF
  56. [Service]
  57. Environment=\"KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf\"
  58. Environment=\"KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml\"
  59. EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
  60. EnvironmentFile=-/etc/sysconfig/kubelet
  61. ExecStart=
  62. ExecStart=$DEST/kubelet \\\$KUBELET_KUBECONFIG_ARGS\\\$KUBELET_CONFIG_ARGS\\\$KUBELET_KUBEADM_ARGS\\\$KUBELET_EXTRA_ARGS
  63. EOF"sudobash-c"cat > /etc/sysconfig/kubelet <<EOF
  64. KUBELET_EXTRA_ARGS=
  65. EOF"#创建必须要的几个目录sudomkdir-p /var/lib/kubelet
  66. sudomkdir-p /etc/kubernetes/manifests
  67. sudo systemctl enable kubelet
  68. if["$QINGCLOUD_KUBECTL_FLAG"=="yes"];thencd$DESTsudocp${TEMP}/kubernetes/server/bin/kubectl .sudochmod +x kubectl
  69. echo-e"${GREEN}[INFO] 成功安装kubectl${DEST}目录 ${NC}"fiif[[-d"${TEMP}/kubernetes"]];thensudorm-rf"${TEMP}/kubernetes-server-linux-amd64"echo-e"${GREEN}[INFO] 临时安装文件${TEMP}/kubernetes-server-linux-amd64正常删除 ${NC}"fi}################################################ 描述: 初始化控制平面节点。# ${QINGCLOUD_ADVERTISE_ADDRESS} ApiServer地址,请根据情况填写,可以是配置的高可用地址# ${LOCAL_IMAGE_DOMAIN} 本地容器镜像仓库地址# ${LOCAL_IMAGE_NAMESPACE} 本地容器镜像命名空间# ${KUBERNETES_VERSION} K8S版本# QingHub Studio官网: https://qinghub.net# 如过您安装遇到问题,请到官网查找官方联系方式或加支持群:# https://qinghub.net###############################################functioninit_cluster(){echo-e"${GREEN}[INFO] ==========初始化第一个控制平面,请根据输出结果初始其他他节点========= ${NC}"
  70. kubeadm init \
  71. --apiserver-advertise-address="${QINGCLOUD_ADVERTISE_ADDRESS}"\
  72. --image-repository "${LOCAL_IMAGE_DOMAIN}/${LOCAL_IMAGE_NAMESPACE}"\
  73. --kubernetes-version "${KUBERNETES_VERSION}"\
  74. --cri-socket=unix:///run/containerd/containerd.sock \
  75. --service-cidr=10.96.0.0/16 \
  76. --pod-network-cidr=172.16.0.0/16
  77. }# 主入口地址,根据实际可以手动执行相关内容functionmain{echo-e"${GREEN}[INFO] ==========开始安装kubernetes========= ${NC}"#安装containerd
  78. install_cri_containerd_cni
  79. #安装cni plugin
  80. install_cni_plugins
  81. #安装containerd 配置文件
  82. install_containerd_config
  83. #安装crictl
  84. install_crictl
  85. #安装runc
  86. install_runc
  87. #安装kubeadm
  88. install_kubeadm
  89. #初始化集群master节点if["$QINGCLOUD_KUBECTL_FLAG"=="yes"];then
  90. init_cluster
  91. fiecho-e"${GREEN}[INFO] ==========完成安装kubernetes========= ${NC}"}
  92. main

执行命令初始化第一个控制平面节点

在上节的安装过程中,实际以及包含了初始化第一个控制平面的脚本,由于其重要性,这里单独提出来详细说明。
通过执行kubeadm init 指令快速初始化控制平面,可以通过直接待参数的方式或通过–config加配置文件的方式实现:

配置参数形式
  1. kubeadm init \
  2. --apiserver-advertise-address="${QINGCLOUD_ADVERTISE_ADDRESS}"\
  3. --image-repository "${LOCAL_IMAGE_DOMAIN}/${LOCAL_IMAGE_NAMESPACE}"\
  4. --kubernetes-version "${KUBERNETES_VERSION}"\
  5. --cri-socket=unix:///run/containerd/containerd.sock \
  6. --service-cidr=10.96.0.0/16 \
  7. --pod-network-cidr=172.16.0.0/16
配置文件形式

第一步:如下命令生成默认配置文件并作修改

  1. kubeadm config print init-defaults > initConfig.yaml

第二步:执行命令

  1. kubeadm init --config=initConfig.yaml
准备kubectl配置文件

根据上面的指令生成的结果,拷贝如下命令后直接执行,使得kubectl后面可以直接运行

  1. mkdir-p$HOME/.kube
  2. sudocp-i /etc/kubernetes/admin.conf $HOME/.kube/config
  3. sudochown$(id-u):$(id-g)$HOME/.kube/config

加入Node节点

根据执行命令生成第一个控制平面的结果,拷贝如下类似的文件到node节点。直接执行:

  1. kubeadm join x.x.x.x:6443 --token picxyk.su53y03l1z0jh333e \
  2. --discovery-token-ca-cert-hash sha256:01fecbd4bdc0513811f7b65a43e41820d703a91c631e262b1ad4634e12cd0205
  3. --cri-socket=unix:///run/containerd/containerd.sock

执行kubectl get nodes 查看node节点执行结果

  1. [root@itserver-master2 kube]# kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. itserver-master2 Ready control-plane 2h v1.29.4
  4. itserver-node4 Ready <none> 1m v1.29.4
  5. [root@itserver-master2 kube]#

部署网络插件

我们采用calico作为网络插件,calico最新版建议的部署方式为两个步骤,执行两个文件即可:
tigera-operator.yaml、custom-resources.yaml

下载tigera-operator.yaml

https://github.com/projectcalico/calico/blob/v3.27.3/manifests/tigera-operator.yaml
https://github.com/projectcalico/calico/blob/v3.27.3/manifests/custom-resources.yaml

修改tigera-operator.yaml文件内容(离线版)

注意:修改镜像地址,请根据自己保存镜像的实际地址修改

  1. [root@web02 v1.29.4]# cat tigera-operator.yaml | grep image:
  2. image:
  3. image: quay.io/tigera/operator:v1.32.7
  4. [root@web02 v1.29.4]# [root@web02 v1.29.4]# sudo sed -i "s#quay.io\/tigera#qinghub.net:5000\/qingcloudtech#g" tigera-operator.yaml[root@web02 v1.29.4]# cat tigera-operator.yaml | grep image:
  5. image:
  6. image: qinghub.net:5000/qingcloudtech/operator:v1.32.7
  7. [root@web02 v1.29.4]# ll
执行kubectl create -f tigera-operator.yaml
  1. [root@itserver-master2 kube]# kubectl create -f tigera-operator.yaml
  2. namespace/tigera-operator created
  3. customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
  4. customresourcedefinition.apiextensions.k8s.io/bgpfilters.crd.projectcalico.org created
  5. customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
  6. customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
  7. customresourcedefinition.apiextensions.k8s.io/caliconodestatuses.crd.projectcalico.org created
  8. customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
  9. customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
  10. customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
  11. customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
  12. customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
  13. customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
  14. customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
  15. customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
  16. customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
  17. customresourcedefinition.apiextensions.k8s.io/ipreservations.crd.projectcalico.org created
  18. customresourcedefinition.apiextensions.k8s.io/kubecontrollersconfigurations.crd.projectcalico.org created
  19. customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
  20. customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created
  21. customresourcedefinition.apiextensions.k8s.io/apiservers.operator.tigera.io created
  22. customresourcedefinition.apiextensions.k8s.io/imagesets.operator.tigera.io created
  23. customresourcedefinition.apiextensions.k8s.io/installations.operator.tigera.io created
  24. customresourcedefinition.apiextensions.k8s.io/tigerastatuses.operator.tigera.io created
  25. serviceaccount/tigera-operator created
  26. clusterrole.rbac.authorization.k8s.io/tigera-operator created
  27. clusterrolebinding.rbac.authorization.k8s.io/tigera-operator created
  28. deployment.apps/tigera-operator created
  29. [root@itserver-master2 kube]#
查看结果
  1. [root@itserver-master2 kube]# kubectl get pods -n tigera-operator
  2. NAME READY STATUS RESTARTS AGE
  3. tigera-operator-6779dc6889-zd4zt 1/1 Running 0 55s
  4. [root@itserver-master2 kube]#
修改custom-resources.yaml

注意: 主要修改内容:cidr: 172.16.0.0/16,需要与控制平面初始化时的地址填写的地址一直

  1. spec:
  2. # Configures Calico networking.
  3. calicoNetwork:
  4. # Note: The ipPools section cannot be modified post-install.
  5. ipPools:
  6. - blockSize: 26
  7. cidr: 172.16.0.0/12
  8. encapsulation: VXLANCrossSubnet
  9. natOutgoing: Enabled
  10. nodeSelector: all()
执行kubectl create -f custom-resources.yaml
  1. [root@itserver-master2 kube]# kubectl create -f custom-resources.yaml
  2. installation.operator.tigera.io/default created
  3. apiserver.operator.tigera.io/default created
  4. [root@itserver-master2 kube]# kubectl get ns
  5. NAME STATUS AGE
  6. calico-system Active 48s
  7. default Active 3h5m
  8. kube-node-lease Active 3h5m
  9. kube-public Active 3h5m
  10. kube-system Active 3h5m
  11. tigera-operator Active 6m35s
  12. [root@itserver-master2 kube]# kubectl get pods -n calico-system
  13. NAME READY STATUS RESTARTS AGE
  14. calico-kube-controllers-68bf945ffc-mf7t2 0/1 ContainerCreating 0 75s
  15. calico-node-27fgm 0/1 Init:ImagePullBackOff 0 75s
  16. calico-typha-5886b45b65-pmsm7 0/1 ErrImagePull 0 75s
  17. csi-node-driver-9b29j 0/2 ContainerCreating 0 75s
  18. [root@itserver-master2 kube]#
calico网络安装后,检查所有空间众的pod:
  1. [root@itserver-master2 certs.d]# kubectl get pods --all-namespaces
  2. NAMESPACE NAME READY STATUS RESTARTS AGE
  3. calico-apiserver calico-apiserver-864697c659-2sdhd 1/1 Running 0 4m18s
  4. calico-apiserver calico-apiserver-864697c659-c2vp9 1/1 Running 0 4m18s
  5. calico-system calico-kube-controllers-68bf945ffc-dvrlf 1/1 Running 0 63m
  6. calico-system calico-node-27fgm 1/1 Running 0 18h
  7. calico-system calico-node-zwpls 1/1 Running 0 17h
  8. calico-system calico-typha-5886b45b65-pmsm7 1/1 Running 0 18h
  9. calico-system csi-node-driver-9b29j 2/2 Running 0 18h
  10. calico-system csi-node-driver-mrtq5 2/2 Running 0 17h
  11. kube-system coredns-67bd986d4c-67fvl 1/1 Running 0 16m
  12. kube-system coredns-67bd986d4c-x7vk7 1/1 Running 0 56m
  13. kube-system etcd-itserver-master2 1/1 Running 1 21h
  14. kube-system kube-apiserver-itserver-master2 1/1 Running 1 21h
  15. kube-system kube-controller-manager-itserver-master2 1/1 Running 1 21h
  16. kube-system kube-proxy-9rv85 1/1 Running 0 21h
  17. kube-system kube-proxy-l9rht 1/1 Running 1 17h
  18. kube-system kube-scheduler-itserver-master2 1/1 Running 1 21h
  19. tigera-operator tigera-operator-6779dc6889-zd4zt 1/1 Running 0 18h
  20. [root@itserver-master2 certs.d]#

看到如上几个空间中的状态都变为runging时,网络部署成功

测试验证

部署nginx

第一步、部署文件准备nginx.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: nginx
  5. labels:
  6. app: nginx
  7. spec:
  8. ports:
  9. - port: 80
  10. name: web
  11. selector:
  12. app: nginx
  13. ---
  14. apiVersion: apps/v1
  15. kind: Deployment
  16. metadata:
  17. name: nginx-deployment
  18. spec:
  19. replicas: 1
  20. selector:
  21. matchLabels:
  22. app: nginx
  23. template:
  24. metadata:
  25. labels:
  26. app: nginx
  27. spec:
  28. containers:
  29. - name: nginx
  30. image: qingcloudtech/nginx:1.25.4
  31. ports:
  32. - containerPort: 80
  33. name: web

第二部、执行命令

  1. kubectl apply -f nginx.yaml

第三步、检查service及deployment

  1. [root@itserver-master2 kube]# kubectl get svc
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. kubernetes ClusterIP 10.96.0.1 <none>443/TCP 22h
  4. nginx ClusterIP 10.101.221.253 <none>80/TCP 17s
  5. [root@itserver-master2 kube]# kubectl get deploy -o wide
  6. NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
  7. nginx-deployment 1/1 11 35s nginx qingcloudtech/nginx:1.25.4 app=nginx
  8. [root@itserver-master2 kube]# [root@itserver-master2 kube]# curl http://172.24.128.130<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>
  9. html { color-scheme: light dark;}
  10. body { width: 35em; margin: 0 auto;
  11. font-family: Tahoma, Verdana, Arial, sans-serif;}</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed and
  12. working. Further configuration is required.</p><p>For online documentation and support please refer to
  13. <a href="http://nginx.org/">nginx.org</a>.<br/>
  14. Commercial support is available at
  15. <a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>[root@itserver-master2 kube]#

至此,完成整个部署过程


你可以通过【QingHub Studio】) 套件直接安装部署,也可以手动按如下文档操作,该项目已经全面开源,完整的脚本可以从如下开源地址获取:
开源地址: https://gitee.com/qingplus/qingcloud-platform
【QingHub Studio集成开发套件】


本文转载自: https://blog.csdn.net/tanlintanlin/article/details/137949646
版权归原作者 轻云UC 所有, 如有侵权,请联系我们删除。

“【k8s】Kubernetes 1.29.4离线安装部署(总)”的评论:

还没有评论