Hey Guys, Gary Here.
With all of the fun stuff happening around Kubernetes and Cloud Foundry, we decided to do some fun stuff to play around with it! One of the (few) capabilities we don’t have with Cloud foundry that we can get with Kubernetes is UDP routing.
To learn more about why UDP routing doesn’t work with the containers in Diego runtime (yet, but will), check out ONSI’s proposal for the feature.
UDP Routing. Why would you use it? In short, for applications that continually post data that isn’t important enough, or would soon be replaced with a more recent copy anyways, UDP packets can be a less intensive alternative than using the TCP routing solution. Or, if you’re really hardcore, you could implement your own verification with UDP, but that would be a blog post in itself 🙂
Overall, setting up Kubernetes and getting it to expose ports was very simple. If you are reading this without any Kubernetes setup, go check out minikube. Even better, you could set up a GCP cluster, vSphere, or (gasp) AWS and follow along. The kubectl commands should be about the same either way.
Once you’ve got your instance set up, check out our kube-udp-tennis repo on Github. We use this repo to store very simple python scripts that accept environment variables for ports and will either send or receive messages based on which script we execute. We also baked these into a Dockerfile to allow Kubernetes to reference an image on docker hub.
Before you worry about deploying your own docker images, know that you are not required to for this example. If you were to deploy the listener, add the service link, then go ahead and deploy the server, this solution would be a working UDP connection! This is because it’s referencing our existing images already on the Docker Hub. Before I go and give you the commands, I want to explain what they do.
kubectl apply -f udplisten-deployment.yaml
this command will go into the udplisten-deployment.yaml file, which gives the specification for our udp-listen application. We spec this out so we can extend it for the udp-listen service.
kubectl apply -f udplisten-service.yaml
this command will go into the udplisten-service.yaml file, which after the udplisten deployment has been made live, will allow us to talk into the port through the service functionality in Kubernetes. Here’s the documentation for services.
At this point, we will have the kubernetes udplisten service running, and we will be ready to deploy our dummy application to talk into it.
kubectl apply -f udpserver-deployment.yaml
This will deploy the udpserver application, and should ping messages into the udplisten-service, which you should see through the logs in the service’s pod.
The way that the udp-server.py application can find and ping into the udplisten-service is by leveraging the Kubernetes Service Functionality. Basically, when we start Kubernetes services, we will be able to find those services using environment variables. From the documentation:
For example, the Service
"redis-master" which exposes TCP port 6379 and has been allocated cluster IP address 10.0.0.11 produces the following environment variables:
[crayon-5bf14522d9566650618598 inline="true" ]<span class="nv">REDIS_MASTER_SERVICE_HOST</span><span class="o">=</span>10.0.0.11
<span class="nv">REDIS_MASTER_SERVICE_PORT</span><span class="o">=</span>6379
<span class="nv">REDIS_MASTER_PORT</span><span class="o">=</span>tcp://10.0.0.11:6379
<span class="nv">REDIS_MASTER_PORT_6379_TCP</span><span class="o">=</span>tcp://10.0.0.11:6379
<span class="nv">REDIS_MASTER_PORT_6379_TCP_PROTO</span><span class="o">=</span>tcp
<span class="nv">REDIS_MASTER_PORT_6379_TCP_PORT</span><span class="o">=</span>6379
<span class="nv">REDIS_MASTER_PORT_6379_TCP_ADDR</span><span class="o">=</span>10.0.0.11
We search, therefore, for the udplistener_service_host and udplistener_service_port to communicate with the udplistener pods directly. Since we defined the UDP protocol as network traffic into the service, this works right out of the box!
Thanks for reading everyone, as always, reach out to us on twitter @DellEMCDojo, or me specifically @garypwhitejr, or post on the blog to get some feedback. Let us know what you think!
Until next time,
Cloud Foundry, Open Source, The way. #DellEMCDojo