Can you use Pod Affinity/Anti-Affinity with DeploymentConfig?
I'm looking for instructions/examples of this...
...assuming my administrator has configured OpenShift itself to support pod affinity/anti-affinity, how do I configure my DeploymentConfig (the method I'm deploying my application) to leverage this? I see examples of using Pod configs, but not DCs. Or maybe I'm missing something?
I'd like to deploy my application (DC) so that the pods that are created are spread around different nodes (I believe using anti-affinity preferred is the way to do this?)
Thanks!
-- Andy
Responses
Both Affinity and Anti-Affinity can configure on DC, Here is a example:
apiVersion: v1
kind: DeploymentConfig
metadata:
name: mysql
spec:
replicas: 4
selector:
app: mysql
template:
metadata:
name: mysql
labels:
app: mysql
spec:
containers:
- name: mysql
image: registry.cmcc.com/rhscl/mysql-57-rhel7:latest
env:
- name: MYSQL_ROOT_PASSWORD
value: redhat
- name: MYSQL_USER
value: test_user
- name: MYSQL_PASSWORD
value: test_pass
- name: MYSQL_DATABASE
value: test_db
ports:
- containerPort: 3306
name: mysql
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "region"
operator: In
values:
- r1
strategy:
type: Rolling
With the above config, all 4 pods are scheduled to the nodes with label region=r1.
apiVersion: v1
kind: DeploymentConfig
metadata:
name: mysql
spec:
replicas: 4
selector:
app: mysql
template:
metadata:
name: mysql
labels:
app: mysql
annotations:
scheduler.alpha.kubernetes.io/affinity: |
{
"podAntiAffinity": {
"requiredDuringSchedulingIgnoredDuringExecution": [{
"labelSelector": {
"matchExpressions": [{
"key": "zone",
"operator": "In",
"values":["z1","z2"]
}]
},
"topologyKey": "kubernetes.io/hostname"
}]
}
}
spec:
containers:
- name: mysql
image: registry.cmcc.com/rhscl/mysql-57-rhel7:latest
env:
- name: MYSQL_ROOT_PASSWORD
value: redhat
- name: MYSQL_USER
value: test_user
- name: MYSQL_PASSWORD
value: test_pass
- name: MYSQL_DATABASE
value: test_db
ports:
- containerPort: 3306
name: mysql
strategy:
type: Rolling
With above config, the 4 pod are deploy toz1 and z2.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
