k8s pv 和 PVC 为何绑定不上


?使用statefuset 部署有状态应用,应用总是处于pending 状态,在开始之前先介绍什么是statefuset, 在 k8s 中一般用 deployment? 管理无状态应用,statefuset 用来管理有状态应用,如 redis 、mysql 、zookper 等分布式应用,这些应用的启动停止都会有严格的顺序?


一、statefulset??

  • headless (无头服务),没有cluserIP, 资源标识符,用于生成可解析的dns 记录?

  • StatefulSet 用于pod 资源的管理

  • volumeClaimTemplates? 提供存储


二、statefulset? 部署

  • 使用nfs 做网络存储

  • 搭建nfs

  • 配置共享存储目录

  • 创建pv

  • 编排 yaml?



? ? 搭建nfs?

? ? ?yum install nfs-utils -y?

? ? mkdir -p /usr/local/k8s/redis/pv{7..12} # 创建挂载目录

?cat?/etc/exports
?
?/usr/local/k8s/redis/pv7?172.16.0.0/16(rw,sync,no_root_squash)
?/usr/local/k8s/redis/pv8?172.16.0.0/16(rw,sync,no_root_squash)
?/usr/local/k8s/redis/pv9?172.16.0.0/16(rw,sync,no_root_squash)
?/usr/local/k8s/redis/pv10?172.16.0.0/16(rw,sync,no_root_squash)
?/usr/local/k8s/redis/pv11?172.16.0.0/16(rw,sync,no_root_squash

??exportfs?-avr

? ? 创建pv?

? ?cat?nfs_pv2.yaml

apiVersion:?v1
kind:?PersistentVolume
metadata:
??name:?nfs-pv7
spec:
??capacity:
????storage:?500M
??accessModes:
????-?ReadWriteMany
??persistentVolumeReclaimPolicy:?Retain
??storageClassName:?slow
??nfs:
????server:?172.16.0.59
????path:?"/usr/local/k8s/redis/pv7"
---
apiVersion:?v1
kind:?PersistentVolume
metadata:
??name:?nfs-pv8

spec:
??capacity:
????storage:?500M
??accessModes:
????-?ReadWriteMany
??storageClassName:?slow
??persistentVolumeReclaimPolicy:?Retain
??nfs:
????server:?172.16.0.59
????path:?"/usr/local/k8s/redis/pv8"
---
apiVersion:?v1
kind:?PersistentVolume
metadata:
??name:?nfs-pv9

spec:
??capacity:
????storage:?500M
??accessModes:
????-?ReadWriteMany
??storageClassName:?slow
??persistentVolumeReclaimPolicy:?Retain
??nfs:
????server:?172.16.0.59
????path:?"/usr/local/k8s/redis/pv9"
---
apiVersion:?v1
kind:?PersistentVolume
metadata:
??name:?nfs-pv10

spec:
??capacity:
????storage:?500M
??accessModes:
????-?ReadWriteMany
??storageClassName:?slow
??persistentVolumeReclaimPolicy:?Retain
??nfs:
????server:?172.16.0.59
????path:?"/usr/local/k8s/redis/pv10"
---
apiVersion:?v1
kind:?PersistentVolume
metadata:
??name:?nfs-pv11

spec:
??capacity:
????storage:?500M
??accessModes:
????-?ReadWriteMany
??storageClassName:?slow
??persistentVolumeReclaimPolicy:?Retain
??nfs:
????server:?172.16.0.59
????path:?"/usr/local/k8s/redis/pv11"
---
apiVersion:?v1
kind:?PersistentVolume
metadata:
??name:?nfs-pv12

spec:
??capacity:
????storage:?500M
??accessModes:
????-?ReadWriteMany
??storageClassName:?slow
??persistentVolumeReclaimPolicy:?Retain
??nfs:
????server:?172.16.0.59
????path:?"/usr/local/k8s/redis/pv12"

? ?kubectl apply -f nfs_pv2.yaml

? ?

?查看 # 创建成功

?

? 编写yaml 编排应用

apiVersion:?v1
kind:?Service
metadata:
??name:?myapp
??labels:
????app:?myapp
spec:
??ports:
??-?port:?80
????name:?web
??clusterIP:?None
??selector:
????app:?myapp-pod
---
apiVersion:?apps/v1
kind:?StatefulSet
metadata:
??name:?myapp
spec:
??serviceName:?myapp
??replicas:?3
??selector:
????matchLabels:
??????app:?myapp-pod
??template:
????metadata:
??????labels:
????????app:?myapp-pod
????spec:
??????containers:
??????-?name:?myapp
????????image:?ikubernetes/myapp:v1
????????resources:
??????????requests:
????????????cpu:?"500m"
????????????memory:?"500Mi"
????????ports:
????????-?containerPort:?80
??????????name:?web
????????volumeMounts:
????????-?name:?myappdata
??????????mountPath:?/usr/share/nginx/html
??volumeClaimTemplates:
??-?metadata:
??????name:?myappdata
????spec:
??????accessModes:?["ReadWriteOnce"]
??????storageClassName:?"slow"
??????resources:
????????requests:
??????????storage:?400Mi

?

??kubectl create -f new-stateful.yaml?

??

? 查看headless 创建成功



解析的规则 如下

? ? ? ? ??myapp-0? ? ? ?myapp? ? ? ? ? ? ? ? ? ? ? ? ?daemon??

FQDN: $(podname).(headless server name).namespace.svc.cluster.local


容器里面如没有nsllokup ,需要安装对应的包,busybox 可以提供类似的功能

?提供yaml 文件

apiVersion:?v1
kind:?Pod
metadata:
??name:?busybox
??namespace:?daemon
spec:
??containers:
??-?name:?busybox
????image:?busybox:1.28.4
????command:
??????-?sleep
??????-?"7600"
????resources:
??????requests:
????????memory:?"200Mi"
????????cpu:?"250m"
????imagePullPolicy:?IfNotPresent
??restartPolicy:?Never