In one of my previous posts I showed how we can run Oracle XE database on a K8s cluster. That approach works fine for the use-cases when we don’t care about the data and we are fine with loosing it when the container is redeployed and the pod is restarted. But if we want to keep the data, if we want it to survive all rescheduling we’ll want to reconsider K8s resources used to run the DB container on the cluster. That said, the yaml file defining the resources looks like this one:
apiVersion: apps/v1beta2 kind: StatefulSet metadata: name: oraclexe labels: run: oraclexe spec: selector: matchLabels: run: oraclexe serviceName: "oraclexe-svc" replicas: 1 template: metadata: labels: run: oraclexe spec: volumes: - name: dshm emptyDir: medium: Memory containers: - image: eugeneflexagon/database:188.8.131.52-xe volumeMounts: - mountPath: /dev/shm name: dshm - mountPath: /u01/app/oracle/oradata name: db imagePullPolicy: Always name: oraclexe ports: - containerPort: 1521 protocol: TCP volumeClaimTemplates: - metadata: name: db spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 100M --- apiVersion: v1 kind: Service metadata: name: oraclexe-svc labels: run: oraclexe spec: selector: run: oraclexe ports: - port: 1521 targetPort: 1521 type: LoadBalancer
There are some interesting things here. First of all this is not a deployment. We are defining here another K8s resource which is called Stateful Set. Unlike a Deployment, a Stateful Set maintains a sticky identity for each of their Pods. These pods are created from the same specification, but they are not interchangeable: each has a persistent identifier that it maintains across any rescheduling.
This guy has been specially designed for stateful applications like database that save their data to a persistent storage. In order to define a persistent storage for our database we use a special
K8s resource Persistent Volume and here in the yaml file we are defining a claim to create a 100mb Persistent Volume with name db. This volume provides read/write access mode for one assigned pod. The volume is called persistent because its lifespan is not maintained by a container and not even by a pod, it’s maintained by a K8s cluster. So it can outlive any containers and pods and save the data. We are referring to this persistence volume in the container definition mounting a volume on path /u01/app/oracle/oradata. This is where Oracle DB XE container stores its data.
|Published on Java Code Geeks with permission by Eugene Fedorenko , partner at our JCG program. See the original article here: Persistent Volumes for Database Containers running on a K8s cluster|
Opinions expressed by Java Code Geeks contributors are their own.