Deploy WordPress in Kubernetes.

Deploying WordPress on Kubernetes requires creating several resources, including Deployments, Services, Persistent-Volume-Claims, and MySQL database.

Accessing the Kubernetes Master Node

To access the master node in a Kubernetes cluster, follow these LINK:

  1. Open a terminal session.

  2. Use the SSH protocol to connect:

    ssh -i access_keuser@master-node-address

    Replace user with your username and master-node-address with the IP address or hostname of the master node.

  3. Enter your password or authenticate with your SSH key when prompted.

Once connected, you can perform administrative tasks on the Kubernetes master node.

Create WordPress Directory

To initiate the process of setting up WordPress, begin by creating a directory for WordPress files:

  1. Open your terminal or command prompt and access the Kubernetes master node by SSH and kubeconfig-file.

  2. Navigate to the Kubernetes Master node where you want to store your WordPress files.

  3. Run the following command to create a new directory in the Kubernetes master node:

mkdir wordpress

This command creates a new folder named wordpress where you can proceed with the installation and configuration of your WordPress site.

Then navigate to the WordPress directory:

cd wordpress

To set up a web application using Kubernetes, you'll need to deploy both MySQL and WordPress. This setup involves creating three primary configuration files: a deployment file for MySQL, a PersistentVolumeClaim (PVC) for ensuring the database storage persists, and a deployment file for WordPress. Each component plays a critical role in ensuring your application is resilient, scalable, and has reliable storage.

WordPress Deployment YAML (wordpress-deployment.yaml):

vi wordpress-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress:latest
        ports:
        - containerPort: 80
        env:
        - name: WORDPRESS_DB_HOST
          value: mysql-service
        - name: WORDPRESS_DB_USER
          value: root
        - name: WORDPRESS_DB_PASSWORD
          value: Password123@#@!
        - name: WORDPRESS_DB_NAME
          value: wordpress_db
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress
spec:
  selector:
    app: wordpress
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

Ensure you replace placeholder values like your_db_user, your_db_password, your_db_name, and your_mysql_root_password with appropriate values for your setup.


Note: To access WordPress in the browser, we are utilizing a LoadBalancer service type.

MySQL Deployment YAML (mysql-deployment.yaml):

vi mysql-deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:latest
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: Password123@#@!
        - name: MYSQL_DATABASE
          value: wordpress_db

Ensure you replace placeholder values like your_db_user, your_db_password, your_db_name, and your_mysql_root_password with appropriate values for your setup.

PersistentVolumeClaim YAML (persistent-volume-claim.yaml):

vi persistent-volume-claim.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

You can apply these YAML files to your Kubernetes cluster using the kubectl apply -f command. For example:

kubectl apply -f wordpress-deployment.yaml

kubectl apply -f mysql-deployment.yaml

kubectl apply -f persistent-volume-claim.yaml

Ensure you replace placeholder values like your_db_user, your_db_password, your_db_name, and your_mysql_root_password with appropriate values for your setup.

This configuration sets up WordPress with a MySQL database backend and a LoadBalancer service for external access to WordPress. Adjustments may be necessary depending on your specific requirements and environment.

To find your external IP address, open your web browser.

Last updated