用例如下:
因此,我们有几个pod使用相同的persistentVolumeClaim,并将accessMode设置为ReadWriteOnce (因为PersistentVolume的存储类只支持ReadWriteOnce)。
来自https://kubernetes.io/docs/concepts/storage/persistent-volumes/,
ReadWriteOnce -- the volume can be mounted as read-write by a single node因此,这些pod应该部署在同一个节点上,以便访问PVC (否则它们将失败)。
我想问一下,是否有任何方法可以配置部署yaml文件,以便它们可以部署在同一个节点上?或者有什么方法可以解决这个问题?
发布于 2020-12-17 09:58:15
使用Chin建议的pod间亲和性解决方案,我能够解决这个问题:
以下是我的Deployment yaml文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-go
namespace: stage
labels:
app: test-go
spec:
replicas: 1
selector:
matchLabels:
app: test-go
template:
metadata:
labels:
app: test-go
service: git
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: service
operator: In
values:
- git
topologyKey: kubernetes.io/hostname
containers:
- name: test-go
image: registry.gitlab.com/xxxxxxx/test-go
imagePullSecrets:
- name: registry-pull-secret在Deployment yaml文件中,在pod模板spec.template.metadata.labels中设置标签,然后根据添加的标签添加podAffinity配置,将topologyKey设置为kubernetes.io/hostname,这样pod将部署在同一节点上。
发布于 2020-12-16 22:05:06
Chin已经提到了inter-pod affinity,这是一个有效的解决方案,但我想再提一个解决方案,可能有一点争议,但在相同的情况下仍然有效。
如果你必须在一个节点上运行所有的容器,你可以将所有这些容器放在一个pod中,它们总是在一起,最重要的是,你可以毫无困难地将持久卷挂载到它上。只需记住,过度使用它被认为是不好的做法;总是喜欢每个pod一个容器,而不是每个pod多个容器,尽管您的用例可能是该规则的一个例外,但对我来说很难说,因为我对这些pod一无所知。
阅读更多关于How pods manage multiple container的内容
https://stackoverflow.com/questions/65313780
复制相似问题