3
votes

I'm new to Kubernetes. I'm using GKE managed service for K8S. There are 2 Deployments nginx, httpd, and created NodePort services for those 2 deploys.

I'm trying to create ingress rule for the services. The nginx ingress controller is installed through helm. I have a domain from freenom and set the Google cloud DNS to use the Static public IP. When I try to hit the ingress URL (domain/nginx), it's giving:

"default backend - 404"


Deployment:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
  replicas: 1
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: httpd
  labels:
    name: httpd
spec:
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
        - name: httpd
          image: httpd
  replicas: 1

Services:

apiVersion: v1
kind: Service
metadata:
  labels:
    name: nginx
  name: nginx-service
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort

Same like for httpd service

Ingress:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test
  annotations:
    kubernetes.io/ingress.global-static-ip-name: testingk8s
spec:
  rules:
    - host: xyz.tk
      http: 
        paths:
          - path: /nginx
            backend:
              serviceName: nginx-service
              servicePort: 80
          - path: /httpd
            backend:
              serviceName: httpd-service
              servicePort: 80

Ingress describe:

Default backend:  default-http-backend:80 (10.48.0.7:8080)
Rules:
  Host           Path  Backends
  ----           ----  --------
  xyz.tk
                 /nginx   nginx-service:80 (10.48.0.25:80)
                 /httpd   httpd-service:80 (10.48.0.26:80)
Annotations:     ingress.kubernetes.io/backends:
                   {"k8s-be-30916--413d33a91e61ca5d":"HEALTHY","k8s-be-31376--413d33a91e61ca5d":"HEALTHY","k8s-be-32702--413d33a91e61ca5d":"HEALTHY"}

Ingress Controller Pod Logs:

I0812 09:38:34.405188       6 event.go:278] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"nginx", Name:"test", UID:"61991dbd-a361-47d2-88cc-548a7c43e743", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"316030", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress nginx/test
I0812 09:38:34.405815       6 controller.go:139] Configuration changes detected, backend reload required.
I0812 09:38:34.532163       6 controller.go:155] Backend successfully reloaded.
I0812 09:38:41.369315       6 status.go:275] updating Ingress nginx/test status from [] to [{35.192.136.218 }]
I0812 09:38:41.374080       6 event.go:278] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"nginx", Name:"test", UID:"61991dbd-a361-47d2-88cc-548a7c43e743", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"316057", FieldPath:""}): type: 'Normal' reason: 'UPDATE' Ingress nginx/test

2
Just one minor note. You are using NodePort as service type. If you are exposing it via ingress controller, typically the type of the service is ClusterIP then. - Lauri

2 Answers

2
votes

Add annotations kubernetes.io/ingress.class: nginx and nginx.ingress.kubernetes.io/rewrite-target: /. So the ingress looks like below

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - host: xyz.tk
      http: 
        paths:
          - path: /nginx
            backend:
              serviceName: nginx-service
              servicePort: 80
          - path: /httpd
            backend:
              serviceName: httpd-service
              servicePort: 80
0
votes

When you do the install of HELM chart, a LoadBalancer with a random IP is created to nginx-insgress, and it is the reason you cannot use a pre-created IP in loadBalancer.

Try to install HELM chart with controller.service.loadBalancerIP configured:

controller.service.loadBalancerIP IP address to assign to load balancer (if supported)

helm install --name nginx-ingress stable/nginx-ingress \
      --set controller.service.loadBalancerIP=<YOUR_EXTERNAL_IP>

NOTE: Yor IP must be in the same region of the cluster. Please refer to this documentation