PHP Avenger
PHP Avenger

Reputation: 1801

Kubernetes: Unable to configure Nginx Ingress to access an internal service

I am following a tutorial to access a pod running inside a Kubernetes cluster behind a service. This Kubernetes cluster is running on Windows 10 using Desktop Docker (by enabling the Kubernetes option)

I am unable to access it using this https://local.ticket.dev/api/users/currentuser it always says "Site can't be reached: local.ticket.dev unexpectedly closed the connection."

I have disabled the redirect but it still redirects HTTP to HTTPs

Request URL: http://local.ticket.dev/api/users/currentuser
Request Method: GET
Status Code: 307 Internal Redirect
Referrer Policy: strict-origin-when-cross-origin

Location: https://local.ticket.dev/api/users/currentuser
Non-Authoritative-Reason: HSTS

Here is visually what I want

Kubernetes Nginx Ingress

kubectl get ing

NAME              CLASS    HOSTS              ADDRESS   PORTS   AGE
ingress-service   <none>   local.ticket.dev             80      29s

kubectl get services Please note it's running on local machine windows 10 with Docker Desktop. and the LoadBalancer external IP always remain pending even after 6 hours

NAME                                       TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
auth-srv                                   ClusterIP      10.96.254.94     <none>        3000/TCP                     45s
kubernetes                                 ClusterIP      10.96.0.1        <none>        443/TCP                      5h17m
nginx-ingress-1629401528-controller        LoadBalancer   10.110.199.210   <pending>     80:31430/TCP,443:32346/TCP   5h13m
nginx-ingress-1629401528-default-backend   ClusterIP      10.108.79.252    <none>        80/TCP                       5h13m

kubectl get pods

NAME                                                        READY   STATUS    RESTARTS   AGE
auth-depl-c98cdf66f-txqxt                                   1/1     Running   0          54s
nginx-ingress-1629401528-controller-569576ddbd-2htxz        1/1     Running   0          5h13m
nginx-ingress-1629401528-default-backend-69c7fc6549-xxf8w   1/1     Running   0          5h13m

How I configured it is as follows

1 - Installation of Nginx by the following command

helm install stable/nginx-ingress --generate-name

2 - Skaffold dev

Listing files to watch...
 - billo/ticket_auth
Generating tags...
 - billo/ticket_auth -> billo/ticket_auth:latest
Some taggers failed. Rerun with -vdebug for errors.
Checking cache...
 - billo/ticket_auth: Found Locally
Starting test...
Tags used in deployment:
 - billo/ticket_auth -> billo/ticket_auth:d869228....
Starting deploy...
 - deployment.apps/auth-depl created
 - service/auth-srv created
 - ingress.networking.k8s.io/ingress-service created
Waiting for deployments to stabilize...
 - deployment/auth-depl is ready.
Deployments stabilized in 2.302 seconds
Waiting for deployments to stabilize...
Deployments stabilized in 6.9904ms
Press Ctrl+C to exit
Watching for changes...
[auth]
[auth] > [email protected] start
[auth] > ts-node-dev --poll src/index.ts
[auth]
[auth] [INFO] 00:59:23 ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.3.5)
[auth] Auth!!!! listen to 3000 port

if I look at the last line it seems that my Auth Pod is running on 3000 port.

auth-depl.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth
  template:
    metadata:
      labels:
        app: auth
    spec:
      containers:
        - name: auth
          image: billo/ticket_auth
          imagePullPolicy: Never
---
apiVersion: v1
kind: Service
metadata:
  name: auth-srv
spec:
  selector:
    app: auth
  ports:
    - name: auth
      protocol: TCP
      port: 3000
      targetPort: 3000

ingress-srv.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
    - host: local.ticket.dev
      http:
        paths:
          - path: /api/users/?(.*)
            pathType: Prefix
            backend:
              service:
                name: auth-srv
                port:
                  number: 3000

Configuration in the Host file

# Added by Docker Desktop
127.0.0.1 host.docker.internal
127.0.0.1 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
127.0.0.1 ingress.local
127.0.0.1 local.ticket.dev

Upvotes: 0

Views: 624

Answers (1)

Harsh Manvar
Harsh Manvar

Reputation: 30160

First Disable the HTTPS redirect first

nginx.ingress.kubernetes.io/ssl-redirect: "false"

add annotation into the ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
    - host: local.ticket.dev
      http:
        paths:
          - path: /api/users/?(.*)
            pathType: Prefix
            backend:
              service:
                name: auth-srv
                port:
                  number: 3000

Did you get the external IP for Nginx controller svc? it's showing pending as you are on the Local system.

You might also need to add entries into host file

manually adding your ingresses' hostnames to /etc/hosts:

127.0.0.1     ingress.local
127.0.0.1     local.ticket.dev
OR 

Host IP       local.ticket.dev

Upvotes: 0

Related Questions