使用kubeadm安装kubernetes集群脚本(更新日期:2024.7.19) ...

立山  金牌会员 | 2024-7-19 15:45:02 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 860|帖子 860|积分 2580

现在kubernetes使用的 CRI(容器运行时)主流的是docker和containerd,以下是一个自动化安装kubernetes集群的脚本,并且支持两种运行时,按照如下步调操作即可。
系统版本为centos 7.9,docker的安装方式我已经试验过没问题了
1. 添加脚本
  1. vi /usr/local/bin/kubestart
复制代码
将以下内容复制进去
  1. #! /bin/bash
  2. ###############################################
  3. ##       Simple Install Your K8S
  4. ###############################################
  5. kube_pod_subnet="10.244.0.0/16"
  6. # kubernetes版本
  7. kube_version="1.23.9"
  8. kube_image_server="registry.cn-hangzhou.aliyuncs.com/google_containers"
  9. crictl_url="https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.21.0/"
  10. crictl_name="crictl-v1.21.0-linux-amd64.tar.gz"
  11. # 关闭SElinux
  12. function init-env-disable-selinux()
  13. {
  14.   sudo setenforce 0
  15.   sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
  16. }
  17. # 禁用防火墙
  18. function init-env-disable-firewalld()
  19. {
  20.   sudo systemctl stop firewalld
  21.   sudo systemctl disable --now firewalld
  22. }
  23. # 添加 kubernetes.repo 文件和一些必要工具
  24. function init-env-repository()
  25. {
  26.   # kubernetes.repo
  27.   sudo echo -e "[kubernetes] \nname=Kubernetes \nbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ \nenabled=1 \ngpgcheck=1 \nrepo_gpgcheck=1 \ngpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg" > /etc/yum.repos.d/kubernetes.repo
  28.   # replace base.repo
  29.   mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
  30.   echo '# CentOS-Base.repo' > CentOS-Base.repo
  31.   echo '#' >> CentOS-Base.repo
  32.   echo '# The mirror system uses the connecting IP address of the client and the' >> CentOS-Base.repo
  33.   echo '# update status of each mirror to pick mirrors that are updated to and' >> CentOS-Base.repo
  34.   echo '# geographically close to the client.  You should use this for CentOS updates' >> CentOS-Base.repo
  35.   echo '# unless you are manually picking other mirrors.' >> CentOS-Base.repo
  36.   echo '#' >> CentOS-Base.repo
  37.   echo '# If the mirrorlist= does not work for you, as a fall back you can try the ' >> CentOS-Base.repo
  38.   echo '# remarked out baseurl= line instead.' >> CentOS-Base.repo
  39.   echo '#' >> CentOS-Base.repo
  40.   echo '#' >> CentOS-Base.repo
  41.   echo ' ' >> CentOS-Base.repo
  42.   echo '[base]' >> CentOS-Base.repo
  43.   echo 'name=CentOS-$releasever - Base - mirrors.aliyun.com' >> CentOS-Base.repo
  44.   echo 'failovermethod=priority' >> CentOS-Base.repo
  45.   echo 'baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/' >> CentOS-Base.repo
  46.   echo '        https://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/' >> CentOS-Base.repo
  47.   echo '        https://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/' >> CentOS-Base.repo
  48.   echo 'gpgcheck=1' >> CentOS-Base.repo
  49.   echo 'gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' >> CentOS-Base.repo
  50.   echo ' ' >> CentOS-Base.repo
  51.   echo '#released updates ' >> CentOS-Base.repo
  52.   echo '[updates]' >> CentOS-Base.repo
  53.   echo 'name=CentOS-$releasever - Updates - mirrors.aliyun.com' >> CentOS-Base.repo
  54.   echo 'failovermethod=priority' >> CentOS-Base.repo
  55.   echo 'baseurl=https://mirrors.aliyun.com/centos/$releasever/updates/$basearch/' >> CentOS-Base.repo
  56.   echo '        https://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/' >> CentOS-Base.repo
  57.   echo '        https://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/' >> CentOS-Base.repo
  58.   echo 'gpgcheck=1' >> CentOS-Base.repo
  59.   echo 'gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' >> CentOS-Base.repo
  60.   echo ' ' >> CentOS-Base.repo
  61.   echo '#additional packages that may be useful' >> CentOS-Base.repo
  62.   echo '[extras]' >> CentOS-Base.repo
  63.   echo 'name=CentOS-$releasever - Extras - mirrors.aliyun.com' >> CentOS-Base.repo
  64.   echo 'failovermethod=priority' >> CentOS-Base.repo
  65.   echo 'baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/' >> CentOS-Base.repo
  66.   echo '        https://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/' >> CentOS-Base.repo
  67.   echo '        https://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/' >> CentOS-Base.repo
  68.   echo 'gpgcheck=1' >> CentOS-Base.repo
  69.   echo 'gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' >> CentOS-Base.repo
  70.   echo ' ' >> CentOS-Base.repo
  71.   echo '#additional packages that extend functionality of existing packages' >> CentOS-Base.repo
  72.   echo '[centosplus]' >> CentOS-Base.repo
  73.   echo 'name=CentOS-$releasever - Plus - mirrors.aliyun.com' >> CentOS-Base.repo
  74.   echo 'failovermethod=priority' >> CentOS-Base.repo
  75.   echo 'baseurl=https://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/' >> CentOS-Base.repo
  76.   echo '        https://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/' >> CentOS-Base.repo
  77.   echo '        https://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/' >> CentOS-Base.repo
  78.   echo 'gpgcheck=1' >> CentOS-Base.repo
  79.   echo 'enabled=0' >> CentOS-Base.repo
  80.   echo 'gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' >> CentOS-Base.repo
  81.   echo ' ' >> CentOS-Base.repo
  82.   echo '#contrib - packages by Centos Users' >> CentOS-Base.repo
  83.   echo '[contrib]' >> CentOS-Base.repo
  84.   echo 'name=CentOS-$releasever - Contrib - mirrors.aliyun.com' >> CentOS-Base.repo
  85.   echo 'failovermethod=priority' >> CentOS-Base.repo
  86.   echo 'baseurl=https://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/' >> CentOS-Base.repo
  87.   echo '        https://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/' >> CentOS-Base.repo
  88.   echo '        https://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/' >> CentOS-Base.repo
  89.   echo 'gpgcheck=1' >> CentOS-Base.repo
  90.   echo 'enabled=0' >> CentOS-Base.repo
  91.   echo 'gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' >> CentOS-Base.repo
  92.   echo ' ' >> CentOS-Base.repo
  93.   # docker repo
  94.   sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  95.   cd /etc/yum.repos.d
  96.   for file in *.repo; do
  97.     if [[ "$file" != "CentOS-Base.repo" && "$file" != "kubernetes.repo" && "$file" != "docker-ce.repo" ]]; then
  98.       mv "$file" "$file.bak"
  99.     fi
  100.   done
  101.   yum clean all
  102.   yum makecache
  103.   # tools
  104.   sudo yum install -y yum-utils device-mapper-persistent-data lvm2
  105. }
  106. # 安装 kubelet kubeadm kubectl
  107. function init-install-kube()
  108. {
  109.   sudo yum install -y kubeadm-${kube_version} kubectl-${kube_version} kubelet-${kube_version} --disableexcludes=kubernetes
  110. }
  111. # 安装docker
  112. function init-docker()
  113. {
  114.   sudo yum install docker-ce docker-ce-cli containerd.io
  115.   systemctl start docker
  116.   systemctl enable docker
  117.   # 更改 cgroupdriver 为 systemd,设置代理
  118.   echo -e "{" > /etc/docker/daemon.json
  119.   echo -e "  "exec-opts": ["native.cgroupdriver=systemd"]," >> /etc/docker/daemon.json
  120.   echo -e "  "registry-mirrors": ["https://docker.m.daocloud.io"]" >> /etc/docker/daemon.json
  121.   echo -e "}" >> /etc/docker/daemon.json
  122.   systemctl restart docker
  123.   systemctl enable kubelet
  124. }
  125. # 安装containerd
  126. function init-containerd()
  127. {
  128.   sudo yum install -y wget containerd.io openvswitch* certbot
  129.   mkdir -p /etc/containerd
  130.   cd /home
  131.   wget $crictl_url$crictl_name
  132.   tar zxf $crictl_name
  133.   mv crictl /usr/local/bin/
  134.   rm -rf $crictl_name
  135.   echo "overlay" > /etc/modules-load.d/containerd.conf  
  136.   echo "br_netfilter" >> /etc/modules-load.d/containerd.conf  
  137.   sudo modprobe overlay
  138.   sudo modprobe br_netfilter
  139.   echo "net.bridge.bridge-nf-call-iptables  = 1" > /etc/sysctl.d/99-kubernetes-cri.conf
  140.   echo "net.ipv4.ip_forward                 = 1" >> /etc/sysctl.d/99-kubernetes-cri.conf
  141.   echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.d/99-kubernetes-cri.conf
  142.   sudo sysctl --system
  143.   sudo containerd config default > /etc/containerd/config.toml
  144.   sed -i 's/k8s.gcr.io\/pause:3.1/registry.cn-hangzhou.aliyuncs.com\/google_containers\/pause:3.2/g' /etc/containerd/config.toml
  145.   sudo systemctl restart containerd
  146.   sudo systemctl enable containerd
  147.   echo "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd" > /etc/sysconfig/kubelet
  148.   echo "runtime-endpoint: unix:///run/containerd/containerd.sock" > /etc/crictl.yaml
  149.   echo "image-endpoint: unix:///run/containerd/containerd.sock" >> /etc/crictl.yaml
  150.   echo "timeout: 10" >> /etc/crictl.yaml
  151.   echo "debug: false" >> /etc/crictl.yaml
  152.   sudo systemctl restart kubelet
  153.   sudo systemctl enable kubelet
  154. }
  155. function init-env-kubeconfig()
  156. {
  157.   mkdir /etc/kubernetes
  158.   echo -e "apiVersion: kubeadm.k8s.io/v1beta3" > /etc/kubernetes/kubeadm.yaml
  159.   echo -e "kind: InitConfiguration" >> /etc/kubernetes/kubeadm.yaml
  160.   echo -e "bootstrapTokens:" >> /etc/kubernetes/kubeadm.yaml
  161.   echo -e "  - ttl: "0"" >> /etc/kubernetes/kubeadm.yaml
  162.   echo -e "---" >> /etc/kubernetes/kubeadm.yaml
  163.   echo -e "apiVersion: kubeadm.k8s.io/v1beta3" >> /etc/kubernetes/kubeadm.yaml
  164.   echo -e "kind: ClusterConfiguration" >> /etc/kubernetes/kubeadm.yaml
  165.   echo -e "networking:" >> /etc/kubernetes/kubeadm.yaml
  166.   echo -e "  podSubnet: "${kube_pod_subnet}"" >> /etc/kubernetes/kubeadm.yaml
  167.   echo -e "kubernetesVersion: "v${kube_version}"" >> /etc/kubernetes/kubeadm.yaml
  168.   echo -e "imageRepository: "${kube_image_server}"" >> /etc/kubernetes/kubeadm.yaml
  169. }
  170. function init-env-kubecomp()
  171. {
  172.   # default calico cni
  173.   echo -e "https://docs.projectcalico.org/manifests/calico.yaml" > /etc/kubernetes/kubeenv.list
  174.   # flannel: cni config url
  175.   # echo -e "https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml" > /etc/kubernetes/kubeenv.list
  176. }
  177. function init-env()
  178. {
  179.   init-env-disable-selinux
  180.   init-env-disable-firewalld
  181.   init-env-repository
  182.   init-install-kube
  183.   if [[ -z $2 ]]
  184.   then
  185.     init-docker
  186.   elif [[ $2 == "docker" ]]
  187.   then
  188.     init-docker
  189.   elif [[ $2 == "containerd" ]]
  190.   then
  191.     init-containerd
  192.   else
  193.     echo "only support docker or containerd"
  194.     exit 1
  195.   fi
  196.   init-env-kubeconfig
  197.   init-env-kubecomp
  198. }
  199. function run-kube()
  200. {
  201.   sudo swapoff -a && sysctl -w vm.swappiness=0
  202.   sudo sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
  203.   systemctl restart kubelet
  204.   echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables
  205.   echo "1" > /proc/sys/net/ipv4/ip_forward
  206.   kubeadm init --config /etc/kubernetes/kubeadm.yaml
  207.   rm -rf $HOME/.kube
  208.   mkdir -p $HOME/.kube
  209.   sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  210.   sudo chown $(id -u):$(id -g) $HOME/.kube/config
  211.   iptables -P FORWARD ACCEPT
  212.   
  213.   while read line
  214.   do
  215.     kubectl apply -f $line
  216.   done  < /etc/kubernetes/kubeenv.list
  217. }
  218. function help()
  219. {
  220.   echo -e "Commands:"
  221.   echo -e "  init-env      :\t(Init): Initialize the environment configuration, simplify configuring node, such as disable selinux, install docker or containerd..."
  222.   echo -e "  run-kube      :\t(Init): deploy Kubernetes as your want by editing /etc/kubernetes/kubeenv.list. Now it includes calico, flannel"
  223. }
  224. case $1 in
  225.   "init-env")
  226.     init-env $*
  227.     ;;
  228.   "run-kube")
  229.     run-kube $*
  230.     ;;
  231.   "--help")
  232.     help
  233.     ;;
  234.   *)
  235.   help
  236.   ;;
  237. esac
复制代码
2. 修改为可执行文件
  1. sudo chmod +x /usr/local/bin/kubestart
复制代码
3. 支持的命令

然后你就可以使用以下两条命令去创建你的k8s集群了
  1. # 初始化各种环境配置
  2. kubestart init-env
  3. # 启动集群,应用cni
  4. kubestart run-kube
  5. # 帮助命令
  6. kubestart help
复制代码
4. 可能遇到的问题


  • 关于镜像拉取,现在dockerhub已经不能使用,脚本里面设置了镜像代理:https://docker.m.daocloud.io,拉不下来的可以自己使用docker pull命令多拉几次,仍旧欠好使或者您的集群可以顺利从dockerhub拉去镜像不必要代理的话,可以去掉这个设置:
  1. vi /etc/docker/daemon.json
  2. # 删除掉这行
  3. "registry-mirrors": ["https://docker.m.daocloud.io"]
  4. # 删除掉之后注意保持json文件格式正确,上一行末尾的逗号去掉,然后保存执行
  5. systemctl restart docker
  6. systemctl enable kubelet
复制代码
PS 脚本分析:

  • 脚本开头的几个参数,是关于你想使用的 k8s 版本以及 crictl 安装包,想要使用其他版本的可以在开头改一下。
想要检察现在支持哪些版本可以使用以下命令:
  1. yum list --showduplicates kubeadm --disableexcludes=kubernetes
复制代码

  • kubestart init-env 命令后面还可以添加其他参数,选择容器运行时,可以使用 docker 或者 containerd,默认是 docker
  • 执行 kubeadm init 命令使用的是 /etc/kubernetes/kubeadm.yaml 设置文件启动的,如果有特殊必要,在执行完 init-env 命令后去修改这个文件内容就可以,其实启动集群的方式就是这条命令:
  1. kubeadm init --config /etc/kubernetes/kubeadm.yaml
复制代码

  • 默认使用的是 calico 作为 cni 应用,你如果想使用其他的,去修改一下脚本里面的 init-env-kubecomp 函数即可。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

立山

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表