Home > Back-end >  Helm ingress controller does not added paths
Helm ingress controller does not added paths

Time:11-22

For a first helm kubernetes deployment with minikube I am trying to set up a Ingress controller. The steps I did.

Helm create ingress

Remove all files from templates folder and clear content of values.yaml and start with a fresh chart.yaml

apiVersion: v2
name: ingress
description: A Helm chart for Ingress Controller
type: application
version: 1.1.0
appVersion: 1.17.0
keywords:
  - ingress
  - nginx
  - api-gateway
home: https://github.com/harry
maintainers:
  - name: Harry
    url: https://github.com/harry
dependencies:
  - name: nginx-ingress
    version: 0.15.1
    repository: https://helm.nginx.com/stable

Next a command that creates the tgz file

helm dependency update

followed by a ingress.yaml in /templates

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ .Values.ingress.name }}
  annotations:
    kubernetes.io/ingress.class: {{ .Values.ingress.annotations.class }}
spec:
  rules:
  {{- range .Values.ingress.hosts }}
    - host: {{ .host | quote }}
      http:
        paths:
        {{- range .paths }}
          - path: {{ .path }}
            pathType: Prefix
            backend:
              service:
                name: {{ .backend.serviceName }}
                port:
                  number: {{ .backend.servicePort }}
        {{- end }}
  {{- end }}

And a default values.yaml

ingress:
  name: ingress-service
  replicaCount: 1
  annotations: 
    class: nginx
  hosts:
    - host: chart-example.local
      paths: 
        - path: /
          backend:
            serviceName: serviceName
            servicePort: 8080

Lastly I apply with

helm install -f ingress.yaml ingress ./ingress

the following ingress.yaml

ingress:
  hosts:
    - host: nutrizone.k8s.com
      paths:
        - path: /api/
          backend:
            serviceName: trala-backend
            servicePort: 8080
        - path: /
          backend:
            serviceName: trala-ui
            servicePort: 8080

Results

kubectl exec pod/ingress-nginx-ingress-controller-55b985bcfd-7p7hk -n default -- cat /etc/nginx/nginx.conf

where /api is nowhere to be found and

location / {
    return 404;
  }

After reinstalling several times and updating to a newer version of nginx (which can be seen in the values.yaml - however old syntax should be converted correctly) I am a bit lost

After first suggestion to update values.yaml

I changed the block in values.yaml and parent ingress.yaml to nginx-ingress and updated the /template/ingress.yaml (i needed indexes to make the name 'nginx-ingress' work)

helm delete ingress
helm install -f ingress.yaml ingress ./ingress

Sadly no difference.

Update 2 add github project https://github.com/Chris2301/helm

CodePudding user response:

dependencies:
  - name: nginx-ingress
    version: 0.15.1
    repository: https://helm.nginx.com/stable

You have declared that the dependency is called "nginx-ingress". In order for Helm to pass the chart values to the subchart, you need to put these inside a block named the same as the chart so, in your case, you would have to put your values inside a block named nginx-ingress

See this question where the OP had the same problem.


UPDATE AFTER VIEWING CODE

What you have done is correct. The config file does not update because the ingress controller uses your ingress resource to determine how to route your request.

For example:

$ kubectl get ingress ingress-service -o yaml | grep "spec:" -A 18
spec:
  rules:
  - host: project.k8s.com
    http:
      paths:
      - backend:
          service:
            name: project-backend
            port:
              number: 8080
        path: /api/
        pathType: Prefix
      - backend:
          service:
            name: nginx
            port:
              number: 80
        path: /
        pathType: Prefix

I modified the service linked to the "/" in order to test the functionality.

I installed the bitnami nginx chart to provide a test backend service:

helm install nginx bitnami/nginx

This created a service called nginx in the default namespace (also where I installed your chart)

$ kubectl get svc 
NAME                    TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)                      AGE
ingress-nginx-ingress   LoadBalancer   10.109.222.226   10.109.222.226   80:30892/TCP,443:30210/TCP   27m
kubernetes              ClusterIP      10.96.0.1        <none>           443/TCP                      38m
nginx                   LoadBalancer   10.97.31.201     10.97.31.201     80:32484/TCP                 25m

So now I can use the external IP for the loadbalancer created by your chart (ingress-nginx-ingress) and hit that using curl. We have to use Host here so that the ingress controller knows which rule to match.

$ curl 10.109.222.226 -H "Host: project.k8s.com"
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

As you can see, the controller returned the nginx content correctly. You can verify this by hitting the nginx service directly also (no need to use the Host header this time)

$ curl 10.97.31.201
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Now if we tried to use the /api/ endpoint, it'll fail because the backend service (project-backend) does not exist:

$ curl 10.109.222.226/api/ -H "Host: project.k8s.com"
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.23.2</center>
</body>
</html>

So to answer your original question, the /api/ is not in the nginx config because the controller uses the ingress resource to determine routing.

Your chart is performing as expected.

  • Related