How do I setup Ingress controller for Kubernetes where one end point for test environment and another for production environment?

1/6/2020

I'm setting up Ingress controller for my service in deployment.yaml of Kubernetes.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {env}
  annotations:
    traefik.ingress.kubernetes.io/frontend-entry-points: "http,https"
    ingress.kubernetes.io/force-hsts: "true"
    traefik.ingress.kubernetes.io/redirect-regex: ^http://(.*)
    traefik.ingress.kubernetes.io/redirect-replacement: https://$1
    kubernetes.io/ingress.class: "traefik"

spec:
  rules:
    - host: “qa.mywebsite.com"
      http:
        paths:
          - backend:
              serviceName: serviceName-{env}
              servicePort:8080

How can I set it up Ingress controller so that the traffic in QA pods are routed to the QA end point, Prod prods' traffic are routed to Prod pods?

-- sofs1
kubernetes
kubernetes-ingress
nginx-ingress
traefik-ingress
yaml

2 Answers

1/7/2020

The separation of environment for different stages is one of the important factor in deploying application in Kuebernetes.

I would recommend here to create two separate ingress.

One for Production and another one for QA. (If you have more environment, you can have more). Ingress Rules would be controlled based on host.

QA environment ingress could be like this.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {env}
  annotations:
    traefik.ingress.kubernetes.io/frontend-entry-points: "http,https"
    ingress.kubernetes.io/force-hsts: "true"
    traefik.ingress.kubernetes.io/redirect-regex: ^http://(.*)
    traefik.ingress.kubernetes.io/redirect-replacement: https://$1
    kubernetes.io/ingress.class: "traefik"

spec:
  rules:
    - host: “qa.mywebsite.com"
      http:
        paths:
          - backend:
              serviceName: serviceName-{env}
              servicePort:8080

Similarly you can also have another ingress in Production Environment(host is different here).

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {env}
  annotations:
    traefik.ingress.kubernetes.io/frontend-entry-points: "http,https"
    ingress.kubernetes.io/force-hsts: "true"
    traefik.ingress.kubernetes.io/redirect-regex: ^http://(.*)
    traefik.ingress.kubernetes.io/redirect-replacement: https://$1
    kubernetes.io/ingress.class: "traefik"

spec:
  rules:
    - host: “prod.mywebsite.com"
      http:
        paths:
          - backend:
              serviceName: serviceName-{env}
              servicePort:8080 
-- nagendra547
Source: StackOverflow

1/7/2020

I do this with helm charts

There are a lot of samples and user guide you can go through first.

the idea is, when you need some things to be set up as input variables, you can define these values in its values.yaml

So your codes will be something like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ .Release.env  }}
  annotations:
    traefik.ingress.kubernetes.io/frontend-entry-points: "http,https"
    ingress.kubernetes.io/force-hsts: "true"
    traefik.ingress.kubernetes.io/redirect-regex: ^http://(.*)
    traefik.ingress.kubernetes.io/redirect-replacement: https://$1
    kubernetes.io/ingress.class: "traefik"

spec:
  rules:
    - host: “qa.mywebsite.com"
      http:
        paths:
          - backend:
              serviceName: serviceName-{{ .Release.env  }}
              servicePort:8080

Reference: https://helm.sh/docs/chart_template_guide/values_files/

There is another similar project namaed kustomize, it was developed by Kubernetes team, but currently not that popular as Helm charts. You can take a look as well if you are interested.

-- BMW
Source: StackOverflow