cat >> nginx-pod.yaml << EOF
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
EOF
alpha.kubernetes.io/fluentd-ds-ready: "true"
beta.kubernetes.io/arch: amd64
beta.kubernetes.io/os: linux
disktype: ssd
kubeadm.alpha.kubernetes.io/role: master
kubernetes.io/hostname: master
IgnoredDuringExecution意味着当节点label改变后,pod将继续运行在这个不满足labelselector的node上面,将来会提供RequiredDuringExecution,让node驱逐不满足affinity条件的pod
根据类型有
NodeAffinity
PodAffinity
PodAntiAffinity
NodeAffinity
下面这个例子将运行pod到kubernetes.io/e2e-az-name值为e2e-az1 或e2e-az2的节点上面
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
annotations:
scheduler.alpha.kubernetes.io/affinity: >
{
"nodeAffinity": {
"requiredDuringSchedulingIgnoredDuringExecution": {
"nodeSelectorTerms": [
{
"matchExpressions": [
{
"key": "kubernetes.io/e2e-az-name",
"operator": "In",
"values": ["e2e-az1", "e2e-az2"]
}
]
}
]
}
}
}
another-annotation-key: another-annotation-value
spec:
containers:
- name: with-node-affinity
image: gcr.io/google_containers/pause:2.0
preferredDuringSchedulingIgnoredDuringExecution 例子:
apiVersion: v1
kind: Pod
metadata:
name: with-labels
annotations:
scheduler.alpha.kubernetes.io/affinity: >
{
"nodeAffinity": {
"preferredDuringSchedulingIgnoredDuringExecution": [
{
"weight" : 1,
"preference": {
"matchExpressions": [
{
"key": "disktype",
"operator": "In",
"values": ["ssd"]
}
]
}
}
]
}
}
another-annotation-key: another-annotation-value
spec:
containers:
- name: test-affinity
env:
- name: MYSQL_ROOT_PASSWORD
value: pass
image: mysql
apiVersion: v1
kind: Pod
metadata:
name: with-pod-affinity
annotations:
scheduler.alpha.kubernetes.io/affinity: >
{
"podAffinity": {
"requiredDuringSchedulingIgnoredDuringExecution": [
{
"labelSelector": {
"matchExpressions": [
{
"key": "security",
"operator": "In",
"values": ["S1"]
}
]
},
"topologyKey": "failure-domain.beta.kubernetes.io/zone"
}
]
},
"podAntiAffinity": {
"requiredDuringSchedulingIgnoredDuringExecution": [
{
"labelSelector": {
"matchExpressions": [
{
"key": "security",
"operator": "In",
"values": ["S2"]
}
]
},
"topologyKey": "kubernetes.io/hostname"
}
]
}
}
spec:
containers:
- name: with-pod-affinity
image: gcr.io/google_containers/pause:2.0
具体字段的定义以及关系可以参考k8s源码的定义
type Affinity struct {
NodeAffinity *NodeAffinity
PodAffinity *PodAffinity
PodAntiAffinity *PodAntiAffinity
}
type NodeAffinity struct {
RequiredDuringSchedulingIgnoredDuringExecution *NodeSelector
PreferredDuringSchedulingIgnoredDuringExecution []PreferredSchedulingTerm
}
type PodAffinity struct {
RequiredDuringSchedulingIgnoredDuringExecution []PodAffinityTerm
PreferredDuringSchedulingIgnoredDuringExecution []WeightedPodAffinityTerm
}
type PodAntiAffinity struct {
RequiredDuringSchedulingIgnoredDuringExecution []PodAffinityTerm
PreferredDuringSchedulingIgnoredDuringExecution []WeightedPodAffinityTerm
}
type PreferredSchedulingTerm struct {
Weight int32
Preference NodeSelectorTerm
}
type NodeSelector struct {
NodeSelectorTerms []NodeSelectorTerm
}
type NodeSelectorTerm struct {
MatchExpressions []NodeSelectorRequirement
}
type NodeSelectorRequirement struct {
Key string
Operator NodeSelectorOperator
Values []string
}
const (
NodeSelectorOpIn NodeSelectorOperator = "In"
NodeSelectorOpNotIn NodeSelectorOperator = "NotIn"
NodeSelectorOpExists NodeSelectorOperator = "Exists"
NodeSelectorOpDoesNotExist NodeSelectorOperator = "DoesNotExist"
NodeSelectorOpGt NodeSelectorOperator = "Gt"
NodeSelectorOpLt NodeSelectorOperator = "Lt"
)
type PodAffinityTerm struct {
LabelSelector *metav1.LabelSelector
Namespaces []string
TopologyKey string
}
type LabelSelector struct {
MatchLabels map[string]string
MatchExpressions []LabelSelectorRequirement
}
type LabelSelectorRequirement struct {
Key string
Operator LabelSelectorOperator
Values []string
}
const (
LabelSelectorOpIn LabelSelectorOperator = "In"
LabelSelectorOpNotIn LabelSelectorOperator = "NotIn"
LabelSelectorOpExists LabelSelectorOperator = "Exists"
LabelSelectorOpDoesNotExist LabelSelectorOperator = "DoesNotExist"
)
type WeightedPodAffinityTerm struct {
Weight int32
PodAffinityTerm PodAffinityTerm
}
type PodAffinityTerm struct {
LabelSelector *metav1.LabelSelector
Namespaces []string
TopologyKey string
}