Ansible playbook for Kubernetes cluster installation on Linux
Dear Techies,
Kubernetes has become the go-to solution for orchestrating containerized applications, making it easier to manage, scale, and deploy services. However, setting up a Kubernetes cluster manually can be a daunting task. Fortunately, Ansible can simplify this process significantly. In this blog, I’ll guide you through an Ansible playbook that automates the installation and configuration of a Kubernetes cluster on Linux, featuring a single control plane node and multiple worker nodes for development and testing environments.
I understand there is kubespray which is more powerful and we could do lots of customizations. But, this playbook is lightweight and simple, might be a good option to look into for a testing environment.
GitHub Public Repository: install-k8s-on-linux
You can find the complete step-by-step guide in the README section of the repository itself, and I will also be providing the same in this blog.
This Ansible playbook automates the installation and configuration of a Kubernetes cluster on Linux, with a single control plane node and multiple worker nodes, using the most recent stable Kubernetes release.
- RedHat-based ( Fedora, RHEL, Rocky Linux, Almalinux, Oracle Linux )
- Debian-based ( Debian, Ubuntu )
- SUSE-based ( OpenSUSE, SLES )
Prerequisites:
- Please install Ansible on the machine where you plan to run the playbook if you haven’t done so already.
- Prepare the cluster nodes by installing even a minimal package set of any of the above Linux distributions.
- Please ensure that you have DNS set up that resolves all the involved hosts, or update the host files on all hosts with the necessary entries for each involved host.
- Create a common Linux user on all cluster nodes to be used for the cluster.
- Enable passwordless SSH authentication from the Ansible host to all cluster nodes.
- Ensure that the common user has sudo privileges without a password on all cluster nodes.
The main playbook installs and configures the latest stable versions of the following required components.
- Container orchestrator : kubernetes
- Container runtime : containerd
- Low-level container runtime : runc ( dependency for containerd )
- CNI plugin : calico
Optional playbooks are available to install the following components once the cluster is ready.
- csi-driver-nfs
- csi-driver-smb
- MetalLB LoadBalancer.
Step-by-step guide to download, set up, and run the playbook.
Step 1: Copy and execute the below command snippet to extract the tarball for the most recent stable release of this Ansible project from GitHub.
curl -sSL https://github.com/Muthukumar-Subramaniam/install-k8s-on-linux/releases/latest/download/inst-k8s-ansible.tar.gz | tar -xzvf - && cd inst-k8s-ansible
Step 2: Update the host-control-plane
file with the necessary hostname.
Example:
Step 3: Update the host-workers
file with the necessary hostnames.
Example:
Step 4: Update the pod-network-cidr
file with the desired pod network CIDR.
Example:
Importent notes on the pod network CIDR:
- Only private IP addresses, as defined in RFC 1918 are allowed.
- The deployment is configured to accept CIDR prefixes exclusively within the /16 to /28 range.
- Ensure that the selected CIDR prefix does not conflict with any existing networks in your infrastructure.
- Choose a CIDR prefix that provides sufficient address space for your cluster.
Step 5: Run the setup.py
script to prepare the environment for the Ansible playbook.
./setup.py
Sample Run:
Step 6: Run the inst-k8s-ansible.yaml
playbook if the setup.py
script completes successfully."
./inst-k8s-ansible.yaml
Sample Outcome:
Great work! Your cluster is now ready to use.
Optional Steps:
To install CSI NFS Driver for the kubernetes cluster if required.
./optional-k8s-csi-nfs-driver.yaml
Sample Outcome:
To install CSI SMB Driver for the kubernetes cluster if required.
./optional-k8s-csi-smb-driver.yaml
Sample Outcome:
To install MetalLB loadbalancer if required.
Please change the address pool range in the playbook as per your environment and requirement.
Example:
./optional-install-metallb.yaml
Sample Outcome:
That’s all for now. Thank you for your kind attention!
Kindly note:
- This playbook is a useful resource for experimenting with Kubernetes and can be customized to meet your specific requirements.
- The playbook utilizes the GitHub API to fetch the current stable versions of all required software components.
- Compatible with a wide range of Linux distributions.
- Your feedback and contributions are invaluable to the success of this project.
- Please report any bugs, suggest new features, or contribute directly to the codebase.
If you find this GitHub project install-k8s-on-linux useful, kindly leave a star.
If you find this post useful, please follow me on GitHub at My GitHub Profile.
Have lots of fun!
Comments
Post a Comment