Sunday, January 12, 2020

How to Install, Configure, and Deploy NGINX on a Kubernetes Cluster with Ubuntu

 What is Kubernetes?

Kubernetes is an open-source container management system that is based on Google Borg. It can be configured to provide highly available, horizontally auto scaling, automated deployments.

This guide shows you how to manually set up a Kubernetes cluster on a ubuntu with a Dashboard



Master node System requirement -
4GBRAM, 2CPU, Ubuntu 18.04 VM/Physical node

On Master Node:-


First install docker and Enable docker to auto start during reboot :
# sudo apt install docker.io
# docker --version
# sudo systemctl enable docker


Install curl and Download the gpg key for kubernetes installation and add to ubuntu :
#  sudo apt install curl
# curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add


Now add the Google's kubernetes repository:
# sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"


Install kubeadm and disable the swap :
# sudo apt-get install kubeadm
# kubeadm version
# sudo swapoff -a

Create this file (daemon.json) and add the below contents to it


# vi /etc/docker/daemon.json

{

     "exec-opts": ["native.cgroupdriver=systemd"]

}



Then

# sudo systemctl daemon-reload
# sudo systemctl restart docker
# sudo systemctl restart kubelet

Set the Master node hostname :
# sudo hostnamectl set-hostname master-node   


Advertise your Master node as API server and specify the POD network
# sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.1.36

You will have the below output after running this command:


################### OUTPUT ########################


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.1.36:6443 --token 120q6i.2276fcfwddizp1jm \
    --discovery-token-ca-cert-hash sha256:7eb84ab94d85234179c298c392dae2d681831358d960a5d43fc562220668a811

PLS NOTE :
------------------

The joining token generated is valid only for 24hours. In case the 24 hours exceed we need to generate the new token using the command "sudo kubeadm token create"



################### OUTPUT ########################


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


To list/See the nodes in your cluster
# kubectl get nodes    >>>>>>> This will list the nodes in your cluster, Now this will list only the MASTER Node


Now deploy the network for pod communications with flannel networking, You can select Calico networking also her I have used Flannel :
# sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml


Once the flannel network is deployed , we can verify the flannel interface for the  ip address assigned
# sudo  ip a show flannel.1


====================================
Now deploy the kubernetes dashoard :
====================================

I have used V2 Dashboard V1 having some bugs.

# kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml   - V1 Dashboard

# kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml    - New Dashboard

Then Run this :
# sudo kubectl proxy --address=0.0.0.0


Kubernetes dashboard URL in browser :
I have used V2 Url :

http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/.   - Old Dashboard

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/.    -New Dashboard



To properly login to the kubernetes dashboard we need to creat a service account and assign the proper role :
# kubectl create serviceaccount dashboard -n default
# kubectl create clusterrolebinding dashboard-admin -n default --clusterrole=cluster-admin --serviceaccount=default:dashboard


Now generate the login key with this command :
# kubectl get secret $(kubectl get serviceaccount dashboard -o jsonpath="{.secrets[0].name}") -o jsonpath="{.data.token}" | base64 --decode

Then paste the tocken on the web link and click sign-in

#########################################
 MANSTER NODE CONFIG DONE
#########################################

Worker node System requirement -
4GBRAM, 1CPU, Ubuntu 18.04 VM/Physical node


On Slave/Worker node :

First install docker and Enable docker to auto start during reboot :
# sudo apt install docker.io
# docker --version
# sudo systemctl enable docker


Install curl and Download the gpg key for kubernetes installation and add to ubuntu :
#  sudo apt install curl
# curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add


Now add the Google's kubernetes repository:
# sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"


Install kubeadm and disable the swap :
# sudo apt-get install kubeadm
# kubeadm version
# sudo swapoff -a

Set the Master node hostname :
# sudo hostnamectl set-hostname Worker-node1    - Set the hostname


Then run the below command as a ROOT user :

kubeadm join 192.168.1.36:6443 --token 120q6i.2276fcfwddizp1jm \
    --discovery-token-ca-cert-hash sha256:7eb84ab94d85234179c298c392dae2d681831358d960a5d43fc562220668a811

" The joining token generated is valid only for 24hours. In case the 24 hours exceed we need to generate the new token using the command :
# sudo kubeadm token create"


To list/See the nodes and the PODS in your cluster
#  sudo kubectl get nodes           >>>>> Now you can see the Worker node also
#  sudo kubectl get pods --all-namespaces



This is one node cluster So I have added only one node, you can add multiple node in your cluster


###########################
WORKER NODE CONFIG DONE
###########################

Now the deployment part

I am deploying nginx via Dashboard :


Open the Master node dashboard > Click the + logo > Create from form > Give a App name "Vipin Demo" > give container image name :nginx" >

Select service as External > Specify the source and traget ports > Then DEPLOY


############# ##################


Connecting to the Dashboard Remotely

If you need to access the Dashboard remotely, you can use SSH tunneling to do port forwarding from your localhost to the node running the kubectl proxy service. The easiest option is to use SSH tunneling to forward a port on your local system to the port configured for the kubectl proxy service on the node that you want to access. This method retains some security as the HTTP connection is encrypted by virtue of the SSH tunnel and authentication is handled by your SSH configuration. For example, on your local system run:

# ssh -L 8001:127.0.0.1:8001 192.168.1.36

Substitute 192.168.1.36 with the IP address of the host where the kubectl proxy service is running. When the SSH connection is established, you can open a browser on your localhost and navigate to:

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/.


You should see the Dashboard log in screen for the remote Kubernetes cluster. Use the same token information to authenticate as if you were connecting to the Dashboard locally.



#############  THANKS ###############