Skip to main content

Mastering Kubernetes Operators: A Wordpress Case Study | Golang Operator | Part Two

Welcome back to the second part of our series on building a Kubernetes operator for managing WordPress deployments! In the first part, we laid the foundation by setting up the basic structure of our operator and defining custom resources. Now, it's time to take the next step and enhance our operator to automate the deployment and management of WordPress and MySQL instances within a Kubernetes cluster.

In this installment, we'll focus on updating our WordPress controller to create deployments and services for both WordPress and MySQL. By the end of this guide, you'll have a fully functional operator capable of handling the complexities of deploying and maintaining WordPress applications in a Kubernetes environment.

So, let's dive in and continue our journey of empowering Kubernetes with the capabilities of our custom operator!

Step 1: Create `wordpress.go` in `internal/controller`

In this step, we'll create a `wordpress.go` file within the `internal/controller` directory. This file will contain the logic for deploying and managing the WordPress application within our Kubernetes cluster. The deployment will define how the WordPress pods are created and managed, while the service will expose these pods to other services within the cluster.

Step 2: Create `mysql.go`

Here, we create `mysql.go`, where we'll implement functions responsible for deploying, managing services, and handling PersistentVolumeClaim (PVC) for MySQL. The PVC ensures that MySQL data persists even if the pod restarts or moves to a different node.

Step 3: Create `common.go`

In this step, we'll develop `common.go`, which will have common functions used across our controllers. This includes functions like `ensureDeployment`, which ensures that the deployment of resources is properly managed, `ensureService`, which handles service creation and management, and `ensurePVC`, which ensures the proper configuration of PersistentVolumeClaim.

Step 4: Modify Reconcile Logic `wordpress_controller.go`

  • The `Reconcile` function compares the desired state specified in the WordPress object with the actual state in the Kubernetes cluster. It ensures that the cluster state reflects the user's desired state.
  • It first fetches the WordPress object from the Kubernetes cluster.
  • Then, it ensures the creation and proper configuration of PVC, Deployment, and Service for both MySQL and WordPress components.
  • If MySQL is not yet up and running, it requeues the reconcile after a delay to check again.
  • Finally, it sets up the controller with the Manager, specifying the types of resources it watches and manages.


This updated code ensures that our WordPress controller handles the deployment and management of MySQL and WordPress components effectively. 

Step 5: Re-Run the Operator

After making changes to our controller, it's necessary to re-run the operator to apply these changes to the Kubernetes cluster. Use the following command to install and run the operator:

make install run

Step 6: Check Resources

To ensure that the resources are created as expected, open another terminal and execute the following command to view all resources within the cluster:

kubectl get all

This command will display the status of pods, services, and other resources, allowing us to verify if the deployment was successful.



Step 7: Expose WordPress Service

Assuming all services are running correctly, we can expose the WordPress service to external access. Use the following command to get the URL for accessing the WordPress service:

minikube service wordpress --url

This command will provide a URL through which we can access the WordPress application from a web browser.

http://127.0.0.1:54304



In wrapping up, our WordPress setup with Kubernetes operators has been a success. We've made deploying and managing applications easier by automating tasks. Through setting up custom rules and controllers, we've kept everything running smoothly. As we move forward, working together and sharing ideas will help us make even better apps. Happy coding!

Source Code : https://github.com/vyas-git/kubernetes-custom-operator

Comments