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
- Kubeadm Installation
- Setup Hosts
- Install Docker
- Disable SWAP
- Install Kubeadm Packages
- Kubernetes Cluster Initialization
- Adding Worker Nodes to the Kubernetes Cluster
- 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 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.
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
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.
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.
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 –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.
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
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.
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
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
You will see the Nginx default page.
On the worker02.
curl worker02:32649
The Kubernetes Cluster installation and configuration on Ubuntu 18.04 has been completed successfully.