<p>In kubernetes, I have the following service:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: test-service
  namespace: default
spec:
  ports:
    - name: tcp
      protocol: TCP
      port: 5555
      targetPort: 5555    
    - name: udp
      protocol: UDP
      port: 5556
      targetPort: 5556
  selector:
    tt: test
</code></pre>
<p>Which exposes two ports, 5555 for TCP and 5556 for UDP. How can expose these ports externally using the same ingress? I tried using nginx to do something like the following but it doesn't work. It complains that mixed ports are not supported.</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: ingress-nginx
data:
  5555: &quot;default/test-service:5555&quot;
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: udp-services
  namespace: ingress-nginx
data:
  5556: &quot;default/test-service:5556&quot;
---
apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  type: LoadBalancer
  ports:
    - name: tcp
      port: 5555
      targetPort: 5555
      protocol: TCP
    - name: udp
      port: 5556
      targetPort: 5556
      protocol: UDP
  args:
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
</code></pre>
<p>Is there a way to do this?</p>

Mohamed
<p>Most cloud providers do not support UDP load-balancing or mix protocols and might have cloud-specific methods to bypass this issue.</p>
<blockquote>
<p>The DigitalOcean CPI does not support mixed protocols in the Service
definition, it accepts TCP only for load balancer Services. It is
possible to ask for HTTP(S) and HTTP2 ports with Service annotations.</p>
<p>Summary: The DO CPI is the current bottleneck with its TCP-only limitation. As long as it is there the implementation of this feature
will have no effect on the DO bills.</p>
</blockquote>
<p>See more: <a href="https://github.com/kubernetes/enhancements/pull/1438/files" rel="nofollow noreferrer">do-mixed-protocols</a>.</p>
<p>A simple solution that may solve your problem is to create a reverse
proxy system on a standalone server - using Nginx and route UDP and
TCP traffic directly to your Kubernetes service.</p>
<p>Follow these two steps:</p>
<p><strong>1.</strong>  Create a NodePort service application</p>
<p><strong>2.</strong>  Create a small server instance and run Nginx with LB config on it</p>
<p>Use <a href="https://kubernetes.io/docs/concepts/services-networking/service/#nodeport" rel="nofollow noreferrer"><strong>NodePort</strong></a>  type of service which will expose your application on your cluster nodes, and makes them accessible through your node IP on a static port. This type supports multi-protocol services. Read more about services <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">here</a>.</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: test-service
  namespace: default
spec:
  type: NodePort
  ports:
  - name: tcp
    protocol: TCP
    port: 5555
    targetPort: 5555
    nodePort: 30010    
  - name: udp
    protocol: UDP
    port: 5556
    targetPort: 5556
    nodePort: 30011
  selector:
    tt: test
</code></pre>
<p>For example this service exposes <code>test</code> pods’ port 5555 through <code>nodeIP:30010</code> with the TCP protocol and port 5556 through <code>nodeIP:30011</code> with UDP. Please adjust ports according to your needs, this is just an example.</p>
<p>Then create a small server instance and run <code>Nginx</code> with LB config.</p>
<p>For this step, you can get a small server from any cloud provider.
Once you have the server,  <code>ssh</code>  inside and run the following to install Nginx:</p>
<pre><code>$ sudo yum install nginx
</code></pre>
<p>In the next step, you will need your node IP addresses, which you can get by running:</p>
<pre><code>$ kubectl get nodes -o wide.
</code></pre>
<p><strong>Note:</strong>  If you have private cluster without external access to your nodes, you will have to set up a point of entry for this use ( for example NAT gateway).</p>
<p>Then you have to add the following to your <code>nginx.conf</code> (run command <code>$ sudo vi /etc/nginx/nginx.conf</code>):</p>
<pre><code>worker_processes 1;  
events {  
    worker_connections  1024;  
}  
stream {  
  upstream tcp_backend {  
    server &lt;node ip 1&gt;:30010;  
    server &lt;node ip 2&gt;:30010;  
    server &lt;node ip 3&gt;:30010;  
         ...  
  }  
  upstream udp_backend {  
    server &lt;node ip 1&gt;:30011;  
    server &lt;node ip 2&gt;:30011;  
    server &lt;node ip 3&gt;:30011;  
         ...  
  }  
  server {  
      listen 5555;  
      proxy_pass tcp_backend;  
      proxy_timeout 1s; }  
  server {  
      listen 5556 udp;  
      proxy_pass udp_backend;  
      proxy_timeout 1s;  
  }  
}
</code></pre>
<p>Now you can start your Nginx server using command:</p>
<pre><code>$ sudo /etc/init.d/nginx start
</code></pre>
<p>If you have already started you Nginx server before applying changes to your config file, you have to restart it - execute commands below:</p>
<pre><code>$ sudo netstat -tulpn    # Get the PID of nginx.conf program  
$ sudo kill -2 &lt;PID of nginx.conf&gt;  
$ sudo service nginx restart
</code></pre>
<p>And now you have UDP/TCP LoadBalancer which you can access through  <code>&lt;server IP&gt;:&lt;nodePort&gt;</code>.</p>
<p>See more: <a href="https://medium.com/asl19-developers/build-your-own-cloud-agnostic-tcp-udp-loadbalancer-for-your-kubernetes-apps-3959335f4ec3" rel="nofollow noreferrer">tcp-udp-loadbalancer</a>.</p>
