Single node kubernetes cluster setup with Ansible

Setting up a kubernetes cluster on cloud of your choice is always a challenge. As a part of my routine experiments with kubernetes, creating a sandbox cluster in few clicks was always on my priority list. Here is my quick script to setup a cluster on a virtual machine in cloud.

I will be using DigitalOcean droplet to spin a cluster from scratch. Terraform (https://www.terraform.io) has alway been a go to orchestration tool to create a virtual machine on cloud. For configuration managment, Ansible (https://www.ansible.com/) comes handy to setup kubernetes cluster in the virutal machine. And to sum it up, GoLang is used to provide the automated script experience.

All the scripts are available at following github repository:

https://github.com/mngaonkar/ghost-blog-cloud-native/tree/master/ghost-blog-kubernetes-infra

Here is step by step process to create a kubernetes cluster.

  1. Get your DigitalOcean token and set as an environment variable
export DO_TOKEN=<your token here>

2. Make sure public and private keys are generated for local machine. The kyes are located at ~/.ssh/id_rsa as private and ~/.ssh/id_rsa.pub as public keys. If the keys are not found, generate the key pair. Also, set the SSH fingerprint environment variable.

ssh-keygen -E md5 -lf ~/.ssh/id_rsa.pub | awk '{print $2}'
MD5:42:c2:c9:5b:e9:01:93:c2:d3:98:e6:66:72:68:bf:2d
export SSH_FINGERPRINT=42:c2:c9:5b:e9:01:93:c2:d3:98:e6:66:72:68:bf:2d

3. Run go script to create virtual machine.

go run deploy_ghost_cluster.go

4. Once the virtual machine is created successfully, update host file with public IP address of virtual machine.

root@206.189.219.245

If the Ansible is run locally then change the host file as follows.

root@127.0.0.1 ansible_connection=local

For Ansible to work on virtual machine, it is necessary to have Python 2.x installed. Install Python on virtual machine.

apt-get install python

5. Run Ansible

ansible-playbook playbook.yml

6. Once the cluster is up and running, check the cluster state by running kubectl command in virtual machine.

kubectl get nodes
NAME STATUS ROLES AGE VERSION
ghost-master   Ready     master    35s       v1.10.4

7. Copy kubernetes config (~/.kube/config) from virtual machine to local machine. Set environment variable KUBECONFIG to point to config file. Now you can run kubectl command from local machine to manage remote kubernetes cluster.

Like what you read? Give Mahadev Gaonkar a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.