Deploy Service to K3s with Helm
Tip
We recommend to use the Sandbox to play around with this example.
Tip
All the files from this section are in k3s-and-helm.zip.
In Deploy Service to K3s, you learn how to manipulate Kubernetes objects using Ansible’s wrap around kubectl. Another option to manipulate Kubernetes objects is using Ansible’s wrap around Helm. Helm is a popular package manager for Kubernetes. The advantages of using Helm are:
many softwares have already being package.
is possible to customise the packages to be installed.
remove packages is a single operation.
Preparation
Access to a Kubernetes cluster is done using kubectl instead of SSH. For authentication, a kubeconfig file is used. The kubeconfig file is created when installing the Kubernetes cluster and you will have to copied it to the proxy (where kubectl will run) used by Ansible. You can use as proxy:
the Ansible control node.
another machine with kubectl and kubeconfig.
For this section, you will use Ansible control node and you will have to copy the kubeconfig.
docker compose cp k3s:/etc/rancher/k3s/k3s.yaml k3s-config
sed -i 's/127.0.0.1/kubernetes/g' k3s-config
docker compose cp k3s-config control-node:/home/ansible/.kube/config
Inventory
k3s:
hosts:
localhost:
ansible_connection: local
The above inventory only defines that is the same as the Ansible control node.
Playbook
- name: Deploy Kubernetes Dashboard
# https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/
hosts:
- k3s
tasks:
- name: Add a repository
kubernetes.core.helm_repository:
name: kubernetes-dashboard
repo_url: https://kubernetes.github.io/dashboard/
- name: Deploy Kubernetes Dashboard
kubernetes.core.helm:
name: kubernetes-dashboard
chart_ref: kubernetes-dashboard/kubernetes-dashboard
namespace: kubernetes-dashboard
create_namespace: true
The above playbook has two tasks:
add a Helm repository.
install a Helm chart.
Running
cd k3s-and-helm
ansible-playbook \
-i inventories/production.yml \
playbook.yaml
returns
PLAY [Deploy Kubernetes Dashboard] *********************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Add a repository] ********************************************************
changed: [localhost]
TASK [Deploy Kubernetes Dashboard] *********************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0