Powerful IT automation engines such as Ansible can be used to automate pretty much any day-to-day IT task, including the deployment of Kubernetes clusters on bare-metal clusters. In this section, we will learn how to deploy a simple Kubernetes cluster using Ansible playbooks.
Getting ready
In this recipe, we will use an Ansible playbook. The examples that will be used in these recipes are accessible through the k8sdevopscookbook GitHub repository.
Before you start executing the commands in this section's recipes, clone the Ansible playbook examples using the following command:
$ git clone https://github.com/k8sdevopscookbook/src.git
You will find the examples stored under the k8sdevopscookbook/src directory.
How to do it…
This section will take you through how to configure a Kubernetes cluster using Ansible. To that end, this section is further divided into the following subsections to make this process easier:
- Installing Ansible
- Provisioning a Kubernetes cluster using an Ansible playbook
- Connecting to the Kubernetes cluster
Installing Ansible
In order to provision a Kubernetes cluster using an Ansible playbook, follow these steps:
- To install Ansible on your Linux workstation, first, we need to add the necessary repositories:
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
- Install Ansible using the following command:
$ sudo apt-get update && sudo apt-get install ansible -y
- Verify its version and make sure Ansible is installed:
$ ansible --version
At the time this recipe was written, the latest Ansible version was 2.9.4.
Provisioning a Kubernetes cluster using an Ansible playbook
In order to provision a Kubernetes cluster using an Ansible playbook, follow these steps:
- Edit the hosts.ini file and replace the master and node IP addresses with your node IPs where you want Kubernetes to be configured:
$ cd src/chapter1/ansible/ && vim hosts.ini
- The hosts.ini file should look as follows:
[master]
192.168.1.10
[node]
192.168.1.[11:13]
[kube-cluster:children]
master
node
- Edit the groups_vars/all.yml file to customize your configuration. The following is an example of how to do this:
kube_version: v1.14.0
token: b0f7b8.8d1767876297d85c
init_opts: ""
kubeadm_opts: ""
service_cidr: "10.96.0.0/12"
pod_network_cidr: "10.244.0.0/16"
calico_etcd_service: "10.96.232.136"
network: calico
network_interface: ""
enable_dashboard: yes
insecure_registries: []
systemd_dir: /lib/systemd/system
system_env_dir: /etc/sysconfig
network_dir: /etc/kubernetes/network
kubeadmin_config: /etc/kubernetes/admin.conf
kube_addon_dir: /etc/kubernetes/addon
- Run the site.yaml playbook to create your cluster:
$ ansible-playbook site.yaml
Your cluster will be deployed based on your configuration.
Connecting to the Kubernetes cluster
To get access to your Kubernetes cluster, you need to follow these steps:
- Copy the configuration file from the master1 node:
$ scp root@master:/etc/kubernetes/admin.conf ~/.kube/config
- Now, use kubectl to manage your cluster.
See also
- The Ansible module for working with Kubernetes: https://docs.ansible.com/ansible/latest/modules/k8s_module.html
- Kubernetes Operators examples using Ansible and the Operator SDK: https://github.com/operator-framework/operator-sdk/blob/master/doc/ansible/user-guide.md