SoFunction
Updated on 2025-03-02

How to automatically deploy k8s using python scripts

1. Preparation

  • After manually deploying k8s on Ubuntu 18.04, try to automate deployment with python scripts
  • This time we use three centos7 virtual machines, one as master to execute scripts, and two as node nodes
  • All three machines are equipped with static IP, you can refer to the previous oneDetailed explanation of the steps to install and configure k8s cluster in centos 7

2. Edit the script

1、

Put/rootNext, used to pull the image from dockerhub.

set -o errexit
set -o nounset
set -o pipefail

##Define the version here, and change the version number yourself according to the list obtained above
KUBE_VERSION=v1.21.3
KUBE_PAUSE_VERSION=3.4.1
ETCD_VERSION=3.4.13-0

##This is the original warehouse name, and it needs to be renamed to this in the endGCR_URL=

##This is the warehouse you want to useDOCKERHUB_URL=gotok8s

##This is the mirror list. The new version needs to change coredns to 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}
)

## Here are loop statements for pulling and renamingfor imageName in ${images[@]} ; do
docker pull $DOCKERHUB_URL/$imageName
docker tag $DOCKERHUB_URL/$imageName $GCR_URL/$imageName
docker rmi $DOCKERHUB_URL/$imageName
done
docker pull coredns/coredns:1.8.0
docker tag coredns/coredns:1.8.0 /coredns/coredns:v1.8.0
docker rmi coredns/coredns:1.8.0

2、k8s_install.py

#!/bin/python3
# -*- coding:utf-8 -*-
# author: fanb
# describe: K8S v1.21.2 One-click script installationimport os
import subprocess
import time


class k8s_install(object):
    def __init__(self,masterip,nodeip):
         = masterip
         = nodeip

    def initialization_shell(self): #Environment initialization shell        # Turn off the firewall        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"

        #Configure domestic yum source on all servers        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//bak && mv /etc//*.repo /etc//bak > /dev/null 2>&1"
         wget_centos = "wget ​​-O /etc// /repo/centos7_base.repo > /dev/null 2>&1"
         wget_epel = "wget ​​-O /etc// /repo/ > /dev/null 2>&1"
         wget_docker = "wget ​​/docker-ce/linux/centos/ -O /etc// > /dev/null 2>&1"


         kubernetes_repo = """
 cat > /etc// << EOF
 [kubernetes]
 name=Kubernetes
 baseurl=/kubernetes/yum/repos/kubernetes-el7-x86_64
 enabled=1
 gpgcheck=1
 repo_gpgcheck=1
 gpgkey=/kubernetes/yum/doc/ /kubernetes/yum/doc/
 EOF
 """

         yum_clean = "yum -y makecache > /dev/null 2>&1"
         yum_makecahe = "yum -y makecache > /dev/null 2>&1"
         #Modify the kernel parameters. Since ipvs has been added to the kernel trunk, the following kernel modules are loaded for kube-proxy
         modprobe_netfilter = "modprobe br_netfilter"
         br_netfilter = "echo 'br_netfilter' > /etc//br_netfilter.conf"

         k8s_conf = """
 cat > /etc// <<EOF
 -nf-call-ip6tables = 1
 -nf-call-iptables = 1
 net.ipv4.ip_forward = 1
 =0
 EOF
 """

         limits_conf = """
 cat > /etc/security/ << EOF
 * soft nofile 65536
 * hard nofile 65536
 * soft nproc 65536
 * hard nproc 65536
 * soft memlock unlimited
 * hard memlock unlimited
 DefaultLimitNOFILE=102400
 DefaultLimitNPROC=102400
 EOF
 """
         sysctl_k8s = "sysctl -p /etc//> /dev/null 2>&1"

         #Time synchronization
         enable_chronyd = "systemctl enable"
         start_chronyd = "systemctl start"
         set_timezone = "timedatectl set-timezone Asia/Shanghai"
         ntpdate = "ntpdate > /dev/null 2>&1"
         chronyc_sources = "chronyc sources > /dev/null 2>&1"

         #Installing docker,kubelet
         remove_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_kubelet

     def shell_command(self):
         masterip_list = (',')
         nodeip_list = (',')
         token_creat = ()
         token_code = ()
         name_num = 0
         node_num = 0
         dir0 = '''echo '{
  "exec-opts":["=systemd"]
 }' > /etc/docker/'''
         dir1 = '''echo 'export KUBECONFIG=/etc/kubernetes/' >> /etc/profile'''
         dir2 = '''echo '199.232.68.133 ' >> /etc/hosts'''
         dir3 = '''echo '{
   "registry-mirrors": [""]
 }' > /etc/docker/'''
         # #Automatically add policy to save server host name and key information. If not added, the hosts recorded in the local knowledge_hosts file will not be able to connect.

         for masterip in masterip_list:
             name_num += 1
             hosts_name = ""
             if masterip == masterip_list[0]: # If it is the current single node
                 print("*"*20,"enter the Master node operation, current IP: %s" %masterip)
                 master_name = "master0%s" % name_num
                 #Set Name
                 hostname = ("hostname %s"%master_name)
                 etc_hostname = ("echo '%s' > /etc/hostname" % master_name)
                 #Set hosts
                 master_host = masterip + " " + master_name
                 etc_hosts = ("echo '%s' >> /etc/hosts" % master_host)
                 for hosts in nodeip_list:
                     name_num += 1
                     hosts_name += hosts + " node0%s" % (name_num - 1) + "\n"
                 ("cat >> /etc/hosts <<EOF \n%sEOF\n" %hosts_name)
                    
                 print("*"*20," Enter the environment initialization, please wait patiently...")
                 for shell in self.initialization_shell():
                     (1)
                     env_init = (shell)
                 print("*"*20,"Environment initialization is completed, install kubernetes...")
                 #Set hosts
                 #Cluster Initialization
                 registry = ("%s" %dir3)
                 restart_docker = ("systemctl restart docker")
                 dockerpull = ("sh /root/")
                 docker_problem = ("%s" %dir0)
                 restart_docker = ("systemctl restart docker")
                 status_docker = ("systemctl status docker")
                 kubeadm_init = ("kubeadm init")
                
                 export_root = ("export KUBECONFIG=/etc/kubernetes/")
                 config = ("%s" %dir1)
                 source = ("source /etc/profile")
                
                 mkdir_kube = ("mkdir -p /root/.kube")
                 kube_config = ("cp -i /etc/kubernetes/ /root/.kube/config")
                
                 kubelet_enable = ("systemctl enable kubelet")
                 kubelet_start = ("systemctl start kubelet")
                
                 manage_yaml = ("sed -i 's/.*- --port=0*/#&amp;/' /etc/kubernetes/manifests/")
                scheduler_yaml = ("sed -i 's/.*- --port=0*/#&amp;/' /etc/kubernetes/manifests/")
               
                
                #Configure the flannel network                print("*" * 20, "Network component flannel is being installed...")
                flannel_before = ("%s" %dir2)
                flannel_wget = ("wget /coreos/flannel/master/Documentation/")
                yum_flannel = ("yum install -y flannel")
                flannel_apply = ("kubectl apply -f /root/")
                print("*" * 20, "Network component flannel installation is completed...")
                
                token_creat = ("kubeadm token create")
                token_code = ("openssl x509 -pubkey -in /etc/kubernetes/pki/ | openssl rsa -pubin -outform der 2&gt;/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'")
                token_creat = token_creat[1].split('\n')[-1]
                token_code = token_code[1]

                # Install slave node                for nodeip in nodeip_list:  
                    ("scp -rp /etc/hosts %s:/etc/hosts" % nodeip)
                    print("*" * 20, "Enter Node operation, current IP: %s" % nodeip)
                    node_num += 1
                    node_name = "node0%s" % (node_num)
                    # Set a name                    ("ssh %s \"hostname %s\"" % (nodeip,node_name))
                    ("ssh %s \"echo '%s' &gt; /etc/hostname\"" % (nodeip,node_name))
                    print("*" * 20, "Enter the environment initialization, please wait patiently...")
                    for shell in self.initialization_shell():
                        (1)
                        ("ssh %s \"%s\"" %(nodeip,shell))
                    enable_node = ("ssh %s \"systemctl enable kubelet\"" %nodeip)  
                    start_node = ("ssh %s \"systemctl start kubelet\"" %nodeip)
                    admin = ("scp /etc/kubernetes/ %s:/root" %nodeip)
                    print("*" * 20, "Joining a cluster...")
                    print("token_creat : ",token_creat)
                    print("token_code : ",token_code)
                    docker_problem = ("scp -r /etc/docker/ %s:/etc/docker" %nodeip)
                    restart_docker = ("ssh %s \"systemctl restart docker\"" %nodeip)
               	    status_docker = ("ssh %s \"systemctl status docker\"" %nodeip)
                    kubeadm_join = ("ssh %s \"kubeadm join %s:6443 --token %s --discovery-token-ca-cert-hash sha256:%s\"" % (nodeip,masterip, str(token_creat), str(token_code)))
                    cni = ("scp -r /etc/cni %s:/etc" %nodeip)
                    print("*" * 20, "Successfully joined the cluster...")
                    
               
                print("*" * 20 ,"Execute the following command to check the K8s cluster\n")
                print("*" * 20,"kubectl get nodes")
                print("*" * 20, "kubectl get cs")
                print("*" * 20, "kubectl get pod -n kube-system")
               

            else:   #Otherwise it is cluster mode                print("Enter cluster mode installation")
                print("Not yet")
                exit()

if __name__ == '__main__':
    # #User input IP:    print("---------0. Please install python3 first and execute this script using python3-----------")
    print("----------1. This script depends on the network. Please connect the network and execute this script-----------")
    print("----------2. Please execute this script on the master node. Please log in to all other nodes on the master node------------------------")
    print("*********3. Please confirm that the master node has logged in with other nodes without password, and then execute this script after confirming again*******************")
    k8s_masterip = input("Please enter K8S_Master IP, multiple IPs are separated by commas: ")
    k8s_nodeip = input("Please enter K8S_node IP, multiple IPs are separated by commas: ")
    ask_ent = input("**********   confirm/Cancel (Y/N) :")
    if ask_ent.upper() == "Y":
        k8s_install = k8s_install(k8s_masterip,k8s_nodeip)
        k8s_install.shell_command()
    else:
        exit()

3. Configure ssh password-free

[root@master ~]# ssh-keygen
[root@master ~]# ssh-copy-id 192.168.139.132
[root@master ~]# ssh-copy-id 192.168.139.133

4. Download python3 and git

root@master ~]# vim k8s_install.py

5. Execute scripts

[root@master ~]# python3 k8s_install.py

6. Success

******************** Execute the following command,examineK8sCluster

******************** kubectl get nodes
******************** kubectl get cs
******************** kubectl get pod -n kube-system
[root@master ~]# kubectl get nodes
NAME       STATUS   ROLES                  AGE     VERSION
master01   Ready    control-plane,master   7m2s    v1.21.2
node01     Ready    &lt;none&gt;                 3m30s   v1.21.2
node02     Ready    &lt;none&gt;                 25s     v1.21.2
[root@master ~]# kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   
[root@master ~]# kubectl get pod -n kube-system
NAME                               READY   STATUS              RESTARTS   AGE
coredns-558bd4d5db-fkqcb           0/1     ContainerCreating   0          6m52s
coredns-558bd4d5db-tvb7j           0/1     ContainerCreating   0          6m52s
etcd-master01                      1/1     Running             0          7m16s
kube-apiserver-master01            1/1     Running             0          7m16s
kube-controller-manager-master01   1/1     Running             0          7m12s
kube-flannel-ds-9hx9s              0/1     Init:0/1            0          43s
kube-flannel-ds-cl9r7              0/1     Init:0/1            0          3m49s
kube-flannel-ds-gn4m4              0/1     CrashLoopBackOff    5          6m52s
kube-proxy-cv5t8                   0/1     ContainerCreating   0          43s
kube-proxy-kjqm7                   0/1     ContainerCreating   0          3m49s
kube-proxy-plbgm                   1/1     Running             0          6m52s
kube-scheduler-master01            1/1     Running             0          7m13s

7. Summary

Reference: /hxz5215/K8Sv1.18_install

Based on the source code of python scripts on GitHub combined with previous experience in configuring k8s on Ubuntu, some changes have been made to the script.

The updated version of k8s is installed. What I installed here is what I installed during the previous learning process.v1.21.2Added a script to pull the required image of k8s from dockerhub. Change calico to the flannel network plugin and deleted the dashboard because I don't have this requirement here.IsDockerSystemdCheckThe problem has been added to the solution script forkubeadm get csunhealthy issues andkubectl get nodesThe notready problem has been added to the solution script has been added to the sshand/etc/cniScript to copy file to node node

This is the end of this article about automatically deploying k8s using python scripts. For more related content related to automatically deploying k8s in python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!