节点本地: hostPath emptyDir
网络:NFS GFS Ceph Openebs
k8s资源: configMap Secret
是一个临时存储卷(pod所在节点),与pod生命周期绑定,如果pod删除卷也会被删除
pod中容器之间数据共享
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: write # 业务容器(写数据)
image: centos:7
command: ["bash","-c","for i in {1..100};do echo $i >>/data/hello;sleep 1;done"]
volumeMounts:
- name: data
mountPath: /data
- name: read # 辅助容器(读数据)
image: centos
command: ["bash","-c","tail -f /data/hello"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
emptyDir: {}
挂载node文件系统(pod所在节点)上文件或者目录到pod中的容器,pod删除后数据会持久化在宿主机上
pod中容器需要访问宿主机文件(cadvisor暴露指标、jenkins打包镜像)
apiVersion: v1
kind: Pod
metadata:
name: testpod
spec:
containers:
- name: test
image: busybox
args:
- /bin/sh
- -c
- sleep 36000
volumeMounts:
- name: data
mountPath: /project #挂载到容器中目录
- name: data2
mountPath: /project2
volumes:
- name: data
hostPath:
path: /tmp #宿主机目录
type: Directory #挂载卷的类型为目录
- name: data2
hostPath:
path: /etc
type: Directory
apiVersion: v1
kind: ConfigMap
metadata:
name: java-demo-config
data:
config: |
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://java-demo-db:3306/test?characterEncoding=utf-8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
freemarker:
allow-request-override: false
cache: true
check-template-location: true
charset: UTF-8
content-type: text/html; charset=utf-8
expose-request-attributes: false
expose-session-attributes: false
expose-spring-macro-helpers: false
suffix: .ftl
template-loader-path:
- classpath:/templates/ #到这都是配置文件
使用场景
场景一:一个共享卷,挂载多个路径。
场景二:ConfigMap或Secret挂载到特定目录的特定路径, 而该目录下已经有其他文件且不希望被覆盖掉。可以通过 subPath 挂载卷的子目录,而不是根目录。
volumeMounts:
- name: config #挂载数据卷的名字
mountPath: "/usr/local/tomcat/webapps/ROOT/WEB-INF/classes/application.yml" #挂载到容器中的目标目录
subPath: application.yml #subpath代表远端存储子路径
volumes:
- name: config #数据卷名
configMap: #类型
name: java-demo-config
items:
- key: "config" #ConfigMap中key config中的内容被挂载到pod的mountPath路径
path: "application.yml" #路径由 spec.containers 中参数 volumeMounts.mountpath 和 volumes 中参数 items.path 拼接而成
cat nacos-env-cm.yaml
apiVersion: v1
data:
nacos_addr: http://nacos-cs:8848
nacos_ns_uc: 845a984f-9b16-46e3-a396-78f84ba669d7
nacos_ns_vsoc_csa: 845a984f-9b16-46e3-a396-78f84ba669d7
nacos_ns_vsoc_front: 614a56fc-e64f-4904-b0f1-635c66f2f82f
kind: ConfigMap
metadata:
name: nacos-env
cat pod.yaml
spec:
containers:
- env:
- name: TZ
value: Asia/Shanghai
envFrom:
- configMapRef:
name: nacos-env
加密:echo admin | base64
YWRtaW4K
解密:echo YWRtaW4K | base64 -d
admin
创建
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
password: MWYyZDFlMmU2N2Rm
username: YWRtaW4=
#将Secret挂载到环境变量中
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-deployment
spec:
replicas: 2
selector:
matchLabels:
app: wordpress
strategy:
type: RollingUpdate
template:
metadata:
labels:
app: wordpress
visualize: "true"
spec:
containers:
- name: "wordpress"
image: "wordpress"
ports:
- containerPort: 80
env:
- name: WORDPRESS_DB_USER
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
kubectl create secret docker-registry harbor-auth --docker-username=admin --docker-password=Harbor12345 --docker-server=reg.ale.com -n dev
yaml中应用imagePullSecrets
spec:
replicas: REPLICAS
selector:
matchLabels:
project: PROJECT
app: APP_NAME
template:
metadata:
labels:
project: PROJECT
app: APP_NAME
spec:
imagePullSecrets:
- name: "harbor-auth"
持久数据卷,对存储资源的抽象,使得存储作为集群中的资源管理
持久数据卷申请,用户定义使用的存储容量,使得用户不需要关心后端存储实现
Pod申请PVC作为卷来使用,Kubernetes通过PVC查找绑定的PV,并挂载到Pod中供程序使用
ACCESS MODES(访问模式):
AccessModes 是用来对 PV 进行访问模式的设置,用于描述用户应用对存储资源的访问权限,访问权限包括下面几种方式:
• ReadWriteOnce(RWO):读写权限,但是只能被单个节点挂载
• ReadOnlyMany(ROX):只读权限,可以被多个节点挂载
• ReadWriteMany(RWX):读写权限,可以被多个节点挂载
RECLAIM POLICY(回收策略):
目前 PV 支持的策略有三种:
• Retain(保留): 保留数据,需要管理员手工清理数据
• Recycle(回收):清除 PV 中的数据,效果相当于执行 rm -rf /ifs/kuberneres/*
• Delete(删除):与 PV 相连的后端存储同时删除
修改回收策略:
spec:
......
persistentVolumeReclaimPolicy: Recycle
......
STATUS(状态):
一个 PV 的生命周期中,可能会处于4中不同的阶段:
• Available(可用):表示可用状态,还未被任何 PVC 绑定
• Bound(已绑定):表示 PV 已经被 PVC 绑定
• Released(已释放):PVC 被删除,但是资源还未被集群重新声明
• Failed(失败): 表示该 PV 的自动回收失败
创建pv
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv1
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
nfs:
path: /nfs/kubernetes
server: 192.168.200.1
创建pod使用pvc
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: web-pv
name: web-pv
spec:
selector:
matchLabels:
app: web-pv
template:
metadata:
labels:
app: web-pv
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumes:
- name: www
persistentVolumeClaim:
claimName: my-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
cat > web-sc.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: web-sc
name: web-sc
spec:
selector:
matchLabels:
app: web-sc
template:
metadata:
labels:
app: web-sc
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumes:
- name: www
persistentVolumeClaim:
claimName: my-pvc3
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc3
spec:
storageClassName: "managed-nfs-storage"
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
EOF
存储类级别,创建后不能更新
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
archiveOnDelete: "false" #是启用数据卷归档,pod删除时,pv打包成压缩包
allowVolumeExpansion: true #是否允许扩容
reclaimPolicy: #可选参数,Delete、Retain,pvc和pod删除时,删除还是保留pv,默认为Delete
pvc级别
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
对已经创建的pv更新保留策略
kubectl patch pv pvc-99648dc8-de7d-49e2-81ed-433989ba33ad -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
前提是存储类支持扩容
对pvc进行扩容时,只需要修改pvc的定义,将resources.requests.storage设置为一个更大的值即可。