How to Deploy a single master Kubernetes cluster with Ansible

Kubernetes is one of the most popular open-source and enterprise-ready container orchestration systems. It’s used to automate the deployment, scaling, and management of containerized applications. Manual Kubernetes installation is a laborious and error-prone process. However it can be dramatically simplified by using configuration management tools such as Ansible. This article demonstrates how to deploy a full-function Kubernetes cluster using Ansbile with our installation package.

Cluster Designing

Our Kubernetes cluster consists of three servers. One of them will be working as Kubernetes Master. The other two are worker nodes. All servers are in the same internal network, Software components the cluster depends on are Kubernetes, Etcd, Docker, Calico, Flannel, Helm and Nginx-ingress-controller.


The servers have the following requirements:

  • Each servers ,it can bare-metal or virtual server, has at least 2 CPU/vCPU cours, 4GB RAM and 10GB disk space, with Ubuntu 16.04 LTS or CentOS/RHEL 7 installed.
  • All servers are in the same network and able to see each other.
  • Ansible Server can be setup one one server with Ansible v2.4 (or later) and python-netaddr installed in the same network.
  • Internet access is available for all servers to download software binaries.
  • Root user remote login has to be enabled on all servers except the Ansible Server.


The following is our server inventory and architecture design.

Server Name IP Address Roles
Master01 Master & Ansible Server
Node01 Worker node
Node02 Worker node

Kubernetes Cluster Installation

Preparing All Servers

For Ubuntu 16:

1.Install python2.7

$ sudo apt update
$ sudo apt install python2.7 -y
$ sudo ln -s /usr/bin/python2.7 /usr/bin/python    

2.Set root remote access

$ sudo passwd root
$ sudo sed -i 's/prohibit-password/yes/' /etc/ssh/sshd_config
$ sudo service ssh restart

For Centos 7:

Install python2.7

# yum install epel-release -y
# yum update -y
# yum install python -y   

Setting Up Kubernetes Cluster

All steps are performed on the master server

1.Set up Ansible Server

The master server is setup as the Ansbile Server

For Ubuntu 16:

$ apt-get install git python-pip -y
$ pip install pip --upgrade 
$ pip install --no-cache-dir ansible    

You might encounter the following error

Traceback (most recent call last):
File "/usr/bin/pip", line 9, in 
from pip import main
ImportError: cannot import name main

A quick fix is to modify /usr/bin/pip, finding the following section

from pip import main
if __name__ == '__main__':

and replacing it with

from pip import __main__
if __name__ == '__main__':

Re-run the command

$ pip install --no-cache-dir ansible 

For CentOS 7:

# yum install git python-pip -y
# pip install pip --upgrade 
# pip install --no-cache-dir ansible  

2.Enable Password-less SSH

Generate the SSH keys on master node:

# ssh-keygen -t rsa -b 2048  

Copy the SSH key from master node to all other nodes in the cluster :

# ssh-copy-id
# ssh-copy-id
# ssh-copy-id  

3.Download installation package


4.Unpack it to /etc folder

# tar -zxvf single-master-k8s-ansible.tar.gz -C /etc/ 

5.Modify the Ansible inventory file/etc/ansible/hosts to fit your environment. Usually you need to replace server’s IP addresses and its interface name. If you prefer to use calico as your network component, change network_plugin’s value to calico

# Kubernetes nodes
[kube-master] node_name=master01 
[kube-node] node_name=node01 node_name=node02
# Choose network plugin (calico or flannel)
# Set up pod network
# The Exec Files Directory On Kubernetes Nodes
# Ansible Working Directory
# Docker Version
# Kubernetes version
# Helm Version
# Kubernetes apps yaml dir
# Kubernetes cert directory
# Kubernetes config directory

6.Setup the Kubernetes cluster

# cd /etc/ansible
# ansible-playbook deploy.yml 

Verify Installation Results

Check Kubernetes version

# kubectl version 


# Check the status of Kubernetes components

kubectl get componentstatus  


Check the worker node status

# kubectl get node  


Check the status of all the pods

# kubectl get pod --all-namespaces   


Verifying Kubernetes dashboard

Open in your browser and select Token option


The Dashboard token can be retrieved by running the following command

# Dashboard_Secret=`kubectl get secret -n kube-system|grep kubernetes-dashboard-token|awk '{print $1}'`
# kubectl describe secret -n kube-system ${Dashboard_Secret} |sed -n '$p'|awk '{print $NF}'  



You are done. Now There’s a full-function working Kubernetes cluster to test out. You can explore our other solutions if you want to know more. Thank you!

1 Master/Node Server
CPUs : E5-2670
RAM : 128GB
Disks : 240GB SSD + 2*960GB SSD + 3TB SATA
$129.00 / Month

More Details

8 Bare-Metal Servers
Master Servers : 3
CPU : E3-1230
RAM : 16GB
High Availability :
Node Servers : 5
CPUs : E5-2670
RAM : 128GB
Disks : 240GB SSD + 2*960GB SSD + 3TB SATA
$872.00 / Month

More Details

We use cookies to help optimize the website and give you the best experience. Privacy Policy