Kubernetes:跨两个不同名称空间的负载平衡

Kubernetes:跨两个不同名称空间的负载平衡,kubernetes,load-balancing,Kubernetes,Load Balancing,我希望在生产中部署同一应用程序的不同版本: kubectl create deployment hello-server --image=gcr.io/google-samples/hello-app:1.0 -n test-a kubectl create deployment hello-server --image=gcr.io/google-samples/hello-app:2.0 -n test-b kubectl expose deployment hello-server --p

我希望在生产中部署同一应用程序的不同版本:

kubectl create deployment hello-server --image=gcr.io/google-samples/hello-app:1.0 -n test-a
kubectl create deployment hello-server --image=gcr.io/google-samples/hello-app:2.0 -n test-b
kubectl expose deployment hello-server --port 80 --target-port 8080 -n test-a
kubectl expose deployment hello-server --port 80 --target-port 8080 -n test-b
我尝试过这样的负载平衡:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.org/server-snippets: |
        upstream test-servers-upstream {
          server hello-server.test-a:80;
          server hello-server.test-b:80;
        }
spec:
  rules:
    - host: test-servers.example.com
      http:
        paths:
          - path: /
            backend:
              serviceName: test-servers-upstream
              servicePort: 80
但它返回一个错误:

/上游测试服务器:80台

服务:

$ kubectl get services -n test-b         
NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
hello-server   ClusterIP   10.128.199.113   <none>        80/TCP    75m
$ kubectl get services -n test-a 
NAME           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
hello-server   ClusterIP   10.128.207.6   <none>        80/TCP    75m
如何实现负载平衡?

Fresh ingress nginx支持基于重量的机箱外部署。 看起来像是你要的。

这里有一个很好的例子。

基本上,创建两个相同的入口,每个名称空间一个入口。 它们只是在注释和指向不同服务方面有所不同

# ingress for a service A in namespace A
kind: Ingress
metadata:
  name: demo-ingress
  namespace: demo-prod
spec:
  rules:
  - host: canary.example.com
    http:
      paths:
      - backend:
          serviceName: demo-prod
          servicePort: 80
        path: /

# ingress for a service B in namespace B
# note canary annotations
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "20"
  name: demo-ingress
  namespace: demo-canary
spec:
  rules:
  - host: canary.example.com
    http:
      paths:
      - backend:
          serviceName: demo-canary
          servicePort: 80
        path: /
请注意,ingress nginx canary实现实际上要求您的服务位于不同的命名空间中。

Fresh ingress nginx支持基于权重的canary部署。 看起来像是你要的。

这里有一个很好的例子。

基本上,创建两个相同的入口,每个名称空间一个入口。 它们只是在注释和指向不同服务方面有所不同

# ingress for a service A in namespace A
kind: Ingress
metadata:
  name: demo-ingress
  namespace: demo-prod
spec:
  rules:
  - host: canary.example.com
    http:
      paths:
      - backend:
          serviceName: demo-prod
          servicePort: 80
        path: /

# ingress for a service B in namespace B
# note canary annotations
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "20"
  name: demo-ingress
  namespace: demo-canary
spec:
  rules:
  - host: canary.example.com
    http:
      paths:
      - backend:
          serviceName: demo-canary
          servicePort: 80
        path: /

请注意,ingress nginx canary实现实际上要求您的服务位于不同的命名空间中。

您能显示服务对象的名称吗?@GiovanniPatruno,以下是服务名称,我无法正确格式化$kubectl get services-n test-a-o宽名称类型CLUSTER-IP EXTERNAL-IP端口年龄选择器hello server clusterp10.128.207.6 80/TCP 71m app=hello server$kubectl get services-n test-b-o宽名称类型CLUSTER-IP EXTERNAL-IP端口年龄选择器hello server ClusterIP 10.128.199.113 80/TCP 71m app=hello-server@GiovanniPatruno,我已经用服务对象更新了这个问题,以便更好地格式化。我正在查看API引用,似乎只能指定一个服务名称。我理解您添加nginx代码段以设置别名的技巧,但恐怕这行不通。您是否尝试过简单地将hello服务器作为ServiceName?附言是我读过的文件页。@GiovanniPatruno好发现。有没有其他方法可以跨多个名称空间实现负载平衡?能否显示服务对象的名称?@GiovanniPatruno,以下是服务名称,我无法正确格式化$kubectl get services-n test-a-o宽名称类型CLUSTER-IP EXTERNAL-IP端口年龄选择器hello server clusterp10.128.207.6 80/TCP 71m app=hello server$kubectl get services-n test-b-o宽名称类型CLUSTER-IP EXTERNAL-IP端口年龄选择器hello server ClusterIP 10.128.199.113 80/TCP 71m app=hello-server@GiovanniPatruno,我已经用服务对象更新了这个问题,以便更好地格式化。我正在查看API引用,似乎只能指定一个服务名称。我理解您添加nginx代码段以设置别名的技巧,但恐怕这行不通。您是否尝试过简单地将hello服务器作为ServiceName?附言是我读过的文件页。@GiovanniPatruno好发现。有没有其他方法可以跨多个名称空间实现负载平衡?