In this article I am going to show you how to install Wordpress with MySQL Database inside Multi Node Kubernetes k8s cluster on AWS using Ansible.
Let’s Start
Brief Introduction to All the technologies used here
What is Wordpress ?
what is wordpress
WordPress is the simplest, most popular way to create your own website or blog.
In fact, WordPress powers over 40.0% of all the websites on the Internet. Yes more than one in four websites that you visit are likely powered by WordPress.
Wordpress is a CMS (content management system)
A content management system is basically a tool that makes it easy to manage important aspects of your website like content without needing to know anything about programming.
For example, not only does WordPress power a huge number of business sites and blogs, it’s also the most popular way to create an eCommerce store as well! With WordPress, you can create:
- Business websites
- eCommerce stores
- Blogs
- Portfolios
- Resumes
- Forums
- Social networks
- Membership sites
What is MySQL ?
what is mysql
MySQL is the most popular Open Source Relational SQL Database Management System. MySQL is one of the best RDBMS being used for developing various web-based software applications
MySQL is used by many database-driven web applications, including Drupal, Joomla, phpBB, and WordPress.
What is Kubernetes ?
What is Kubernetes ?
Kubernetes commonly stylized as K8s is an open-source container-orchestration system for automating computer application deployment, scaling, and management.
- It was originally designed by Google and is now maintained by the Cloud Native Computing Foundation.
- It aims to provide a “platform for automating deployment, scaling, and operations of application containers across clusters of hosts”
- It works with a range of container tools and runs containers in a cluster, often with images built using Docker.
- Kubernetes originally interfaced with the Docker runtime through a “Dockershim”
What is Multi Node Kubernetes Cluster ?
multi node kubernetes cluster
A Kubernetes cluster is a set of node machines for running containerized applications. If you’re running Kubernetes, you’re running a cluster. At a minimum, a cluster contains a control plane and one or more compute machines, or nodes. Nodes actually run the applications and workloads.
Multi Node Kubernetes Cluster is a Group of nodes in which there is one Master Node and Multiple Worker/Slave Nodes.
What is AWS Cloud ?
what is aws
Amazon Web Services (AWS) is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis.
- These cloud computing web services provide a variety of basic abstract technical infrastructure and distributed computing building blocks and tools.
- One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet.
- AWS’s version of virtual computers emulates most of the attributes of a real computer, including hardware:
- central processing units (CPUs)
- graphics processing units (GPUs) for processing
- local/RAM memory
- hard-disk/SSD storage
- a choice of operating systems
- networking
- pre-loaded application software such as web servers, databases, and customer relationship management (CRM).
What is Ansible ?
what is ansible
Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.
Benefits of Using Ansible
- Free: Ansible is an open-source tool.
- Very simple to set up and use: No special coding skills are necessary to use Ansible’s playbooks (more on playbooks later).
- Powerful: Ansible lets you model even highly complex IT workflows.
- Flexible: You can orchestrate the entire application environment no matter where it’s deployed. You can also customize it based on your needs.
- Agentless: You don’t need to install any other software or firewall ports on the client systems you want to automate. You also don’t have to set up a separate management structure.
- Efficient: Because you don’t need to install any extra software, there’s more room for application resources on your server
Prerequisite
- Your Preferred Choice of OS (I am using Redhat 8)
- Ansible Installed
- AWS account
Step to Follow:
📌 Automate Kubernetes Cluster Using Ansible
🔅 Launch ec2-instances on AWS Cloud eg. for master and slave.
🔅 Create roles that will configure master node and slave node seperately.
🔅 Launch a wordpress and mysql database connected to it in the respectine slaves.
🔅 Expose the wordpress pod and client able hit the wordpress ip with its respective port.
Note: You will get all the code and files on my github profile I have given the github link below at the end of this article.
Step 1:
Creating Ansible Playbook for Launching AWS EC2 instance.
Ansible Playbook for launching Ec2 instances
I have written this Playbook to launch EC2 instance. This Playbook will launch 3 EC2 Instances for Kubernetes Cluster one Master Node and two Worker Node.
3 ec2 instances Launched Successfully
master_node IP
13.212.210.46worker_node IP
13.229.54.140
13.212.239.24
Step 2:
Create Playbook to configure k8s Master and k8s Worker Nodes on the above created EC2 Instances using kubeadm.
Master Node Playbook:
First of All I have written a Playbook that will configure k8s Master Node in Aws Master Ec2 Instance.
Ansible Playbook for master node
Ansible Playbook for master node
Ansible Playbook for master node
This playbook will:
- Install docker inside Master Node EC2 instance
- Configure YUM to install Kubelet, Kubeadm, Kubectl
- Enable all the services of docker and kubernetes
- Initialize Kubeadm services
- Install flannel
(Flannel is a simple, lightweight layer 3 fabric for Kubernetes. Flannel manages an IPv4 network between multiple nodes in a cluster. It does not control how containers are networked to the host, only how the traffic is transported between hosts)
- Create Join command for workers/slave to join the cluster with master
Worker/Slave Node:
Now I have written Worker/Slave Node Playbook to configure Slave Node inside Worker Ec2 instances.
Ansible Playbook for slave node
Ansible Playbook for slave node
This playbook will:
- Install docker inside Master Node EC2 instance
- Configure YUM to install Kubelet, Kubeadm, Kubectl
- Enable all the services of docker and kubernetes
- Initialize Kubeadm services
- Install flannel
- Restart All the services again
- Fetching Join command to join the cluster
Step 3:
Convert Playbook into roles and Upload those role on your Ansible Galaxy repo.
Now I have created Ansible Roles Named Slave_node and Master_node to make it more organized.
and put all the files and playbook inside these roles required to setup kubernetes cluster.
code to create ansible roles for salve node
code to create ansible roles for master node
Step 4:
This is ansible Configuration file ansible.cfg you need to have privilege escalation for AWS EC2 instances to setup with ansible playbook.
For more Info about this you can read my another Article
Configure Webserver on AWS ec2 using Ansible
Hello Everyone
ansible.cfg file for ansible to setup ec2 instances on aws
Now Its Time To Test All the Codes That We have Written:
- First We have to Launch EC2 instances in AWS
Use this command:
ansible-playbook ec2.yml
ec2.yml ansible playbook
Aws ec2 dashboard
You will get all the IP Address of EC2 instances in Ansible Host Inventory using AWS python script for Dynamic Inventory
ansible inventory file with ip address
If you want to get all the IP address of EC2 instances created above dynamically to the Ansible inventory file than use this Python scripts given below.
Configure Webserver on AWS ec2 using Ansible
This is cluster.yml file
That will help us to execute the two Ansible roles together.
Ansible role cluster.yml
2. Use this command to run both the Ansible roles:
ansible-playbook cluster.yml
cluster.yml running
3 . Now go inside master node to check if the worker node are connected or not
Use this command:
kubectl get nodes
checking for nodes for k8s cluster
You can see 1 master and 2 none which means Worker/slave Node
Deploying httpd image in kubernetes cluster
Just for testing purpose i have created a demo deployment of httpd server in this cluster.
getting pods info for deployed httpd image
AS you can see the pod is running successfully.
Step 5:
After The Multi Node Cluster Setup is Done
5. Now it’s time to install Wordpress with MySQL Database
- Wordpress yml file for kubernetes
wordpress yml file for kubernetes
wordpress yml file for kubernetes
The above photos is of Wordpress yml file for kubernetes
This file will be used to Install Wordpress inside Kubernetes Cluster
2. MySQL yml file for kubernetes
mysql yml file for kubernetes
mysql yml file for kubernetes
The above photos is of MySQL yml file for kubernetes
This file will be used to Install MySQL inside Kubernetes Cluster
3. MySQL PVC yml file for kubernetes
mysql pvc file for kubernetes
The above photos is of MySQL PVC yml file for kubernetes
This file will be used to Install MySQL PVC inside Kubernetes Cluster
4. Wordpress PVC for Kubernetes
wordpress pvc file for kubernetes
The above photos is of Wordpress PVC yml file for kubernetes
This file will be used to Install Wordpress PVC inside Kubernetes Cluster
5. Secret file for Kubernetes
secret yml file for mysql
The above photos is of Secret file yml file for kubernetes
This file will be used to Install Secret file inside Kubernetes Cluster
Now It’ Time to Play Ansible Playbook
ansible-playbook final_setup.py
ansible playbook output for installing wordpress and mysql
ansible playbook output for installing wordpress and mysql
Now Check all the Deployments, Pods and PVC using
kubectl get all
kubectl pvc
kubectl get all
kubectl get pvc
As You can See Worpdress is installed Successfully inside Kubernetes Cluster in AWS Using Ansible in very Few Steps
worpdress installation completed
wordpress installation process