This example tutorial describes how to deploy the WordPress application using vSphere Pods in the vSphere with Tanzu environment.

The WordPress deployment includes containers for the WordPress frontend and the MySQL backend, and services for both. Secret objects are also required.

This tutorial uses a Deployment object. In production environment, you typically use StatefulSets for both WordPress and MySQL containers.

Prerequisites

Deploy WordPress

Use this workflow to deploy the WordPress application using vSphere Pods.

Part 1. Access Your Namespace

Use these steps to access your namespace.

Procedure

  1. Log in to the Supervisor.
     kubectl vsphere login --server=SVC-IP-ADDRESS --vsphere-username wcp-user@vsphere.local 
  2. Switch to the vSphere Namespace.
     kubectl config use-context VSPHERE-PODS-NAMESPACE
  3. Verify that the storage policy you created, vwt-storage-policy, is available in the namespace as a storage class.

Part 2. Create WordPress PVCs

Use these commands to create WordPress PVCs.

Procedure

  1. Create the MySQL PVC.
     kubectl apply -f mysql-pvc.yaml
  2. Create the WordPress PVC.
     kubectl apply -f wordpress-pvc.yaml
  3. Verify the PVCs.
     kubectl get pvc,pv

Part 3. Create Secrets

The public Docker Hub is the default container registry for Kubernetes. Docker Hub now limits image pulls. You need to have a paid account and add the account key to the secret YAML in the data.dockerconfigjson field.

Procedure

  1. Create Docker Hub registry secret.
     kubectl apply -f regcred.yaml
  2. Create the mysql password secret.
    The MySQL DB password is required. Within the secret the password needs to be base64-encoded.
    kubectl apply -f mysql-pass.yaml
  3. Verify secrets.
    kubectl get secrets

Part 4. Create Services

Follow these steps to create services.

Procedure

  1. Create the MySQL service.
    kubectl apply -f mysql-service.yaml
  2. Create the WordPress service.
    kubectl apply -f wordpress-service.yaml
  3. Verify services.
    kubectl get services

Part 5. Create Pod Deployments

Use this task to create pod deployments.

This tutorial uses Deployment objects. In production environment, you should use StatefulSets for both WordPress and MySQL containers.

Procedure

  1. Create the MySQL deployment.
    kubectl apply -f mysql-deployment-vsphere-pod.yaml
    Note: When a vSphere Pod is created, the system creates a VM for the containers in the pod. By default, the VM has a 512 MB RAM limit. The MySQL container requires more memory. The pod deployment spec mysql-deployment-vsphere-pod.yaml includes a section that increases the memory given to the vSphere Pod VM. If you do not include this section, the pod deployment fails with an out of memory (OOM) exception. You don't need to increase the RAM when deploying a MySQL pod to a TKG cluster.
  2. Create the WordPress deployment.
    kubectl apply -f wordpress-deployment.yaml
  3. Verify your deployment.
    kubectl get deployments	

Part 6. Test WordPress

Follow these steps to test your WordPress deployment.

Procedure

  1. Verify all objects are created and running.
    kubectl get pv,pvc,secrets,rolebinding,services,deployments,pods
  2. Get the EXTERNAL-IP address from the WordPress service.
    kubectl get service wordpress
    
    NAME        TYPE           CLUSTER-IP    EXTERNAL-IP     PORT(S)        AGE
    wordpress   LoadBalancer   10.96.9.180   10.197.154.73   80:30941/TCP   87s
  3. Browse to the EXTERNAL-IP address.
  4. Configure the WordPress instance.
    Username: administrator
    Password: use the provided strong password

Example YAML Files for the WordPress Deployment

Use these example YAML files when you deploy the WordPress application with vSphere Pods.

mysql-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: vwt-storage-policy
  resources:
    requests:
      storage: 20Gi

wordpress-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress-pvc
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: vwt-storage-policy  
  resources:
    requests:
      storage: 20Gi

regcred.yaml

apiVersion: v1
kind: Secret
metadata:
  name: regcred
data:
  .dockerconfigjson: ewoJImF1dGhzIjog....zZG1KcE5WUmtXRUozWpc
type: kubernetes.io/dockerconfigjson

mysql-pass.yaml

apiVersion: v1
data:
  password: YWRtaW4=  #admin base64 encoded
kind: Secret
metadata:
  name: mysql-pass

mysql-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None

wordpress-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
  type: LoadBalancer

mysql-deployment-vsphere-pod.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        #increased resource limits required for this pod vm
        #default pod VM RAM is 512MB; MySQL container needs more 
        #without extra RAM OOM error prevents deployment
        #extra RAM not required for Kuberentes cluster
        resources:
          limits:
            memory: 1024Mi
            cpu: 1
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pvc
      imagePullSecrets:
      - name: regcred  

wordpress-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      containers:
      - image: wordpress:4.8-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wordpress-pvc
      imagePullSecrets:
      - name: regcred