Skip to content

Ansible Deployment of Kubernetes Workloads

References

  • https://galaxy.ansible.com/community/kubernetes

Introduction

Ansible is well known as a great automation tool, useful for configuration management, state management, application deployment and upgrades. It can also be used to effectively manage Kubernetes workloads as well.

Prerequisites

In order to work with a Kubernetes cluster, the community.kubernetes ansible galaxy collection will need to be installed on the management workstation. It is also presumed that there is a working cluster administrative configuration file located at ~/.kube/config.

Sample Playbook

There are a number of modules within the community.kubernetes collection that can be used to directly manage Kubernetes objects, but the way that I've decided to use it is to have Ansible apply pre-existing Kubernetes yaml manifest files. The reason for this is that the manifest files probably already exist as a result of creating an application deployment, so, without having to recreate the entire deployment within an Ansible playbook, we can affect Kubernetes objects from either kubectl or Ansible.

Note that this presumes that the filestore backing the persistent volumes (PV) have already been created, and probably contain either the application's initial state, or current state for an existing application.

$ cat website-wiki.yml
---
#####################################################################
#
# website-wiki tiddlywiki playbook
#
# - requires that the 'devpath' variable be set
#
#####################################################################

- hosts: localhost
  tasks:

  - debug: msg="Deploying website-wiki app."

  - name: Create the tiddlywiki namespace
    community.kubernetes.k8s:
      name: tiddlywiki
      api_version: v1
      kind: Namespace
      state: present

  - name: Create the PV object
    community.kubernetes.k8s:
      state: present
      src: "{{ devpath }}/k8s/tiddlywiki/website-wiki/website-wiki_pv.yml"

  - name: Create the PVC object
    community.kubernetes.k8s:
      state: present
      namespace: tiddlywiki
      src: "{{ devpath }}/k8s/tiddlywiki/website-wiki/website-wiki_pvc.yml"

  - name: Create the Secrets object
    community.kubernetes.k8s:
      state: present
      namespace: tiddlywiki
      src: "{{ devpath }}/k8s/tiddlywiki/website-wiki/website-wiki_secret.yml"

  - name: Create the deployment object
    community.kubernetes.k8s:
      state: present
      namespace: tiddlywiki
      src: "{{ devpath }}/k8s/tiddlywiki/website-wiki/website-wiki_deployment.yml"

  - name: Create the service object
    community.kubernetes.k8s:
      state: present
      namespace: tiddlywiki
      src: "{{ devpath }}/k8s/tiddlywiki/website-wiki/website-wiki_service.yml"

  - name: Create the ingress object
    community.kubernetes.k8s:
      state: present
      namespace: tiddlywiki
      src: "{{ devpath }}/k8s/tiddlywiki/website-wiki/website-wiki_ingress.yml"

# EOF

Sample Shell Deployment Script

  • This shell script simply calls an Ansible playbook for each Kubernetes application to deploy.
$ cat k8s_deployment.sh 
#!/bin/bash
#####################################################################

devpath='/home/rdr231/dev'

ansible-playbook -i localhost, -e "devpath=${devpath}" heimdall.yml

ansible-playbook -i localhost, -e "devpath=${devpath}" gitea-mysql.yml
ansible-playbook -i localhost, -e "devpath=${devpath}" gitea-app.yml

ansible-playbook -i localhost, -e "devpath=${devpath}" transmission.yml
ansible-playbook -i localhost, -e "devpath=${devpath}" flexget.yml

ansible-playbook -i localhost, -e "devpath=${devpath}" mosquitto.yml
ansible-playbook -i localhost, -e "devpath=${devpath}" motioneye.yml
ansible-playbook -i localhost, -e "devpath=${devpath}" home-assistant.yml

ansible-playbook -i localhost, -e "devpath=${devpath}" notes-wiki.yml
ansible-playbook -i localhost, -e "devpath=${devpath}" wfh-wiki.yml
ansible-playbook -i localhost, -e "devpath=${devpath}" website-wiki.yml

ansible-playbook -i localhost, -e "devpath=${devpath}" delinit.yml
ansible-playbook -i localhost, -e "devpath=${devpath}" website.yml

# EOF

Conclusion

Using this method the deployment script completes in roughly two minutes. Depending on the current container image cache, the applications are all up and running within 30 seconds to a few minutes later.


Created: 2021-06-09 01:27
Last update: 2021-09-01 02:36