Create a Kubernetes Cluster With Vagrant and Ansible

Image for post
Image for post
MULTIPLE GENERATIONS OF STARS IN THE TARANTULA NEBULA By The Hubble Heritage Team
https://commons.wikimedia.org/w/index.php?curid=461570

Introduction

The TL;DR Version

git clone https://github.com/MonadicT/kube
cluster.conf
sudo apt install ansible
./create-cluster.sh
./verify-cluster.sh
./deploy-wordpress.sh

Longer Version

Initial Preparation

Creating Virtual Machines

Ansible Setup

sudo apt install ansible
vagrant ssh-config > ssh_config
[ssh_connection]
ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s -F ssh_config

Ansible Inventory File

[defaults]# some basic default values...inventory      = hosts

Create Kubernetes Master

ansible-playbook kmaster.yml
- name: Pick IP from the same network as master
set_fact: hostip="{{ ansible_all_ipv4_addresses|ipaddr(master_ip_nw) }}"
- name: Send bridge packets to iptables for processing
block:
- lineinfile:
path: /etc/sysctl.conf
line: net.bridge.bridge-nf-call-iptables=1
create: yes
- lineinfile:
path: /etc/sysctl.conf
line: net.bridge.bridge-nf-call-ip6tables=1
create: yes
- command: sysctl net.bridge.bridge-nf-call-iptables=1
- command: sysctl net.bridge.bridge-nf-call-ip6tables=1
become: true
- name: Initialize cluster
command: kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address "{{ hostip[0] }}"
when: inited.rc > 0
become: true

Create Kubernetes Workers

ansible-playbook kworker.yml
# Join all workers to cluster
JOIN_CMD=$(ansible masters -m shell -b -a "kubeadm token create --print-join-command"|awk '{sub(/.*>>/, "");print}')
ansible workers -m shell -b -a "$JOIN_CMD"

Create Storage Provisioner

Bash it all in!

./create-cluster.sh
./verify-cluster.sh

Deploy Wordpress and MySql

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
WordPress

Closing Remarks

Written by

I Make Software. Elixir, Elm, Python. Lots of Python, Java, Clojure, SmallTalk and C. HA73 Java :) Love Emacs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store