I am using Docker Desktop for Windows with WSL2 and I am trying to deploy the following Deployment to my Docker Desktop's Kubernetes cluster:
apiVersion: apps/v1
kind: Deployment
metadata:
name: udp-listener
labels:
app: udp-listener
spec:
replicas: 1
selector:
matchLabels:
app: udp-listener
template:
metadata:
labels:
app: udp-listener
spec:
containers:
- name: udp-listener
image: mendhak/udp-listener
ports:
- containerPort: 5005
hostPort: 5005
protocol: UDP
It is a simple container that exposes a UDP listener on port 5005. The container logs every UDP message it receives. It also exposes that port to outside the cluster on port 5005 of the host too.
The problem I am having is that when I try to connect to the UDP port from Windows using nc localhost 5005, it does not work. However, if I instead try to expose the container port using a NodePort service with the command kubectl expose deployment udp-listener --port=5005 --name=udp-listener-svc --protocol=UDP --type=NodePort and then run nc localhost EXPOSED_PORT, it works perfectly and I see every UDP message I send in the container's logs.
What is even stranger is that this does not seem to be an issue with TCP ports: if I try to deploy a TCP listener pod--instead of a UDP one--, change the protocol of the pod's hostPort to TCP, and then connect to it using the same netcat command from Windows, it works! It also works with a TCP NodePort service.
TL;DR:
- I can communicate from outside the cluster with a pod running a TCP listener container that is exposed through the container's
hostPort - However, I cannot communicate from outside the cluster with a pod running a UDP listener container that is exposed through the container's
hostPort