Search code examples
nginxkubernetesgoogle-kubernetes-engineload-balancingrtmp

How to set up RTMP protocol in Ingress Nginx?


I have a Kubernetes cluster set up on Google Kubernetes Engine. I have a pod that hosts an RTMP application. This application requires two ports, one for HTTP and one for RTMP. I have set up an ingress Nginx load balancer. It routes the HTTP traffic correctly. I want to be able to route the RTMP traffic as well. But when I write RTMP in my YAML file. It shows it as an unknown field. I am using skaffold and Google Cloud Build to deploy.

Here is the Ingress-srv file

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: 'true'
spec:
  rules:
    - http:
        paths:
          - path: /?(.*)
            backend:
              serviceName: stream-server-srv
              servicePort: 8000

What I want to be able to do is have an entry for RTMP on port 1935. That I can then use to stream love videos using RTMP protocol.


Solution

  • RTMP is a TCP-based protocol and standard Ingress does not support TCP services.

    The NGINX Ingress controller (which it looks like you are using) can be configured to expose TCP services.

    First, you'll need to make sure that you expose both the HTTP and RTMP ports in your stream-server-srv Service:

    apiVersion: v1
    kind: Service
    metadata:
      name: stream-server-srv
      namespace: default
    spec:
      selector:
        app: stream-server
      type: ClusterIP
      ports:
        - name: http-port
          port: 8000
          targetPort: 8000
          protocol: TCP
        - name: rtmp-port
          port: 1935
          targetPort: 1935
          protocol: TCP
    

    (replace default with your namespace)

    You will also need to make sure that the Service used to expose the NGINX ingress exposes port 1935 as well. For example:

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app.kubernetes.io/component: controller
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
        app.kubernetes.io/version: 1.2.0
      name: ingress-nginx-controller
      namespace: ingress-nginx
    spec:
      externalTrafficPolicy: Local
      ipFamilies:
      - IPv4
      ipFamilyPolicy: SingleStack
      ports:
      - appProtocol: http
        name: http
        port: 80
        protocol: TCP
        targetPort: http
      - appProtocol: https
        name: https
        port: 443
        protocol: TCP
        targetPort: https
      - name: rtmp
        port: 1935
        protocol: TCP
        targetPort: 1935
      selector:
        app.kubernetes.io/component: controller
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/name: ingress-nginx
      type: LoadBalancer
    

    Finally, you'll need to update / patch the NGINX tcp services ConfigMap:

    kubectl patch configmap tcp-services -n ingress-nginx --patch '{"data":{"1935":"default/stream-server-srv:1935"}}'
    

    (replace "default/stream-server-srv" with your namespace/servicename)