Ansible + K8s for the laziest person -Part 2

Adilson Cesar
4 min readMay 27, 2020

In the previous guideline, we spent a lot of time typing in commands in our terminal. When you start operating at scale, there’s no way you can manage dozens or hundreds of applications and deployments this way and keep your sanity.

In this part, We will run an extremely simple playbook on the same machine running Ansible, and then learned how to automate all the manual Minikube and Kubernetes deployment steps for the Hello Go app from Part 1.

It’s easier than you think!

Pre-requisites

Ansible’s only real dependency is Python. Once Python is installed, the simplest way to get Ansible running is to use pip, a simple package manager for Python.

# pip install ansible
# pip install openshift

You can also more details here.

YAML is very picky about indentation! Make sure you have the hosts and gather_facts keywords on the same indent (2 spaces), and also keep in mind that YAML only allows spaces (no tabs!) for indentation.

SO, you can create a playbook named main.yml playbook:

# cat >> main.yml <<'EOF'# Setting host
- hosts: localhost
gather_facts: false
#Variables
vars:
ansible_python_interpreter: '{{ ansible_playbook_python }}'
image_name: hello-go
image_tag: latest
replicas: 4
# Pre-Tasks to validate if Minikube is running
pre_tasks:
- name: Check Minikube's status.
command: minikube status
register: minikube_status
changed_when: false
ignore_errors: true
# Otherwise will start minikube
- name: Start Minikube if it's not running.
command: minikube start
when: "not minikube_status.stdout or 'Running' not in minikube_status.stdout"
# Check existing images
tasks:
- name: Get existing image hash.
shell: |
eval $(minikube docker-env)
docker images -q {{ image_name }}
register: image_hash
changed_when: false
# Otherwise will create an image from Dockerfile location
- name: Build image if it's not already built.
shell: |
eval $(minikube docker-env)
docker build -t {{ image_name }} ../GoProject
when: not image_hash.stdout
# Create Kubernetes resources to run Hello Go.
- name: Create a Deployment for Hello Go.
k8s:
state: present
definition:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-go
namespace: default
spec:
replicas: "{{ replicas }}"
selector:
matchLabels:
app: hello-go
template:
metadata:
labels:
app: hello-go
spec:
containers:
- name: hello-go
image: "{{ image_name }}:{{ image_tag }}"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8180
# Create Kubernetes Service (DNS required)
- name: Create a Service for Hello Go.
k8s:
state: present
definition:
apiVersion: v1
kind: Service
metadata:
name: hello-go
namespace: default
spec:
type: LoadBalancer
ports:
- port: 8180
targetPort: 8180
selector:
app: hello-go
post_tasks:
#Example export using minikube services
- name: Expose Hello Go on the host via Minikube.
command: minikube service hello-go --url=true
changed_when: false
register: minikube_service
- debug:
msg: "Hello Go URL: {{ minikube_service['stdout_lines'][0] }}"
- name: Verify Hello Go is responding.
uri:
url: "{{ minikube_service['stdout_lines'][0] }}/test"
return_content: true
register: hello_go_response
failed_when: "'/test' not in hello_go_response.content"
- debug:
msg: "Testing URL Hello GO Requested: {{hello_go_response.content}} Status: {{hello_go_response.status}}"
EOF

Go ahead and run this playbook using the ansible-playbook command.

# ansible-playbook main.yml
Ansible Playbook command output.

Here we go! As magic, we have implemented your go application one simple command.

# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/hello-go-866d446899-7qv7z 1/1 Running 0 54m
pod/hello-go-866d446899-fbrkj 1/1 Running 0 54m
pod/hello-go-866d446899-shr9r 1/1 Running 0 56m
pod/hello-go-866d446899-wtnwf 1/1 Running 0 56m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello-go LoadBalancer 10.101.147.110 <pending>8180:31690/TCP 56m
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 59m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/hello-go 4/4 4 4 56m
NAME DESIRED CURRENT READY AGE
replicaset.apps/hello-go-866d446899 4 4 4 56m

BTW, Check your dashboard feature provided by minikube.

# minikube dashboard
🔌 Enabling dashboard ...
🤔 Verifying dashboard health ...
🚀 Launching proxy ...
🤔 Verifying proxy health ...
🎉 Opening http://127.0.0.1:58829/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...

--

--

Adilson Cesar

I design, implement and support Linux Data Centers for telecommunications and finance companies.