Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Monday, April 20, 2020

How to set up Kubernetes cluster in freshly deployed Ubuntu virtual machine

Setup kubernetest cluster in ubuntu virtual machine

Kubernetes is the best available orchestration tool for dockers. To set up Kubernetes in the ubuntu box please follow the below steps. Docker is the prerequisite to install Kubernetes. The steps provided below will take care of the docker installation before proceeding with the Kubernetes installation.

Procedure
Prerequisite: Have root access to the ubuntu machine.
  1. ssh to ubuntu virtual machine. 
  2. Run the command apt-get update.
  3. Copy the script from Script Content as displayed below and save it as install_kubernetes.sh
  4. Edit the script and change the apiserver-advertise-address in the script.
  5. To get the IP address just run ip addr
  6. Run the script in the ubuntu machine as sh install_kubernetes.sh
Script Content:

#!/bin/bash

echo "installing docker"
apt-get update
apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository \
   "deb https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
   $(lsb_release -cs) \
   stable"
apt-get update && apt-get install -y docker-ce=$(apt-cache madison docker-ce | grep 17.03 | head -1 | awk '{print $3}')

echo "installing kubernetes"
apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet=1.13.5-00 kubeadm=1.13.5-00 kubectl=1.13.5-00

echo "deploying kubernetes (with calico)..."
kubeadm init --pod-network-cidr=192.168.0.0/16 --apiserver-advertise-address="172.31.X.X" 
export KUBECONFIG=/etc/kubernetes/admin.conf



kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml


The output of the above command will be like below: Please save the below output for future use. 

master_install_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 10.149.X.X:6443 --token 6zqewj.0aycerc78v3es6gk \
    --discovery-token-ca-cert-hash sha256:4aa355c1340feccabaceda0ebaeab0996e040c998ed6255d8ec2357cf66e

Please run the above 3 commands on the master node.
Once done you can verify the installment using the below command.

kubectl cluster-info 
Output:

Now the cluster is set up with one node i.e. master, verify this information by running the command.

kubectl get nodes  
Output :


Add Nodes to Kubernetes cluster 


Now to add other nodes in this cluster, setup another ubuntu machine. 

Run the below script in that ubuntu machine. 

#!/bin/bash
echo "installing docker"
apt-get update
apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository \
   "deb https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
   $(lsb_release -cs) \
   stable"
apt-get update && apt-get install -y docker-ce=$(apt-cache madison docker-ce | grep 17.03 | head -1 | awk '{print $3}')

echo "installing kubeadm and kubectl"
apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet=1.13.5-00 kubeadm=1.13.5-00 kubectl=1.13.5-00


after running the above script , copy the kubeadm join comment which we stored as part of the master node installation. as depicted under master_install_output heading , and run it in second ubuntu machine.


kubeadm join 10.149.X.X:6443 --token 6zqewj.0aycerc78v3es6gk \
    --discovery-token-ca-cert-hash sha256:4aa355c1340feccabaceda0ebaeab0996e040c998ed6255d8ec2357cf66e

This will join this machine to our already setup cluster. 

No comments: