Tuesday, June 30, 2020

Kubernetes installations

Kubernetes installation models:
-------------------------------------

1.Kubeadm

2.Kops

3.Minikube

and we do have other models from AWS(EKS),GCP(GKE),AZURE(AKS)

1. Kubernetes Cluster setup with Kubeadm on GCP

Kubeadm can be installed in Bare-metal or any vmware or any  Cloud computing technologies  such as GCP,AWS,AZURE.

in general Master cluster setup will be minimum 3 master nodes and multiple worker nodes in production grade setup.

each Master nodes have at least 4 GB RAM .and 2 CPU cores.and worker nodes should be 2GB RAM and 1 CPU core to practice the Kubernetes components.

firstly will go with Single master and 2 workers kubernetes setup in GCP.

for this we need GCP account and need to be subscribe a project. as i have done with basic steps.

creating 4 compute engines, 1 for Master , 2 for worker and 1 for kubernetes management server  and this will help us to install the kubernetes libraries. OS selection is centos 7.

followed Official document and add my best practice to setup the cluster.





login to all the 4 servers and with root and follow the steps,

Pre-requisites:

execute below commands one by one to configure the Kubernetes setup. in the 3 server(master-01,worker-01 & worker-02)

1. add below entries on the 3 server under file path vi /etc/hosts file 

master-01  xx.xxx.x.xx
worker-01  xx.xxx.x.xx
worker-02  xx.xxx.x.xx

2. To Stop Firewall and NetworkManager :  

systemctl stop firewalld NetworkManager && systemctl disable firewalld NetworkManager

3. To Disable selinux  :      
    Go to path  and edit as follow vi /etc/sysconfig/selinux
    change the parameter from SELINUX=enforcing  to SELINUX=disabled           and save 

4. To off the SWAP  :       
  Execute the command  swapoff -a

5. To disable ipv6:  
   go the file path  vi /etc/sysctl.conf  and  add the following entries
         net.ipv6.conf.all.disable_ipv6 = 1
        net.ipv6.conf.default.disable_ipv6 = 1
        net.ipv4.ip_forward=1 
and save the file and execute below command to check the ipv6 and ipv4 disabled.

to check the status of the IPV6 and IPV4 with command  sysctl -p

with all above steps we have completed with Prerequisites. 

now will go with installation of CRI (Container Runtime environment) , in my case i used Docker.

Installation steps to Docker CE :

execute the below steps as it is in the centos servers.

yum install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce-18.06.2.ce
mkdir -p /etc/docker

Setup daemon.

cat > /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}
EOF

To enable the docker service.

mkdir -p /etc/systemd/system/docker.service.d

Restart Docker & enable docker

systemctl daemon-reload
systemctl restart docker
systemctl enable docker


Now we have to install the Kubernetes packages , steps are as follows.


cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
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
exclude=kubelet kubeadm kubectl
EOF

to specific version selection  just run the command with any one of the package: yum search kubelet --show-duplicate --disableexcludes=kubernetes

now im going to select kubelet-1.18.2-0.x86_64  version packages,

yum install -y kubelet-1.18.2-0.x86_64 kubeadm-1.18.2-0.x86_64 --disableexcludes=kubernetes
yum install -y kubectl-1.18.2-0.x86_64 --disableexcludes=kubernetes



# to enable the kubelet
systemctl enable --now kubelet

As a requirement for Linux Node's iptables to correctly see bridged traffic, we should ensure net.bridge.bridge-nf-call-iptables is set to 1 in our sysctl config

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

go to master-01 with root user

execute the below commands 

kubeadm config images pull



in my case i'm not using any loadbalencers like ha-proxy, so just simply executing the kubeadm init.

kubeadm init 


you can see that below message :

Your Kubernetes control-plane has initialised successfully!

To start using your cluster, you need to run the following commands.

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config


Then  go to each worker node and execute the below  Join command  join  as root: ( this will get after initiate the kubeadm init 

kubeadm join 10.128.15.227:6443 --token 8ynfsy.t2gfni04ilrdzuy1 \
    --discovery-token-ca-cert-hash sha256:f25b63ea2956d1fb3d2fd1938f8a8217a4496b2a4071e2ca7d12e7fbcb70b517

on worker-01


on Worker-02


so we are done with the cluster set up to check the status of the cluster.

go to management VM instance and install the kubectl then add the .kube file and execute the below command for that

copy the content from  cat /root/.kube/config on  master-01 and paste it in to the management server path vi /root/.kube/config  

kubectl get nodes


but it is not desired output of the clusters nodes. please do the below steps to fix those.


to set all the worker and master in the same network, execute below command from management server to get the Status of the nodes.

kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"


to set the Role of the workers ,execute the be command from management servers

kubectl label node worker-01 node-role.kubernetes.io/worker=worker
kubectl label node worker-02 node-role.kubernetes.io/worker=worker


now all the issues got fixed and we got the desired output of the Nodes.



References:
CRI:
https://kubernetes.io/docs/setup/production-environment/container-runtimes/

Kubernetes configurations with kubeadm:
https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/

Kubectl:
https://kubernetes.io/docs/reference/kubectl/overview/