Deploying WordPress and MySQL on K8S Cluster inside AWS using Ansible

Divya Kurothe
FAUN — Developer Community 🐾
7 min readJul 14, 2021

--

In this article, we are going to integrate these five technologies i.e., we will be launching the Kubernetes cluster on AWS Cloud and deploy MySQL and WordPress on the cluster and see the power of automation.

Now let us have an overview of the technologies used:

Kubernetes

Containers are a good way to bundle and run your applications. In a production environment, you need to manage the containers that run the applications and ensure that there is no downtime. For example, if a container goes down, another container needs to start. Wouldn’t it be easier if this behavior was handled by a system?

That’s how Kubernetes comes to the rescue! Kubernetes provides you with a framework to run distributed systems resiliently. It takes care of scaling and failover for your application, provides deployment patterns, and more. For example, Kubernetes can easily manage a canary deployment for your system.

Kubernetes provides you with:

  • Service discovery and load balancing Kubernetes can expose a container using the DNS name or using their own IP address. If traffic to a container is high, Kubernetes is able to load balance and distribute the network traffic so that the deployment is stable.
  • Storage orchestration Kubernetes allows you to automatically mount a storage system of your choice, such as local storage, public cloud providers, and more.
  • Automated rollouts and rollbacks You can describe the desired state for your deployed containers using Kubernetes, and it can change the actual state to the desired state at a controlled rate. For example, you can automate Kubernetes to create new containers for your deployment, remove existing containers and adopt all their resources to the new container.
  • Automatic bin packing You provide Kubernetes with a cluster of nodes that it can use to run containerized tasks. You tell Kubernetes how much CPU and memory (RAM) each container needs. Kubernetes can fit containers onto your nodes to make the best use of your resources.
  • Self-healing Kubernetes restarts containers that fail, replaces containers, kills containers that don’t respond to your user-defined health check, and doesn’t advertise them to clients until they are ready to serve.
  • Secret and configuration management Kubernetes lets you store and manage sensitive information, such as passwords, OAuth tokens, and SSH keys. You can deploy and update secrets and application configuration without rebuilding your container images, and without exposing secrets in your stack configuration.

To know more about Kubernetes and its use cases in the industry, you can read the following article:

Ansible

Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

Designed for multi-tier deployments since day one, Ansible models your IT infrastructure by describing how all of your systems inter-relate, rather than just managing one system at a time.

It uses no agents and no additional custom security infrastructure, so it’s easy to deploy — and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.

To know more about Ansible and its use cases in the industry, you can read the following article:

WordPress

WordPress is the simplest, most popular way to create your own website or blog. WordPress powers over 40.0% of all the websites on the Internet. That is, more than one in four websites that you visit are likely powered by WordPress.

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.

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.

Problem Statement:

1. Launch ec2-instances on AWS Cloud eg. for master and slave.

2. Create roles that will configure the master node and slave node separately.

3. Launch a WordPress and MySQL database connected to it in the respective slaves.

4. Expose the WordPress pod and the client should be able to hit the WordPress IP with its respective port.

Let’s Start:

First and foremost I’ve created a directory named “k8s_cluster_role” inside which I’ve created a local ansible.cfg file as follows:

Then I’ve written the following playbook for launching ec2 instances in AWS:

This playbook is using credentials.yml and vars.yml file

  • I have created credentials.yml to store AWS access_key and secret_key and I’ve discussed how to create the same in the former part of this article.
  • vars.yml is needed to store all the necessary details required for launching the instance(such as image-id, key pair, etc)(PS- We could have also asked the user to enter it during the runtime using vars_prompt but it would be cumbersome to do it every time we run the playbook).
  • Then I have created two roles in this directory, one for configuring k8s-master node and the other for k8s-slave nodes using the command:
ansible-galaxy init k8s-master
ansible-galaxy init k8s-slave

If you go inside these two created roles, you can see the following tree structure:

k8s_cluster_role/ 
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml

To know more about roles you can refer to:

In k8s_cluster_role/k8s-master/tasks/main.yml we have written the following code:

In k8s_cluster_role/k8s-slave/tasks/main.yml we have written the following code:

Till this point roles for master and slave are ready and now only a small playbook is need to be created to execute both the roles.

Here is the output of my created ansible galaxy collection:

For launching EC2 instances:

And here is my final playbook which will execute both roles:

The entire Kubernetes Multi-Node Cluster has been successfully configured on AWS cloud using Ansible in just once click.

Now, in the next part we will use this Kubernetes cluster to launch a multi-tier application. We will be using one CMS (WordPress) and one Database (MySQL) and then I will be connecting them.

For this I’ve created a role named wp which will contain all the files and ansible will transfer this file to AWS instance to deploy the same in the cluster.

First things first, we’ll create a wp role using:

ansible-galaxy init wp

Now in files directory of the role, we have to put these three file:

  1. kustomization.yml file: A Secret is an object that stores a piece of sensitive data like a password or key. Kubectl supports the management of Kubernetes objects using a kustomization file. You can create a Secret by generators in kustomization.yaml.

2. mysql.yml file: The MySQL container mounts the PersistentVolume at /var/lib/mysql. The MYSQL_ROOT_PASSWORD environment variable sets the database password from the Secret.

3. wordpress.yml file: The WordPress container mounts the PersistentVolume at /var/www/html for website data files. The WORDPRESS_DB_HOST environment variable sets the name of the MySQL Service defined above, and WordPress will access the database by Service. The WORDPRESS_DB_PASSWORD environment variable sets the database password from the Secret kustomize generated.

main.yml file of task directory of wp role to copy the above files to master node and setup a multitier application on top of Kubernetes cluster:

Now to run to all the three roles together (i.e., k8s mater, k8s slave and wp role) we need to make some changes in final.yml and add wp role in it. Or if you have already created cluster then you only need to run wp role.

Output :

That’s all folks. Thankyou for reading, hope you enjoyed it :)

This task was done in collaboration with Prithviraj Singh. So a special thanks to him for his efforts and constant support ^_^

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--