所以有:
statefulset.spec.volumeClaimTemplate[]
将前两个绑定在一起。假设我有名为 pv0 和 pv1 的持久卷,以及一个名为 couchdb 的2个副本的状态集。具体而言,statefulset是:
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: couchdb
spec:
...
replicas: 2
template:
...
spec:
containers:
- name: couchdb
image: klaemo/couchdb:1.6
volumeMounts:
- name: db
mountPath: /usr/local/var/lib/couchdb
volumes:
- name: db
persistentVolumeClaim
claimName: db
volumeClaimTemplates:
- metadata:
name: db
spec:
...
此 StatefulSet 生成两个名为 db-couchdb-0 和 db-couchdb-1 的 PersistentVolumeClaim 。问题是不能保证pvc db-couchdb-0 总是绑定到 pv0 。
问题是:如何确保 StatefulSet 控制器管理的 PersistentVolumeClaim 的受控绑定?
我尝试添加这样的音量选择器:
selector:
matchLabels:
name: couchdb
到statefulset.spec.volumeClaimTemplate[0].spec
,但 name 的值不会被模板化。这两个声明最终都会寻找标有 name = couchdb 的 PersistentVolume 。
答案 0 :(得分:0)
答案 1 :(得分:0)
您要查找的是持久性卷中的claimRef
,该持久性卷具有PVC的名称和名称空间,您要将PV绑定到该名称和名称空间。请查看以下json:
Pv-0.json
{
"kind": "PersistentVolume",
"apiVersion": "v1",
"metadata": {
"name": "pv-data-vol-0",
"labels": {
"type": "local"
}
},
"spec": {
"capacity": {
"storage": "10Gi"
},
"accessModes": [
"ReadWriteOnce"
],
"storageClassName": "local-storage",
"local": {
"path": "/prafull/data/pv-0"
},
"claimRef": {
"namespace": "default",
"name": "data-test-sf-0"
},
"nodeAffinity": {
"required": {
"nodeSelectorTerms": [
{
"matchExpressions": [
{
"key": "kubernetes.io/hostname",
"operator": "In",
"values": [
"ip-10-0-1-46.ec2.internal"
]
}
]
}
]
}
}
}
}
Pv-1.json
{
"kind": "PersistentVolume",
"apiVersion": "v1",
"metadata": {
"name": "pv-data-vol-1",
"labels": {
"type": "local"
}
},
"spec": {
"capacity": {
"storage": "10Gi"
},
"accessModes": [
"ReadWriteOnce"
],
"storageClassName": "local-storage",
"local": {
"path": "/prafull/data/pv-1"
},
"claimRef": {
"namespace": "default",
"name": "data-test-sf-1"
},
"nodeAffinity": {
"required": {
"nodeSelectorTerms": [
{
"matchExpressions": [
{
"key": "kubernetes.io/hostname",
"operator": "In",
"values": [
"ip-10-0-1-46.ec2.internal"
]
}
]
}
]
}
}
}
}
Statefulset.json
{
"kind": "StatefulSet",
"apiVersion": "apps/v1beta1",
"metadata": {
"name": "test-sf",
"labels": {
"state": "test-sf"
}
},
"spec": {
"replicas": 2,
"template": {
"metadata": {
"labels": {
"app": "test-sf"
},
"annotations": {
"pod.alpha.kubernetes.io/initialized": "true"
}
}
...
...
},
"volumeClaimTemplates": [
{
"metadata": {
"name": "data"
},
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"storageClassName": "local-storage",
"resources": {
"requests": {
"storage": "10Gi"
}
}
}
}
]
}
}
volumeClaimTemplate
将创建两个PVC test-sf-data-0
和test-sf-data-1
。这两个PV定义包含claimRef
部分,该部分具有应绑定PV的namespace
和PVC name
。请注意,您必须提供namespace
作为必需项,因为PV与名称空间无关,并且在两个不同的名称空间上可能存在两个具有相同名称的PVC。因此,如果我们不提供名称空间名称,那么kubernetes控制器管理器将如何理解应该绑定到哪个PVC,PV。
希望这可以回答您的问题。