How to setup Kubernetes on Linux

HI All,As you all know the significance of container orchestration services are growing day by day, so i would like to take this opportunity to give a brief overview about evolution of deployment models,advantages of using kubernetes,core components in a cluster environment, step-by-step guide to deploy the cluster and a command line cheat sheet for your daily hands-on reference.

Evolution of Deployments
1.Traditional Deployments
2.Virtualized Deployments
3.Containerized Deployments

Benefits of using Kubernetes
1.Service discovery and load balancing
2.Storage Orchestration
3.Automated rollouts and rollbacks
4.Automate bin packing
5.Self healing
6.Secrets and Configuration Management

Kubernetes Architecture and core components
A Kubernetes cluster consists of a set of worker machines, called nodes, that run containerized applications. Every cluster has at least one worker node.
The worker node(s) host the Pods that are the components of the application workload. The control plane manages the worker nodes and the Pods in the cluster.

Architecture of Kubernetes

Control Plane
The control plane is a collection of processes that coordinate and manage the cluster’s state, segmented by responsibilities. It also makes scheduling decisions to facilitate the applications and cloud workflows that the worker nodes run.

Components
1.kube-apiserver
The API server is a component of the Kubernetes control plane that exposes the Kubernetes API. The API server is the front end for the Kubernetes control plane.
2.etcd
Consistent and highly-available key value store used as Kubernetes’ backing store for all cluster data.
3.kube-scheduler
It watches for newly created Pods with no assigned node, and selects a node for them to run on.
4.kube-controller-manager
It runs controller processes and Logically, each controller is a separate process, but to reduce complexity, they are all compiled into a single binary and run in a single process.
5.cloud-controller-manager
It ink your cluster into your cloud provider’s API, and separates out the components that interact with that cloud platform from components that just interact with your cluster.

Node Components
Node components run on every node, maintaining running pods and providing the Kubernetes runtime environment

  1. Kubelet
    An agent that runs on each node in the cluster. It makes sure that containers are running in a Pod.
    2.kube-proxy
    kube-proxy is a network proxy that runs on each node in your cluster, implementing part of the Kubernetes Service concept.
    3.Container runtime
    The container runtime is the software that is responsible for running containers.

Kubernetes Setup

1 Master and 2-worked node setup

Execute the below commands on all the three instances(master and worker nodes)
1.Once we have logged in, we need to elevate privileges using sudo:
sudo su

Disable SELinux:
setenforce 0
sed -i — follow-symlinks ‘s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/sysconfig/selinux

Enable the br_netfilter module for cluster communication:
modprobe br_netfilter
echo ‘1’ > /proc/sys/net/bridge/bridge-nf-call-iptables

Ensure that the Docker dependencies are satisfied:
yum install -y yum-utils device-mapper-persistent-data lvm2

Add the Docker repo and install Docker:
yum-config-manager — add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce

Set the cgroup driver for Docker to systemd, reload systemd, then enable and start Docker:
sed -i ‘/^ExecStart/ s/$/ — exec-opt native.cgroupdriver=systemd/’ /usr/lib/systemd/system/docker.service
systemctl daemon-reload
systemctl enable docker — now

Add the Kubernetes repo:
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=0
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF

Install Kubernetes:
yum install -y kubelet kubeadm kubectl

Enable the kubelet service. The kubelet service will fail to start until the cluster is initialized, this is expected:
systemctl enable kubelet

Note: Complete the following section on the MASTER ONLY!

Initialize the cluster using the IP range for Flannel:
kubeadm init — pod-network-cidr=10.244.0.0/16

Copy the kubeadmn join command that is in the output. We will need this later.

Exit sudo, copy the admin.conf to your home directory, and take ownership.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Deploy Flannel:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Check the cluster state: kubectl get pods — all-namespaces

Note: Complete the following steps on the NODES ONLY!
Run the join command that you copied earlier, this requires running the command prefaced with sudo on the nodes (if we hadn’t run sudo su to begin with). Then we’ll check the nodes from the master.

on-Master
kubectl get nodes

Create and Scale a Deployment Using kubectl

These commands will only be run on the master node.
Create a simple deployment:
kubectl create deployment nginx — image=nginx

Inspect the pod:
kubectl get pods

Scale the deployment:
kubectl scale deployment nginx — replicas=4

Inspect the pods. We should have four now:
kubectl get pods

Kubectl frequently used commands

Viewing, Finding Resources
# List all services in the namespacekubectl get services
# List all pods in all namespaces
kubectl get pods — all-namespaces
# List all pods in the current namespace, with more details
kubectl get pods -o wide
# List a particular deployment
kubectl get deployment my-dep
# List all pods in the namespace
kubectl get pods
# Get a pod’s YAML
kubectl get pod my-pod -o yaml
# Describe commands with verbose output
kubectl describe nodes my-node
kubectl describe pods my-pod

Updating Resources
# Rolling update “www” containers of “frontend” deployment, updating the image
kubectl set image deployment/frontend www=image:v2
# Check the history of deployments including the revision
kubectl rollout history deployment/frontend
# Rollback to the previous deployment
kubectl rollout undo deployment/frontend
# Rollback to a specific revision
kubectl rollout undo deployment/frontend — to-revision=2
# Watch rolling update status of “frontend” deployment until completion
kubectl rollout status -w deployment/frontend
# Rolling restart of the “frontend” deployment
kubectl rollout restart deployment/frontend

Deleting Resources
# Delete a pod using the type and name specified in pod.json
kubectl delete -f ./pod.json
# Delete pods and services with same names “baz” and “foo”
kubectl delete pod,service baz foo
# Delete pods and services with label name=myLabel
kubectl delete pods,services -l name=myLabel
# Delete all pods and services in namespace my-ns,
kubectl -n my-ns delete pod,svc — all

Scaling Resources
# Scale a replicaset named ‘foo’ to 3
kubectl scale — replicas=3 rs/foo
# Scale a resource specified in “foo.yaml” to 3
kubectl scale — replicas=3 -f foo.yaml
# If the deployment named mysql’s current size is 2, scale mysql to 3
kubectl scale — current-replicas=2 — replicas=3 deployment/mysql
# Scale multiple replication controllers
kubectl scale — replicas=5 rc/foo rc/bar rc/baz

Conclusion:Due to rapid expansion of container mode of architectural solutions, it is always recommended to have a good idea about Kubernetes or other tools, so by this article i tried my best to make u understand the concept as good as possible.