Search code examples
kubernetesrabbitmqkubernetes-ingresstraefiktraefik-ingress

Traefik ingress for multiple ports


I've deployed rabbitmq cluster in k3s cluster using rabbitmq rabbitmq cluster operator. As a result, it created a ClusterIP service looks like this:

Name:              rabbitmq
Namespace:         rabbits
Labels:            app.kubernetes.io/component=rabbitmq
                   app.kubernetes.io/name=rabbitmq
                   app.kubernetes.io/part-of=rabbitmq
Annotations:       <none>
Selector:          app.kubernetes.io/name=rabbitmq
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.43.48.11
IPs:               10.43.48.11
Port:              amqp  5672/TCP
TargetPort:        5672/TCP
Endpoints:         10.42.2.55:5672,10.42.2.56:5672
Port:              management  15672/TCP
TargetPort:        15672/TCP
Endpoints:         10.42.2.55:15672,10.42.2.56:15672
Port:              mqtt  1883/TCP
TargetPort:        1883/TCP
Endpoints:         10.42.2.55:1883,10.42.2.56:1883
Port:              web-mqtt  15675/TCP
TargetPort:        15675/TCP
Endpoints:         10.42.2.55:15675,10.42.2.56:15675
Port:              stomp  61613/TCP
TargetPort:        61613/TCP
Endpoints:         10.42.2.55:61613,10.42.2.56:61613
Port:              web-stomp  15674/TCP
TargetPort:        15674/TCP
Endpoints:         10.42.2.55:15674,10.42.2.56:15674
Port:              prometheus  15692/TCP
TargetPort:        15692/TCP
Endpoints:         10.42.2.55:15692,10.42.2.56:15692
Session Affinity:  None
Events:            <none>

I want to open port 15672 for admin portal, and port 5672 for applications outside of the kubernetes cluster. I tried the following but didn't work:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: rabbitmq-admin-ingress
  annotations:
    kubernetes.io/ingress.class: "traefik"
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: rabbitmq
          servicePort: 15672
      - path : /
        backend:
          serviceName: rabbitmq
          servicePort: 5672

Anyone can help me, what I'm doing wrong?

Thanks in advance.


Solution

  • You can't have both paths pointing to the same location. Also, you should set a FQDN for your Ingress.

    Try using two Ingresses, with different names - which would work in most cases:

    ---
    apiVersion: networking.k8s.io/v1beta1
    kind: Ingress
    metadata:
      name: rabbitmq-admin-ingress-one
      annotations:
        kubernetes.io/ingress.class: "traefik"
    spec:
      rules:
      - http:
          paths:
          - path: /
            backend:
              serviceName: rabbitmq
              servicePort: 15672
        host: host1.example.com
    ---
    apiVersion: networking.k8s.io/v1beta1
    kind: Ingress
    metadata:
      name: rabbitmq-admin-ingress-two
      annotations:
        kubernetes.io/ingress.class: "traefik"
    spec:
      rules:
      - http:
          paths:
          - path : /
            backend:
              serviceName: rabbitmq
              servicePort: 5672
        host: host2.example.com
    

    Or use two paths on a single Ingress - may involve some additional paths rewriting:

    ---
    apiVersion: networking.k8s.io/v1beta1
    kind: Ingress
    metadata:
      name: rabbitmq-admin-ingress
      annotations:
        kubernetes.io/ingress.class: "traefik"
    spec:
      rules:
      - http:
          paths:
          - path: /one
            backend:
              serviceName: rabbitmq
              servicePort: 15672
          - path : /two
            backend:
              serviceName: rabbitmq
              servicePort: 5672
        host: host1.example.com