Launch wordpress pod & connect to mysql after configuring k8s automatically using Ansible
📌 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
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.
- create yml file to create service and deployment for mysql pod
- Here we create a service for mysql and expose port number 3306
- 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
- Create secret
- Create mysql deployment and service
- Create wordpress deployment and service
- 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:
- 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.