Provisioning Kubernetes on top of AWS with the help of Ansible.

Vibhanshusharma
4 min readApr 7, 2021
Provisioning K8S.

To do this task, we need to launch 3 instances in the AWS (2 as slaves and 1 as master) and then we will configure Kubernetes in them. There are a lot of steps so I will create separate Ansible-Roles for configuring Kubernetes and launching instances.

Roles required for this provisioning.

These are the roles created for launching instances in the AWS. It will launch one Master node and two Slave nodes on AWS.

In main.yml file under tasks we will write a code

— -
# tasks file for instance_provisioning
- name: Create security group
ec2_group:
name: “multi-node-cluster k8s”
description: “Security group for k8s multinode cluster”
region: ap-south-1
vpc_id: vpc-<value>
aws_access_key: “{{ access_key }}”
aws_secret_key: “{{ secret_key }}”
rules:
— proto: all
cidr_ip: 0.0.0.0/0
register: result_sec_group

- name: Launching 1st slave
ec2_instance:
aws_access_key: “{{ access_key }}”
aws_secret_key: “{{ secret_key }}”
region: “ap-south-1”
name: “slave1”
tags:
group: slave1
instance_type: “t2.micro”
wait: yes
wait_timeout: 30
key_name: hadoop-master
image_id: ami-<value>
security_group: “{{ result_sec_group.group_id }}”
state: present
vpc_subnet_id: subnet-<value>
register: slave1

- name: Launching 2nd slave
ec2_instance:
aws_access_key: “{{ access_key }}”
aws_secret_key: “{{ secret_key }}”
region: “ap-south-1”
name: “slave2”
tags:
group: slave2
instance_type: “t2.micro”
wait: yes
wait_timeout: 30
key_name: hadoop-master
image_id: ami-<value>
security_group: “{{ result_sec_group.group_id }}”
state: present
vpc_subnet_id: subnet-<value>
register: slave2

- name: Launching Master
ec2_instance:
aws_access_key: “{{ access_key }}”
aws_secret_key: “{{ secret_key }}”
region: “ap-south-1”
name: “master”
tags:
group: master
instance_type: “t2.micro”
wait: yes
wait_timeout: 30
key_name: hadoop-master
image_id: ami-<value>
security_group: “{{ result_sec_group.group_id }}”
state: present
vpc_subnet_id: subnet-<value>
register: master

- name: Adding hosts to inventory using pem path
blockinfile:
dest: “{{ inv_path }}”
marker: “”
create: yes
block: |
[master]
{{ master.instances[0].public_ip_address }} ansible_user=ec2-user ansible_ssh_private_key_file=”{{ key_path }}” ansible_connection=ssh
[slaves]
{{ slave1.instances[0].public_ip_address }} ansible_user=ec2-user ansible_ssh_private_key_file=”{{ key_path }}” ansible_connection=ssh
{{ slave2.instances[0].public_ip_address }} ansible_user=ec2-user ansible_ssh_private_key_file=”{{ key_path }}” ansible_connection=ssh

- name: Refreshing of Inventory
meta: refresh_inventory

  • name: Please wait for the instances
    pause:
    minutes: 1

Roles for Configuring master node

In main.yml file under task the following code will take place

— -
# tasks file for master_config
- name: “Creating repo of Kubernetes ”
yum_repository:
name: “kube”
description: “kubernetes repository”
baseurl: “https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64"
enabled: 1
gpgcheck: 1
repo_gpgcheck: 1
gpgkey: “https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg"

- name: “Downloading of Kubeadm”
package:
name: “kubeadm”
state: present

- name: “Starting of Kubelet”
service:
name: “kubelet”
state: started

- name: “Downloading Docker”
package:
name: “docker”
state: present

- name: “Starting Docker”
service:
name: “docker”
state: started

- name: “Copy drivers file”
copy:
src: daemon.json
dest: /etc/docker/daemon.json

- name: “Restarting Docker”
service:
name: “docker”
state: restarted

- name: “Download IProute-tc”
package:
name: “iproute-tc”
state: present

- name: “Uploading IProute file”
copy:
src: k8s.conf
dest: /etc/sysctl.d/k8s.conf

- name: “Starting service”
shell: “sysctl — system”

- name: “Initializing Kubeadm”
shell: “kubeadm init — pod-network-cidr={{ ip_range }} — ignore-preflight-errors=NumCPU — ignore-preflight-errors=Mem”

- name: “Setting it as master node-1”
file:
path: “/root/.kube”
state: directory

- name: “Setting it as master node-2”
copy:
src: “/etc/kubernetes/admin.conf”
dest: “/root/.kube/config”
remote_src: yes
owner: root

- name: “Configuring kube-flannel”
template:
src: kube-flannel.yml
dest: /root/

  • name: “Running kube-flannel”
    shell: “kubectl apply -f /root/kube-flannel.yml”

This will first setup the Kubernetes repository and then download and configure kubeadm.

Role for configuring Slave node:-

In main.yml file under tasks this code will be written.

# tasks file for slave_config

-- name: “Creating repo of Kubernetes ”
yum_repository:
name: “kube”
description: “kubernetes repository”
baseurl: “https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64"
enabled: 1
gpgcheck: 1
repo_gpgcheck: 1
gpgkey: “https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg"

- name: “Download Kubeadm”
package:
name: “kubeadm”
state: present

- name: “Start Kubelet”
service:
name: “kubelet”
state: started

- name: “Download Docker”
package:
name: “docker”
state: present

- name: “Start Docker”
service:
name: “docker”
state: started

- name: “Copy drivers file”
copy:
src: daemon.json
dest: /etc/docker/daemon.json

- name: “Restart Docker”
service:
name: “docker”
state: restarted

- name: “Download IProute-tc”
package:
name: “iproute-tc”
state: present

- name: “Upload IProute file”
copy:
src: k8s.conf
dest: /etc/sysctl.d/k8s.conf

  • name: “Start service”
    shell: “sysctl — system”

This play will configure the two slave nodes. The steps are almost the same as the master node.

Now collect all the files in a workspace

In that workspace create a yml file, which I named as multi_node_cluster.yml

and the code inside is

- hosts: localhost
vars_files:
— pass.yml
roles:
— role: instance_provisioning

- hosts: master
roles:
— role: master_config

- hosts: slaves
roles:
— role:slave_config

- hosts: master
tasks:
— name: Generate Token
shell: “kubeadm token create — print-join-command”
register: x

  • name: Saving generated token
    add_host:
    name: “token”

Now in the end run the above playbook using command

ansible-playbook <yml file_name.yml>

Thank you!

--

--