Ingress redirect to a localhost tcp service

11/7/2021

I have a local website. The website was created by a docker-compose and it is listening on a localhost port 3000.

When I try:

curl 127.0.0.1:3000

I can see the response.

What I did:

From my domain provider I edited the DNS to point to my server, then I changed nginx-ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: virtual-host-ingress
  namespace: ingress-basic
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/use-regex: "true"
    cert-manager.io/cluster-issuer: "letsencrypt-pp"
spec:
  tls:
  - hosts:
    - nextformulainvesting.com
    secretName: ***
  rules:
  - host: "nextformulainvesting.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: e-frontend-saleor
            port:
              number: 80

and I created the service:

apiVersion: v1
kind: Service
metadata:
  name: e-frontend-saleor
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000

But with the service or without the service I receive the error 503 Service Temporarily Unavailable.

How can I use nginx-ingress to point to my local TCP service?

-- inyourmind
kubernetes
nginx-ingress

2 Answers

11/8/2021

Your service that you have created is for forwarding the traffic to deployments

As your service is running out side of Kubernetes cluster you should be using the Endpoint in this case

apiVersion: v1
kind: Endpoints
metadata:
  name: my-service
subsets:
  - addresses:
      - IP: <External IP>
    ports:
      - port: 3000

and you can use this Endpoint to ingress so that it will route the traffic.

Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: virtual-host-ingress
  namespace: ingress-basic
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/use-regex: "true"
    cert-manager.io/cluster-issuer: "letsencrypt-pp"
spec:
  tls:
  - hosts:
    - nextformulainvesting.com
    secretName: ***
  rules:
  - host: "nextformulainvesting.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: my-service
            port:
              number: 3000
-- Harsh Manvar
Source: StackOverflow

11/15/2021

To clarify the issue I am posting a community wiki answer.

The answer that helped to resolve this issue is available at this link. Based on that - the clue of the case is to create manually a Service and an Endpoint objects for external server.

After that one can create an Ingress object that will point to Service external-ip with adequate port .

Here are the examples of objects provided in similar question.

  • Service and an Endpoint objects:
apiVersion: v1
kind: Service
metadata:
  name: external-ip
spec:
  ports:
  - name: app
    port: 80
    protocol: TCP
    targetPort: 5678
  clusterIP: None
  type: ClusterIP
---
apiVersion: v1
kind: Endpoints
metadata:
  name: external-ip
subsets:
- addresses:
  - ip: 10.0.40.1
  ports:
  - name: app
    port: 5678
    protocol: TCP
  • Ingress object:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: external-service
spec:
  rules:
  - host: service.example.com
    http:
      paths:
      - backend:
          serviceName: external-ip
          servicePort: 80
        path: /

See also this reference.

-- kkopczak
Source: StackOverflow