Thursday, May 3, 2018

How to setup Kubernetes Cluster on Ubuntu | Create Kubernetes Clusters on Ubuntu

Kubernetes is an open source container orchestration platform initially developed by Google. Kubernetes poses high scalability, easier container management and helps to reduce the delay in communication in containers (Docker). It is used to orchestrate and manage docker containers in cluster environment.

Let us see how to install Kubernetes on Ubuntu on Amazon EC2.

Pre-requisites

  • At least 2 Ubuntu machines: one for master and one for worker
  • open port 6443 in firewall rules
Install Docker first

sudo apt-get update && sudo apt-get install -y apt-transport-https
sudo apt-get install -y docker.io
Install necessary dependencies for Kubernetes to run and also all the nodes that will join Kubernetes cluster.

Step 1 Installing Kubernetes
sudo su -
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
 Next add a repository by creating the file /etc/apt/sources.list.d/kubernetes.list and enter the following content:
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF 

After saving the file, do the below command: 
sudo apt-get update

Install Kubernetes with the following commands:
sudo apt-get install -y kubelet kubeadm kubectl kubernetes-cni

kubeadm version

Step 2 Initialize your master

With everything installed, go to the machine that will serve as the Kubernetes master and issue the command:

sudo kubeadm init

Your Kubernetes master has initialized successfully!

Before you join a node, you need to issue the following commands (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 can now join any number of machines by running the following on each node as root:

kubeadm join 172.31.29.135:6443 --token rgsh66.wtfp0z8tt8u9t0ye --discovery-token-ca-cert-hash sha256:d7e7f5485cf3e43b7f2231213ec587739506c396536621624f87a9d6aadb2800

Get all nodes by executing in master

kubectl get nodes

You must deploy a pod network before anything will actually function properly. 
Do the install of Flannel pod network.
 This can be done with two commands (run on the master):

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml

serviceaccount "flannel" created

configmap "kube-flannel-cfg" created

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/k8s-manifests/kube-flannel-rbac.yml

clusterrole.rbac.authorization.k8s.io "flannel" configured
clusterrolebinding.rbac.authorization.k8s.io "flannel" configured

sudo kubectl get pods --all-namespaces

This will display if pod network has been deployed successfully.

kubectl cluster-info

this command will display 

Kubernetes master is running at https://172.31.9.117:6443
KubeDNS is running at https://172.31.9.117:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

Joining a node

First do the same installation (all step #1 commands) on this node.

With everything in place, you are ready to join the node to the master. To do this, go to the node's terminal and issue the command:
sudo kubeadm join --token TOKEN MASTER_IP:6443 --discovery-token-ca-cert-hash

Where TOKEN is the token you were presented after initializing the master and MASTER_IP is the IP address of the master.

Once the node has joined, go back to the master and issue the below command:
sudo kubectl get nodes 
to see the node has successfully joined.
NAME               STATUS    ROLES     AGE       VERSION
ip-172-31-25-31    Ready     master    19m       v1.10.3
ip-172-31-39-149   Ready     <none>    8m        v1.10.3

Deploying a service

At this point, you are ready to deploy a service on your Kubernetes cluster. To deploy an NGINX service (and expose the service on port 80), run the following commands (from the master):
 
sudo kubectl run --image=nginx nginx-app --port=80
sudo kubectl expose deployment nginx-app --port=80 --name=nginx-http

Kubernetes cluster is setup

You now have a basic Kubernetes cluster, consisting of a master and a single node.

No comments:

Post a Comment