很多这样的网站都说无头服务的好处是直接检测Pod的IP地址,直接访问Pod。但是这个解释对我来说没有意义。原因是即使我们不使用无头服务,我们仍然可以直接访问Pod。
我认为下面的例子:
- 1 pod(这是客户端)
- 3 pod由StatefulSet管理
1.使用无头服务
apiVersion: v1
kind: Service
metadata:
name: my-headless-service
spec:
type: ClusterIP
clusterIP: None
selector:
app: my-app
ports:
- name: http
port: 80
targetPort: 8080
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-statefulset
spec:
serviceName: my-headless-service
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- name: http
containerPort: 8080
---
# Client Pod
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
name: myapp
spec:
containers:
- name: myapp
image: willfarrell/ping
ports:
- containerPort: 8080
字符串
从名为myapp-pod的Pod中,我们可以访问由StatefulSet管理的每个Pod,如下所示
ping my-statefulset-0.my-headless-service
ping my-statefulset-1.my-headless-service
ping my-statefulset-2.my-headless-service
型
1.没有无头服务
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: ClusterIP
selector:
app: my-app
ports:
- name: http
port: 80
targetPort: 8080
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-statefulset
spec:
serviceName: my-service
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- name: http
containerPort: 8080
---
# Client Pod
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
name: myapp
spec:
containers:
- name: myapp
image: willfarrell/ping
ports:
- containerPort: 8080
型
从名为myapp-pod的Pod中,我们还可以访问由StatefulSet管理的每个Pod,如下所示
ping my-statefulset-0.my-service
ping my-statefulset-1.my-service
ping my-statefulset-2.my-service
型
这样我们就可以访问StatefulSet管理的Pod,而不需要headless service。
1(使用无头服务)和2(不使用无头服务)有什么区别吗?
1条答案
按热度按时间js81xvg61#
进一步探索的好文章:https://www.goglides.dev/bkpandey/headless-services-in-kubernetes-what-why-and-how-39fl