Lab Deployment of K8s using Ansible Part 1

This series will document my journey in creating a Kubernetes cluster in my home lab, which consists of a handful of mac minis with ESXi 7.02 installed and some shared storage. I appreciated that most people would use a public cloud offering for this service but to understand how this service works, I decided to deploy it locally, this way I would understand the architecture for k8s in greater detail.

Another reason to deploy it in this fashion is to have a greater understanding of VMware’s Tanzu and what value it adds to an existing customer or new customers that are looking at managing k8s within thier premises, with a longer-term view of aligning AVI and NSX-T elements so I have a complete picture of the portfolio of products.

Another technology I looking at consuming is Ansible, my long term plan is to have an SDDC deployed using Ansible. To align with this strategy I’m going to automate the deployment using Ansible. I will not blog on how to deploy it, as there are many resources that explain this in great detail. I find the DigitalOcean tutorials great How to Install and Configure Ansible on Ubuntu 18.04 | DigitalOcean

Without waffling on any further, my plan is to guide people into deploying an ansible cluster using Ansible. I will post both yml and the cli so people can consume the information how they wish.

The Goal for this series is to deliver the following using Ansible:

Deploy One (1) master node
The master node (a node in Kubernetes refers to a server) is responsible for managing the state of the cluster. It runs Etcd, which stores cluster data among components that schedule workloads to worker nodes.

Deploy Three (3) worker nodes
Worker nodes are the servers where your workloads (i.e. containerized applications and services) will run. A worker will continue to run your workload once they’re assigned to it, even if the master goes down once scheduling is complete. A cluster’s capacity can be increased by adding workers.

Prerequisites

  • An SSH key pair on your local Linux/macOS/BSD machine.
  • Familiarity with Ansible playbooks.
RoleNameIPvCPUMemoryAnsible Inventory Group membership
Masterlab-vc-ct01172.16.1.15024GBK8_Lab01_Master & K8_Lab01
Workerlab-vc-k8w01172.16.1.15124GBK8_Lab01_Workers & K8_Lab01
Workerlab-vc-k8w02172.16.1.15224GBK8_Lab01_Workers & K8_Lab01
Workerlab-vc-k8w03172.16.1.15324GBK8_Lab01_Workers % K8_Lab01
Virtual Machine configuration

Ansible Inventory for the nodes

#lab-vc

[K8_Lab01_Workers]
lab-vc-k8w01 ansible_host=172.16.1.151
lab-vc-k8w02 ansible_host=172.16.1.152
lab-vc-k8w03 ansible_host=172.16.1.153

[K8_Lab01_Master]
lab-vc-ct01 ansible_host=172.16.1.150

[K8_Lab01:children]
K8_Lab01_Master
K8_Lab01_Workers

Updating the VMs

aptget update updates the list of available packages and their versions

sudo apt-get update

The below playbook performs the above and performs old packages and removes legacy dependencies.

# apt-update.yml

---
  - hosts: K8_Lab01
    become: true
    tasks:
    - name: Update apt repo and cache on all Debian/Ubuntu boxes
      apt: update_cache=yes cache_valid_time=3600
    - name: Remove useless packages from the cache
      apt: autoclean=yes
    - name: Remove dependencies that are no longer required
      apt: autoremove=yes

Container Runtime

With K8s you have a choice of Container Runtimes (See below link) for the purposes of the deployment we are going to use Docker

https://kubernetes.io/docs/setup/production-environment/container-runtimes/#docker

Deploy Docker Prerequisites on all nodes

CLI

sudo apt-get update && sudo apt-get install -y \
  apt-transport-https ca-certificates curl software-properties-common gnupg2

Playbook

#########################################################
# K8 Playbooks: Docker prerequisites
#########################################################
---
- hosts: K8_Lab01
  become: true
  tasks:
    - name: Update the repository cache and update/install  package "apt-transport-https" to latest version
      apt:
        name: apt-transport-https
        state: latest
        update_cache: yes

    - name: Update/Install  "ca-certificates" to latest version
      apt:
        name: ca-certificates
        state: latest

    - name: Update/Install "software-properties-common" to latest version
      apt:
        name: software-properties-common
        state: latest
    
    - name: Update/Install "gnupg2" to latest version
      apt:
        name: gnupg2
        state: latest

    - name: Update/Install "curl" to latest version
      apt:
        name: curl
        state: latest

    - name: Update/Install  "lsb-release" to latest version
      apt:
        name: lsb-release
        state: latest

Deploy docker-ce

CLI

Install Docker Engine on Ubuntu | Docker Documentation

Playbook to add the Docker Repo and install docker-ce on all LAB01 nodes

#################################################
# Deploy Docker
#################################################
---
- hosts: K8_Lab01
  become: true
  tasks:
    - name: Add Docker GPG apt Key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Add Docker Repository
      apt_repository:
        repo: deb https://download.docker.com/linux/ubuntu bionic stable
        state: present

    - name: Update apt and install docker-ce
      apt: update_cache=yes name=docker-ce state=latest