> For the complete documentation index, see [llms.txt](https://learn.farizizwan.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.farizizwan.com/devops-and-devsecops/kubernetes/poc-lab/high-availability-portable-mini-kubernetes-data-center-with-k3s/lab-2-automate-the-k3s-clusters-deployment-with-ansible.md).

# Lab 2: Automate the K3s clusters deployment with Ansible

Starting a local Kubernetes cluster in our home lab environment may become complicated if we use kubeadm or any other distributions.

I seek to establish a Kubernetes cluster that operates seamlessly within an Ansible-driven ecosystem, allowing for scripted control over the cluster's nodes. This setup enables the automation of both the construction and decommissioning of nodes within the cluster, providing the flexibility to manage its lifecycle as required.

Therefore, I chose `k3s-ansible` to build my on-premise Kubernetes cluster.

Continue later...

## Post-Deployment

After the deployment, you may need to get a copy of kube config file from each of the master nodes.

Run the following commands

```
$ scp k3s-master-node01:~/.kube/config ~/.kube/k3s-master-node01
```

Please replace k3s-master-node01 with the hostname or IP addresses of your master node. You may need to repeat the command if you have more than one master node.

Please also replace the following parameters in each of the kube config file, so that when you run `$ kubectl config get-context` you will see the name of the nodes correctly.

Example config file:

```
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJkakNDQVIyZ0F3SUJBZ0lCQURBS0JnZ3Foa2pP
    server: https://192.168.2.1:6443
  name: k3s-master-node01 //change here
contexts:
- context:
    cluster: k3s-master-node01 //change here
    user: default //remain here. we are gonna use default user
  name: k3s-master-node01 //change here
current-context: k3s-master-node01 //change here
kind: Config
preferences: {}
users:
- name: default //remain here
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJrakNDQVRlZ0F3SUJBZ0lJTFBqR0ZBcndTUlF3
```

Then, test the connectivity with the following command

```
$ kubectl get nodes
```

My results:&#x20;

<figure><img src="/files/dQpi0Vb51UzPVPD83P67" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://learn.farizizwan.com/devops-and-devsecops/kubernetes/poc-lab/high-availability-portable-mini-kubernetes-data-center-with-k3s/lab-2-automate-the-k3s-clusters-deployment-with-ansible.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
