Launch wordpress pod & connect to mysql after configuring k8s automatically using Ansible

Yukta chakravarty
4 min readMay 15, 2021

📌 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 respective slaves.

🔅 Expose the wordpress pod and client able hit the wordpress ip with its respective port.

How to configure k8s Multi Node Cluster?

Refer this blog on how to configure k8s Multi Node Cluster

To launch wordpress pod and Mysql pod

Create secret to store credentials

yml file to create secret

You can create secret file as above , user,root_pass,mysql_pass, database are the key name which will be used as reference in other files. The values should given in base64 encoded format.

  1. create yml file to create service and deployment for mysql pod
  1. Here we create a service for mysql and expose port number 3306
  2. We create deployment for mysql

The image of mysql needs value for user,password,database and root password

We give these using environment variables

MYSQL_USER

MYSQL_PASSWORD

MYSQL_DATABASE

MYSQL_ROOT_PASSWORD

Here instead of giving credentials inside code I used secret file and then gave reference in the code

I f you directly want to give in code you can use value keyword instead of valueFrom

Here, mysecret is name of secret created

2. Create yml file to create service,deployment for wordpress and connect to mysql pod

Note: First create mysql pod then only create wordpress pod as we need mysql pod service to connect while creating wordpress pod

We create service for wordpress pod with port 80 exposed

Create deployment for wordpress

To connect wordpress to mysql database we need to give database host name, we can give this using environment variable WORDPRESS_DB_HOST and its value is name of service we created for mysql pod

Here also we can use secret to give credentials.

Additional tasks to be performed after configuring MultiNode Cluster

  1. Create secret
  2. Create mysql deployment and service
  3. Create wordpress deployment and service
  4. Get the NodePort number for wordpress-svc and add rule in security group to allow all traffic for that node

Alternatively, you can also set Nodeport number before while creating service for wordpress using nodePort keyword and add rule for allowing traffic to that port

You can find code here:

  1. First Run k8s_setup playbook to launch ec2 instances

2. Run k8s_setup1 playbook to configure k8s Multi Node Cluster and launch wordpress , mysql pod and connect them

Here we can see 31757 is node port number for wordpress-svc so add rule to allow it as below

Use publicIP:NodePortNumber to connect to wordpress

Hence, wordpress and mysql pods have been launched and they are connected.

--

--