之前添加服务都只会添加到同一个节点中,下面的方法可以将同一个服务分散到不同的节点里
apiVersion: v1
kind: Service
metadata:
name: java-question
labels:
name: java-question
spec:
selector:
app: java-question #这里选择器一定要选择容器的标签,之前写name:kube-node是错的。
type: NodePort #这里代表是NodePort类型的
ports:
- port: 8806 #这里的端口和clusterIP对应,即ip:8080,供内部访问。
targetPort: 8806 #端口一定要和container暴露出来的端口对应
protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: java-question
name: java-question
spec:
replicas: 2
selector:
matchLabels:
app: java-question
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
template:
metadata:
creationTimestamp: null
labels:
app: java-question
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- java-question
topologyKey: "kubernetes.io/hostname"
podAntiAffinity:
# 软策略
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- java-question
# 拓扑域(node上的标签key)
topologyKey: kubernetes.io/hostname
containers:
- image: registry.cn-zhangjiakou.aliyuncs.com/xxxx
name: java-question
imagePullPolicy: Always
resources: {}
ports:
- containerPort: 8806
env:
- name: TZ
value: Asia/Shanghai
imagePullSecrets:
- name: aliyuncs-registry
status: {}