Get Started with Kubernetes – Part 3

By Style Sync
| Published on
35652-oke_logo

 

On this, the last blog post of a three-blog series on using Kubernetes, let’s learn how we can save our SQL application databases on a persistent volume or storage. We will do this to ensure we never lose any data, for when the POD is redeployed, destroyed, or just decommissioned.

In this blog post, I will demonstrate how we can save our MS-SQL databases to a persistent volume. To begin, we need a NAS server, in addition to the Kubernetes cluster where we will mount the NFS path to the Cluster nodes. We will use this NFS path to attach to the MS-SQL POD. So, let’s get started:

Prepare the NAS folder

I am using a TrueNas in my lab. I created an NFS share called NFS that I will use through this demonstration. After the NFS share is created at the storage side, we must mount the share on to all the Cluster Nodes. Use the following command to mount the share:

sudo mount -t nfs [NAS-IP-Address]:/mnt/ProdNas/nfs /mnt/nfs/

note: /mnt/ProdNas/nfs is the path of the NAS folder

To ensure the NFS share is persistent and will remount after reboot – add the following line to the /etc/fstab file:

[NAS-IP-Address]:/mnt/ProdNas/nfs /mnt/nfs nfs defaults 0 0

Create the Kubernetes Volume

After the NFS folder has mounted on each of the cluster nodes, we are ready to create the PersistentVolume by creating a .yaml file, lets call it nfs-pvol.yaml, and paste the following script into it:

apiVersionv1

kindPersistentVolume

metadata:

  name: nfs-pvol

  labels:

    typelocal

spec:

  storageClassNamemanual

  capacity:

    storage5Gi

  accessModes:

    – ReadWriteMany

  nfs:-pvol.yaml

    server[NAS_IP_Address]

    path“[NAS/Path]”

Save the nfs-pvol.yaml file and close it.

Now that we have created the PersistentVolume, we are ready to create the PersistentVolumeClaim .yaml file, let’s call it nfs-pvc.yaml, to be able to claim the storagevolume space, and then use it with our applications. To do so, we must paste the following script into our .yaml file:

piVersionv1

kindPersistentVolumeClaim

metadata:

  name: nfs-pvc

spec:

  storageClassNamemanual

  accessModes:

    – ReadWriteMany

  resources:

    requests:

      storage5Gi

Save the nfs-pvc.yaml file and close it.

After we have completed creating the two .yaml files above, we can use the following commands to execute the mounting of, and claiming the storage for, the NFS volume:

To attach the Volume, run this command: kubectl apply -f nfs-pvol.yaml

To claim the storage space for the volume, run this command: kubectl apply -f nfs-pvc.yaml

 

After those commands above have run successfully, let’s confirm that the Volume and VolumeClaim are attached as expected by running the following command:

kubectl get pv,pvc

 

Note: The yellow highlighted text in the screenshot above shows that the persistentvolume/nfs-pvol of 5Gi is bound and claimed by the persistentvolumeclaim/nfs-pvc of 5Gi; and that the access mode is RWX (Access Modes – RWS is not highlighted in the screenshot).

Update the MS-SQL .yaml file

The next step is to update the MS-SQL .yaml file to use the NFS path to store our databases.

From our last blog post in Part 2, we learned how to deploy the MS-SQL server. All that is required now is to modify the .yaml file to include the PersistentVolume, as shown below:

apiVersionapps/v

kindDeployment

metadata:

  namemssql-express

  labels:

    appmssql-express

spec:

  replicas1

  selector:

    matchLabels:

      appmssql-express

  template:

    metadata:

      labels:

        appmssql-express

    spec:

      volumes:

      – namenfsvolume01

        persistentVolumeClaim:

            claimNamenfs-pvc

      containers:

      – namemssql-express

        imagemcr.microsoft.com/mssql/server:2017-latest-ubuntu

        volumeMounts:

          – namenfsvolume01

            mountPath“/var/opt/mssql/data”

        ports:

        – containerPort1433

        env:

        – nameSA_PASSWORD

          valueFrom:

            secretKeyRef:

              namemssql-secret

              keymssql-password

        – nameACCEPT_EULA

          value‘Y’

        – nameMSSQL_PID

          valueExpress 

If you examine that file above (same as on the previous blog post) you can notice several differences; they are:

volumes:

namenfsvolume01

persistentVolumeClaim:

claimNamenfs-pvc

In this first section, we instruct the deployment to use the volumeClaim, called: nfs-pvc, and assign a volume name: nfsvolume01. The second difference is:

volumeMounts:

namenfsvolume01

  mountPath“/var/opt/mssql/data”

In this part, we mount the volumeClaim name nfsvolume01 to the /var/opt/mssql/data, which is a path inside the pod OS. This path is the default path for MS-SQL databases and log files.

Put it all together

When I build my own application deployments, I like to consolidate all the .yaml files into one big file that includes all the application deployment configuration. For example, the following mssqldeployment.yaml file will deploy the MS-SQL application, and includes the following configuration steps:

  • PersistentVolume configuration
  • PersistentVolumeClain configuration
  • SQL password Secret
  • SQL Deployment
  • SQL Service expose

apiVersionv1

kindPersistentVolume

metadata:

  namenfs-pvol

spec:

  storageClassNamemanual

  capacity:

    storage10Gi

  accessModes:

    – ReadWriteMany

  nfs:

    server192.168.33.180

    path“/mnt/ProdNas/nfs”

apiVersionv1

kindPersistentVolumeClaim

metadata:

  namenfs-pvc

spec:

  storageClassNamemanual

  accessModes:

    – ReadWriteMany

  resources:

    requests:

      storage9Gi

apiVersionv1

kindSecret

metadata:

    namemssql-secret

typeOpaque

data:

    mssql-passwordUEBzc3cwcmQK

apiVersionapps/v1

kindDeployment

metadata:

  namemssql-express

  labels:

    appmssql-express

spec:

  replicas1

  selector:

    matchLabels:

      appmssql-express

  template:

    metadata:

      labels:

        appmssql-express

    spec:

      volumes:

      – namenfsvolume01

        persistentVolumeClaim:

            claimNamenfs-pvc

      containers:

      – namemssql-express

        imagemcr.microsoft.com/mssql/server:2017-latest-ubuntu

        volumeMounts:

          – namenfsvolume01

            mountPath“/var/opt/mssql/data”

        ports:

        – containerPort1433

        env:

        – nameSA_PASSWORD

          valueFrom:

            secretKeyRef:

              namemssql-secret

              keymssql-password

        – nameACCEPT_EULA

          value‘Y’

        – nameMSSQL_PID

          valueExpress 

apiVersionv1

kindService

metadata:

  namemssql-express-service

spec:

  selector:

    appmssql-express

  typeLoadBalancer  

  ports:

    – protocolTCP

      port1433

      targetPort1433

Summary

At the successful completion of the application deployment, we can create a database to check on the OS NFS mount point folder. If the database was created successfully, the newly created database should be alongside all the SQL default datebases, as shown in the following screenshot:

 

Following the two previous, and this Get Started with Kubernetes blog posts will help you get started with this great technology. It will assist you to build your application while you are learning the Kubernetes concepts.

In the next blog post, I will take you on a deep dive into each Kubernetes topic to give you a better understanding of the concepts and frameworks used in this and the two previous blog posts. Until then, I hope you have been able to follow the steps so far. Please let me know if you have any questions.

 
 
 
 

 

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to Our Newsletter

Table of Contents

Related Insights