飞道的博客

k8s单机版使用本地存储local-path-provisioner

514人阅读  评论(0)

k8s单机环境中,我们经常使用本机路径作为持久化存储。在k8s官方文档中有两种方式使用本机存储,一种是hostPath,另一种是local volume,这两种都不支持动态扩容,并且程序移植改动比较大,而local-path-provisioner很好的弥补了这一缺陷

local-path-provisioner常见的有两个仓库

rancher

https://github.com/rancher/local-path-provisioner

kubernetes-sigs版

https://github.com/kubernetes-sigs/sig-storage-local-static-provisioner

注意kubernetes-sigs版不支持动态扩容/动态供给dynamically provisioning,所以建议使用rancher版。这里介绍的都是rancher版。

官方的安装和样例使用


   
  1. #安装
  2. kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml
  3. #使用
  4. kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/examples/pvc/pvc.yaml
  5. kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/examples/pod/pod.yaml

默认的存储路径为/opt/local-path-provisioner,默认安装到local-path-storage命名空间

虽然provisioner是安装到local-path-storage命名空间了,那么别的命名空间能用吗?答案是可以的,因为sc是不区分命名空间的,后续pvc都是向sc申请空间的

这里仍然使用之前部署mysql作为例子部署讲解

local-path-storage.yaml


   
  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4. name: local-path-storage
  5. ---
  6. apiVersion: v1
  7. kind: ServiceAccount
  8. metadata:
  9. name: local-path-provisioner-service-account
  10. namespace: local-path-storage
  11. ---
  12. apiVersion: rbac.authorization.k8s.io/v1
  13. kind: ClusterRole
  14. metadata:
  15. name: local-path-provisioner-role
  16. rules:
  17. - apiGroups: [ "" ]
  18. resources: [ "nodes", "persistentvolumeclaims", "configmaps" ]
  19. verbs: [ "get", "list", "watch" ]
  20. - apiGroups: [ "" ]
  21. resources: [ "endpoints", "persistentvolumes", "pods" ]
  22. verbs: [ "*" ]
  23. - apiGroups: [ "" ]
  24. resources: [ "events" ]
  25. verbs: [ "create", "patch" ]
  26. - apiGroups: [ "storage.k8s.io" ]
  27. resources: [ "storageclasses" ]
  28. verbs: [ "get", "list", "watch" ]
  29. ---
  30. apiVersion: rbac.authorization.k8s.io/v1
  31. kind: ClusterRoleBinding
  32. metadata:
  33. name: local-path-provisioner-bind
  34. roleRef:
  35. apiGroup: rbac.authorization.k8s.io
  36. kind: ClusterRole
  37. name: local-path-provisioner-role
  38. subjects:
  39. - kind: ServiceAccount
  40. name: local-path-provisioner-service-account
  41. namespace: local-path-storage
  42. ---
  43. apiVersion: apps/v1
  44. kind: Deployment
  45. metadata:
  46. name: local-path-provisioner
  47. namespace: local-path-storage
  48. spec:
  49. replicas: 1
  50. selector:
  51. matchLabels:
  52. app: local-path-provisioner
  53. template:
  54. metadata:
  55. labels:
  56. app: local-path-provisioner
  57. spec:
  58. serviceAccountName: local-path-provisioner-service-account
  59. containers:
  60. - name: local-path-provisioner
  61. image: rancher/local-path-provisioner:master-head
  62. imagePullPolicy: IfNotPresent
  63. command:
  64. - local-path-provisioner
  65. - --debug
  66. - start
  67. - --config
  68. - /etc/config/config.json
  69. volumeMounts:
  70. - name: config-volume
  71. mountPath: /etc/config/
  72. env:
  73. - name: POD_NAMESPACE
  74. valueFrom:
  75. fieldRef:
  76. fieldPath: metadata.namespace
  77. volumes:
  78. - name: config-volume
  79. configMap:
  80. name: local-path-config
  81. ---
  82. apiVersion: storage.k8s.io/v1
  83. kind: StorageClass
  84. metadata:
  85. name: local-path
  86. provisioner: rancher.io/local-path
  87. volumeBindingMode: WaitForFirstConsumer
  88. reclaimPolicy: Delete
  89. ---
  90. kind: ConfigMap
  91. apiVersion: v1
  92. metadata:
  93. name: local-path-config
  94. namespace: local-path-storage
  95. data:
  96. config.json: |-
  97. {
  98. "nodePathMap":[
  99. {
  100. "node":"DEFAULT_PATH_FOR_NON_LISTED_NODES",
  101. "paths":["/opt/local-path-provisioner"]
  102. }
  103. ]
  104. }
  105. setup: |-
  106. #!/bin/sh
  107. set -eu
  108. mkdir -m 0777 -p "$VOL_DIR"
  109. teardown: |-
  110. #!/bin/sh
  111. set -eu
  112. rm -rf "$VOL_DIR"
  113. helperPod.yaml: |-
  114. apiVersion: v1
  115. kind: Pod
  116. metadata:
  117. name: helper-pod
  118. spec:
  119. containers:
  120. - name: helper-pod
  121. image: busybox
  122. imagePullPolicy: IfNotPresent

mysql-pvc.yaml


   
  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: mysql-pvc
  5. namespace: default
  6. spec:
  7. accessModes:
  8. - ReadWriteOnce #在rancher/local-path-provisioner里,这里不能用ReadWriteMany
  9. storageClassName: local-path #此处为你命名的StorageClass name
  10. resources:
  11. requests:
  12. storage: 4Gi

mysql-config.yaml


   
  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: mysql-config
  5. namespace: default
  6. data:
  7. my.cnf: |
  8. [client]
  9. default-character-set=utf8mb4
  10. [mysql]
  11. default-character-set=utf8mb4
  12. [mysqld]
  13. max_connections = 2000
  14. secure_file_priv=/var/lib/mysql
  15. sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

mysql-deploy.yaml


   
  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: mysql-deploy
  6. namespace: default
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: mysql-deploy
  11. template:
  12. metadata:
  13. labels:
  14. app: mysql-deploy
  15. spec:
  16. containers:
  17. - name: mysql-deploy
  18. image: mysql:8.0.28
  19. args:
  20. - --default_authentication_plugin=mysql_native_password
  21. - --character-set-server=utf8mb4
  22. - --collation-server=utf8mb4_unicode_ci
  23. env:
  24. - name: MYSQL_ROOT_PASSWORD
  25. value: "12345678"
  26. ports:
  27. - containerPort: 3306
  28. volumeMounts:
  29. - name: mysql-persistent-storage
  30. mountPath: /var/lib/mysql
  31. - name: mysql-config
  32. mountPath: /etc/mysql/conf.d/my.cnf
  33. volumes:
  34. - name: mysql-persistent-storage
  35. persistentVolumeClaim:
  36. claimName: mysql-pvc
  37. - name: mysql-config
  38. configMap:
  39. name: mysql-config
  40. ---
  41. apiVersion: v1
  42. kind: Service
  43. metadata:
  44. name: mysql-deploy
  45. namespace: default
  46. spec:
  47. type: NodePort
  48. ports:
  49. - port: 3306
  50. targetPort: 3306
  51. nodePort: 31111
  52. selector:
  53. app: mysql-deploy

安装


   
  1. kubectl apply -f local-path-storage.yaml
  2. kubectl apply -f mysql-pvc.yaml
  3. kubectl apply -f mysql-config.yaml
  4. kubectl apply -f mysql-deploy.yaml

卸载


   
  1. kubectl delete -f mysql-deploy.yaml
  2. kubectl delete -f mysql-config.yaml
  3. kubectl delete -f mysql-pvc.yaml
  4. kubectl delete -f local-path-storage.yaml


转载:https://blog.csdn.net/gsls200808/article/details/128767940
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场