Search This Blog

Tuesday, 28 April 2020

Creating a Single instance stateful MySQL pod on vSphere 7 with Kubernetes

In the vSphere environment, the persistent volume objects are backed by virtual disks that reside on datastores. Datastores are represented by storage policies. After the vSphere administrator creates a storage policy, for example gold, and assigns it to a namespace in a Supervisor Cluster, the storage policy appears as a matching Kubernetes storage class in the Supervisor Namespace and any available Tanzu Kubernetes clusters.

In this example below we will show how to get a Single instance Stateful MySQL application pod on vSphere 7 with Kubernetes. For an introduction to vSphere 7 with Kubernetes see this blog link below.

A first look a running a Kubernetes cluster on "vSphere 7 with Kubernetes"


1. If you followed the Blog above you will have a Namespace as shown in the image below. The namespace we are using is called "ns1"

2. Click on "ns1" and ensure you have added storage using the "Storage" card

3. Now let's connect to our supervisor cluster and switch to the Namespace "ns1"

--vsphere-username VCENTER-SSO-USER


$ kubectl vsphere login --insecure-skip-tls-verify --server -u administrator@vsphere.local

Logged in successfully.

You have access to the following contexts:

If the context you wish to use is not in this list, you may need to try
logging in again later, or contact your cluster administrator.

To change context, use `kubectl config use-context `

4. At this point we need to switch to the Namespace we created at step 2 which is "ns1".

$ kubectl config use-context ns1
Switched to context "ns1".

5. Use one of the following commands to verify that the storage class is the one which we added to the Namespace as per #2 above, in this case "pacific-gold-storage-policy".
$ kubectl get storageclass
NAME                          PROVISIONER              AGE
pacific-gold-storage-policy   5d20h

$ kubectl describe namespace ns1
Name:         ns1
Labels:       vSphereClusterID=domain-c8
Annotations:  ncp/extpoolid: domain-c8:1d3e6bfb-af68-4494-a9bf-c8560a7a6aef-ippool-10-193-191-129-10-193-191-190
              vmware-system-resource-pool: resgroup-67
              vmware-system-vm-folder: group-v68
Status:       Active

Resource Quotas
 Name:                                                                     ns1-storagequota
 Resource                                                                  Used  Hard
 --------                                                                  ---   ---  20Gi  9223372036854775807

No resource limits.

As a DevOps engineer, you can use the storage class in your persistent volume claim specifications. You can then deploy an application that uses storage from the persistent volume claim.

6. At this point we can create a Persistent Volume Claim using YAML as follows. In the example below we reference storage class name ""pacific-gold-storage-policy".

Note: We are using a Supervisor Cluster Namespace here for our Stateful MySQL application but the storage class name will also appear in any Tanzu Kubernetes clusters you have created.


apiVersion: v1
kind: PersistentVolumeClaim
  name: mysql-pv-claim
  storageClassName: pacific-gold-storage-policy
    - ReadWriteOnce
      storage: 20Gi

$ kubectl apply -f mysql-pvc.yaml
persistentvolumeclaim/mysql-pv-claim created

7. Let's view the PVC we just created
$ kubectl get pvc
NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS                  AGE
mysql-pv-claim   Bound    pvc-a60f2787-ccf4-4142-8bf5-14082ae33403   20Gi       RWO            pacific-gold-storage-policy   39s

8. Now let's create a Deployment that will mount this PVC we created above using the name "mysql-pv-claim"


apiVersion: v1
kind: Service
  name: mysql
  - port: 3306
    app: mysql
  clusterIP: None
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
  name: mysql
      app: mysql
    type: Recreate
        app: mysql
      - image: mysql:5.6
        name: mysql
          # Use secret in real usage
        - name: MYSQL_ROOT_PASSWORD
          value: password
        - containerPort: 3306
          name: mysql
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      - name: mysql-persistent-storage
          claimName: mysql-pv-claim

$ kubectl apply -f mysql-deployment.yaml
service/mysql created
deployment.apps/mysql created

9. Let's verify we have a running Deployment with a MySQL POD as shown below
$ kubectl get all
NAME                        READY   STATUS    RESTARTS   AGE
pod/mysql-c85f7f79c-gskkr   1/1     Running   0          78s
pod/nginx                   1/1     Running   0          3d21h

NAME                                          TYPE           CLUSTER-IP    EXTERNAL-IP     PORT(S)          AGE
service/mysql                                 ClusterIP      None          <none>          3306/TCP         79s
service/tkg-cluster-1-60657ac113b7b5a0ebaab   LoadBalancer   80:32078/TCP     5d19h
service/tkg-cluster-1-control-plane-service   LoadBalancer   6443:30659/TCP   5d19h

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mysql   1/1     1            1           79s

NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/mysql-c85f7f79c   1         1         1       79s

10. If we return to vSphere client we will see our MySQL Stateful deployment as shown below

11. We can also view the PVC we have created in vSphere client as well

12. Finally let's connect to the MySQL database which is done as follows by

$ kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ppassword
If you don't see a command prompt, try pressing enter.
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.47 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
| Database            |
| information_schema  |
| #mysql50#lost+found |
| mysql               |
| performance_schema  |
4 rows in set (0.02 sec)


More Information

Deploy a Stateful Application

Display Storage Classes in a Supervisor Namespace or Tanzu Kubernetes Cluster

No comments: