☸️Configuring K8S Multi-Node Cluster over AWS ☁️ ️using Ansible

  1. We will launch three ec2-instances over AWS cloud one as Master Node and two are Slave/Worker Nodes.

> Creating role to create
ec2-instance.
ansible galaxy init ec2-launch
---
# tasks file for ec2-launch
- name: "Provisioning ec2 instances over AWS Cloud"
ec2:
image: "{{ image_id }}"
instance_type: "{{ instance_type }}"
region: "{{ region }}"
key_name: "{{ key }}"
wait: yes
count: 1
state: present
vpc_subnet_id: "{{ vpc_subnet_id }}"
group_id: "{{ security_group_id }}"
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
instance_tags:
Name: "{{ item }}"
loop: "{{ OS_Names }}"
---
# vars file for ec2-launch
image_id: "ami-0eeb03e72075b9bcc"
instance_type: "t2.micro"
region: "ap-south-1"
key: "arth"
vpc_subnet_id: "subnet-f7d4c19f"
security_group_id: "sg-2904234e"
aws_access_key: "enter access key"
aws_secret_key: "enter secret key"
OS_Names:
- "K8S_Master"
- "K8S_Slave1"
- "K8S_Slave2"
- hosts: localhost
roles:
- role: "/k8s-multi-node-cluster/ec2-launch"
ansible-playbook setup.yml

Kubernetes Multi-Node Cluster

To setup k8s multi node cluster manually visit https://github.com/hrishabhsharma/Kubernetes-Multi-Node-Cluster-Over-AWS-Cloud

> Creating role to configure k8s-master.
ansible galaxy init k8s-master
> Creating role to configure k8s-slaves.
ansible galaxy init k8s-slaves
  1. “Install docker (As we are using Amazon Linux 2 image so we don’t need to configure repo for docker)”
---
# tasks file for k8s-master
- name: "Installing docker"
package:
name: docker
state: present
- name: "Starting and Enabling docker service"
service:
name: docker
state: started
enabled: yes
- name: "Configuring yum repo for kubernetes"
copy:
src: "/k8s-multi-node-cluster/k8s-master/files/kubernetes.repo"
dest: "/etc/yum.repos.d/kubernetes.repo"
- name: "Installing kubeadm, kubectl, kubelet"
yum:
name: "{{ item }}"
state: present
disable_excludes: kubernetes
loop: "{{ packages }}"
- name: "Starting and Enabling kubelet"
service:
name: kubelet
state: started
enabled: yes
- name: "Pulling Images using kubeadm"
shell: "kubeadm config images pull"
changed_when: false
- name: "Changing the driver in the docker"
copy:
content: |
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
dest: /etc/docker/daemon.json
register: output
- name: "Restarting Docker"
service:
name: docker
state: restarted
when: output.changed == true
- name: "Installing iproute-tc"
package:
name: iproute-tc
state: present
- name: "Setting bridge-nf-call-iptables to 1"
shell: |
echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables
changed_when: false
- name: "Initializing Master"
shell: "kubeadm init --pod-network-cidr={{ cidr }} --ignore-preflight-errors=NumCPU --ignore-preflight-errors=Mem --node-name=master"
ignore_errors: yes
- name: "Creating .kube directory"
shell: "mkdir -p $HOME/.kube"
- name: "Copying /etc/kubernetes/admin.conf $HOME/.kube/config"
shell: "sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config"
- name: "changing owner permission"
shell: "sudo chown $(id -u):$(id -g) $HOME/.kube/config"
- name: "Configuring with flannel plugin"
shell: "kubectl apply -f
https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
- name: "Generating Token"
shell: "kubeadm token create --print-join-command"
register: token
ignore_errors: yes
- debug:
var: token.stdout_lines
register: token
---
# vars file for k8s-master
cidr: "10.244.0.0/16"
packages:
- kubeadm
- kubelet
- kubectl
  1. “Install docker (As we are using Amazon Linux 2 image so we don’t need to configure repo for docker)”
---
# tasks file for k8s-slaves
- name: "Installing docker"
package:
name: docker
state: present
- name: "Starting and Enabling docker service"
service:
name: docker
state: started
enabled: yes
- name: "Configuring yum repo for kubernetes"
copy:
src: "/k8s-multi-node-cluster/k8s-master/files/kubernetes.repo"
dest: "/etc/yum.repos.d/kubernetes.repo"
- name: "Installing kubeadm, kubectl, kubelet"
yum:
name: "{{ item }}"
state: present
disable_excludes: kubernetes
loop: "{{ packages }}"
- name: "Starting and Enabling kubelet"
service:
name: kubelet
state: started
enabled: yes
- name: "Changing the driver in the docker"
copy:
content: |
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
dest: /etc/docker/daemon.json
register: output
- name: "Restarting Docker"
service:
name: docker
state: restarted
when: output.changed == true
- name: "Installing iproute-tc"
package:
name: iproute-tc
state: present
- name: "Setting bridge-nf-call-iptables to 1"
shell: |
echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables
echo "1" > /proc/sys/net/bridge/bridge-nf-call-ip6tables
changed_when: false
- name: "Joining Slave to Master"
shell: "{{ master_token }}"
ignore_errors: yes
register: joined
- debug:
var: joined.stdout_lines
---
# vars file for k8s-slaves
packages:
- kubeadm
- kubelet
- kubectl
- hosts: ["tag_Name_K8S_Master"]
roles:
- name: "Configuring Master Node"
role: "/k8s-multi-node-cluster/k8s-master"
- hosts: ["tag_Name_K8S_Slave1", "tag_Name_K8S_Slave2"]
vars_prompt:
- name: "master_token"
prompt: "Enter Token to Join Slaves to Master: "
private: no
roles:
- name: "Configuring Slave Nodes"
role: "/k8s-multi-node-cluster/k8s-slaves"
ansible-playbook k8s-playbook.yml

Thanks for reading !!!😊✨ keep Learning!

Miles to go…

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

Recommended from Medium

Weather Data Analysis and Visualization with Pandas

Keycloak: Event Listener SPI & Publish to Kafka

Creating a society without profanity & hateful speeches (The Idealistic world) using NLP and…

How to modify the website language using selenium python bindings.

Operators in C++

Hello,Iam Rajat Raut.i

On Building Abstractions Too Soon

Laravel Innovations: Making your own webhook mechanism through notifications

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
rishabhsharma

rishabhsharma

Miles to go…

More from Medium

Best SSO Provider: Why LoginRadius Is Considered As The Best SSO Solution

Secure your database access with HashiCorp Vault

Create an Anthos Service Mesh for Multiple GKE Clusters using Terraform

Terraform environment