Rapid Development of Kubernetes Services With Telepresence
Telepresence is an open source tool that simplifies the development of multi-container applications running on Kubernetes.
Join the DZone community and get the full member experience.Join For Free
Imagine you're developing a new Kubernetes service. Typically, the way you'd test is by changing the code, rebuilding the image, pushing the image to a Docker registry, and then redeploying the Kubernetes
Deployment. This can be slow.
Or, you can use Telepresence. Telepresence will proxy a remote
Deployment to a process running on your machine. That means you can develop locally, editing code as you go, but test your service inside the Kubernetes cluster.
Let's say you're working on the following minimal server,
#!/usr/bin/env python3 from http.server import BaseHTTPRequestHandler, HTTPServer class RequestHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'text/plain') self.end_headers() self.wfile.write(b"Hello, world!\n") return httpd = HTTPServer(('', 8080), RequestHandler) httpd.serve_forever()
You start a proxy inside your Kubernetes cluster that will forward requests from the cluster to your local process, and in the resulting shell you start the web server:
localhost$ telepresence --new-deployment hello-world --expose 8080 localhost$ python3 helloworld.py
This will create a new
hello-world, which will listen on port 8080 and forward traffic to the process on your machine on port 8080.
You can see this if you start a container inside the Kubernetes cluster and connect to that
Service. In a new terminal run:
localhost$ kubectl --restart=Never run -i -t --image=alpine console /bin/sh kubernetes# wget -O - -q http://hello-world:8080/ Hello, world!
Now, switch back to the other terminal, kill
helloworld.py and edit it so it returns a different string. For example:
python3 helloworld.py ^C localhost$ sed s/Hello/Goodbye/g -i helloworld.py localhost$ grep Goodbye helloworld.py self.wfile.write(b"Goodbye, world!\n") localhost$ python3 helloworld.py
Now that we've restarted our local process with new code, we can send it another query from the other terminal where we have a shell running inside a Kubernetes pod:
kubernetes# wget -O - -q http://hello-world:8080/ Goodbye, world! kubernetes# exit
And there you have it: You edit your code locally, and changes are reflected immediately to clients inside the Kubernetes cluster without having to redeploy, create Docker images, and so on.
If you're interested in trying Telepresence on your own you can install locally with Homebrew, apt, or dnf.
Or check out these other tutorials:
- Debugging services on Kubernetes.
- Using Telepresence with Google Container Engine.
- Getting started with OpenShift and Telepresence.
- Telepresence and Minikube.
Published at DZone with permission of Abhay Saxena. See the original article here.
Opinions expressed by DZone contributors are their own.