3
votes

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
3
Can you share the yaml files you are using for the serviceA and its ingress? Also do not change things in your ingress-controller. This is most likely something regarding your ingress only.cookiedough

3 Answers

2
votes

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

1
votes

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
1
votes

It is possible if you modify the service declaration for the Nginx ingress controller. In the Nginx deploy.yaml you will find:

# Source: ingress-nginx/templates/controller-service.yaml
apiVersion: v1
kind: Service
...
spec:
  type: LoadBalancer
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: http
    - name: https
      port: 443
      protocol: TCP
      targetPort: https

add the following snippet to use HTTP on port 7049 also:

    - name: http-symbols
      port: 7049
      protocol: TCP
      targetPort: http

and redeploy the Nginx ingress controller with kubectl apply -f deploy.yaml