環境
- kubernetes 1.20.4
- Spring Boot 2.5.0-M3
目標
publishNotReadyAddresses 表示是否將沒有就緒的 Pod 的地址關聯到服務上。
默認情況是 false,只有就緒狀態的 Pod 的地址才會關聯到服務上。
示例
Deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-k8s
spec:
selector:
matchLabels:
app: spring-k8s
template:
metadata:
labels:
app: spring-k8s
spec:
containers:
- name: spring-k8s
image: jiangbo920827/spring-k8s:liveness
readinessProbe:
failureThreshold: 30
exec:
command: ["sh", "-c", "cat /root/test.txt"]
ports:
- containerPort: 8080
添加了一個就緒探針,因為這個文件不存在,所以 Pod 一直是未就緒,直到失敗。
查看 Pod
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
spring-k8s-d8ccf5994-zbxzg 0/1 Running 0 4m16s
Service.yaml
apiVersion: v1
kind: Service
metadata:
name: spring-k8s
spec:
selector:
app: spring-k8s
ports:
- port: 80
targetPort: 8080
查看不發布就緒
[root@master ~]# kubectl describe svc spring-k8s
Name: spring-k8s
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=spring-k8s
Type: ClusterIP
IP Families: <none>
IP: 10.104.121.41
IPs: 10.104.121.41
Port: <unset> 80/TCP
TargetPort: 8080/TCP
Endpoints:
Session Affinity: None
Events: <none>
發布未就緒 Pod
apiVersion: v1
kind: Service
metadata:
name: spring-k8s
spec:
selector:
app: spring-k8s
publishNotReadyAddresses: true
ports:
- port: 80
targetPort: 8080
查看發布未就緒 Pod
[root@master ~]# kubectl describe svc spring-k8s
Name: spring-k8s
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=spring-k8s
Type: ClusterIP
IP Families: <none>
IP: 10.104.121.41
IPs: 10.104.121.41
Port: <unset> 80/TCP
TargetPort: 8080/TCP
Endpoints: 10.244.2.232:8080
Session Affinity: None
Events: <none>
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
spring-k8s-d8ccf5994-zbxzg 0/1 Running 0 7m16s
總結
publishNotReadyAddresses 可以控制是否將未就緒的 Pod 發布到 Service 上,一般不建議為 true。
如果 Pod 還未就緒的話,發送到上面的請求可能得不到正常的響應。