1、pv 简单介绍

PersistenVolume(PV):对存储资源创建和使用的抽象,使得存储作为集群中的资源管理 PV分为静态和动态,动态能够自动创建PV • PersistentVolumeClaim(PVC):让用户不需要关心具体的Volume实现细节 容器与PV、PVC之间的关系,可以如下图所示: 总的来说,PV是提供者,PVC是消费者,消费的过程就是绑定。 参考网址:1、https://www.cnblogs.com/weifeng1463/p/10037803.html  2、https://blog.csdn.net/qq_25611295/article/details/86065053

2、nfs 搭建:

  1. yum install nfs-utils
  2.  
  3. vim /etc/exports
  4. /data/k8s/ 172.16.1.0/24(sync,rw,no_root_squash)
  5.  
  6. systemctl start nfs; systemctl start rpcbind
  7. systemctl enable nfs
  8.  
  9. 测试:
  10. yum install nfs-utils
  11. showmount -e 172.16.1.131

3、PersistentVolume 静态绑定 (手工创建PV、PVC)

  1. [root@VM_0_48_centos prometheus]# cat mypv.yaml
  2. apiVersion: v1
  3. kind: PersistentVolume
  4. metadata:
  5. name: pv001
  6. spec:
  7. capacity:
  8. storage: 10Gi
  9. accessModes:
  10. - ReadWriteMany
  11. nfs:
  12. path: /data/k8s
  13. server: 172.19.0.14
  14.  
  15. [root@VM_0_48_centos prometheus]# cat mypvc.yaml ###会根据大小和类型自动匹配到上面的PV
  16. kind: PersistentVolumeClaim
  17. apiVersion: v1
  18. metadata:
  19. namespace: kube-system
  20. name: prometheus-claim
  21. spec:
  22. accessModes:
  23. - ReadWriteMany
  24. resources:
  25. requests:
  26. storage: 10Gi
  27.  
  28. [root@VM_0_48_centos prometheus]# kubectl get pv,pvc -n kube-system
  29. NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
  30. persistentvolume/pv001 10Gi RWX Retain Bound kube-system/prometheus-claim 17m
  31.  
  32. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  33. persistentvolumeclaim/prometheus-claim Bound pv001 10Gi RWX

4、PersistentVolume 静态PVC使用案例

  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. name: prometheus
  5. namespace: kube-system
  6. labels:
  7. k8s-app: prometheus
  8. kubernetes.io/cluster-service: "true"
  9. addonmanager.kubernetes.io/mode: Reconcile
  10. version: v2.2.1
  11. spec:
  12. serviceName: "prometheus"
  13. replicas: 1
  14. podManagementPolicy: "Parallel"
  15. updateStrategy:
  16. type: "RollingUpdate"
  17. selector:
  18. matchLabels:
  19. k8s-app: prometheus
  20. template:
  21. metadata:
  22. labels:
  23. k8s-app: prometheus
  24. annotations:
  25. scheduler.alpha.kubernetes.io/critical-pod: ''
  26. spec:
  27. priorityClassName: system-cluster-critical
  28. serviceAccountName: prometheus
  29. initContainers:
  30. - name: "init-chown-data"
  31. image: "busybox:latest"
  32. imagePullPolicy: "IfNotPresent"
  33. command: ["chown", "-R", "65534:65534", "/data"]
  34. volumeMounts:
  35. - name: prometheus-data
  36. mountPath: /data
  37. subPath: ""
  38. containers:
  39. - name: prometheus-server-configmap-reload
  40. image: "jimmidyson/configmap-reload:v0.1"
  41. imagePullPolicy: "IfNotPresent"
  42. args:
  43. - --volume-dir=/etc/config
  44. - --webhook-url=http://localhost:9090/-/reload
  45. volumeMounts:
  46. - name: config-volume
  47. mountPath: /etc/config
  48. readOnly: true
  49. resources:
  50. limits:
  51. cpu: 10m
  52. memory: 10Mi
  53. requests:
  54. cpu: 10m
  55. memory: 10Mi
  56.  
  57. - name: prometheus-server
  58. image: "prom/prometheus:v2.2.1"
  59. imagePullPolicy: "IfNotPresent"
  60. args:
  61. - --config.file=/etc/config/prometheus.yml
  62. - --storage.tsdb.path=/data
  63. - --web.console.libraries=/etc/prometheus/console_libraries
  64. - --web.console.templates=/etc/prometheus/consoles
  65. - --web.enable-lifecycle
  66. ports:
  67. - containerPort: 9090
  68. readinessProbe:
  69. httpGet:
  70. path: /-/ready
  71. port: 9090
  72. initialDelaySeconds: 30
  73. timeoutSeconds: 30
  74. livenessProbe:
  75. httpGet:
  76. path: /-/healthy
  77. port: 9090
  78. initialDelaySeconds: 30
  79. timeoutSeconds: 30
  80. # based on 10 running nodes with 30 pods each
  81. resources:
  82. limits:
  83. cpu: 200m
  84. memory: 1000Mi
  85. requests:
  86. cpu: 200m
  87. memory: 1000Mi
  88.  
  89. volumeMounts:
  90. - name: config-volume
  91. mountPath: /etc/config
  92. - name: prometheus-data
  93. mountPath: /data
  94. subPath: ""
  95. terminationGracePeriodSeconds: 300
  96. volumes:
  97. - name: config-volume
  98. configMap:
  99. name: prometheus-config
  100. - name: prometheus-data
  101. persistentVolumeClaim: #申明使用静态PVC永久化存储
  102. claimName: prometheus-claim

  

5、动态PV,K8S调用资源对象自动创建PV。生产环境常用

当我们k8s业务上来的时候,大量的pvc,此时我们人工创建匹配的话,工作量就会非常大了,需要动态的自动挂载相应的存储。

我们需要使用到StorageClass,来对接存储,靠他来自动关联pvc,并创建pv。 Kubernetes支持动态供给的存储插件: https://kubernetes.io/docs/concepts/storage/storage-classes/ 因为NFS不支持动态存储,所以我们需要借用这个存储插件。 nfs动态相关部署可以参考: https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client/deploy

6、存储对象申明和授权。

  1. 定义一个storage
  2. [root@VM_0_48_centos prometheus]# cat storageclass-nfs.yaml
  3. apiVersion: storage.k8s.io/v1beta1
  4. kind: StorageClass
  5. metadata:
  6. name: managed-nfs-storage
  7. provisioner: fuseim.pri/ifs
  8.  
  9. 因为storage自动创建pv需要经过kube-apiserver,所以要进行授权
  10. [root@VM_0_48_centos prometheus]# cat storageclass-rbac.yaml
  11. apiVersion: v1
  12. kind: ServiceAccount
  13. metadata:
  14. name: nfs-client-provisioner
  15.  
  16. ---
  17.  
  18. kind: ClusterRole
  19. apiVersion: rbac.authorization.k8s.io/v1beta1
  20. metadata:
  21. name: nfs-client-provisioner-runner
  22. rules:
  23. - apiGroups: [""]
  24. resources: ["persistentvolumes"]
  25. verbs: ["get", "list", "watch", "create", "delete"]
  26. - apiGroups: [""]
  27. resources: ["persistentvolumeclaims"]
  28. verbs: ["get", "list", "watch", "update"]
  29. - apiGroups: ["storage.k8s.io"]
  30. resources: ["storageclasses"]
  31. verbs: ["get", "list", "watch"]
  32. - apiGroups: [""]
  33. resources: ["events"]
  34. verbs: ["list", "watch", "create", "update", "patch"]
  35.  
  36. ---
  37.  
  38. kind: ClusterRoleBinding
  39. apiVersion: rbac.authorization.k8s.io/v1beta1
  40. metadata:
  41. name: run-nfs-client-provisioner
  42. subjects:
  43. - kind: ServiceAccount
  44. name: nfs-client-provisioner
  45. namespace: default
  46. roleRef:
  47. kind: ClusterRole
  48. name: nfs-client-provisioner-runner
  49. apiGroup: rbac.authorization.k8s.io
  50.  
  51. 部署一个自动创建pv的服务
  52. [root@VM_0_48_centos prometheus]# cat prometheus-statefulset.yaml
  53. apiVersion: apps/v1
  54. kind: StatefulSet
  55. metadata:
  56. name: prometheus
  57. namespace: kube-system
  58. labels:
  59. k8s-app: prometheus
  60. kubernetes.io/cluster-service: "true"
  61. addonmanager.kubernetes.io/mode: Reconcile
  62. version: v2.2.1
  63. spec:
  64. serviceName: "prometheus"
  65. replicas: 1
  66. podManagementPolicy: "Parallel"
  67. updateStrategy:
  68. type: "RollingUpdate"
  69. selector:
  70. matchLabels:
  71. k8s-app: prometheus
  72. template:
  73. metadata:
  74. labels:
  75. k8s-app: prometheus
  76. annotations:
  77. scheduler.alpha.kubernetes.io/critical-pod: ''
  78. spec:
  79. priorityClassName: system-cluster-critical
  80. serviceAccountName: prometheus
  81. initContainers:
  82. - name: "init-chown-data"
  83. image: "busybox:latest"
  84. imagePullPolicy: "IfNotPresent"
  85. command: ["chown", "-R", "65534:65534", "/data"]
  86. volumeMounts:
  87. - name: prometheus-data
  88. mountPath: /data
  89. subPath: ""
  90. containers:
  91. - name: prometheus-server-configmap-reload
  92. image: "jimmidyson/configmap-reload:v0.1"
  93. imagePullPolicy: "IfNotPresent"
  94. args:
  95. - --volume-dir=/etc/config
  96. - --webhook-url=http://localhost:9090/-/reload
  97. volumeMounts:
  98. - name: config-volume
  99. mountPath: /etc/config
  100. readOnly: true
  101. resources:
  102. limits:
  103. cpu: 10m
  104. memory: 10Mi
  105. requests:
  106. cpu: 10m
  107. memory: 10Mi
  108.  
  109. - name: prometheus-server
  110. image: "prom/prometheus:v2.2.1"
  111. imagePullPolicy: "IfNotPresent"
  112. args:
  113. - --config.file=/etc/config/prometheus.yml
  114. - --storage.tsdb.path=/data
  115. - --web.console.libraries=/etc/prometheus/console_libraries
  116. - --web.console.templates=/etc/prometheus/consoles
  117. - --web.enable-lifecycle
  118. ports:
  119. - containerPort: 9090
  120. readinessProbe:
  121. httpGet:
  122. path: /-/ready
  123. port: 9090
  124. initialDelaySeconds: 30
  125. timeoutSeconds: 30
  126. livenessProbe:
  127. httpGet:
  128. path: /-/healthy
  129. port: 9090
  130. initialDelaySeconds: 30
  131. timeoutSeconds: 30
  132. # based on 10 running nodes with 30 pods each
  133. resources:
  134. limits:
  135. cpu: 200m
  136. memory: 1000Mi
  137. requests:
  138. cpu: 200m
  139. memory: 1000Mi
  140.  
  141. volumeMounts:
  142. - name: config-volume
  143. mountPath: /etc/config
  144. - name: prometheus-data
  145. mountPath: /data
  146. subPath: ""
  147. terminationGracePeriodSeconds: 300
  148. volumes:
  149. - name: config-volume
  150. configMap:
  151. name: prometheus-config
  152. - name: prometheus-data
  153. persistentVolumeClaim:
  154. claimName: prometheus-claim

7、效果测试

  1. [root@VM_0_48_centos prometheus]# cat test.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: nginx
  6. labels:
  7. app: nginx
  8. spec:
  9. ports:
  10. - port: 80
  11. name: web
  12. clusterIP: None
  13. selector:
  14. app: nginx
  15. ---
  16. apiVersion: apps/v1
  17. kind: StatefulSet
  18. metadata:
  19. name: web
  20. spec:
  21. serviceName: "nginx"
  22. replicas: 3
  23. selector:
  24. matchLabels:
  25. app: nginx
  26. template:
  27. metadata:
  28. labels:
  29. app: nginx
  30. spec:
  31. containers:
  32. - name: nginx
  33. image: nginx
  34. ports:
  35. - containerPort: 80
  36. name: web
  37. volumeMounts:
  38. - name: www
  39. mountPath: /usr/share/nginx/html
  40. volumeClaimTemplates:
  41. - metadata:
  42. name: www
  43. spec:
  44. accessModes: [ "ReadWriteOnce" ]
  45. storageClassName: "managed-nfs-storage"
  46. resources:
  47. requests:
  48. storage: 1Gi
  49.  
  50. kubectl exec -it web-0 sh
  51. # cd /usr/share/nginx/html
  52. # touch 1.txt

  

k8s-静态PV和动态PV的更多相关文章

  1. 4.k8s存储之Volume、PV、PVC和StatefulSet

    3.Volume 容器磁盘上的文件的生命周期是短暂的,这就使得在容器中运行重要应用时会出现一些问题.首先,当容器崩溃时,kubelet 会重启它,但是容器中的文件将丢失--容器以干净的状态(镜像最初的 ...

  2. Kubernetes 动态PV使用

    Kubernetes 动态PV使用 Kubernetes支持动态供给的存储插件:https://kubernetes.io/docs/concepts/storage/storage-classes/ ...

  3. 8.3 k8s部署jenkins,通过pv/pvc结合NFS服务器持久化

    1.制作jenkins docker镜像 1.1 下载jenkins wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.30 ...

  4. Android GradientDrawable(shape标签定义) 静态使用和动态使用(圆角,渐变实现)

    Android GradientDrawable使用优势: 1. 快速实现一些基本图形(线,矩形,圆,椭圆,圆环) 2. 快速实现一些圆角,渐变,阴影等效果 3. 代替图片设置为View的背景 4. ...

  5. C++ 系列:静态库与动态库

    转载自http://www.cnblogs.com/skynet/p/3372855.html 这次分享的宗旨是——让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择 ...

  6. Android开发4: Notification编程基础、Broadcast的使用及其静态注册、动态注册方式

    前言 啦啦啦~(博主每次开篇都要卖个萌,大家是不是都厌倦了呢~) 本篇博文希望帮助大家掌握 Broadcast 编程基础,实现动态注册 Broadcast 和静态注册 Broadcast 的方式以及学 ...

  7. C++静态库与动态库

    C++静态库与动态库 这次分享的宗旨是--让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择.这里不深入介绍静态库.动态库的底层格式,内存布局等,有兴趣的同学,推荐一 ...

  8. Linux下Gcc生成和使用静态库和动态库详解(转)

    一.基本概念 1.1什么是库 在windows平台和linux平台下都大量存在着库. 本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行. 由于windows和linux的平台不同( ...

  9. VS中Debug和Realease、及静态库和动态库的区别整理(转)

    原文出自:http://www.cnblogs.com/chensu/p/5632486.html 一.Debug和Realease区别产生的原因 Debug 通常称为调试版本,它包含调试信息,并且不 ...

随机推荐

  1. linux主机名称文件修改

    目录 一:linux主机名称查看与修改 1.查看自己主机名: 3.临时修改 4.永久修改 一:linux主机名称查看与修改 1.查看自己主机名: [root@localhost ~]# echo $H ...

  2. 在海外上传文件到中国AWS S3

    s3cmd --access_key= --secret_key=xxxx --region=cn-north-1 --host=s3.cn-north-1.amazonaws.com.cn --ho ...

  3. Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能

    Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能 背景 注册-登录-修改密码⼀般需要发送验证码,但是容易被 攻击恶意调⽤ 什么是短信-邮箱轰炸机 手机短信轰炸机是批.循环给 ...

  4. Java IO: ByteArrayOutputStream使用

    感谢原文作者:小思思smile 原文链接:https://blog.csdn.net/u014049880/article/details/52329333/ 更多请查阅Java API文档! 在创建 ...

  5. 什么是Native方法 (转)

    一个Native Method就是一个java调用非java代码的接口(NDK也跟这有关吗?(疑问)一个Native Method由非java语言实现 在定义一个native method时,并不提供 ...

  6. libcurl库openssl编译

    openssl编译 win32: perl Configure  no-shared VC-WIN32 --prefix=E:/lib/openssl-1.1.1 linux: ./config -f ...

  7. 浮动float、浮动影响和清除浮动

    普通流(normal flow) 这个单词很多人翻译为 文档流 , 字面翻译 普通流 或者标准流都可以. 前面我们说过,网页布局的核心,就是用CSS来摆放盒子位置.如何把盒子摆放到合适的位置? CSS ...

  8. laravel中closure和curry 科里化函数式编程

    推荐值得的一看博客文档:谢谢作者  : https://my.oschina.net/zhmsong 函数式编程curry的概念: 只传递给函数一部分参数来调用函数,然后返回一个函数去处理剩下的参数. ...

  9. onclick="func()"和 onclick = "return func()"区别

    onclick="func()" 表示只会执行 func , 但是不会传回 func 中之回传值onclick = "return func()" 则是 执行 ...

  10. Web集群调度器-Haproxy

    Web集群调度器-Haproxy 目录 Web集群调度器-Haproxy 一.Web集群调度器 1.常用的Web集群调度器 2. Haproxy应用分析 3. Haproxy的主要特性 4. 常用集群 ...