Search code examples
kuberneteskubernetes-helmkubernetes-ingressnginx-ingress

use single ingress controller for calling multiple services


I am using helm charts for deploying different services. I have parameterized my charts, so for every services I just pass new app name and it creates new service and related stuff.

All these services are residing in different namespace.

I have created ingress controller, which I want to use for all the services by just adding new rules for each service.

I tested deployment for one service, it worked successfully but when I am trying to deploy remaining services Its giving me error:

rendered manifests contain a resource that already exists. Unable to continue with install: Ingress "dev-ingress" in namespace "dev" exists and cannot be imported into the current release: invalid ownership metadata; annotation validation error: missing key "meta.helm.sh/release-name": must be set to

I want to understand Ideal flow to achieve this.

  1. Can I create services in different namespace and user one ingress controller?
  2. How can I configure my parameterized helm charts to deploy service in same ingress and just create new rule?

Here are my charts -

Service.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.metadata.name }}-svc
  labels:
    {{- include "helmcharts.labels" . | nindent 4 }}
spec:
  ports:
      - name: http
        protocol: TCP
        port: 80
  selector:
    {{- include "helmcharts.selectorLabels" . | nindent 4 }}

Ingress.yaml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: dev-ingress
{{- if .Values.ingress.management.annotations }}
  annotations:
{{ toYaml .Values.ingress.management.annotations | indent 4 }}
{{- end }}
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: {{ .Values.metadata.name }}-svc
          servicePort: 80
        path: /{{ .Values.env.path }}

Deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.metadata.name }}-deployment
  labels:
    {{- include "helmcharts.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  strategy:
     type: Recreate
   {{- end }}
   selector:
     matchLabels:
      {{- include "helmcharts.selectorLabels" . | nindent 6 }}
   template:
     metadata:
      {{- with .Values.podAnnotations }}
       annotations:
         {{- toYaml . | nindent 8 }}
       {{- end }}
       labels:
        {{- include "helmcharts.selectorLabels" . | nindent 8 }}
     spec:
       {{- with .Values.imagePullSecrets }}
       imagePullSecrets:
          {{- toYaml . | nindent 8 }}
        {{- end }}
       serviceAccountName: {{ include "helmcharts.serviceAccountName" . }}
        automountServiceAccountToken: false
        securityContext:
         {{- toYaml .Values.podSecurityContext | nindent 8 }}
        containers:
          - name: {{ .Values.env.containerName }}
           securityContext:
             {{- toYaml .Values.securityContext | nindent 12 }}
           image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default 
        .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - containerPort: 80
             protocol: "TCP"

values.yaml

metadata:
  name: #{App-Name}#
env:
  name: dev
  initial: d
  containerName: #{Container-Name}#
  path: #{App}#-dev

ingress:
  management:
    annotations:
      kubernetes.io/ingress.class: "nginx"
       nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
       nginx.ingress.kubernetes.io/affinity: "cookie"
       nginx.ingress.kubernetes.io/session-cookie-name: "route"
       nginx.ingress.kubernetes.io/session-cookie-hash: "sha1"

Solution

  • This is a Community Wiki answer, posted for better visibility, so feel free to edit it and add any additional details you consider important.

    OP confirmed in comments that this issue was solved based on the suggestions provided by user meaningqo in his comment:

    your ingress controller is able to watch multiple diferent ingress ressources. so there is no need for having one ingress that gets added more rules when you add new applications. basically, just change the name of the ingress in your helm-charts and you should be good to go – meaningqo

    solution provided by meaninggqo worked for me – megha