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: 10Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: /data/test-local-pv
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- k8s-node01
EOF
3、创建pvc
$ cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-local-pvc
namespace: default
spec:
storageClassName: local-storage
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
resources:
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: 1
selector:
matchLabels:
app: tools
template:
metadata:
labels:
app: tools
spec:
containers:
- name: tools
image: registry.cn-guangzhou.aliyuncs.com/jiaxzeng6918/tools:v1.1
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
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模式存储不能限制存储大小