CSI Persistent Volume

  1. kind: PersistentVolume
  2. metadata:
  3. name: longhorn-vol-pv
  4. spec:
  5. capacity:
  6. storage: 2Gi
  7. volumeMode: Filesystem
  8. accessModes:
  9. - ReadWriteOnce
  10. persistentVolumeReclaimPolicy: Delete
  11. storageClassName: longhorn
  12. csi:
  13. driver: driver.longhorn.io
  14. fsType: ext4
  15. volumeAttributes:
  16. numberOfReplicas: '3'
  17. staleReplicaTimeout: '2880'
  18. volumeHandle: existing-longhorn-volume
  19. ---
  20. apiVersion: v1
  21. kind: PersistentVolumeClaim
  22. metadata:
  23. name: longhorn-vol-pvc
  24. spec:
  25. accessModes:
  26. - ReadWriteOnce
  27. resources:
  28. requests:
  29. storage: 2Gi
  30. volumeName: longhorn-vol-pv
  31. storageClassName: longhorn
  32. ---
  33. apiVersion: v1
  34. kind: Pod
  35. metadata:
  36. name: volume-pv-test
  37. namespace: default
  38. spec:
  39. restartPolicy: Always
  40. containers:
  41. - name: volume-pv-test
  42. image: nginx:stable-alpine
  43. imagePullPolicy: IfNotPresent
  44. livenessProbe:
  45. exec:
  46. command:
  47. - ls
  48. - /data/lost+found
  49. initialDelaySeconds: 5
  50. periodSeconds: 5
  51. volumeMounts:
  52. - name: vol
  53. mountPath: /data
  54. ports:
  55. - containerPort: 80
  56. volumes:
  57. - name: vol
  58. persistentVolumeClaim:
  59. 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. apiVersion: apps/v1
  26. kind: Deployment
  27. metadata:
  28. name: mysql
  29. labels:
  30. app: mysql
  31. spec:
  32. selector:
  33. matchLabels:
  34. app: mysql # has to match .spec.template.metadata.labels
  35. strategy:
  36. template:
  37. metadata:
  38. labels:
  39. app: mysql
  40. spec:
  41. restartPolicy: Always
  42. containers:
  43. - image: mysql:5.6
  44. name: mysql
  45. livenessProbe:
  46. exec:
  47. command:
  48. - ls
  49. - /var/lib/mysql/lost+found
  50. initialDelaySeconds: 5
  51. periodSeconds: 5
  52. env:
  53. - name: MYSQL_ROOT_PASSWORD
  54. value: changeme
  55. ports:
  56. - containerPort: 3306
  57. name: mysql
  58. volumeMounts:
  59. - name: mysql-volume
  60. mountPath: /var/lib/mysql
  61. env:
  62. - name: MYSQL_ROOT_PASSWORD
  63. value: "rancher"
  64. volumes:
  65. - name: mysql-volume
  66. persistentVolumeClaim:
  67. claimName: mysql-pvc

Restore to File

  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. imagePullPolicy: IfNotPresent
  11. livenessProbe:
  12. exec:
  13. command:
  14. - ls
  15. initialDelaySeconds: 5
  16. periodSeconds: 5
  17. volumeMounts:
  18. - name: volv
  19. mountPath: /data
  20. ports:
  21. - containerPort: 80
  22. volumes:
  23. - name: volv
  24. persistentVolumeClaim:
  25. claimName: longhorn-simple-pvc

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. # ]'