问题是似乎不可能以与部署相同的方式使用 LoadBalancer 公开这些 pod(因为 statefulset 中 pod 的唯一性)。
为什么不?这是我使用默认 Nginx 的 StatefulSet
$ k -n test get statefulset
NAME DESIRED CURRENT AGE
web 2 2 5d
$ k -n test get pods
web-0 1/1 Running 0 5d
web-1 1/1 Running 0 5d
这是我的服务类型 LoadBalancer,在 Minikube 的情况下它是 NodePort(实际上)
$ k -n test get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx LoadBalancer 10.110.22.74 <pending> 80:32710/TCP 5d
让我们用 curl 运行一些 pod 并向 ClusterIP 发出一些请求:
$ kubectl -n test run -i --tty tools --image=ellerbrock/alpine-bash-curl-ssl -- bash
bash-4.4$ curl 10.110.22.74 &> /dev/null
bash-4.4$ curl 10.110.22.74 &> /dev/null
bash-4.4$ curl 10.110.22.74 &> /dev/null
bash-4.4$ curl 10.110.22.74 &> /dev/null
让我们看看 Nginx 日志:
$ k -n test logs web-0
172.17.0.7 - - [18/Apr/2019:23:35:04 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.61.0"
172.17.0.7 - - [18/Apr/2019:23:35:05 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.61.0"
172.17.0.7 - - [18/Apr/2019:23:35:17 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.61.0"
$ k -n test logs web-1
172.17.0.7 - - [18/Apr/2019:23:35:15 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.61.0"
172.17.0.7 - 是我的卷曲吊舱:
NAME READY STATUS RESTARTS AGE IP NODE
tools-654cfc5cdc-8zttt 1/1 Running 1 5d 172.17.0.7 minikube
实际上,在 StatefulSet 的 pod 之间进行负载平衡的情况下,ClusterIP 完全足够了,因为您有一个 Endpoints 列表
$ k -n test get endpoints
NAME ENDPOINTS AGE
nginx 172.17.0.5:80,172.17.0.6:80 5d
YAML:
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
type: LoadBalancer
ports:
- port: 80
name: web
selector:
app: nginx