How To Install Kubernetes Cluster In Linux
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed the basic architecture of the Kubernetes cluster.
https://cloudaffaire.com/what-is-kubernetes/
In this blog post, we will create our 1st Kubernetes 2 node cluster using CentOS7 VM.
Prerequisite for this demo:
- Three CentOS7 VM instance with internet and intranet connectivity.
How To Install Kubernetes Cluster In Linux:
Step 1: Configure your master node.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
################################################ ## How To Install Kubernetes Cluster In Linux ## ################################################ ## Systems used for this demo # hostnames ip os role ram cpu # --------- ------------ -------- ------------ ---- --- # system1 192.168.0.10 Centos 7 Master Node 2 GB 2 cores # system2 192.168.0.20 Centos 7 Worker Node One 1 GB 1 core # system3 192.168.0.30 Centos 7 Worker Node Two 1 GB 1 core ## --------------------- ## Configure Master Node ## --------------------- ## Login to the Master Node (192.168.0.10) as root user ## Set the hostname for system1 ## hostnamectl set-hostname 'system1' ## cat < ## 192.168.0.10 system1 system1 ## 192.168.0.20 system2 system2 ## 192.168.0.30 system3 system3 ## EOF # Set SELinux in permissive mode (effectively disabling it) setenforce 0 sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config ## Disable firewall systemctl stop firewalld systemctl disable firewalld ## Disable SWAP sudo swapoff -a sed -i.bak -r 's/(.+ swap .+)/#\1/' /etc/fstab ## Update Iptables Settings cat < net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl net.bridge.bridge-nf-call-iptables=1 sysctl net.ipv4.ip_forward=1 sysctl --system echo "1" > /proc/sys/net/ipv4/ip_forward ## Create the yum repository cat < [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOF ## Install and enable docker yum install -y docker systemctl enable docker systemctl start docker ## Install kubeadm kubelet and kubectl yum install -y kubelet kubeadm kubectl systemctl enable kubelet systemctl start kubelet ## Restart the systemd daemon and kubelet systemctl daemon-reload systemctl restart kubelet ## Initialize the Kubernetes master node kubeadm init --pod-network-cidr=10.244.0.0/16 ## Your Kubernetes control-plane has initialized successfully! ## ## To start using your cluster, you need to run the following as a regular user: ## ## mkdir -p $HOME/.kube ## sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config ## sudo chown $(id -u):$(id -g) $HOME/.kube/config ## ## You should now deploy a pod network to the cluster. ## Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: ## https://kubernetes.io/docs/concepts/cluster-administration/addons/ ## ## Then you can join any number of worker nodes by running the following on each as root: ## ## kubeadm join 192.168.0.10:6443 --token boq2jb.qk3gu4v01l5cg2xc \ ## --discovery-token-ca-cert-hash sha256:35ad26fc926cb98e16f10447a1b43bc947d07c2c19b380c148d4c1478c7bf834 ## Note down the above join command which will be used later to join worker nodes into the master. ## Enable kubernetes environment configuration # for regular user mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config # for root user (applicable for this demo) export KUBECONFIG=/etc/kubernetes/admin.conf ## Install flannel network plugin for kubernetes cluster network kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml |
Step 2: Configure worker nodes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
## ------------------------- ## Configure Worker Node One ## ------------------------- ## Login to the Worker Node One (192.168.0.20) as root user ## Set the hostname for system2 ## hostnamectl set-hostname 'system2' ## cat < ## 192.168.0.10 system1 system1 ## 192.168.0.20 system2 system2 ## 192.168.0.30 system3 system3 ## EOF # Set SELinux in permissive mode (effectively disabling it) setenforce 0 sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config ## Disable firewall systemctl stop firewalld systemctl disable firewalld ## Disable SWAP sudo swapoff -a sed -i.bak -r 's/(.+ swap .+)/#\1/' /etc/fstab ## Update Iptables Settings cat < net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl net.bridge.bridge-nf-call-iptables=1 sysctl net.ipv4.ip_forward=1 sysctl --system echo "1" > /proc/sys/net/ipv4/ip_forward ## Create the yum repository cat < [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOF ## Install and enable docker yum install -y docker systemctl enable docker systemctl start docker ## Install kubeadm kubelet and kubectl yum install -y kubelet kubeadm kubectl systemctl enable kubelet systemctl start kubelet ## Restart the systemd daemon and kubelet systemctl daemon-reload systemctl restart kubelet ## Join the worker node 1 to master node (change token, hash and ip as required) kubeadm join 192.168.0.10:6443 --token boq2jb.qk3gu4v01l5cg2xc \ --discovery-token-ca-cert-hash sha256:35ad26fc926cb98e16f10447a1b43bc947d07c2c19b380c148d4c1478c7bf834 ## This node has joined the cluster: ## * Certificate signing request was sent to apiserver and a response was received. ## * The Kubelet was informed of the new secure connection details. ## ## Run 'kubectl get nodes' on the control-plane to see this node join the cluster. ## ------------------------- ## Configure Worker Node Two ## ------------------------- ## Login to the Worker Node Two (192.168.0.30) as root user ## Set the hostname for system3 ## hostnamectl set-hostname 'system3' ## cat < ## 192.168.0.10 system1 system1 ## 192.168.0.20 system2 system2 ## 192.168.0.30 system3 system3 ## EOF # Set SELinux in permissive mode (effectively disabling it) setenforce 0 sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config ## Disable firewall systemctl stop firewalld systemctl disable firewalld ## Disable SWAP sudo swapoff -a sed -i.bak -r 's/(.+ swap .+)/#\1/' /etc/fstab ## Update Iptables Settings cat < net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl net.bridge.bridge-nf-call-iptables=1 sysctl net.ipv4.ip_forward=1 sysctl --system echo "1" > /proc/sys/net/ipv4/ip_forward ## Create the yum repository cat < [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOF ## Install and enable docker yum install -y docker systemctl enable docker systemctl start docker ## Install kubeadm kubelet and kubectl yum install -y kubelet kubeadm kubectl systemctl enable kubelet systemctl start kubelet ## Restart the systemd daemon and kubelet systemctl daemon-reload systemctl restart kubelet ## Join the worker node 1 to master node (change token, hash and ip as required) kubeadm join 192.168.0.10:6443 --token boq2jb.qk3gu4v01l5cg2xc \ --discovery-token-ca-cert-hash sha256:35ad26fc926cb98e16f10447a1b43bc947d07c2c19b380c148d4c1478c7bf834 ## This node has joined the cluster: ## * Certificate signing request was sent to apiserver and a response was received. ## * The Kubelet was informed of the new secure connection details. ## ## Run 'kubectl get nodes' on the control-plane to see this node join the cluster. |
Step 3: Validate your Kubernetes cluster.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
## -------------------------------- ## Validate Your Kubernetes Cluster ## -------------------------------- ## Check nodes from master node (192.168.0.10) kubectl get nodes ## NAME STATUS ROLES AGE VERSION ## system1 Ready master 93m v1.18.0 ## system2 Ready ## system3 Ready ## List all pods used by kubernetes system (we have not deployed any app, these are used by cluster itself) kubectl get pod --all-namespaces ## kube-system coredns-66bff467f8-fv9h4 1/1 Running 1 41h ## kube-system coredns-66bff467f8-h868v 1/1 Running 1 41h ## kube-system etcd-system1 1/1 Running 1 41h ## kube-system kube-apiserver-system1 1/1 Running 1 41h ## kube-system kube-controller-manager-system1 1/1 Running 1 41h ## kube-system kube-flannel-ds-amd64-qlqrw 1/1 Running 1 41h ## kube-system kube-flannel-ds-amd64-tvtsw 1/1 Running 1 41h ## kube-system kube-flannel-ds-amd64-ws2s5 1/1 Running 1 41h ## kube-system kube-proxy-2h76j 1/1 Running 1 41h ## kube-system kube-proxy-9tt8v 1/1 Running 1 41h ## kube-system kube-proxy-gblcj 1/1 Running 1 41h ## kube-system kube-scheduler-system1 1/1 Running 1 41h |
Step 4: View Kubernetes configuration and log files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
## ------------------------------------ ## Kubernetes Configuration & Log Files ## ------------------------------------ ## Check kubernetes cluster configuration kubeadm config view ## Kubernetes Config folder ls /etc/kubernetes/ ## Certificate files ls /etc/kubernetes/pki/ ## Credentials to API server cat /etc/kubernetes/kubelet.conf ## Superuser credentials cat /etc/kubernetes/admin.conf ## kubectl config file cat ~/.kube/config ## Kubernets working dir ls /var/lib/kubelet/ ## Docker working dir ls /var/lib/docker/ ls /var/log/containers/ ## Etcd working dir ls /var/lib/etcd/ ## Network cni ls /etc/cni/net.d/ ## Pod Log files ls /var/log/pods/ ## Cluster Log Files (Available only if logging is enabled in /var/log/ path) ## On worker node (on 192.168.0.20 and 192.168.0.30) cat /var/log/kubelet.log cat /var/log/kube-proxy.log ## On master node (on 192.168.0.10) cat /var/log/kube-apiserver.log cat /var/log/kube-scheduler.log cat /var/log/kube-controller-manager.log |
Hope you enjoyed this article. In the next blog post, we will discuss pod in Kubernetes and will deploy our 1st Pod in this cluster.
To get more details on Kubernetes you can follow the below link.
https://kubernetes.io/docs/home/
To get more details on Docker you can follow the below links.
https://cloudaffaire.com/category/devops/docker/