🎡 Launching Kubernetes Cluster using Ansible on AWS🎡

🔰 What is Kubernetes ?

🔰 Why you need Kubernetes and what it can do ?

🎡 Kubernetes provides you with:

🔰 What is Ansible?

🔰 Ansible Architecture :

♦️ Ansible Playbooks :

♦️ Inventory :

🔰 Let’s Begin…

We don’t have any hosts or instances running

1. Launching EC2 Instances using Ansible with dynamic inventory :

>>> Create Role :

>>> In tasks file :

ec2:
key_name: "{{ key_name }}"
instance_type: "{{ instance_type }}"
image: "{{ image_id }}"
wait: yes
count: "{{ count }}"
# instance_tags:
# name: "sample_os"
vpc_subnet_id: "{{ subnet_id }}"
assign_public_ip: yes
state: present
region: "{{ region }}"
group_id: "{{ sg_group_id }}"
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
instance_tags:
Name: "{{ item }}"
loop: "{{ OS_name }}"

>>> In vars file :

aws_secret_key: "aws_secret_key"
key_name: "testing"
image_id: "ami-038f1ca1bd58a5790"
count: 1
subnet_id: "subnet-52bc140d"
region: "us-east-1"
sg_group_id: "sg-0639b0dd0a69545ea"
instance_type: "t2.micro"
OS_name:
- "K8S_Master"
- "K8S_Node1"
- "K8S_Node2"

>>> In playbook setup.yml :

- hosts: localhost
gather_facts: False
#vars_files: secret.yml
roles:
- name: "EC2 Launch"
role: /root/task23/k8s/ec2/

>>> Run the playbook :

ansible-playbook setup.yml

👉🏻 Instances launched and hosts file is been updated

2. Setting Up Master Node and Worker Nodes

👉🏻 In Master Node,

>>> Create Role :

>>> In tasks file :

- name: "Creating Repo for Kubernetes"
copy:
src: kubernetes.repo
dest: /etc/yum.repos.d/kubernetes.repo
- name: "Installing Software"
package:
name: "{{ item }}"
state: present
loop: "{{ package_name }}"
- name: "Starting services"
service:
name: "{{ item }}"
state: started
loop: "{{ package }}"
- name: "Changing driver to systemd"
copy:
src: daemon.json
dest: /etc/docker/daemon.json
- name: "Restart Docker Services"
service:
name: docker
state: restarted
- name: "Pulling Images"
shell: kubeadm config images pull
- name: "Bridge to 1"
shell: echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables
- name: "kubeadm init"
shell: kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=NumCPU --ignore-preflight-errors=Mem
ignore_errors: yes
- name: "Creating .kube directory"
file:
path: $HOME/.kube
state: directory
- name: "Copying file"
shell: cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
- name: "Changing Owner Permissions"
shell: chown $(id -u):$(id -g) $HOME/.kube/config
- name: "Setting up Flannel"
shell: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
changed_when: False
- name: "Token Creation"
shell: kubeadm token create --print-join-command
register: token
- name: "Printing Token"
debug:
var: token.stdout

>>> In vars file :

package_name:
- "docker"
- "kubelet"
- "kubeadm"
- "kubectl"
- "iproute-tc"
package:
- "docker"
- "kubelet"

👉🏻 In Worker Nodes,

>>> Create Role :

>>> In tasks file :

- name: "Creating Repo for Kubernetes"
copy:
src: kubernetes.repo
dest: /etc/yum.repos.d/kubernetes.repo
- name: "Installing Software"
package:
name: "{{ item }}"
state: present
loop: "{{ package_name }}"
- name: "Starting services"
service:
name: "{{ item }}"
state: started
loop: "{{ package }}"
- name: "Changing driver to systemd"
copy:
src: daemon.json
dest: /etc/docker/daemon.json
- name: "Restart Docker Services"
service:
name: docker
state: restarted
- name: "Bridge to 1"
shell: echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables
- name: "Using token"
shell: "{{ token }}"

>>> In vars file :

package_name:
- "docker"
- "kubelet"
- "kubeadm"
- "kubectl"
- "iproute-tc"
package:
- "docker"
- "kubelet"

>>> In playbook k8s_setup.yml :

- hosts: "tag_Name_K8S_Master"
roles:
- name: "K8S Master"
role: /root/k8s/k8s_master
- hosts: ["tag_Name_K8S_Node1", "tag_Name_K8S_Node2"]
vars_prompt:
- name: token
prompt: "Enter token :"
private: no
roles:
- name: "K8S_Nodes"
role: /root/k8s/k8s_nodes

>>> Run the playbook :

ansible-playbook k8s_setup.yml

🔰 In AWS instance Master Node ,

All the nodes are ready and we can deploy the pods

kubectl get nodes

👉🏻 Finally on browser ,

http://<master_ip>:<port_no>

🔰 GitHub URL :

Finally our Task is completed successfully !!!!😄✌🏻

Thanks for Reading !! 🙌🏻😁📃

🔰 Keep Learning !! Keep Sharing !! 🔰

Arth Learner - LinuxWorld Informatics Pvt Ltd