Examples

For reference, this page provides examples of Kubernetes resources that use Longhorn storage.

Block Volume

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: longhorn-block-vol
  5. spec:
  6. accessModes:
  7. - ReadWriteOnce
  8. volumeMode: Block
  9. storageClassName: longhorn
  10. resources:
  11. requests:
  12. storage: 2Gi
  13. ---
  14. apiVersion: v1
  15. kind: Pod
  16. metadata:
  17. name: block-volume-test
  18. namespace: default
  19. spec:
  20. containers:
  21. - name: block-volume-test
  22. image: nginx:stable-alpine
  23. imagePullPolicy: IfNotPresent
  24. volumeDevices:
  25. - devicePath: /dev/longhorn/testblk
  26. name: block-vol
  27. ports:
  28. - containerPort: 80
  29. volumes:
  30. - name: block-vol
  31. persistentVolumeClaim:
  32. claimName: longhorn-block-vol

CSI Persistent Volume

  1. apiVersion: v1
  2. kind: PersistentVolume
  3. metadata:
  4. name: longhorn-vol-pv
  5. spec:
  6. capacity:
  7. storage: 2Gi
  8. volumeMode: Filesystem
  9. accessModes:
  10. - ReadWriteOnce
  11. persistentVolumeReclaimPolicy: Delete
  12. storageClassName: longhorn
  13. csi:
  14. driver: driver.longhorn.io
  15. fsType: ext4
  16. volumeAttributes:
  17. numberOfReplicas: '3'
  18. staleReplicaTimeout: '2880'
  19. volumeHandle: existing-longhorn-volume
  20. ---
  21. apiVersion: v1
  22. kind: PersistentVolumeClaim
  23. metadata:
  24. name: longhorn-vol-pvc
  25. spec:
  26. accessModes:
  27. - ReadWriteOnce
  28. resources:
  29. requests:
  30. storage: 2Gi
  31. volumeName: longhorn-vol-pv
  32. storageClassName: longhorn
  33. ---
  34. apiVersion: v1
  35. kind: Pod
  36. metadata:
  37. name: volume-pv-test
  38. namespace: default
  39. spec:
  40. restartPolicy: Always
  41. containers:
  42. - name: volume-pv-test
  43. image: nginx:stable-alpine
  44. imagePullPolicy: IfNotPresent
  45. livenessProbe:
  46. exec:
  47. command:
  48. - ls
  49. - /data/lost+found
  50. initialDelaySeconds: 5
  51. periodSeconds: 5
  52. volumeMounts:
  53. - name: vol
  54. mountPath: /data
  55. ports:
  56. - containerPort: 80
  57. volumes:
  58. - name: vol
  59. persistentVolumeClaim:
  60. claimName: longhorn-vol-pvc

Deployment

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: mysql
  5. labels:
  6. app: mysql
  7. spec:
  8. ports:
  9. - port: 3306
  10. selector:
  11. app: mysql
  12. clusterIP: None
  13. ---
  14. apiVersion: v1
  15. kind: PersistentVolumeClaim
  16. metadata:
  17. name: mysql-pvc
  18. spec:
  19. accessModes:
  20. - ReadWriteOnce
  21. storageClassName: longhorn
  22. resources:
  23. requests:
  24. storage: 2Gi
  25. ---
  26. apiVersion: apps/v1
  27. kind: Deployment
  28. metadata:
  29. name: mysql
  30. labels:
  31. app: mysql
  32. spec:
  33. selector:
  34. matchLabels:
  35. app: mysql # has to match .spec.template.metadata.labels
  36. strategy:
  37. type: Recreate
  38. template:
  39. metadata:
  40. labels:
  41. app: mysql
  42. spec:
  43. restartPolicy: Always
  44. containers:
  45. - image: mysql:5.6
  46. name: mysql
  47. livenessProbe:
  48. exec:
  49. command:
  50. - ls
  51. - /var/lib/mysql/lost+found
  52. initialDelaySeconds: 5
  53. periodSeconds: 5
  54. env:
  55. - name: MYSQL_ROOT_PASSWORD
  56. value: changeme
  57. ports:
  58. - containerPort: 3306
  59. name: mysql
  60. volumeMounts:
  61. - name: mysql-volume
  62. mountPath: /var/lib/mysql
  63. volumes:
  64. - name: mysql-volume
  65. persistentVolumeClaim:
  66. claimName: mysql-pvc

Pod with PersistentVolumeClaim

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: longhorn-volv-pvc
  5. spec:
  6. accessModes:
  7. - ReadWriteOnce
  8. storageClassName: longhorn
  9. resources:
  10. requests:
  11. storage: 2Gi
  12. ---
  13. apiVersion: v1
  14. kind: Pod
  15. metadata:
  16. name: volume-test
  17. namespace: default
  18. spec:
  19. restartPolicy: Always
  20. containers:
  21. - name: volume-test
  22. image: nginx:stable-alpine
  23. imagePullPolicy: IfNotPresent
  24. livenessProbe:
  25. exec:
  26. command:
  27. - ls
  28. - /data/lost+found
  29. initialDelaySeconds: 5
  30. periodSeconds: 5
  31. volumeMounts:
  32. - name: volv
  33. mountPath: /data
  34. ports:
  35. - containerPort: 80
  36. volumes:
  37. - name: volv
  38. persistentVolumeClaim:
  39. claimName: longhorn-volv-pvc

Restore to File

For more information about restoring to file, refer to this section.

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: restore-to-file
  5. namespace: longhorn-system
  6. spec:
  7. nodeName: <NODE_NAME>
  8. containers:
  9. - name: restore-to-file
  10. command:
  11. # set restore-to-file arguments here
  12. - /bin/sh
  13. - -c
  14. - longhorn backup restore-to-file
  15. '<BACKUP_URL>'
  16. --output-file '/tmp/restore/<OUTPUT_FILE>'
  17. --output-format <OUTPUT_FORMAT>
  18. # the version of longhorn engine should be v0.4.1 or higher
  19. image: longhorn/longhorn-engine:v0.4.1
  20. imagePullPolicy: IfNotPresent
  21. securityContext:
  22. privileged: true
  23. volumeMounts:
  24. - name: disk-directory
  25. mountPath: /tmp/restore # the argument <output-file> should be in this directory
  26. env:
  27. # set Backup Target Credential Secret here.
  28. - name: AWS_ACCESS_KEY_ID
  29. valueFrom:
  30. secretKeyRef:
  31. name: <S3_SECRET_NAME>
  32. key: AWS_ACCESS_KEY_ID
  33. - name: AWS_SECRET_ACCESS_KEY
  34. valueFrom:
  35. secretKeyRef:
  36. name: <S3_SECRET_NAME>
  37. key: AWS_SECRET_ACCESS_KEY
  38. - name: AWS_ENDPOINTS
  39. valueFrom:
  40. secretKeyRef:
  41. name: <S3_SECRET_NAME>
  42. key: AWS_ENDPOINTS
  43. volumes:
  44. # the output file can be found on this host path
  45. - name: disk-directory
  46. hostPath:
  47. path: /tmp/restore
  48. restartPolicy: Never

Simple Pod

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: longhorn-simple-pod
  5. namespace: default
  6. spec:
  7. restartPolicy: Always
  8. containers:
  9. - name: volume-test
  10. image: nginx:stable-alpine
  11. imagePullPolicy: IfNotPresent
  12. livenessProbe:
  13. exec:
  14. command:
  15. - ls
  16. - /data/lost+found
  17. initialDelaySeconds: 5
  18. periodSeconds: 5
  19. volumeMounts:
  20. - name: volv
  21. mountPath: /data
  22. ports:
  23. - containerPort: 80
  24. volumes:
  25. - name: volv
  26. persistentVolumeClaim:
  27. claimName: longhorn-simple-pvc

Simple PersistentVolumeClaim

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: longhorn-simple-pvc
  5. spec:
  6. accessModes:
  7. - ReadWriteOnce
  8. storageClassName: longhorn
  9. resources:
  10. requests:
  11. storage: 1Gi

StatefulSet

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: nginx
  5. labels:
  6. app: nginx
  7. spec:
  8. ports:
  9. - port: 80
  10. name: web
  11. selector:
  12. app: nginx
  13. type: NodePort
  14. ---
  15. apiVersion: apps/v1
  16. kind: StatefulSet
  17. metadata:
  18. name: web
  19. spec:
  20. selector:
  21. matchLabels:
  22. app: nginx # has to match .spec.template.metadata.labels
  23. serviceName: "nginx"
  24. replicas: 2 # by default is 1
  25. template:
  26. metadata:
  27. labels:
  28. app: nginx # has to match .spec.selector.matchLabels
  29. spec:
  30. restartPolicy: Always
  31. terminationGracePeriodSeconds: 10
  32. containers:
  33. - name: nginx
  34. image: registry.k8s.io/nginx-slim:0.8
  35. livenessProbe:
  36. exec:
  37. command:
  38. - ls
  39. - /usr/share/nginx/html/lost+found
  40. initialDelaySeconds: 5
  41. periodSeconds: 5
  42. ports:
  43. - containerPort: 80
  44. name: web
  45. volumeMounts:
  46. - name: www
  47. mountPath: /usr/share/nginx/html
  48. volumeClaimTemplates:
  49. - metadata:
  50. name: www
  51. spec:
  52. accessModes: [ "ReadWriteOnce" ]
  53. storageClassName: "longhorn"
  54. resources:
  55. requests:
  56. storage: 1Gi

StorageClass

  1. kind: StorageClass
  2. apiVersion: storage.k8s.io/v1
  3. metadata:
  4. name: longhorn
  5. provisioner: driver.longhorn.io
  6. allowVolumeExpansion: true
  7. reclaimPolicy: Delete
  8. volumeBindingMode: Immediate
  9. parameters:
  10. numberOfReplicas: "3"
  11. staleReplicaTimeout: "2880" # 48 hours in minutes
  12. fromBackup: ""
  13. fsType: "ext4"
  14. # mkfsParams: "-I 256 -b 4096 -O ^metadata_csum,^64bit"
  15. # backingImage: "bi-test"
  16. # backingImageDataSourceType: "download"
  17. # backingImageDataSourceParameters: '{"url": "https://backing-image-example.s3-region.amazonaws.com/test-backing-image"}'
  18. # backingImageChecksum: "SHA512 checksum of the backing image"
  19. # diskSelector: "ssd,fast"
  20. # nodeSelector: "storage,fast"
  21. # recurringJobSelector: '[
  22. # {
  23. # "name":"snap",
  24. # "isGroup":true,
  25. # },
  26. # {
  27. # "name":"backup",
  28. # "isGroup":false,
  29. # }
  30. # ]'

Note that Longhorn supports automatic remount only for the workload pod that is managed by a controller (e.g. deployment, statefulset, daemonset, etc…). See here for details.


© 2019-2024 Longhorn Authors | Documentation Distributed under CC-BY-4.0