Kubernetes is the opensource software for the containerized application to deploy, scale and manage the clusters of hosts. There are many ways to install and configure Kubernetes components. Kubeadm will take care of installing Kubernetes Components. With the help of Ansible and Kubeadm, we can completely set up the Kubernetes clusters. In this article, we will discuss How to Install and Configure Kubernetes Cluster with Kubeadm and Ansible on Ubuntu 16.04 and deploy a sample application on the clusters.
Architecture of Kubernetes.
Before getting into the setup. Let us discuss the Architecture of the Kubernetes. This will help us understand the components and use of it. Basically, Kubernetes is having two major types of nodes called Master and Worker nodes.

Master Node:
Master Node is having following components.
- API Server – It creates an interface between Cluster nodes and other components of the Kubernetes master nodes.
- Etcd – This is the Key-Value storage which stores the state at any point of any cluster.
- Scheduler – This component watches newly created pods that are not assigned to any workload or service and assign it for the same.
-
Kube – Control Manager – Kube control manager is for Kubernetes native components. This Control manager includes following sub-components
- Node Controller – When a node is going down, this component will notify and respond.
- Replication Controller – This will take care of the number of pods per replication.
- Endpoint Controller – It will take care of endpoints of all services.
- Service Account Controller – This will take care of account management for API and other RBAC.
-
Cloud – Control Manager – Cloud control Manager will interact with Cloud providers to run and control the cloud-based containers. This feature is available only after Kubernetes 1.6. This includes the following sub-components.
- Node Controller – Checks if a node in the cloud is down or not and it will notify and response for the situation.
- Route Controller – This will create network level routing in cloud infrastructure.
- Service Controller – This is basically a load-balancer controller from the cloud.
- Volume Controller – This is for maintaining the volumes in terms of creating, attaching mounting and interacting with the cloud provider.
Worker Node:
Worker Node is responsible for maintaining the runtime or workload of the application or service. This is having the following components.
- Kubelet – This is responsible for running containers inside the pods on every node. This is the communication point to the Kubernetes master and lets the master node orchestrate the worker node.
- Kube-proxy – This will enable connection forwarding and maintain network rules so that the end user of the application can interact with the worker node.
- Pods – Pod is the collection of containers. Where one or more container runtimes are running inside the pods.
Requirements
We are going to set up following nodes
- Master Node – Where Kubernetes Master will run
- Worker Node 1 – This is the node where application or service will run.
- Worker Node 2 – This will also run Application or service.
All these three nodes are going to be in Ubuntu 16.04. So, we need to make the note of the following requirements
- Three Ubuntu 16.04 Machines connected in the network.
- Ansible Installed on a machine and that should be able to connect with above three machines.
- Knowledge of Ansible and Docker Containers
Let us get in to the Installation and configuration of Kubernetes clusters step by step.
How to Install and Configure Kubernetes Cluster
So, Here we go. We will see how to Install and Configure Kubernetes Cluster with Kubeadm and Ansible. To make it clear again, Ansible is to install Kubernetes dependencies and kubeadm. Then kubeadm will take care of installing kubernetes components.
Step 1: Ansible setup for Kubernetes
Before starting with Ansible, All the Ansible scripts that are used here are published in GitHub ( https://github.com/digitalvarys/kube-ansible). Please clone if you wish to use it.
For hosting Ansible orchestration, either take machine which will communicate with all the nodes we selected or configure Ansible in local machine. First, create the workspace folder for the Kubernetes.
mkdir ~/kube-ansible cd ~/kube-ansible
This folder is going to be the workspace for Ansible. Then Create hosts
file by passing following command
nano ~/kube-ansible /hosts
Then add the node groups in the hosts
file. Add the following lines in the hosts
file.
[masters] master ansible_host=172.168.0.10 ansible_user=root [workers] worker1 ansible_host=172.168.0.11 ansible_user=root worker2 ansible_host=172.168.0.12 ansible_user=root [all:vars] ansible_python_interpreter=/usr/bin/python3
In this, We have grouped two clusters called master and workers. Where ” masters” group will have the kubernetes master node and ” worker ” will have worker clusters nodes.
Step 2: Creating Non-root user
Kubernetes need to be installed with a non-root user. So, with Ansible we will create a non-root user called ” ubuntu ” in all the nodes. For the same create a YAML file by passing the following command.
nano ~/kube-ansible/non-root-user.yml
then add following lines in the file.
- hosts: all become: yes tasks: - name: creating 'ubuntu' user user: name=ubuntu append=yes state=present createhome=yes shell=/bin/bash - name: 'ubuntu' for passwordless sudo lineinfile: dest: /etc/sudoers line: 'ubuntu ALL=(ALL) NOPASSWD: ALL' validate: 'visudo -cf %s' - name: authorized keys for the ubuntu user authorized_key: user=ubuntu key="{{item}}" with_file: - ~/.ssh/id_rsa.pub
This file will create a user called ” ubuntu” and set password-less sudo entry for ” ubuntu ” user and then add an ssh key for the authorization for “ubuntu” user.
Once the above file is saved, run the following command to run the ansible playbook.
ansible-playbook -i hosts ~/kube-ansible/non-root-user.yml
This will give the following console output.
PLAY [all] **** TASK [Gathering Facts] **** ok: [master] ok: [worker1] ok: [worker2] TASK [creating 'ubuntu' user] **** changed: [master] changed: [worker1] changed: [worker2] TASK ['ubuntu' for passwordless sudo] **** changed: [master] changed: [worker1] changed: [worker2] TASK [authorized keys for the ubuntu user] **** changed: [worker1] => (item=ssh-rsa AAAAB3... changed: [worker2] => (item=ssh-rsa AAAAB3... changed: [master] => (item=ssh-rsa AAAAB3... PLAY RECAP **** master : ok=5 changed=4 unreachable=0 failed=0 worker1 : ok=5 changed=4 unreachable=0 failed=0 worker2 : ok=5 changed=4 unreachable=0 failed=0
Next, we will go for installing Kubernetes binaries.
Step 3: Installing Kubernetes Binaries.
To run all Kubernetes setup, we need to install all the following necessary components.
- Container Runtime: There are many container runtimes available like rkt or Docker. In our case, we are going to install Docker.
- Kubeadm: This will take care of installing the many cluster components.
- Kubelet: system level service to communicate master and handle node operations.
- Kubectl: OperateKubernetes master from Command line interface. This will talk with API server.
So, all these components are going to be installed using APT package manager with ansible. Open a yaml file by passing following command in the command line.
nano ~/kube-ansible/kube-dependencies.yml
Then add the following lines in the above file.
- hosts: all become: yes tasks: - name: Docker Installation apt: name: docker.io state: present update_cache: true - name: install APT Transport HTTPS apt: name: apt-transport-https state: present - name: add Kubernetes apt-key for APT repository apt_key: url: https://packages.cloud.google.com/apt/doc/apt-key.gpg state: present - name: add Kubernetes APT repository apt_repository: repo: deb http://apt.kubernetes.io/ kubernetes-xenial main state: present filename: 'kubernetes' - name: install kubelet apt: name: kubelet=1.14.0-00 state: present update_cache: true - name: install kubeadm apt: name: kubeadm=1.14.0-00 state: present - hosts: master become: yes tasks: - name: install kubectl apt: name: kubectl=1.14.0-00 state: present force: yes
The above code will do the needed dependent components in order.
- The First task will install Docker runtime
- Then external https source to apt will be installed
- After that, adds Kubernetes apt-key for APT repository
- Then, adds Kubernetes to the APT repository
- After that, Installing kubelet and kubeadm on all the instances.
- Then, only on the master node, kubectl is installed.
Once after adding the above lines, run the ansible playbook by passing following command.
ansible-playbook -i hosts ~/kube-ansible/kube-dependencies.yml
This will through following output
PLAY [all] **** TASK [Gathering Facts] **** ok: [worker1] ok: [worker2] ok: [master] TASK [Docker Installation] **** changed: [master] changed: [worker1] changed: [worker2] TASK [install APT Transport HTTPS] ***** ok: [master] ok: [worker1] changed: [worker2] TASK [add Kubernetes apt-key for APT repository] ***** changed: [master] changed: [worker1] changed: [worker2] TASK [add Kubernetes APT repository] ***** changed: [master] changed: [worker1] changed: [worker2] TASK [install kubelet] ***** changed: [master] changed: [worker1] changed: [worker2] TASK [install kubeadm] ***** changed: [master] changed: [worker1] changed: [worker2] PLAY [master] ***** TASK [Gathering Facts] ***** ok: [master] TASK [install kubectl] ****** ok: [master] PLAY RECAP **** master : ok=9 changed=5 unreachable=0 failed=0 worker1 : ok=7 changed=5 unreachable=0 failed=0 worker2 : ok=7 changed=5 unreachable=0 failed=0
Now, all the kubernetes dependencies are installed in all the nodes with Ansible. Now, let us configure the master node.
Step 4: Master node setup
To set up the master node, let us create ansible playbook by passing the following command.
nano ~/kube-ansible/master-cluster.yml
Then add the following lines on the above file.
- hosts: master become: yes tasks: - name: Start the cluster shell: kubeadm init --pod-network-cidr=10.244.0.0/16 >> cluster.txt args: chdir: $HOME creates: cluster.txt - name: create .kube directory become: yes become_user: ubuntu file: path: $HOME/.kube state: directory mode: 0755 - name: copy admin.conf to user's kube config copy: src: /etc/kubernetes/admin.conf dest: /home/ubuntu/.kube/config remote_src: yes owner: ubuntu - name: install Pod network become: yes become_user: ubuntu shell: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml >> pod_setup.txt args: chdir: $HOME creates: pod_setup.txt
In the above playbook, We have started the cluster first. Then “.kube” Directory will be created with the right permission. Then copying admin configuration file in the “.kube” file. After that, Configuring pod network configuration by installing ” flannel “.
Once after adding the above lines. Run the ansible playbook by passing the following command.
ansible-playbook -i hosts ~/kube-ansible/master-cluster.yml
then this will through following output.
PLAY [master] **** TASK [Gathering Facts] **** ok: [master] TASK [Start the cluster] **** changed: [master] TASK [create .kube directory] **** changed: [master] TASK [copy admin.conf to user's kube config] ***** changed: [master] TASK [install Pod network] ***** changed: [master] PLAY RECAP **** master : ok=5 changed=4 unreachable=0 failed=0
Verify the node status by passing the following command into the Master node.
kubectl get nodes
and this should give the following output
NAME STATUS ROLES AGE VERSION master Ready master 0d v2.13.0
Now, we will set up the worker nodes.
Step 5: Worker nodes setup
In the worker node, we have already installed all the necessary Kubernetes components. Now we just need to attach worker node to master node. To create the same, Open the worker ansible playbook by passing the following command.
nano ~/kube-ansible/workers-cluster.yml
Then, add the following lines.
- hosts: master become: yes gather_facts: false tasks: - name: get join command shell: kubeadm token create --print-join-command register: join_command_raw - name: set join command set_fact: join_command: "{{ join_command_raw.stdout_lines[0] }}" - hosts: workers become: yes tasks: - name: join cluster shell: "{{ hostvars['master'].join_command }} >> node_joined.txt" args: chdir: $HOME creates: node_joined.txt
In this, we are adding a task on the master node to create a token to join the worker node to the master node. Then in the worker node, creating a task to join the master node.
To run the Ansible playbook, we need to pass the following command.
ansible-playbook -i hosts ~/kube-ansible/workers-cluster.yml
This will through the following output.
PLAY [master] **** TASK [get join command] **** changed: [master] TASK [set join command] ***** ok: [master] PLAY [workers] ***** TASK [Gathering Facts] ***** ok: [worker1] ok: [worker2] TASK [join cluster] ***** changed: [worker1] changed: [worker2] PLAY RECAP ***** master : ok=2 changed=1 unreachable=0 failed=0 worker1 : ok=2 changed=1 unreachable=0 failed=0 worker2 : ok=2 changed=1 unreachable=0 failed=0
Now we have configured the worker nodes. Then, let us verify the cluster by passing following command on the master node.
kubectl get nodes
and this should give the following output now.
NAME STATUS ROLES AGE VERSION master Ready master 0d v2.13.0 worker1 Ready <none> 0d v1.26.0 worker2 Ready <none> 0d v1.26.0
If you are seeing the above output, Then, our configurations are working. Now, let us see how we can deploy a sample application on our Kubernetes cluster.
Step 6: Deploying and Running the sample application on the kubernetes cluster.
Let us deploy Apache server on the clusters by passing following command
kubectl create deployment apache-server --image=httpd
This command will create a pod with a container from the image of httpd. Now let us create a service with apache server by passing following command
kubectl expose deploy apache-server --port 80 --target-port 80 --type NodePort
Now, lets see the running services by passing following command.
kubectl get services
This will open the following output
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE Apache-server NodePort 10.109.228.209 <none> 80:80/TCP 1d
Now we can even test the application on the browser by hitting the following URL, http://172.168.0.11:80
or http://172.168.0.12:80
.
Conclusion:
In this article, we have discussed How to Install and Configure the Kubernetes Cluster with Kubeadm and Ansible on Ubuntu 16.04. Running application Kubernetes is an art. We have to learn it from the fine resource. To learn more about the Kubernetes, I recommend this ebook
Use this link to get the offer price.
We will discuss more on the Kubernetes in our upcoming articles. Stay tuned for more DevOps, DevSecOps, and Agile related topics.
Article by channel:
Everything you need to know about Digital Transformation
The best articles, news and events direct to your inbox
Read more articles tagged: Cloud