|
| 1 | +import subprocess |
| 2 | +from subprocess import STDOUT, check_call |
| 3 | +import apt |
| 4 | +import sys |
| 5 | +import os |
| 6 | +import time |
| 7 | + |
| 8 | +def install_pkg(): |
| 9 | + try: |
| 10 | + check_call(["curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 11 | + check_call(["sudo apt-key fingerprint 0EBFCD88"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 12 | + check_call(['sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"'], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 13 | + check_call(["curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 14 | + check_call(["sudo mkdir -p /etc/apt/sources.list.d/"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 15 | + check_call(['echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list'], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 16 | + check_call(["sudo apt update"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 17 | + pkg_name = ["apt-transport-https", "ca-certificates", "curl", "gnupg-agent", "software-properties-common", "docker-ce=5:19.03.12~3-0~ubuntu-bionic", "docker-ce-cli=5:19.03.12~3-0~ubuntu-bionic", "containerd.io", "kubelet=1.18.8-00", "kubectl=1.18.8-00", "kubeadm=1.18.8-00"] |
| 18 | + for item in pkg_name: |
| 19 | + print("Installing {}".format(item)) |
| 20 | + check_call(["sudo", "apt", "install", "--allow-downgrades", "-y", item], stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 21 | + check_call(["sudo systemctl start docker"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 22 | + check_call(["sudo systemctl enable docker"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 23 | + check_call(["sudo apt-mark hold kubelet kubeadm kubectl"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 24 | + check_call(["sudo swapoff -a"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 25 | + except Exception as e: |
| 26 | + print(e) |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +def install_k8s(): |
| 31 | + try: |
| 32 | + kube = check_call(["kubectl cluster-info"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 33 | + if kube == 0: |
| 34 | + print("**********************************************") |
| 35 | + print("Kubernetes Cluster is up and running") |
| 36 | + print("**********************************************") |
| 37 | + print(" ") |
| 38 | + check_call(["kubectl get pods -A"], shell=True) |
| 39 | + elif kube != 0: |
| 40 | + print("Kubernetes Cluster is starting") |
| 41 | + except Exception as e: |
| 42 | + print(" ") |
| 43 | + print("**********************************************") |
| 44 | + print("Installing Docker and Kubernetes Dependencies") |
| 45 | + print("**********************************************") |
| 46 | + print(" ") |
| 47 | + install_pkg() |
| 48 | + print(" ") |
| 49 | + print("**********************************************") |
| 50 | + print("Installing Kubernetes Cluster") |
| 51 | + print("**********************************************") |
| 52 | + print(" ") |
| 53 | + check_call(["sudo kubeadm reset --force"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 54 | + print("Initializing kubernetes cluster 'sudo kubeadm init --pod-network-cidr=192.168.0.0/16'") |
| 55 | + check_call(["sudo kubeadm init --pod-network-cidr=192.168.0.0/16"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 56 | + check_call(["sudo rm -rf $HOME/.kube"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 57 | + check_call(["mkdir -p $HOME/.kube"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 58 | + check_call(["sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 59 | + check_call(["sudo chown $(id -u):$(id -g) $HOME/.kube/config"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 60 | + print("Applying Calico CNI to kubernetes cluster") |
| 61 | + check_call(["kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 62 | + check_call(["kubectl taint nodes --all node-role.kubernetes.io/master-"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 63 | + time.sleep(15) |
| 64 | + print(" ") |
| 65 | + print("Kubeadm Token to Join the workers to the cluster") |
| 66 | + print(" ") |
| 67 | + print('-' * 150) |
| 68 | + #print("-------------------------------------------------------------------------------------------------") |
| 69 | + check_call(["sudo kubeadm token create --print-join-command"], shell=True, stderr=open(os.devnull,'wb')) |
| 70 | + print('-' * 150) |
| 71 | + print(" ") |
| 72 | + print("**********************************************") |
| 73 | + print("Kubernetes cluster is up and running") |
| 74 | + print("**********************************************") |
| 75 | + print(" ") |
| 76 | + check_call(["kubectl get pods -A"], shell=True) |
| 77 | + |
| 78 | +def remove_reset(): |
| 79 | + print("**********************************************") |
| 80 | + print("Remove and Reset cluster") |
| 81 | + print("**********************************************") |
| 82 | + try: |
| 83 | + check_call(["sudo kubeadm reset --force"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 84 | + check_call(["sudo rm -rf $HOME/.kube"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 85 | + check_call(["sudo apt autoremove -y"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 86 | + check_call(["sudo apt purge -y --allow-change-held-packages kubelet kubeadm kubectl docker-ce* container* "], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 87 | + check_call(["sudo rm -rf /var/lib/etcd /etc/kubernetes /usr/bin/helm /var/lib/docker /etc/docker /var/log/containers"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 88 | + except Exception as e: |
| 89 | + print(e) |
| 90 | + check_call(["sudo apt purge -y --allow-change-held-packages kubelet kubeadm kubectl docker-ce* container* "], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 91 | + check_call(["sudo rm -rf /var/lib/etcd /etc/kubernetes /usr/bin/helm /var/lib/docker /etc/docker /var/log/containers"], shell=True, stdout=open(os.devnull,'wb'), stderr=STDOUT) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + if len(sys.argv) < 2: |
| 96 | + print("Usage: \n \n python kubernetes_setup.py install: Install Kubernetes stack\n python kubernetes_setup.py uninstall: Uninstall Kubernetes stack") |
| 97 | + sys.exit() |
| 98 | + elif len(sys.argv) == 2: |
| 99 | + if sys.argv[1] == "install": |
| 100 | + install_k8s() |
| 101 | + elif sys.argv[1] == "uninstall": |
| 102 | + remove_reset() |
| 103 | + else: |
| 104 | + print("Usage: \n python kubernetes_setup.py install: Install Kubernetes stack\n python kubernetes_setup.py uninstall: Uninstall Kubernetes stack") |
| 105 | + sys.exit() |
0 commit comments