How to Install and Configure Kubernetes and Docker 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

What we will do

  1. Kubeadm Installation
    1. Setup Hosts
    2. Install Docker
    3. Disable SWAP
    4. Install Kubeadm Packages
  2. Kubernetes Cluster Initialization
  3. Adding Worker Nodes to the Kubernetes Cluster
  4. Testing

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

Edit hosts file on all server using the vim editor.

sudo vim /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.

Install kubeadm

Install Docker

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

Install Docker using the apt command below.

sudo apt install docker.io -y

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.

Install Docker

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 vim /etc/fstab

Make a comment on the SWAP partition type.

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

Disable swap space

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.

cd /etc/apt/
sudo vim sources.list.d/kubernetes.list

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.

Install kubeadm package

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 "1.11.0"

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 the result as shown below.

Initialize Kubernetes Cluster

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.

Deploy flannel network

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

kubectl get nodes
kubectl get pods --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.

get node list

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

Adding Worker Nodes to the Kubernetes Cluster

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

Add second worker node

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.

List nodes

Step 4 - Testing

In this step, we will deploy the Nginx web server inside the cluster. We will deploy the Nginx web server using the YAML template.

Create a new directory named 'nginx' and go to that directory.

mkdir -p nginx/
cd nginx/

Now create the Nginx Deployment YAML file 'nginx-deployment.yaml' using the vim editor.

sudo vim nginx-deployment.yaml

Paste configurations below.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.0
        ports:
        - containerPort: 80

Save and exit.

Note:

  • We're creating a new 'Deployment' named 'nginx-deployment'.
  • Setup the app label as 'nginx' with '3' replicas.
  • The 'nginx-deployment' will have containers named 'nginx', based on 'nginx:1.14.0' docker image, and will expose the default HTTP port 80.

Now create the deployment by running the kubectl command below.

kubectl create -f nginx-deployment.yaml

After creating a new 'nginx-deployment', check the deployments list inside the cluster.

kubectl get deployments
kubectl describe deployment nginx-deployment

Test Kubernetes

Now check Kubernetes Pods and you will see the 'nginx-deployment-xxx' pod, check details of the pod.

kubectl get pods
kubectl describe pods nginx-deployment-6cb5f7bf4f-t5xfh

You will get nginx-deployment pods with 3 replicas on the worker nodes.

Get pods

Next, we need to create a new service for our 'nginx-deployment'.

Create a new YAML file named 'nginx-service.yaml'.

vim nginx-service.yaml

Paste the configuration below.

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    run: nginx-service
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: nginx

Save and exit.

Note:

  • We're creating a new kubernetes service named 'nginx-service'.
  • Type of the service is 'NodePort' with the TargetPort HTTP default port 80.
  • The service belongs to the app named 'nginx' based on our deployment 'nginx-deployment'.

Create the kubernetes service using the kubectl command below.

kubectl create -f nginx-service.yaml

Now check all available services on the cluster and you will get the 'nginx-service' on the list, then check details of service.

kubectl get service
kubectl describe service nginx-service

Configure nginx service

And you will see the 'nginx-service' NodePort is running on port '32649'.

Check using the curl command to all worker nodes.

On the worker01.

curl worker01:32649

Test nginx with curl

You will see the Nginx default page.

On the worker02.

curl worker02:32649

Test second worker with curl

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

Reference

Share this page:

Suggested articles

8 Comment(s)

Add comment

Comments

By: nmentityvibes at: 2018-11-28 11:42:13

I am getting the following error message when trying to install docker using "sudo apt install docker.io -y" as mentioned in this documentation. I am using Ubuntu 18.04.1 LTS (Bionic Beaver)". I have tried all possible ways but was not successful. Please help.

By: till at: 2018-11-28 11:43:26

Which error do you get?

By: nmentityvibes at: 2018-11-28 11:45:39

E: Package 'docker.io' has no installation candidate

By: Phi Huynh at: 2019-02-13 04:47:49

Thanks, it works well for my case.

Phi

By: Eric at: 2019-03-20 05:10:54

I am getting the following error in Step 2

[email protected]:/etc/apt# kubeadm init --pod-network-cidr=10.244.10.0/16 --apiserver-advertise-address=10.0.15.10 --kubernetes-version "1.11.0"

this version of kubeadm only supports deploying clusters with the control plane version >= 1.12.0. Current version: v1.11.0

[email protected]:/etc/apt#

 

By: Apoloj at: 2019-03-28 23:15:36

Successfully deployed.my observations and solutions:+Setup Hosts: I set up hosts via VM.Installed the unbuntu server 18.04Ran minimal installationAdded the requirements below via cli

+Kubeadm Installation+Install Docker+Install Kubeadm PackagesIssue: not able to install repositoryResolved: installed software-properties-common right after host insallationsudo apt install apt-transport-https ca-certificates curl software-properties-common

Kubernetes Cluster InitializationIssue: sudo kubeadm init --pod-network-cidr=10.244.10.0/16 --apiserver-advertise-address=10.0.15.10 --kubernetes-version "1.11.0"Solution: change the kubernetes version to match my kubernetes-version "1.14.0" sudo kubeadm init --pod-network-cidr=10.244.10.0/16 --apiserver-advertise-address=YourMasternode_ip --kubernetes-version "1.14.0"

Adding Worker Nodes to the Kubernetes ClusterIssue: I was having trouble joining the worker nodes to the clusterSolution: Swap was turn on at the master node and turned it back offsudo swapoff -a

Issue: trouble setting yaml when cut/paste the script to viSolution: copy/paste the script to notepad++ matached the spacing from notepad++ on vi and it worked

TestingDeployed local Kubenetes successfully

Thank very much for the lab. I looked for days for one I could successfully complete.

By: Stallion at: 2019-04-07 06:38:41

The Kube version that gets downloaded is not 1.14 . If you run into issue that says "this version of kubeadm only supports deploying clusters with the control plane version >= 1.12.0. Current version: v1.11.0"

 

Change the command to :sudo kubeadm init --pod-network-cidr=10.244.10.0/16 --apiserver-advertise-address=13.12.. --kubernetes-version "1.14.0"

 

Also if somebody runs into a problem that says :

"Unfortunately, an error has occurred:

        timed out waiting for the condition

 

This error is likely caused by:

        - The kubelet is not running

        - The kubelet is unhealthy due to a misconfiguration of the node "

 

Try changing the advertise id to 0.0.0.0 like:

sudo kubeadm init --pod-network-cidr=10.244.10.0/16 --apiserver-advertise-address=0.0.0.0 --kubernetes-version "1.14.0"

By: celsocesila at: 2019-04-18 09:25:47

I get an error with coredns pods, they don't initialize properly and stay in CrashLoopBackOff.

The log shows:

2019-04-18T09:19:51.659Z [INFO] CoreDNS-1.3.1 2019-04-18T09:19:51.659Z [INFO] linux/amd64, go1.11.4, 6b56a9c CoreDNS-1.3.1 linux/amd64, go1.11.4, 6b56a9c 2019-04-18T09:19:51.659Z [INFO] plugin/reload: Running configuration MD5 = 599b9eb76b8c147408aed6a0bbe0f669 E0418 09:20:16.659817       1 reflector.go:134] github.com/coredns/coredns/plugin/kubernetes/controller.go:322: Failed to list *v1.Namespace: Get https://10.96.0.1:443/api/v1/namespaces?limit=500&resourceVersion=0: dial tcp 10.96.0.1:443: i/o timeout E0418 09:20:16.659817       1 reflector.go:134] github.com/coredns/coredns/plugin/kubernetes/controller.go:322: Failed to list *v1.Namespace: Get https://10.96.0.1:443/api/v1/namespaces?limit=500&resourceVersion=0: dial tcp 10.96.0.1:443: i/o timeout log: exiting because of error: log: cannot create log: open /tmp/coredns.coredns-fb8b8dccf-nsv69.unknownuser.log.ERROR.20190418-092016.1: no such file or directory