问题
The use case is like this:
So we have several pods using the same persistentVolumeClaim
with the accessMode
set to ReadWriteOnce
(because the storage class of the PersistentVolume
only support ReadWriteOnce
).
From https://kubernetes.io/docs/concepts/storage/persistent-volumes/,
ReadWriteOnce -- the volume can be mounted as read-write by a single node
So these pods should be deployed on the same node in order to access the PVC (otherwise they will fail).
I would like to ask if there are any ways to config the deployment yaml file so that they can be deployed on the same node? or are there any ways to solve this problem?
回答1:
With the inter-pod affinity solution as suggested by Chin, I was able to solve the problem:
The following is my Deployment
yaml file:
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
In the Deployment
yaml file, set a label in the pod template spec.template.metadata.labels
, and then add podAffinity
config based on the label added, set topologyKey
to kubernetes.io/hostname
so that the pods will be deployed on the same node.
回答2:
Chin aready mentioned inter-pod affinity and this is a valid solution, but I would like to mention one more solution, maybe a little bit controversial but still valid in same cases.
If you have to have all containers running on one node, you can just put all of these conatiners in one pod and they will always go together, and what is most important, you can mount persistent volumes to it without any troubles. Just remember that overdoing it is considered bad practice; always prefer one container per pod over multiple containers per pod, although your usecase may be na exception to that rule, but its hard for me to say since I dont know anything about these pods.
Read some more about How pods manage multiple container
来源:https://stackoverflow.com/questions/65313780/kubernetes-how-to-config-a-group-of-pods-to-be-deployed-on-the-same-node