DEV Community

kelseyevans
kelseyevans

Posted on • Originally published at telepresence.io

Rapid development of Kubernetes services with Telepresence

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, helloworld.py:

#!/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()
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This will create a new Deployment and Service named 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!
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Additional Resources

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:

Have questions? Ask in the Telepresence Gitter chatroom or file an issue on GitHub.

This post originally appeared in the Telepresence documentation.

Top comments (1)

Collapse
 
ben profile image
Ben Halpern

Looks cool Kelsey. You may want to make an org account to make it more clear that this is coming from the source: dev.to/settings/organization