Search code examples
kuberneteskubernetes-ingress

Only allow requests to certain paths using k8s ingress


I've set up an ingress to route traffic to my http server, however I would like to leave some routes inaccessible from outside of the cluster.

Example routes:

/status -> end point to determine service status

/users/names -> returns users

/users/ages -> returns ages

current ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  namespace: my-namespace
  name: my-app-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: localhost
    http:
      paths:
      - pathType: Prefix
        path: /
        backend:
          service:
            name: my-service
            port:
              number: 8080

this works currently but leaves all routes accessible. What I want to do is only have routes that fall under the /users path open, so that would be both /users/names and /users/ages. That would leave /status inaccessible from outside of the cluster. Is this achievable from changing the ingress configuration? Any help would be appreciated.


Solution

  • Just specify the path that you want to expose via the ingress like this:

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      namespace: my-namespace
      name: my-app-ingress
      annotations:
        kubernetes.io/ingress.class: nginx
    spec:
      rules:
      - host: localhost
        http:
          paths:
          - pathType: Prefix
            path: /users # <- add the path here
            backend:
              service:
                name: my-service
                port:
                  number: 8080