这篇文章主要为大家展示了“怎么利用python脚本自动部署k8s”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么利用python脚本自动部署k8s”这篇文章吧。
一、准备
通过之前在Ubuntu18.04上手动部署过k8s之后,尝试用python脚本进行自动化部署
这次用的是三台centos7的虚拟机,一台作为master执行脚本,两台作为node节点
三台机器都配置好静态IP,可以参考之前的在centos 7中安装配置k8s集群的步骤详解
二、编辑脚本
1、k8s.sh
放在/root
下,用于从dockerhub拉取镜像。
set -o errexitset -o nounsetset -o pipefail##这里定义版本,按照上面得到的列表自己改一下版本号KUBE_VERSION=v1.21.3KUBE_PAUSE_VERSION=3.4.1ETCD_VERSION=3.4.13-0##这是原始仓库名,最后需要改名成这个GCR_URL=k8s.gcr.io##这里就是写你要使用的仓库DOCKERHUB_URL=gotok8s##这里是镜像列表,新版本要把coredns改成coredns/corednsimages=(kube-proxy:${KUBE_VERSION}kube-scheduler:${KUBE_VERSION}kube-controller-manager:${KUBE_VERSION}kube-apiserver:${KUBE_VERSION}pause:${KUBE_PAUSE_VERSION}etcd:${ETCD_VERSION})##这里是拉取和改名的循环语句for imageName in ${images[@]} ; dodocker pull $DOCKERHUB_URL/$imageNamedocker tag $DOCKERHUB_URL/$imageName $GCR_URL/$imageNamedocker rmi $DOCKERHUB_URL/$imageNamedonedocker pull coredns/coredns:1.8.0docker tag coredns/coredns:1.8.0 k8s.gcr.io/coredns/coredns:v1.8.0docker rmi coredns/coredns:1.8.0
2、k8s_install.py
#!/bin/python3# -*- coding:utf-8 -*-# author: fanb# describe: K8S v1.21.2 一键脚本安装import osimport subprocessimport timeclass k8s_install(object): def __init__(self,masterip,nodeip): self.masterip = masterip self.nodeip = nodeip def initialization_shell(self): #环境初始化shell # 关闭防火墙 setenforce = "setenforce 0" sed_selinux = "sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux" sed_selinux1 = "sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config" sed_selinux2 = "sed -i 's/^SELINUX=permissive/SELINUX=disabled/g' /etc/sysconfig/selinux" sed_selinux3 = "sed -i 's/^SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config" stop_firewalld = "systemctl stop firewalld" disable_firewalld = "systemctl disable firewalld" swapoff_a = "swapoff -a" sed_swapoff = "sed -i 's/.*swap.*/#&/' /etc/fstab" #在所有服务器配置国内yum源 yum_install = "yum install -y wget git chrony yum-utils device-mapper-persistent-data lvm2 ipset ipvsadm > /dev/null 2>&1" mkdir_repo = "mkdir /etc/yum.repos.d/bak && mv /etc/yum.repos.d#&/' /etc/kubernetes/manifests/kube-controller-manager.yaml") scheduler_yaml = os.system("sed -i 's/.*- --port=0*/#&/' /etc/kubernetes/manifests/kube-scheduler.yaml") #配置flannel网络 print("*" * 20, "正在安装网络组件flannel....") flannel_before = os.system("%s" %dir2) flannel_wget = os.system("wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml") yum_flannel = os.system("yum install -y flannel") flannel_apply = os.system("kubectl apply -f /root/kube-flannel.yml") print("*" * 20, "网络组件flannel安装完成....") token_creat = subprocess.getstatusoutput("kubeadm token create") token_code = subprocess.getstatusoutput("openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'") token_creat = token_creat[1].split('\n')[-1] token_code = token_code[1] # 安装从节点 for nodeip in nodeip_list: os.system("scp -rp /etc/hosts %s:/etc/hosts" % nodeip) print("*" * 20, "进入Node节点操作,当前IP: %s" % nodeip) node_num += 1 node_name = "node0%s" % (node_num) # 设置名字 os.system("ssh %s \"hostname %s\"" % (nodeip,node_name)) os.system("ssh %s \"echo '%s' > /etc/hostname\"" % (nodeip,node_name)) print("*" * 20, "进入环境初始化,请耐心等待....") for shell in self.initialization_shell(): time.sleep(1) os.system("ssh %s \"%s\"" %(nodeip,shell)) enable_node = os.system("ssh %s \"systemctl enable kubelet\"" %nodeip) start_node = os.system("ssh %s \"systemctl start kubelet\"" %nodeip) admin = os.system("scp /etc/kubernetes/admin.conf %s:/root" %nodeip) print("*" * 20, "正在加入集群....") print("token_creat : ",token_creat) print("token_code : ",token_code) docker_problem = os.system("scp -r /etc/docker/daemon.json %s:/etc/docker" %nodeip) restart_docker = os.system("ssh %s \"systemctl restart docker\"" %nodeip) status_docker = os.system("ssh %s \"systemctl status docker\"" %nodeip) kubeadm_join = os.system("ssh %s \"kubeadm join %s:6443 --token %s --discovery-token-ca-cert-hash sha256:%s\"" % (nodeip,masterip, str(token_creat), str(token_code))) cni = os.system("scp -r /etc/cni %s:/etc" %nodeip) print("*" * 20, "加入集群成功....") print("*" * 20 ,"执行以下命令,检查K8s集群\n") print("*" * 20,"kubectl get nodes") print("*" * 20, "kubectl get cs") print("*" * 20, "kubectl get pod -n kube-system") else: #否则就是集群模式 print("进入集群模式安装") print("暂无") exit()if __name__ == '__main__': # #用户输入IP: print("----------0、请先安装python3 并使用python3 执行此脚本------------") print("----------1、此脚本依赖网络,请连接好网络执行此脚本-----------") print("----------2、请将此脚本在主节点上执行,请在主节点上对其他所有节点做免密登录-----------") print("**********3、请确认主节点已对其他节点做好免密登录,再次确认后再执行此脚本**********") k8s_masterip = input("请输入K8S_Master IP, 多个IP以逗号分隔: ") k8s_nodeip = input("请输入K8S_node IP,多个IP以逗号分隔: ") ask_ent = input("********** 确认/取消 (Y/N) :") if ask_ent.upper() == "Y": k8s_install = k8s_install(k8s_masterip,k8s_nodeip) k8s_install.shell_command() else: exit()
三、配置ssh免密
[root@master ~]# ssh-keygen[root@master ~]# ssh-copy-id 192.168.139.132[root@master ~]# ssh-copy-id 192.168.139.133
四、下载python3和git
root@master ~]# vim k8s_install.py
五、执行脚本
[root@master ~]# python3 k8s_install.py
六、成功
******************** 执行以下命令,检查K8s集群******************** kubectl get nodes******************** kubectl get cs******************** kubectl get pod -n kube-system[root@master ~]# kubectl get nodesNAME STATUS ROLES AGE VERSIONmaster01 Ready control-plane,master 7m2s v1.21.2node01 Ready <none> 3m30s v1.21.2node02 Ready <none> 25s v1.21.2[root@master ~]# kubectl get csWarning: v1 ComponentStatus is deprecated in v1.19+NAME STATUS MESSAGE ERRORscheduler Healthy ok controller-manager Healthy ok etcd-0 Healthy {"health":"true"} [root@master ~]# kubectl get pod -n kube-systemNAME READY STATUS RESTARTS AGEcoredns-558bd4d5db-fkqcb 0/1 ContainerCreating 0 6m52scoredns-558bd4d5db-tvb7j 0/1 ContainerCreating 0 6m52setcd-master01 1/1 Running 0 7m16skube-apiserver-master01 1/1 Running 0 7m16skube-controller-manager-master01 1/1 Running 0 7m12skube-flannel-ds-9hx9s 0/1 Init:0/1 0 43skube-flannel-ds-cl9r7 0/1 Init:0/1 0 3m49skube-flannel-ds-gn4m4 0/1 CrashLoopBackOff 5 6m52skube-proxy-cv5t8 0/1 ContainerCreating 0 43skube-proxy-kjqm7 0/1 ContainerCreating 0 3m49skube-proxy-plbgm 1/1 Running 0 6m52skube-scheduler-master01 1/1 Running 0 7m13s
以上是“怎么利用python脚本自动部署k8s”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!