tuker
tuker

Reputation: 13

forward from ingress nginx controller to different nginx pods according to port numbers

in my k8s system I have a nginx ingress controller as LoadBalancer and accessing it to ddns adress like hedehodo.ddns.net and this triggering to forward web traffic to another nginx port. Now I deployed another nginx which works on node.js app but I cannot forward nginx ingress controller for any request to port 3000 to go another nginx

here is the nginx ingress controller yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: default
spec:
  rules:
  - host: hedehodo.ddns.net
    http:
      paths:
      - path: /
        backend:
          serviceName: my-nginx
          servicePort: 80
      - path: /
        backend:
          serviceName: helloapp-deployment
          servicePort: 3000

helloapp deployment works a Loadbalancer and I can access it from IP:3000

could any body help me?

Upvotes: 1

Views: 1897

Answers (2)

Highway of Life
Highway of Life

Reputation: 24421

Each host cannot share multiple duplicate paths, so in your example, the request to host: hedehodo.ddns.net will always map to the first service listed: my-nginx:80.

To use another service, you have to specify a different path. That path can use any service that you want. Your ingress should always point to a service, and that service can point to a deployment.

You should also use HTTPS by default for your ingress.

Ingress example:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
spec:
  rules:
    - host: my.example.net
      http:
        paths:
          - path: /
            backend:
              serviceName: my-nginx
              servicePort: 80
          - path: /hello
            backend:
              serviceName: helloapp-svc
              servicePort: 3000

Service example:

---
apiVersion: v1
kind: Service
metadata:
  name: helloapp-svc
spec:
  ports:
    - port: 3000
      name: app
      protocol: TCP
      targetPort: 3000
  selector:
    app: helloapp
  type: NodePort

Deployment example:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloapp
  labels:
    app: helloapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: helloapp
  template:
    metadata:
      labels:
        app: helloapp
    spec:
      containers:
        - name: node
          image: my-node-img:v1
          ports:
            - name: web
              containerPort: 3000

Upvotes: 4

Rakesh Gupta
Rakesh Gupta

Reputation: 3760

You can't have the same "path: /" for the same host. Change the path to a different one for your the new service.

Upvotes: 0

Related Questions