如何利用python脚本自动部署k8s
目录
- 一、准备
- 二、编辑脚本
- 1、k8s.sh
- 2、k8s_install.py
- 三、配置ssh免密
- 四、下载python3和git
- 五、执行脚本
- 六、成功
- 七、总结
一、准备
- 通过之前在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 = masteripself.nodeip = nodeipdef initialization_shell(self): #环境初始化shell# 关闭防火墙setenforce = "setenforce0"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 wgetgit 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/*.repo /etc/yum.repos.d/bak > /dev/null 2>&1"wget_centos = "wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo > /dev/null 2>&1"wget_epel = "wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo > /dev/null 2>&1"wget_docker = "wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo > /dev/null 2>&1"kubernetes_repo = """cat >/etc/yum.repos.d/kubernetes.repo << EOF[kubernetes]name=Kubernetesbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64enabled=1gpgcheck=1repo_gpgcheck=1gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpgEOF"""yum_clean = "yum -y makecache > /dev/null 2>&1"yum_makecahe = "yum -y makecache > /dev/null 2>&1"#修改内核参数,由于ipvs已经加入到了内核的主干,所以为kube-proxy开启ipvs的前提需要加载以下的内核模块modprobe_netfilter = "modprobe br_netfilter"br_netfilter = "echo 'br_netfilter' > /etc/modules-load.d/br_netfilter.conf"k8s_conf = """cat > /etc/sysctl.d/k8s.conf </etc/security/limits.conf << EOF* soft nofile 65536* hard nofile 65536* soft nproc 65536* hard nproc 65536* softmemlockunlimited* hard memlockunlimitedDefaultLimitNOFILE=102400DefaultLimitNPROC=102400EOF"""sysctl_k8s = "sysctl -p /etc/sysctl.d/k8s.conf > /dev/null 2>&1"#时间同步enable_chronyd = "systemctl enable chronyd.service"start_chronyd = "systemctl start chronyd.service"set_timezone = "timedatectl set-timezone Asia/Shanghai"ntpdate = "ntpdate ntp1.aliyun.com > /dev/null 2>&1"chronyc_sources = "chronyc sources > /dev/null 2>&1"#安装docker,kubeletremove_docker = "yum remove -y docker docker-ce docker-common docker-selinux docker-engine > /dev/null 2>&1"install_docker = "yum install -y docker-ce > /dev/null 2>&1"start_docker = "systemctl start docker > /dev/null 2>&1"docker_reload = "systemctl daemon-reload > /dev/null 2>&1"enable_docker = "systemctl enable docker> /dev/null 2>&1"restart_docker = "systemctl restart docker > /dev/null 2>&1"install_kubelet = "yum install -y kubelet-1.21.2 kubeadm-1.21.2 kubectl-1.21.2 --disableexcludes=kubernetes > /dev/null 2>&1"enable_kubelet = "systemctl enable kubelet > /dev/null 2>&1"start_kubelet = "systemctl start kubelet > /dev/null 2>&1"return setenforce,sed_selinux,sed_selinux1,sed_selinux2,sed_selinux3,stop_firewalld,disable_firewalld,swapoff_a,sed_swapoff,yum_install,\mkdir_repo,wget_centos,wget_epel,wget_docker,kubernetes_repo,yum_clean,yum_makecahe,modprobe_netfilter,br_netfilter,k8s_conf,limits_conf,\sysctl_k8s,enable_chronyd,start_chronyd,set_timezone,ntpdate,chronyc_sources,remove_docker,install_docker,start_docker,docker_reload,enable_docker,restart_docker,\install_kubelet,enable_kubelet,start_kubeletdef shell_command(self):masterip_list = self.masterip.split(',')nodeip_list = self.nodeip.split(',')token_creat = ()token_code = ()name_num = 0node_num = 0dir0 = '''echo '{ "exec-opts":["native.cgroupdriver=systemd"]}' > /etc/docker/daemon.json'''dir1 = '''echo 'export KUBECONFIG=/etc/kubernetes/admin.conf' >> /etc/profile'''dir2 = '''echo '199.232.68.133 raw.githubusercontent.com' >> /etc/hosts'''dir3 = '''echo '{"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]}' > /etc/docker/daemon.json'''# #自动添加策略,保存服务器的主机名和密钥信息,如果不添加,那么不再本地know_hosts文件中记录的主机将无法连接for masterip in masterip_list:name_num += 1hosts_name = ""if masterip == masterip_list[0]:# 如果是当前单节点print("*"*20,"进入Master节点操作,当前IP: %s" %masterip)master_name = "master0%s" % name_num#设置名字hostname = os.system("hostname %s"%master_name)etc_hostname =os.system("echo '%s' > /etc/hostname" % master_name)#设置hostsmaster_host = masterip + "" + master_nameetc_hosts = os.system("echo '%s' >> /etc/hosts" % master_host)for hosts in nodeip_list:name_num += 1hosts_name += hosts + "node0%s" % (name_num - 1) + "\n"os.system("cat >> /etc/hosts < /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 += 1node_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 nodesNAMESTATUSROLESAGEVERSIONmaster01Readycontrol-plane,master7m2sv1.21.2node01Ready3m30sv1.21.2node02Ready 25sv1.21.2[root@master ~]# kubectl get csWarning: v1 ComponentStatus is deprecated in v1.19+NAMESTATUSMESSAGEERRORschedulerHealthyokcontroller-managerHealthyoketcd-0Healthy{"health":"true"}[root@master ~]# kubectl get pod -n kube-systemNAMEREADYSTATUSRESTARTSAGEcoredns-558bd4d5db-fkqcb0/1ContainerCreating06m52scoredns-558bd4d5db-tvb7j0/1ContainerCreating06m52setcd-master011/1Running07m16skube-apiserver-master011/1Running07m16skube-controller-manager-master011/1Running07m12skube-flannel-ds-9hx9s0/1Init:0/1043skube-flannel-ds-cl9r70/1Init:0/103m49skube-flannel-ds-gn4m40/1CrashLoopBackOff56m52skube-proxy-cv5t80/1ContainerCreating043skube-proxy-kjqm70/1ContainerCreating03m49skube-proxy-plbgm1/1Running06m52skube-scheduler-master011/1Running07m13s
七、总结 参考:https://github.com/hxz5215/K8Sv1.18_install
根据GitHub上的python脚本源码结合之前在Ubuntu上配置k8s的经验,对脚本进行了一些改变。安装了更新版本的k8s,我这里安装的是之前学习过程中安装过的
v1.21.2
添加了一个从dockerhub拉取k8s所需镜像的脚本将calico换成了flannel网络插件删除了dashboard,因为我这里没有这个需求针对 IsDockerSystemdCheck
问题加入了解决脚本对于 kubeadm get cs
的unhealthy问题和 kubectl get nodes
的notready问题都加入了解决脚本加入了通过ssh将admin.conf
和 /etc/cni
文件拷贝到node节点的脚本【如何利用python脚本自动部署k8s】到此这篇关于利用python脚本自动部署k8s的文章就介绍到这了,更多相关python自动部署k8s内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- 如何寻找情感问答App的分析切入点
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- 如何在Mac中的文件选择框中打开系统隐藏文件夹
- 漫画初学者如何学习漫画背景的透视画法(这篇教程请收藏好了!)