hostpath也是本地存储,为什么要使用local模式的存储呢?
因为创建deployment时,无需指定调度具体节点。会自动调度到pv所在的节点运行。
Tip:local模式存储节点异常后,意味着pod启动不起来
1、创建local模式存储类
$ cat <<EOF | kubectl apply -f -
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
EOF
2、创建pv
$ cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolume
metadata:name: test
spec:capacity:storage: 10GivolumeMode: FilesystemaccessModes:- ReadWriteOncepersistentVolumeReclaimPolicy: DeletestorageClassName: local-storagelocal:path: /data/test-local-pvnodeAffinity:required:nodeSelectorTerms:- matchExpressions:- key: kubernetes.io/hostnameoperator: Invalues:- k8s-node01
EOF
3、创建pvc
$ cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: test-local-pvcnamespace: default
spec:storageClassName: local-storagevolumeMode: FilesystemaccessModes: - ReadWriteOnceresources:requests:storage: 10G
EOF
Tip:local模式存储,创建好pv和pvc并不会马上绑定。因为local模式的存储类
volumeBindingMode
的值必须是WaitForFirstConsumer
,所以只有当pod使用pvc时,pv和pvc才进行绑定的。
4、创建业务容器并挂载
$ cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:name: tools
spec:replicas: 1selector:matchLabels:app: toolstemplate:metadata:labels:app: toolsspec:containers:- name: toolsimage: registry.cn-guangzhou.aliyuncs.com/jiaxzeng6918/tools:v1.1volumeMounts:- name: datamountPath: /datavolumes:- name: datapersistentVolumeClaim:claimName: test-local-pvc
EOF
5、查看运行pod运行节点
$ kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tools-6f6f7cd4bf-jjlbg 1/1 Running 0 3m16s 10.244.0.209 k8s-node01 <none> <none>
6、验证pod是否挂载成功
$ kubectl exec -it tools-6f6f7cd4bf-jjlbg -- df -h /data
Filesystem Size Used Avail Use% Mounted on
/dev/vda3 62G 4.1G 58G 7% /data
Tip:local模式存储不能限制存储大小