-->

DEVOPSZONES

  • Recent blogs

    How to Install Kubernetes 1.21 on CentOS 7 / RHEL 7

    What is Kubernetes?

    Kubernetes is a container management tool. It is Donated by Google to the Opensource community. It has now become the defacto container management tool of choice.

    In Kubernetes setup we have one master (control) node and multiple worker nodes. The worker-nodes are then managed from the master node, thus ensuring that the cluster is managed from a central point.

    You can also deploy a single-node Kubernetes cluster. You can use Minikube, which is a tool that runs a single-node Kubernetes cluster in a virtual machine on your node.


    For this tutorial, we will walk-through a multi-node Kubernetes cluster installation based on CentOS 7 . 

    How to Install Kubernetes :

    Prerequisites:

    1. Multiple servers running Centos 7 (1 Master Node2 Worker Nodes). It is recommended that your Master Node have at least 2 CPUs.
    2. Internet connectivity on all your nodes. We will be fetching kubernetes and docker packages from the repository. 
    3. You will also need access to an account with sudo or root privileges. In this tutorial, I will be using my root account.

    Kubernetes architecture
    Kubernetes architecture


    Installations Steps of Kubernetes 1.21 on Master Node

    For Kubernetes to work, you will need a containerization engine. For this installation, we will use docker. 

    Setup Hostname, Firewall and SELinux

    On your master node, set the hostname and if you don’t have a DNS server, then also update your /etc/hosts file.

    # hostnamectl set-hostname kubemaster
    # cat <<EOF>> /etc/hosts
    192.168.56.104 kubemaster
    192.168.56.105 kubeworker1
    192.168.56.106 kubeworker2
    EOF

    Next, disable SElinux and update your firewall rules or disable your firewall.

    # sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
    # reboot

    Disable Swap in all nodes using “swapoff -a” command and remove or comment out swap partitions or swap file from fstab file.

    disable swap


    Set the following firewall rules on ports.

    # firewall-cmd --permanent --add-port=6443/tcp
    # firewall-cmd --permanent --add-port=2379-2380/tcp
    # firewall-cmd --permanent --add-port=10250/tcp
    # firewall-cmd --permanent --add-port=10251/tcp
    # firewall-cmd --permanent --add-port=10252/tcp
    # firewall-cmd --permanent --add-port=10255/tcp
    # firewall-cmd –reload
    # modprobe br_netfilter
    # echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables

    Or you can Disable Firewall.

    [root@devopszones ~]#systemctl disable  firewalld

    Add the Kubernetes Repo

    You will need to add Kubernetes repositories manually as they do not come installed by default on CentOS 7.

    cat <<EOF > /etc/yum.repos.d/kubernetes.repo
    [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
    Kubernetes Repo


    Install Kubeadm and Docker

    # yum install kubeadm docker -y 
    

    When the installation completes successfully, enable and start both the services.

    # systemctl enable kubelet &&  systemctl start kubelet
    # systemctl enable docker &&  systemctl start docker

    Initialize Kubernetes Master

    Initializing Kubernetes master is a fully automated process that is managed by the “kubeadm init“ command which you will run.

    # kubeadm init --control-plane-endpoint 192.168.56.104
    Kubeadm init
    Kubeadm init
    Kubeadm init
    Initialize Kubernetes master


     kubelet: failed to run Kubelet: failed to create kubelet: misconfiguration: kubelet cgroup driver: "systemd" is different from docker cgroup driver: "cgroupfs"

    As we can see in the output that kubernetes master has been initialized successfully. Execute the beneath commands to use the cluster as root user.

    [root@devopszones ~]# mkdir -p $HOME/.kube
    [root@devopszones ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    [root@devopszones ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config

    Deploy a pod network to the cluster


    To make the cluster status readyrunning, deploy the pod network so that containers of different host communicated each other.  POD network is the overlay network between the worker nodes.

    Run the Following command to deploy network.

    [root@devopszones ~]#kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
    serviceaccount/weave-net created
    clusterrole.rbac.authorization.k8s.io/weave-net created
    clusterrolebinding.rbac.authorization.k8s.io/weave-net created
    role.rbac.authorization.k8s.io/weave-net created
    rolebinding.rbac.authorization.k8s.io/weave-net created
    daemonset.apps/weave-net created
    [root@devopszones ~]#
    Now run the following commands to verify the status

    [root@devopszones ~]#kubectl get pods -A
    NAMESPACE     NAME                                 READY   STATUS    RESTARTS   AGE
    kube-system   coredns-558bd4d5db-4c6d7             1/1     Running   0          5m5s
    kube-system   coredns-558bd4d5db-d7995             1/1     Running   0          5m5s
    kube-system   etcd-kubemaster                      1/1     Running   0          5m2s
    kube-system   kube-apiserver-kubemaster            1/1     Running   0          5m11s
    kube-system   kube-controller-manager-kubemaster   1/1     Running   0          5m10s
    kube-system   kube-proxy-qg4js                     1/1     Running   0          5m5s
    kube-system   kube-scheduler-kubemaster            1/1     Running   0          5m2s
    kube-system   weave-net-skv44                      2/2     Running   0          21s
    [root@devopszones ~]#
    [root@devopszones ~]#kubectl get nodes
    NAME          STATUS     ROLES                  AGE     VERSION
    kubemaster    Ready      control-plane,master   6m57s   v1.21.3
    

    Setting Up Worker Nodes to Join Kubernetes Cluster

    The following steps will run on the worker nodes. These steps should be run on every worker node when joining the Kubernetes cluster.

    Setup Hostname, Firewall and SELinux

    On your master node, set the hostname and if you don’t have a DNS server, then also update your /etc/hosts file.

    # hostnamectl set-hostname kubeworker1
    # cat <<EOF>> /etc/hosts
    192.168.56.104 kubemaster
    192.168.56.105 kubeworker1
    192.168.56.106 kubeworker2
    EOF

    Next, disable SElinux and update your firewall rules or disable your firewall.

    # sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
    # reboot

    Disable Swap in all nodes using “swapoff -a” command and remove or comment out swap partitions or swap file from fstab file.

    disable swap


    Set the following firewall rules on ports.

    # firewall-cmd --permanent --add-port=6443/tcp
    # firewall-cmd --permanent --add-port=2379-2380/tcp
    # firewall-cmd --permanent --add-port=10250/tcp
    # firewall-cmd --permanent --add-port=10251/tcp
    # firewall-cmd --permanent --add-port=10252/tcp
    # firewall-cmd --permanent --add-port=10255/tcp
    # firewall-cmd –reload
    # modprobe br_netfilter
    # echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables

    Or you can Disable Firewall.

    [root@devopszones ~]#systemctl disable  firewalld

    Add the Kubernetes Repo

    You will need to add Kubernetes repositories manually as they do not come installed by default on CentOS 7.

    cat <<EOF > /etc/yum.repos.d/kubernetes.repo
    [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
    Kubernetes Repo


    Install Kubeadm and Docker

    # yum install kubeadm docker -y 
    

    When the installation completes successfully, enable and start both the services.

    # systemctl enable kubelet &&  systemctl start kubelet
    # systemctl enable docker &&  systemctl start docker

    Join the Worker Node to the Kubernetes Cluster

    We now require the token that kubeadm init generated, to join the cluster. You can copy and paste it to your kubeworker1 and kubeworker2 if you had copied it somewhere.

    [root@devopszones ~]#kubeadm join 192.168.56.104:6443 --token sqs3wu.k5dacuzfzc1f4uro \
    >         --discovery-token-ca-cert-hash sha256:00a0029c032bfb3d06516a27340e5e552d6ec8da9c70d4c3908ea5b0cd230aee
    
    Kubernetes worker node join
    Kubernetes worker node join


    Do the same thing for other Worker node.


    Now verify Nodes status from master node using kubectl command

    [root@devopszones ~]#kubectl get nodes
    NAME          STATUS   ROLES                  AGE   VERSION
    kubemaster    Ready    control-plane,master   72m   v1.21.3
    kubeworker1   Ready    <none>                 66m   v1.21.3
    kubeworker2   Ready    <none>                 64m   v1.21.3
    [root@devopszones ~]#
    

    Now we have successfully joined two worker nodes to the cluster. You can now begin to create your pods and deploy your services. Please find the video guide below. Please share the article to help others.


    1 comment:

    1. Disable SELINUX? Not a good idea folks. Just sayin'

      ReplyDelete