Rodrigo Estebanez
Rodrigo Estebanez

Reputation: 867

Kubernetes: load-balance across two different namespaces

I want to deploy different versions of the same application in production:

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

I've tried to load-balance it like this:

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

but it returns an error:

/ test-servers-upstream:80 (<error: endpoints "test-servers-upstream" not found>)

Services:

$ 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

How can I load-balance it?

Upvotes: 3

Views: 1616

Answers (1)

Olesya Bolobova
Olesya Bolobova

Reputation: 1653

Fresh ingress-nginx supports weight-based canary deployments out of box.
Looks like what you're asking for.
https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#canary.

Great example here.
https://medium.com/@domi.stoehr/canary-deployments-on-kubernetes-without-service-mesh-425b7e4cc862

Basically, you create 2 identical ingresses, one for each namespace.
They are different only in annotations and in pointing to different services.

# 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: /

Note, that ingress-nginx canary implementation actually requires your services to be in different namespaces.

Upvotes: 4

Related Questions