我只是想知道,是否可以在 Kubernetes 服务定义中引用服务而不是部署(使用服务标签而不是部署 matchLabels)?
我的意思是假设我定义了一个服务A,它公开了一个部署AD,现在我想定义另一个服务B,但是这次我希望它指向之前定义的服务而不是它的选择器引用一个部署AD服务A?这在 Kubernetes 中是否可行?例如,请参见下面的场景
**Deployment A-D**
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
**ServiceA**
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
run: my-nginx-1
spec:
ports:
- port: 80
protocol: TCP
selector:
run: my-nginx
**ServiceB**
apiVersion: v1
kind: Service
metadata:
name: my-nginx-wrapper-service
labels:
run: my-nginx-2
spec:
ports:
- port: 80
protocol: TCP
selector:
run: my-nginx-1 //service label instead of deployment
更新:
headless service
apiVersion: v1
kind: Service
metadata:
name: access-service
annotations:
getambassador.io/config: |
---
apiVersion: ambassador/v1
kind: Mapping
name: productreadservice-mapping
prefix: /Apps/ProductReadService/.*
prefix_regex: true
rewrite: ""
service: access-service:80
spec:
clusterIP: None
ports:
- name: http
port: 80
targetPort: 8082
endpoint object
apiVersion: v1
kind: Endpoints
metadata:
name: access-service
subsets:
- addresses:
- ip: ip of the service i wish to access
ports:
- port: 8082
protocol: TCP