|NO.Z.00210|——————————|^^ 标准 ^^|——|KuberNetes&高级调度.V13|——|Pod亲和力反亲和力.v01|pod亲和力_同namespace|
一、Pod亲和力和反亲和力概述
### --- pod亲和力说明
~~~ Pod亲和力:尽量将Pod部署在一起
~~~ Pod反亲和力:不尽量将Pod部署在一起
### --- 官方地址:
~~~ https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/

二、Pod亲和力
### --- 将busybox和demo-nginx部署在一块;查看资源文件
~~~ 导出yaml文件
[root@k8s-master01 ~]# kubectl get deploy demo-nginx -oyaml > pod-Affinity-demo-nginx.yaml
### --- 准备yaml文件参数;查看node节点的主机名
~~~ topologykey的值改为主机名:kubernetes.io/hostname
[root@k8s-master01 ~]# kubectl get node --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master01 Ready <none> 21d v1.20.0 another-node-label-key=another-node-label-value,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master01,kubernetes.io/os=linux,node.kubernetes.io/node=
k8s-master02 Ready <none> 21d v1.20.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master02,kubernetes.io/os=linux,node.kubernetes.io/node=
k8s-master03 Ready <none> 21d v1.20.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ds=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master03,kubernetes.io/os=linux,node.kubernetes.io/node=
k8s-node01 Ready <none> 21d v1.20.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ds=true,ingress=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux,node.kubernetes.io/node=,test.gt=20
k8s-node02 Ready <none> 21d v1.20.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ds=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux,node.kubernetes.io/node=,region=subnet7,test.gt=10
### --- 为busybox打标签region=beijing
[root@k8s-master01 ~]# kubectl label pod busybox region=beijing
pod/busybox labeled
### --- 查看当前环境有什么容器
[root@k8s-master01 ~]# kubectl get po --show-labels
NAME READY STATUS RESTARTS AGE LABELS
busybox 1/1 Running 0 48s region=beijing
demo-nginx-7fd59f5b5d-47jvk 2/2 Running 0 4m36s app=demo-nginx,pod-template-hash=7fd59f5b5d
demo-nginx-7fd59f5b5d-mmtp8 2/2 Running 0 4m36s app=demo-nginx,pod-template-hash=7fd59f5b5d//注:把busybox容器和nginx部署在一块
三、将busybox和demo-nginx部署在一块;创建pod亲和力yaml文件;同一namespace下
### --- 编辑pod亲和力yaml文件:把busybox和nginx部署在一块
[root@k8s-master01 ~]# vim pod-Affinity-demo-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: demo-nginx
name: demo-nginx
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: demo-nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: demo-nginx
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: region
operator: In
values:
- beijing
topologyKey: kubernetes.io/hostname
containers:
- command:
- sh
- -c
- sleep 36000000000
image: nginx
imagePullPolicy: IfNotPresent
name: nginx2
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /mnt
name: cache-volume
- mountPath: /tmp/nfs
name: nfs-test
- command:
- sh
- -c
- sleep 36000000000
image: nginx
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
name: web
protocol: TCP
resources:
limits:
cpu: 100m
memory: 270Mi
requests:
cpu: 100m
memory: 70Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/nginx/nginx.conf
name: config-volume
subPath: etc/nginx/nginx.conf
- mountPath: /mnt/
name: config-volume-non-subpath
- mountPath: /tmp/1
name: test-hostpath
- mountPath: /tmp/2
name: cache-volume
- mountPath: /tmp/pvc
name: pvc-test
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
shareProcessNamespace: true
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /etc/hosts
type: File
name: test-hostpath
- configMap:
defaultMode: 420
items:
- key: nginx.conf
path: etc/nginx/nginx.conf
name: nginx-conf
name: config-volume
- configMap:
defaultMode: 420
name: nginx-conf
name: config-volume-non-subpath
- emptyDir:
medium: Memory
name: cache-volume
- name: nfs-test
nfs:
path: /data/k8s-data/testDir
server: 192.168.1.14
- name: pvc-test
persistentVolumeClaim:
claimName: myclaim
### --- 配置文件注释
~~~ 把busybox和pod的容器部署在一块
~~~ 加入数据库和应用pod都是有节点的,若是你想要把前端应用和后端应用和数据库部署在同一个节点上
~~~ 可以通过容器亲和力将它们部署在一块
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: region
operator: In
values:
- beijing
topologyKey: kubernetes.io/hostname
四、pod亲和力同一namespace下部署
### --- 重新触发容器
[root@k8s-master01 ~]# kubectl replace -f pod-Affinity-demo-nginx.yaml
deployment.apps/demo-nginx replaced
### --- 查看部署结果
~~~ 可以看到pod和busybox都部署在了k8s-master02节点上面
~~~ 和指定的pod部署在一块,是可以跨namespace的
[root@k8s-master01 ~]# kubectl get po -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox 1/1 Running 0 2m30s 172.25.92.90 k8s-master02 <none> <none>
demo-nginx-7fd59f5b5d-m9zx9 2/2 Running 0 29s 172.25.92.91 k8s-master02 <none> <none>
demo-nginx-7fd59f5b5d-x4ckb 2/2 Running 0 28s 172.25.92.92 k8s-master02 <none> <none>
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了