How can I expose a service on multiple ports over HTTP in ingress?

3/28/2019

I have a service in Kubernetes which I have to expose on multiple ports over HTTP. I use Nginx-Ingress and was able to expose my service over Port 80 successfully.(http://serviceA.example.com --> service-a:80)

However I am not able to use a diffrent port for Http then Port 80. How can I tell nginx-ingress to listen on Port 7049 aswell.

I've already tried to expose Port 7049 on the nginx Service and added the annotation nginx.org/listen-ports: "80,7049" to the nginx controller. Neither worked for me.

I expect the following output:

http://serviceA.example.com --> service-a:80

http://serviceA.example.com:7049 --> service-a:7049

ingress-service.yml

apiVersion: v1
kind: Service
metadata:
  name: nginx-ingress
  namespace: nginx-ingress
spec:
  externalTrafficPolicy: Local
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  - port: 443
    targetPort: 443
    protocol: TCP
    name: https
  selector:
    app: nginx-ingress

my-service.yml

apiVersion: v1
kind: Service
metadata:
  name: my-service
  labels:
    app: my-service
spec:
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  - port: 443
    targetPort: 443
    protocol: TCP
    name: https
  - port: 7049
    targetPort: 7049
    name: symbols
  selector:
    app: my-service

my-service-ingress.yml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-service
spec:
  rules:
  - host: myservice.example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: my-service
          servicePort: 80
-- meilel
kubernetes
kubernetes-ingress
nginx
nginx-ingress

2 Answers

3/28/2019

Ingress Object is used to expose application only for HTTP and HTTPS Traffic.

Ingress, added in Kubernetes v1.1, exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource.

what-is-ingress

You can have different type of routing such as path based or hostname based routing but the port number for the nginx will be 80 or& 443.

If you want to expose your application on different port than 80 and 443 , you need to use LoadBalancer type service

-- Suresh Vishnoi
Source: StackOverflow

3/28/2019

If you need to expose ingress on non-standard port (i.e. other than 80/443) you can use NodePort type of service for that. By default NodePort type of service opens a random port on the node from the range 30000 - 32767 where the service becomes reachable. You'd need to override two things in such a case:

1) To override the default node port range use the following flag when starting your apiserver: --service-node-port-range=7000-9000

2) To force the assignment of the concrete port in the NodePort service use the following service deployment in your .yaml file

apiVersion: v1
kind: Service
metadata:
  labels:
    k8s-app: ingress-nginx
  name: ingress-nginx
  namespace: ingress-nginx
spec:
  ports:
  - name: http
    nodePort: 7049
    protocol: TCP
    port: 80
  selector:
    k8s-app: ingress-nginx
  type: NodePort
-- Bernard Halas
Source: StackOverflow