我们可以为来自不同部署的 link 两个 PODs 创建服务 >
Can we create service to link two PODs from different Deployments >
我的应用程序必须使用 POD 进行部署。
我可以创建一个服务来在这 2 个 PODs 中分配负载,这是不同部署的一部分吗?
如果是这样,如何?
是的,你可以做到。
将公共标签密钥对添加到部署 pod 规范,并使用该公共标签作为服务定义中的选择器
使用上面定义的服务,请求将在所有匹配的 pods.
之间进行负载平衡
是的,这是可以实现的。在 Kubernete documentation 上可以找到有关如何执行此操作的详细说明。但是,请记住,两个部署应提供相同的功能,因为输出应具有相同的格式。
A Kubernetes Service is an abstraction which defines a logical set of Pods running somewhere in your cluster, that all provide the same functionality
. When created, each Service is assigned a unique IP address (also called clusterIP). This address is tied to the lifespan of the Service, and will not change while the Service is alive. Pods can be configured to talk to the Service, and know that communication to the Service will be automatically load-balanced out to some pod that is a member of the Service.
基于文档中的示例。
1. nginx
部署。请记住,Deployment 可以有多个标签。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
run: nginx
env: dev
replicas: 2
template:
metadata:
labels:
run: nginx
env: dev
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
2. nginx-second
部署
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-second
spec:
selector:
matchLabels:
run: nginx
env: prod
replicas: 2
template:
metadata:
labels:
run: nginx
env: prod
spec:
containers:
- name: nginx-second
image: nginx
ports:
- containerPort: 80
现在要将 Deployments
与Services
配对,您必须使用基于Deployments
标签的Selector。您可以在下面找到 2 service
个 YAML。 nginx-service
指向两个部署,nginx-service-1
仅指向 nginx-second
部署。
## Both Deployments
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
ports:
- port: 80
protocol: TCP
selector:
run: nginx
---
### To nginx-second deployment
apiVersion: v1
kind: Service
metadata:
name: nginx-service-1
spec:
ports:
- port: 80
protocol: TCP
selector:
env: prod
您可以通过检查端点来验证 service
绑定到部署。
$ kubectl get pods -l run=nginx -o yaml | grep podIP
podIP: 10.32.0.9
podIP: 10.32.2.10
podIP: 10.32.0.10
podIP: 10.32.2.11
$ kk get ep nginx-service
NAME ENDPOINTS AGE
nginx-service 10.32.0.10:80,10.32.0.9:80,10.32.2.10:80 + 1 more... 3m33s
$ kk get ep nginx-service-1
NAME ENDPOINTS AGE
nginx-service-1 10.32.0.10:80,10.32.2.11:80 3m36s
我的应用程序必须使用 POD 进行部署。 我可以创建一个服务来在这 2 个 PODs 中分配负载,这是不同部署的一部分吗? 如果是这样,如何?
是的,你可以做到。 将公共标签密钥对添加到部署 pod 规范,并使用该公共标签作为服务定义中的选择器
使用上面定义的服务,请求将在所有匹配的 pods.
之间进行负载平衡是的,这是可以实现的。在 Kubernete documentation 上可以找到有关如何执行此操作的详细说明。但是,请记住,两个部署应提供相同的功能,因为输出应具有相同的格式。
A Kubernetes Service is an abstraction which defines a logical set of Pods running somewhere in your cluster,
that all provide the same functionality
. When created, each Service is assigned a unique IP address (also called clusterIP). This address is tied to the lifespan of the Service, and will not change while the Service is alive. Pods can be configured to talk to the Service, and know that communication to the Service will be automatically load-balanced out to some pod that is a member of the Service.
基于文档中的示例。
1. nginx
部署。请记住,Deployment 可以有多个标签。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
run: nginx
env: dev
replicas: 2
template:
metadata:
labels:
run: nginx
env: dev
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
2. nginx-second
部署
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-second
spec:
selector:
matchLabels:
run: nginx
env: prod
replicas: 2
template:
metadata:
labels:
run: nginx
env: prod
spec:
containers:
- name: nginx-second
image: nginx
ports:
- containerPort: 80
现在要将 Deployments
与Services
配对,您必须使用基于Deployments
标签的Selector。您可以在下面找到 2 service
个 YAML。 nginx-service
指向两个部署,nginx-service-1
仅指向 nginx-second
部署。
## Both Deployments
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
ports:
- port: 80
protocol: TCP
selector:
run: nginx
---
### To nginx-second deployment
apiVersion: v1
kind: Service
metadata:
name: nginx-service-1
spec:
ports:
- port: 80
protocol: TCP
selector:
env: prod
您可以通过检查端点来验证 service
绑定到部署。
$ kubectl get pods -l run=nginx -o yaml | grep podIP
podIP: 10.32.0.9
podIP: 10.32.2.10
podIP: 10.32.0.10
podIP: 10.32.2.11
$ kk get ep nginx-service
NAME ENDPOINTS AGE
nginx-service 10.32.0.10:80,10.32.0.9:80,10.32.2.10:80 + 1 more... 3m33s
$ kk get ep nginx-service-1
NAME ENDPOINTS AGE
nginx-service-1 10.32.0.10:80,10.32.2.11:80 3m36s