Integrating RDS with k8s
Hello Connections Welcome to this article!
Task Description:-
Deploy the WordPress application on Kubernetes and AWS using terraform including the following steps;
1. Write an Infrastructure as code using Terraform, which automatically deploy the WordPress application
2. On AWS, use RDS service for the relational database for WordPress application.
3. Deploy WordPress as a container either on top of Minikube or EKS or Fargate service on AWS
4. The WordPress application should be accessible from the public world if deployed on AWS or through workstation if deployed on Minikube.
Creating k8s Cluster:-
For creating the k8s cluster we have to install the minikube in our PC,we can also use AWS EKS(Elastic Kubernetes Services) managed Service Offered by AWS.
Minikube installation:- https://kubernetes.io/docs/tasks/tools/install-minikube/
After Minikube installation we also need kubectl software which contains the configuration management of the Kubernetes cluster.
Kubectl installation:- https://kubernetes.io/docs/tasks/tools/install-kubectl/
now we can check both the software installed or not.
For creating the Single Node Kubernetes cluster where only one master and one managed node we can use minikube.
minikube start --driver=kvm2
After this command will run successfully we can see in our VM that the Kubernetes cluster has been created. Now-on-wards we just have to run this command to start the Kubernetes cluster.
minikube start
Creating Terraform code:- we just need to run the minikube start command to start our K8S cluster and it will automatically configure kubectl for us.
resource "null_resource" "nullremote1"{ provisioner "local-exec" { command = "minikube start" } }
Launching Database on AWS RDS:- AWS RDS stands for Relational Database Services which is managed service by Amazon we just need to tell whatever we want and all the instances and databases will be managed by Amazon.
Here we are launching using terraform, we have to tell the configuration what we want.
resource "aws_db_instance" "mydb" { allocated_storage = 10 storage_type = "gp2" engine = "mysql" engine_version = "5.7" instance_class = "db.t2.micro" name = "mysqldb" username = "yogesh" password = "redhat123" parameter_group_name = "default.mysql5.7" vpc_security_group_ids=["sg-012a5e5d7582997de"] publicly_accessible=true }
After Database will be created we can now create a WordPress instance and can connect to our Database. We can see our database by visiting the AWS console.
Hybrid Setup:- Here we are creating the MySql database on AWS by using Relational database service and Minikube is running on our local system so we can say the database is on the public cloud and WordPress is running on the Local system this setup is also known as Hybrid Cloud Setup.
Creating WordPress Pod:- For creating the WordPress pod we have to give some Information about our pod. Like image and labels, Important is container Information where we can also provide environment variable which will be run when the Pod is being created.
resource "kubernetes_pod" "wordp" { depends_on=[null_resource.nullremote1] metadata { name = "mywordp" labels = { app = "MyApp" } } spec { container { image = "wordpress" name = "wp" env{ name = "WORDPRESS_DB_HOST" value = aws_db_instance.mydb.address } env{ name = "WORDPRESS_DB_USER" value = aws_db_instance.mydb.username } env{ name = "WORDPRESS_DB_PASSWORD" value = aws_db_instance.mydb.password } env{ name = "WORDPRESS_DB_NAME" value = aws_db_instance.mydb.name } } } }
We can check it manually
Exposing WordPress Container:-
Now our pod has been created now we will expose the pod using services and nodeport so that we can access it.
resource "kubernetes_service" "example" { depends_on=[null_resource.nullremote1] metadata { name = "mywordpress" } spec { selector = { app = "${kubernetes_pod.wordp.metadata.0.labels.app}" } session_affinity = "ClientIP" port { port = 80 target_port = 80 } type = "NodePort" } }
Now we will run the Terraform code.
#terraform init #terraform validate #terraform apply --auto-approve
We can also destroy whole Infrastructure in just one single click
#terraform destroy
Now we can see our site by vising NodeIP and automatically allocated node port.
Thank You so much for visiting my article!!
for any queries dm me!
Code link :- Task-6