Skip to content

Category: DevOps

Configure Docker, Kubernetes with Dashboard and Nginx on Ubuntu 18.04 LTS

Kubernetes is an open source platform for managing containerized applications. It allows you to manage, scale, and automatically deploy your containerized applications in the clustered environment. Kubernetes is developed by Google.

With Kubernetes, you can orchestrate containers across multiple hosts, scale the containerized applications with all resources on the fly, and have a centralized container management environment.

In this tutorial, I will show you step-by-step how to install and configure Kubernetes on Ubuntu 18.04. We will be using 1 server ‘k8s-master’ as the Kubernetes Host Master, and 2 servers as Kubernetes workers, ‘worker01’ and ‘worker02’.

Prerequisites

  • 3 Ubuntu Servers
  • 10.0.15.10  k8s-master
  • 10.0.15.21  worker01
  • 10.0.15.22  worker02
  • Root privileges

Step 1 – Kubeadm Installation

In this first step, we will prepare those 3 servers for Kubernetes installation, so run all commands on the master and worker nodes.

We will prepare all servers for Kubernetes installation by changing the existing configuration on servers, and also installing some packages, including docker and kubernetes itself.

Setup Hosts

Setup Hostname of each PC.

nano /etc/hostname

Put the name of the host for each PC. Then save and exit.

Now Setup static IP for each PC.

nano /etc/network/interfaces

Paste the code given bellow. Just change the IP according to the PC.

auto lo
iface lo inet loopback

auto enp0s8
iface enp0s8 inet static
address 10.0.15.10

Edit host file of each node including k8s-master

sudo nano /etc/hosts

Paste hosts configuration below.

10.0.15.10  k8s-master
10.0.15.21  worker01
10.0.15.22  worker02

Save and exit.

Now test ping all servers hostname.

ping -c 3 k8s-master
ping -c 3 worker01
ping -c 3 worker02

Make sure all IP address get resolved as a hostname and you get successful ping response.

Install Docker

In this tutorial, we will install Docker from the Ubuntu repository.

Install Docker using the apt command below.

apt-get install -y docker.io

After the installation is complete, start the docker service and enable it to launch everytime at system boot.

sudo systemctl start docker
sudo systemctl enable docker

Docker installation has been completed.

Disable SWAP

In order to set up the Kubernetes Linux servers, we need to disable the SWAP.Check the swap list and disable it.

sudo swapon -s
sudo swapoff -a

To disable the SWAP permanently, we need to edit the ‘/etc/fstab’ file.

sudo nano /etc/fstab

Make a comment on the SWAP partition type.

#/dev/mapper/hakase--labs--vg-swap_1 none            swap    sw              0       0

Save and exit, then reboot the system.

sudo reboot

Install Kubeadm Packages

In this tutorial, we will be using Kubeadm packages to set up the Kubernetes Cluster. We will install the Kubeadm packages from the official Kubernetes repository.

Install apt-transport-https.

sudo apt install -y apt-transport-https

Add the Kubernetes Key.

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

And add the Kubernetes Repository by creating a new repo.list file on the ‘/etc/apt/sources.list.d’ directory.

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF

paste kubernetes repository below.

deb http://apt.kubernetes.io/ kubernetes-xenial main

Note:

We’re still using the Xenial Ubuntu 16.04 repository for our Kubeadm Installation.

Now update the repository and install kubeadm packages using apt commands below.

sudo apt update
sudo apt install -y kubeadm kubelet kubectl

Wait for kubeadm packages installations.

Edit 10-kubeadm.conf

nano /etc/systemd/system/kubelet.service.d/10-kubeadm.conf 

Add the line given bellow.

Environment="cgroup-driver=systemd/cgroup-driver=cgroupfs"

Reboot all machines.

Step 2 – Kubernetes Cluster Initialization

In this step, we will initialize Kubernetes on the ‘k8s-master’ node. Run all commands in this stage only on the ‘k8s-master’ server. Initialize the Kubernetes cluster using the kubeadm command below.

sudo kubeadm init --pod-network-cidr=10.244.10.0/16 --apiserver-advertise-address=10.0.15.10 --kubernetes-version=v1.14.2

Note:

  • –apiserver-advertise-address = determines which IP address Kubernetes should advertise its API server on.
  • –pod-network-cidr = specify the range of IP addresses for the pod network. We’re using the ‘flannel’ virtual network. If you want to use another pod network such as weave-net or calico, change the range IP address.

When the Kubernetes initialization is complete, you will get a kubeadm join … … …‘ command . Copy the ‘kubeadm join … … …‘ command to your text editor. The command will be used to register new worker nodes to the kubernetes cluster.

Now in order to use Kubernetes, we need to run some commands as shown in the result.

Create new ‘.kube’ configuration directory and copy the configuration ‘admin.conf’ from ‘/etc/kubernetes’ directory.

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Next, deploy the flannel network to the kubernetes cluster using the kubectl command.

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

The flannel network has been deployed to the Kubernetes cluster.

Wait for a minute and then check kubernetes node and pods using commands below.

kubectl get nodes
kubectl get pods -o wide --all-namespaces

And you will get the ‘k8s-master’ node is running as a ‘master’ cluster with status ‘ready’, and all ‘kube-system’ pods that are needed for the cluster is up and running.

Kubernetes cluster master initialization and configuration has been completed.

Step 3 – Adding Worker Nodes to the Kubernetes Cluster

In this step, we will add two node workers ‘worker01’ and ‘worker02’ to the Kubernetes Cluster.

Connect to the ‘worker01’ server and run the kubeadm join command that you get from the cluster initialization.

kubeadm join 10.0.15.10:6443 --token daync8.5dcgj6c6xc7l8hay --discovery-token-ca-cert-hash sha256:65a3e69531d323c335613dea1e498656236bba22e6cf3d5c54b21d744ef97dcd

Connect to the ‘worker02’ server and run the kubeadm join command that you get from the cluster intialization..

kubeadm join 10.0.15.10:6443 --token daync8.5dcgj6c6xc7l8hay --discovery-token-ca-cert-hash sha256:65a3e69531d323c335613dea1e498656236bba22e6cf3d5c54b21d744ef97dcd

Wait for some minutes and back to the ‘k8s-master’ node master and check node status.

kubectl get nodes

You will see those worker nodes ‘worker01’ and ‘worker02’ are part of the Kubernetes Cluster.

Create Dashboard

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml

Start Proxy

kubectl proxy

Now open this link with your browser.

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

Create Service Account Dashboard

kubectl create serviceaccount dashboard -n default

cluster role binding

kubectl create clusterrolebinding kubernetes-dashboard --clusterrole=cluster-admin --serviceaccount=kube-system:kubernetes-dashboard

Generate token

kubectl -n kube-system get secret

kubectl -n kube-system describe secret kubernetes-dashboard-token-p8bsj

Now you will get a token. Use this token to login http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

Configure NGINX

kubectl create deployment nginx --image=nginx

kubectl get deployments

kubectl describe deployment nginx

kubectl create service nodeport nginx --tcp=80:80

Now you will get a port number. Use that port to access the nginx webserver.

http://k8s-master:31555/

The Kubernetes Cluster installation and configuration on Ubuntu 18.04 has been completed successfully.

Reference

Leave a Comment