Kubernetes Setup with WordPress using Ansible

What is Kubernetes ?

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.

Kubernetes provides you with:

✍ Service discovery and load balancing
✍ Storage orchestration
✍ Automated rollouts and rollbacks
✍ Automatic bin packing
✍ Self-healing
✍ Secret and configuration management

What is Ansible ?

Ansible is a configuration management system written in Python using a declarative markup language to describe configurations. It is used to automate software configuration and deployment.

WordPress

WordPress is a free and open-source content management system (CMS) written in PHP and paired with a MySQL or MariaDB database.

MYSQL

A relational database organizes data into one or more data tables in which data types may be related to each other; these relations help structure the data. SQL is a language programmers use to create, modify and extract data from the relational database, as well as control user access to the database. In addition to relational databases and SQL, an RDBMS like MySQL works with an operating system to implement a relational database in a computer’s storage system, manages users, allows for network access and facilitates testing database integrity and creation of backups.

Configure K8S Multi Node Cluster over AWS Cloud using Ansible :

We don’t have any hosts or instances running

1. Launching EC2 Instances using Ansible :

♻ Create Role :

ansible-galaxy init ec2

♻ 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 :

ansible-galaxy init k8s_master

♻ 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 :

ansible-galaxy init k8s_nodes

♻ 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

3. Launching WordPress and MySQL DataBase Pods

♻ Create Role :

ansible-galaxy init wordpress

♻ In tasks file :

- name: "Launching MYSQL DB Pod"
shell: kubectl run "{{ db_pod }}" --image "{{ sql_image }}" --env=MYSQL_ROOT_PASSWORD={{ mysql_root_password }} --env=MYSQL_DATABASE={{ database_name }} --env=MYSQL_USER={{ mysql_user }} --env=MYSQL_PASSWORD={{ mysql_password }}
- name: "Launching WordPress Pod"
shell: kubectl run "{{ wp_pod }}" --image "{{ wp_image }}"
- name: "Exposing Pod"
shell: kubectl expose pod "{{ wp_pod }}" --type=NodePort --port=80
- name: "Get svc"
shell: kubectl get svc
register: svc
- name: "Print"
debug:
var: svc.stdout
- name: "Database IP"
shell: "kubectl get pods -o wide"
register: db_ip
- debug:
var: "db_ip.stdout_lines"

♻ In vars file :

db_pod: "mydb1"
wp_pod: "mywp1"
sql_image: "mysql:5.7"
wp_image: "wordpress:5.1.1-php7.3-apache"
mysql_root_password: "redhat"
database_name: "mywpdb"
mysql_user: "myuser"
mysql_password: "redhat"

♻ In playbook wordpress_setup.yml :

- hosts: "tag_Name_K8S_Master"
roles:
- name: "WordPress Application"
role: /root/task23/k8s/wordpress

♻ Run the playbook :

ansible-playbook wordpress_setup.yml

♻ On browser:

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

Thanks for Reading !! 📃

--

--

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