K8s environment setup with Ansible
Last time we took a try on ansible with the three virtual machines we created with pve. And this time we are going to use ansible to set up a k8s environment with these three virtual machines. I’m going to edit the playbook step by step, following the official installation guide of k8s and docker. All my nodes are running on Ubuntu 20.04.
Starting of the playbook
- name: Kubernetes
hosts: homelab_vms
vars_files:
- homelab.yaml
become: yes
tasks:
- name: Ping my hosts
ansible.builtin.ping:
This is the starting of the playbook. We are going to use the hosts defined in another yaml file with the password variables storing in the vault. And we are going to use the root account to do the following tasks (defined in become: yes
). And first of all we are going to ping the hosts to make sure they are reachable.
Install software require
- name: apt update
ansible.builtin.apt:
update_cache: yes
- name: Prepare software
ansible.builtin.apt:
name: ca-certificates, curl
state: present
These are defined as two tasks. The first one is to update the apt cache, and the second one is to install the required software.
Install docker
Add docker apt repository
Before we install docker, we need to set up the Docker repository. Afterward, we can install and update Docker from the repository.
To set up the repository, we need to firstly add Docker’s official GPG key, and then add the repository to apt sources.
We first need to create a directory for the keyring, and then download the key to the directory. We followed the documents and example for ansible.builtin.file and ansible.builtin.get_url.
There are lots of guide using the apt_key
module to fetch the gpg key and use the key to add the apt repo, but it is deprecated and we should use the ansible.builtin.apt_key
module instead.
After we add the repository. We need update the apt cache again to make sure the new repository is available.
- name: Add Docker apt repository
block:
- name: Create keyring directory if not exist
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
- name: Get Docker gpg key
ansible.builtin.get_url:
url: https://download.docker.com/linux/ubuntu/gpg
dest: /etc/apt/keyrings/docker.asc
- name: Add docker apt repository
ansible.builtin.apt_repository:
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu focal stable"
state: present
- name: apt update
ansible.builtin.apt:
update_cache: yes
Install docker
After we add the repository, we can install docker with the ansible.builtin.apt
module.
- name: Install docker
ansible.builtin.apt:
name: docker-ce docker-ce-cli containerd.io docker-compose-plugin
state: present
Install k8s
Installing k8s is similar to installing docker. We need to add the apt repository first, and then install the k8s packages. But, before we add the repository, we need to disable swap.
Disable swap
We can do this with the ansible.builtin.command
or the ansible.builtin.shell
module. They works almost the same but the ansible.builtin.shell
module runs the command through a shell (/bin/sh).
We also need to disable swap in the fstab file. We can use the ansible.builtin.replace
module to do this. This module “Replace all instances of a particular string in a file using a back-referenced regular expression”.
- name: Disable swap
block:
- name: Disable SWAP
# Use shell to perform actions inline
shell: |
swapoff -a
- name: Disable SWAP in fstab
replace:
path: /etc/fstab
regexp: '^([^#].*?\sswap\s+sw\s+.*)$'
replace: '# \1'
Add k8s apt repository
Similar to docker, almost the same. And please note that The legacy Linux package repositories apt.kubernetes.io and yum.kubernetes.io have been deprecated and will be frozen starting from September 13, 2023.. Here we are using the new repository.
- name: Add K8s apt repository
block:
- name: get gpg key
ansible.builtin.get_url:
url: https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key
dest: /etc/apt/keyrings/kubernetes.asc
- name: add k8s apt repository
ansible.builtin.apt_repository:
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/kubernetes.asc] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /"
state: present
- name: apt update
ansible.builtin.apt:
update_cache: yes
Install k8s packages
Same as above.
- name: Install Kubernetes
apt:
name: kubectl, kubelet, kubeadm
state: present
Conclusion
We are done so far. Ansible is really a powerful tool to manage the configuration of multiple machines. Playbook can be clone and run on any machine, and it will automatically set up the environment. It is really convenient and easy to use.