I have a kubernetes cluster with three nodes. I need to deploy a mysql server to the cluster and I need persistence for that mysql. So when I redeploy or restart my pod it won't wipe the data.
But if I use a storageClass which uses azure-disk it will only be mounted to the host.
metadata:
name: mysql-storage
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "managed-premium"
resources:
requests:
storage: 5Gi
So if I delete or redeploy the pod it could start up on another host where the disk is not mounted and then my data is gone.
I tried changing the accessModes to ReadWriteMany but that failed when claiming the volume. Saying that it only supported ReadWriteOnce.
You cannot really solve this problem. There are several workaround for this problem, like you can create a software storage system on top of managed disks and use that to store data, you can use azure files persistent storage, it can be mounted from any pod (and from many pods).
Managed disks can only be mounted to one node (but I think all the pods on the node can access this disk), So ReadWriteMany doesnt make a lot of sense. You can use StatefulSets to define your mysql, it will always get mounted to the same node, but I dont think you really need it, PVC should get remounted to the node hosting your pod, the only downside is that it takes somewhere around 1 minute to reattach the managed disk to another node.
You should be using StatefulSet with PersistentVolumeClaims (PVC).
The following guide gives a nice example on how to do exactly what you want.
kubernetes.io: Run a Replicated Stateful Application
In the guide, a few things you need to pay attention:
StatefulSet provides guarantees about the ordering and uniqueness of these Pods. And the definition contains a volumeClaimTemplates
volumeClaimTemplates will ensure each pod gets a PersistentVolumeClaims where they can write data.
PersistentVolumeClaims reserves the storage and then is later attached\mounted to the POD
Another detail, you should not delete the resources if you want to preserve the data and integrity, you should upgrade.