create a folder keys in the same level as ansible folder. If you have already created VMs, copy the private keys into the keys folder and replace the ansible_ssh_private_key_file inside hosts.yaml with each VM private key.
ok: [master-node-1] => {
"msg": [
"Nodes to remove: '[]'.",
"Nodes to add '['worker-node-3']'."
]
}
TASK [master : pause] ****************************************************************************************************************************************************************************************************************************************
[master : pause]
Do you want to apply the above changes? (Y/n):
after you accept changes the kubernetes cluster will scale up/down depends on your desired state, also it will update the loadbalancer haproxyconfig file to the desired workloads
to verify, run :
export KUBECONFIG=$PWD/playbooks/.kube/config
kubectl get nodes
Prerequisite
this arhcitecture need at least:
for system level, you need to do:
ansible.netcommon 7.1.0 ansible.posix 2.0.0 ansible.utils 5.1.2 community.crypto 2.24.0 community.docker 4.3.1 community.general 10.3.0 hetzner.hcloud 4.2.2 kubernetes.core 5.1.0
Scenario # 1 (You have VMs)
create a folder
keysin the same level as ansible folder. If you have already created VMs, copy the private keys into thekeysfolder and replace theansible_ssh_private_key_fileinsidehosts.yamlwith each VM private key.here is an example of the
hosts.yamlformat:then test if ansible can ssh into all the hosts using:
ansible all -i inventory/hosts.yaml -m pingScenario # 2 (else)
If you didn’t create VMs, you can run the terraform file
main.tfto create ones.first, you need to create ssh-keys, you either create an ssh-key for each (loadbalancer,master,worker) Vms or single ssh-key for all Vms
to create an ssh-key run the following:
ssh-keygen -o -a 100 -t ed25519 -f vm_nodethen move into terraform folder and because we’re using hetzner to create Vms, create a file .tfvars and add the following:
finally run
If terraform finished succesfully a
hosts.yamlfile will be created underansible/inventoryInstall Kubernetes
Now after you got
hosts.yamlmove into the
ansiblefolder and run the following commandsexport ANSIBLE_ROLES_PATH=$PWD/rolesexport ANSIBLE_HOST_KEY_CHECKING=Falsecreate a file
.envand add the following secrets:RANDOM_TOKEN: the format of token must be like the following: abcdef.abcdef0123456789CERTIFICATE_KEY: The certificate key is a hex encoded string that is an AES key of size 32 bytes. you can use [https://www.electricneutron.com/encryption-key-generator/] and choose AES 256 bit(HEX).then to allow ansible noticing the .env file, we need to export it like the following:
export $(grep -v '^#' .env | xargs)then test if ansible can ssh into all the hosts using:
ansible all -i inventory/hosts.yaml -m pingif all the hosts pinged just fine, start creating the cluster by running:
ansible-playbook -i inventory/hosts.yaml playbooks/setup-cluster.yamlafter the playbook run successfully, you will see a kubeconfig file in
ansible/playbooks/.kube/configScale Nodes
we treat the
hosts.yamlas the source of truth to our resources, so to scale up or down the nodes, it will be enough to modify the hosts.yaml fileexample, the current
hosts.yamlis:If we need to scale it up, we can just add another worker node
or to scale down we remove the desired worker node
Note: you can both scale up and down at the same time, but if you do it, we will run the scale up first then scale down
Note: the number of control-plane nodes need to be odd number
Now to run the syncing process, use the following command:
ansible-playbook -i inventory/hosts.yaml playbooks/sync-resources.yamlyou will prompted with following message:
after you accept changes the kubernetes cluster will scale up/down depends on your desired state, also it will update the loadbalancer haproxyconfig file to the desired workloads
to verify, run :
Upgrade Cluster
Rotate Certs
ansible-playbook -i inventory/hosts.yaml playbooks/rotate-certs.yamlUpgrade Version
this will update the whole cluster versions
first you need to change the
kubernetes_versionandkubernetes_package_versionto the desired version, then apply:ansible-playbook -i inventory/hosts.yaml playbooks/upgrade-cluster.yamlOS Security Patch
to patch os with the security patches run :
ansible-playbook -i inventory/hosts.yaml playbooks/os-patch.yaml