Kubernetes

Learn to deploy a Kubernetes cluster in 30 minutes using KubeAdm

  • On Linked-In
eCloudControl Engineering Team4 min read
Learn to deploy a Kubernetes cluster in 30 minutes using KubeAdm

Learn to deploy a Kubernetes cluster in 30 minutes using KubeAdm

**Pre-Requisites: **

Create 3 VMs with 2 core 8 GB and 10-100 GB HDD

1. Docker set up in master and nodes

1a. Login into master and update your existing list of packages

$ sudo apt update

1b. Install the prerequisite packages which let apt to use packages over HTTPS

$ sudo apt install apt-transport-https ca-certificates curl software-properties-common -y

1c. Add the GPG key for the official Docker repository to your system

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

1d. Add the Docker repository to APT sources

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

1e. Update the packages with the Docker packages from the newly added repo

$ sudo apt update

1f. Make sure you are about to install from the Docker repo

$ sudo apt-cache policy docker-ce

1g. Run the following command to install Docker

$ sudo apt-get install docker-ce=18.06.3~ce~3-0~ubuntu -y containerd.io=1.2.2-1

1h. Run the following command to check if the docker is running

$ sudo systemctl status docker

1i. To avoid sudo requirement in executing the docker command, add your user to docker group

$ sudo usermod -aG docker ${USER}

1j. To apply the new group membership, run the following command

$ sudo su - ${USER}

You will be prompted to enter your user password to continue

1k. Confirm that your user is now added to the docker group by typing

$ id -nG

Repeat the above steps for both the nodes.

2. Kubernetes master set up

2a. Login to the master and install kubelet, kubeadm and kubectl

$ sudo apt-get update && sudo apt-get install -y apt-transport-https curl
$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
$ cat
$ sudo apt-get update
$ sudo apt-get install -y --allow-change-held-packages kubelet=1.18.15-00 kubeadm=1.18.15-00 kubectl=1.18.15-00
$ sudo apt-mark hold kubelet kubeadm kubectl

2b. Initialise kubeadm

$ sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Your Kubernetes master has initialized successfully!

2c. To start using your cluster, you need to run the following command 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

Copy the join command and make a note of it.

For example:

$ kubeadm join XX.X.X.XXX:6443 --token d3i30u.7p5xuyzkc5t2fm0x
--discovery-token-ca-cert-hash
sha256:4d3e70e50a8b3ecfacc64585942b93564c9af555ab90c576c59e63def90245ac

2d. Set up Flannel

$ sudo sysctl net.bridge.bridge-nf-call-iptables=1
$ sudo kubectl apply -f
https://raw.githubusercontent.com/coreos/flannel/
2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml

2e. Verify the creation of the master in the cluster.

$ kubectl get nodes NAME STATUS ROLES AGE VERSION ip-XX-X-X-XXX Ready master 3m53s v1.18.15

$ kubectl get pods --all-namespaces* NAMESPACE NAME READY STATUS RESTART AGE kube-system coredns-54ff9cd656-ww6sw 1/1 Running 0 4m2s kube-system coredns-54ff9cd656-xlrdr 1/1 Running 0 4m2s kube-system etcd-ip-XX-X-X-XXX 1/1 Running 0 2m59s kube-system kube-apiserver-ip-XX-X-X-XXX 1/1 Running 0 3m11s kube-system kube-controller-manager-ip-XX-X-X-XXX 1/1 Running 0 3m15s kube-system kube-flannel-ds-amd64-4rl8d 1/1 Running 0 45s kube-system kube-proxy-jpp7w 1/1 Running 0 4m2s kube-system kube-scheduler-ip-XX-X-X-XXX 1/1 Running 0 3m23s

3. Kubernetes node set up

3a. Login into node 1 and install kubelet, kubeadm and kubectl

$ sudo apt-get update && sudo apt-get install -y apt-transport-https curl
$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
$ cat
$ sudo apt-get update
$ sudo apt-get install -y --allow-change-held-packages kubelet=1.18.15-00 kubeadm=1.18.15-00 kubectl=1.18.15-00
$ sudo apt-mark hold kubelet kubeadm kubectl

3b. Join cluster from the command copied above in the master.

$ sudo kubeadm join XX.X.X.XXX:6443 --token d3i30u.7p5xuyzkc5t2fm0x --discovery-token-ca-cert-hash sha256:4d3e70e50a8b3ecfacc64585942b93564c9af555ab90c576c59e63def90245ac

This node has joined the cluster:

Repeat the steps for node 2.

4. Verify creation of nodes in cluster.

Login into master and verify the cluster

$ kubectl get nodes

NAME STATUS ROLES AGE VERSION ip-XX-X-X-XXX Ready 105m v1.18.15 ip-XXX-X-X-XX Ready master 123m v1.18.15 ip-XX-X-X-XX Ready 111m v1.18.15

4. Deploying the nginx with nodeport service

Use the following yaml for deploying nginx with nodeport service to test the cluster

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      run: nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    run: nginx
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
  selector:
    run: nginx

4. Test the deployment

$ curl -vk master-node:nodeport
$ curl -vk node01:nodeport
$ curl -vk node02:nodeport

About The Author

A. Nagesh

SR Cloud Dev-Ops Engineer | Cloud Control

Senior Cloud DevOps Engineer with more than five years of experience in supporting, automating, and optimizing deployments to hybrid cloud platforms using DevOps processes, CI/CD, containers and Kubernetes in both Production and Development environments

On Linked-In