Ansible Role to Configure K8S Multi- Node Cluster over AWS Cloud

Kubernetes Cluster

TASK :

https://github.com/ansible/ansible/tree/stable-2.9/contrib/inventory
export AWS_ACCESS_KEY_ID='RB123'
export AWS_SECRET_ACCESS_KEY='asd123'
aws_access_key_id = AXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXXXX

Ansible role to launch AWS EC2 instances

ansible-galaxy role init kubernetes.instance
# AWS Region Name# Amazon AMI ID # Instance type for instance # Subnet ID # Security group to attach with instance # key-pair for ssh into instance # Access & secret key for IAM user
aws_region: ap-south-1
aws_ami: ami-08f63db601b82ff5f
aws_instance_type: t2.micro
subnet_id: subnet-00566450d0ea4c288
security_group_id: sg-7a400518
key: ansible-key
access_key: AXXXXXXXXXXXXXX
secret_key: XXXXXXXXXXXXXXXXXXX

# tasks file for kubernetes.instance
- name: Task to launch EC2 Instance
ec2_instance:
region: "{{ aws_region }}"
image_id: "{{ aws_ami }}"
instance_type: "{{ aws_instance_type }}"
vpc_subnet_id: "{{ subnet_id }}"
security_group: "{{ security_group_id }}"
key_name: "{{ key }}"
name: "{{ item }}"
state: present
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
loop: "{{ name }}"

Ansible role to configure Kubernetes Master node

ansible-galaxy role init kubernetes.master
# Packages to be installed for Kubernetes
pkgs:
- kubelet
- kubectl
- kubeadm
- docker
- iproute-tc
# Services to be started
services:
- kubelet
- docker
# Path for docker configuration directory
docker_daemon: "/etc/docker/daemon.json"
# Path for sysctl.d configuration directory
kubernetes_config: "/etc/sysctl.d/k8s.conf"
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
# tasks file for kubernetes.master- name: Configure yum repo for Kubernetes
yum_repository:
name: kubernetes
baseurl: https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled: yes
gpgcheck: yes
repo_gpgcheck: yes
gpgkey:
- https://packages.cloud.google.com/yum/doc/yum-key.gpg
- https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
description: "Yum repository for Kubernetes"
- name: Install required packages
yum:
name: "{{ item }}"
state: present
loop: "{{ pkgs }}"
- name: Start and enable kubelet
service:
name: "{{ item }}"
state: started
enabled: yes
loop: "{{ services }}"
register: svc_start
- name: Docker config image
shell:
cmd: "kubeadm config images pull"
when: svc_start.changed
- name: Change docker cgroup
copy:
dest: "{{ docker_daemon }}"
src: daemon.json
notify: restart docker
- name: Force handler to execute
meta: flush_handlers
- name: Copy k8s.conf file to master
copy:
src: k8s.conf
dest: "{{ kubernetes_config }}"
register: result
- name: Load settings from all system configuration files
shell:
cmd: sysctl --system
when: result.changed
- name: Start the kubernetes master
shell:
cmd: "kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=NumCPU --ignore-preflight-errors=Mem"
when: result.changed
- name: Create folder for kube
file:
path: $HOME/.kube
state: directory
register: kube_dir
- name: Copy admin.conf file to kube
copy:
src: "/etc/kubernetes/admin.conf"
dest: "$HOME/.kube/config"
owner: "{{ kube_dir.owner }}"
group: "{{ kube_dir.group }}"
remote_src: yes
when: kube_dir.changed
- name: Get join command
shell:
cmd: "kubeadm token create --print-join-command"
register: output
- name: "Dummy host to register join command"
add_host:
name: Dummy_Host
join_cmd: "{{ output.stdout }}"
when: output.changed- name: Setup flannel network
command: "kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
when: result.changed
# handlers file for kubernetes.master
- name: restart docker
service:
name: "{{ services[1] }}"
state: restarted

Ansible role to configure Kubernetes Worker node

ansible-galaxy role init kubernetes.worker
# Packages to be installed for Kubernetes
pkgs:
- kubelet
- kubectl
- kubeadm
- docker
- iproute-tc
# Services to be started
services:
- kubelet
- docker
# Path for docker configuration directory
docker_daemon: "/etc/docker/daemon.json"
# Path for sysctl.d configuration directory
kubernetes_config: "/etc/sysctl.d/k8s.conf"
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
# tasks file for kubernetes.worker- name: Configure yum repo for Kubernetes
yum_repository:
name: kubernetes
baseurl: https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled: yes
gpgcheck: yes
repo_gpgcheck: yes
gpgkey:
- https://packages.cloud.google.com/yum/doc/yum-key.gpg
- https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
description: "Yum repository for Kubernetes"
- name: Install required packages
yum:
name: "{{ item }}"
state: present
loop: "{{ pkgs }}"
- name: Start and enable kubelet
service:
name: "{{ item }}"
state: started
enabled: yes
loop: "{{ services }}"
register: svc_start
- name: Change docker cgroup
copy:
dest: "{{ docker_daemon }}"
src: daemon.json
when: svc_start.changed
notify: restart docker- name: Force handler to execute
meta: flush_handlers
- name: Copy k8s.conf file to worker
copy:
src: k8s.conf
dest: "{{ kubernetes_config }}"
register: result
- name: Load settings from all system configuration files
shell: sysctl --system
when: result.changed
- name: "Join the worker to master"
command: "{{ hostvars.Dummy_Host.join_cmd }}"
when: result.changed
# handlers file for kubernetes.master
- name: restart docker
service:
name: "{{ services[1] }}"
state: restarted

Create Playbook to use the role

- hosts: localhost
roles:
- kubernetes.instance
tasks:
- meta: refresh_inventory
- hosts: tag_Name_K8S_Master
roles:
- kubernetes.master
- hosts: tag_Name_K8S_Worker1
roles:
- kubernetes.worker
- hosts: tag_Name_K8S_Worker2
roles:
- kubernetes.worker
ansible-playbook setup.yml

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store