arrow-rightgithublinkedinscroll-to-topyoutubezig-zag

Exposing TCP and UDP Services via ingress on Minikube

Last Updated On

Minikube comes with ingress addon which uses NGINX Ingress Controller under the hood. This addon is capable of forwarding http/https traffic to a specific service but lacks the ability to route TCP/UDP traffic which might be necessary for messaging services, databases, etc. There's a related issue on github.

Due to the fact that there is no out of the box solution yet, here's workaround how to forward TCP and UDP traffic on Minikube:

  1. Disable default ingress addon (if it's enabled) by executing following command:

    $ minikube addons disable ingress
    ✅ ingress was successfully disabled

    You can list addons by executing the following command:

    $ minikube addons list
    ... 
    - ingress: disabled
    ...
  2. Deploy standalone nginx ingress controller to minikube:

    kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml

    Here's a link to the official documentation if you're wondering where it came from.

  3. As a next step, we need to tweak the configuration of nginx-ingress-controller by enabling host network (more about host network in this article - Accessing Kubernetes Pods from Outside of the Cluster).

    Find whether nginx-ingress-controller controller pod is running:

    $ kubectl get deploy nginx-ingress-controller -n ingress-nginx
    
    $ NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
      nginx-ingress-controller   1/1     1            1           8m39s

    Get current controller configuration and save it to a file. In this example, I will create a file in the current directory called nginx-ingress-controller.yaml :

    kubectl get deploy nginx-ingress-controller -n ingress-nginx -o yaml>> nginx-ingress-controller.yaml

    Open it and add the following configuration:

    spec.template.spec.hostNetwork = true

    In my situation this was right above the containers key:

    ...
    spec:
       hostNetwork: true # <--
       containers:
       - args:
         - /nginx-ingress-controller
         - --configmap=$(POD_NAMESPACE)/nginx-configuration
         - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
         - --udp-services-configmap=$(POD_NAMESPACE)/udp-services
         - --publish-service=$(POD_NAMESPACE)/ingress-nginx
         - --annotations-prefix=nginx.ingress.kubernetes.io
         env:
    ...

    Save the edited file and apply it by executing:

    $ kubectl apply -f nginx-ingress-controller.yaml 
    deployment.extensions/nginx-ingress-controller configured

    Sidenote: sometimes it might be required to apply this config by using --force flag:

    $ kubectl apply -f nginx-ingress-controller.yaml --force
    deployment.extensions/nginx-ingress-controller configured

    Special thanks to MerlinPong who pointed to this fix.

  4. Enable TCP/UDP forwarding like it's showing in the example:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: tcp-services
      namespace: ingress-nginx
    data:
      3306: "default/mysql-service:3306" # You should only change this config, lines above should stay the same