Run a local process in Kubernetes with Telepresence

Learn how to develop and debug applications running in Kubernetes.

Kentaro Wakayama

Kentaro Wakayama

September 6, 2017

Run a local process in Kubernetes with Telepresence

We use Telepresence in our Kubernetes development stack at Coder Society. Telepresence lets you connect a process running locally on your laptop to a Kubernetes cluster. This is useful for development as well as debugging purposes since it gives you access to the running Kubernetes services without having to expose them publicly.

You can find the code which we use in this article on GitHub: https://github.com/coder-society/kubernetes-with-telepresence

Setup Telepresence

We assume that you already have a running Kubernetes cluster and kubectl installed.

On OS X you can install Telepresence with Homebrew:

$ brew cask install osxfuse  
$ brew install datawire/blackbird/telepresence

You can find installation instructions for other platforms here.

Verify that your Kubernetes cluster is running:

$ kubectl get nodes  
NAME                                              STATUS    AGE       VERSION  
ip-172-20-113-130.eu-central-1.compute.internal   Ready     14m       v1.7.0  
ip-172-20-119-81.eu-central-1.compute.internal    Ready     16m       v1.7.0  
ip-172-20-46-215.eu-central-1.compute.internal    Ready     14m       v1.7.0  
ip-172-20-56-176.eu-central-1.compute.internal    Ready     16m       v1.7.0  
ip-172-20-66-175.eu-central-1.compute.internal    Ready     14m       v1.7.0  
ip-172-20-76-235.eu-central-1.compute.internal    Ready     16m       v1.7.0

Start a shell with Telepresence

Run curl ifconfig.co to get your public IP address:

$ curl ifconfig.co  
37.209.72.147

Start a shell that proxies network traffic to Kubernetes and verify that the IP address differs from your public IP address:

$ telepresence --run-shell  
$ curl ifconfig.co  
52.29.24.213

Access a private Kubernetes service

We prepared a simple echo-server which you can deploy to your Kubernetes cluster:

$ kubectl apply -f [./echo-server/echo-server.yaml](https://github.com/coder-society/kubernetes-with-telepresence/blob/master/echo-server/echo-server.yaml)

Verify that the pods are running:

$ kubectl get pods  
NAME                           READY     STATUS    RESTARTS   AGE  
echo-server-1203620631-mk089   1/1       Running   0          6s  
echo-server-1203620631-pv53n   1/1       Running   0          6s  
echo-server-1203620631-r035z   1/1       Running   0          6s

The echo-server is accessible through a Kubernetes service which is not publicly exposed. You can use Telepresence to make a request to the service name. Execute the curl command with the --run option:

$ telepresence --run curl http://echo-server  
{"status":"ok","time":1502747082460,"hostname":"echo-server-1203620631-r035z"}

Run a local node process within a private Kubernetes cluster

We created a simple task-runner which makes every 3 seconds a request to the echo-server. You can start the local node process with the Telepresence — run option:

$ telepresence --run node [task-runner/index.js](https://github.com/coder-society/kubernetes-with-telepresence/blob/master/task-runner/index.js)  
{"status":"ok","time":1502747926293,"hostname":"echo-server-1203620631-r035z"}  
{"status":"ok","time":1502747929273,"hostname":"echo-server-1203620631-pv53n"}  
{"status":"ok","time":1502747932260,"hostname":"echo-server-1203620631-mk089"}

Swap a Kubernetes deployment with a local process

Deploy the echo-server and task-runner to the Kubernetes cluster:

$ kubectl apply -f [./echo-server/echo-server.yaml](https://github.com/coder-society/kubernetes-with-telepresence/blob/master/echo-server/echo-server.yaml)  
$ kubectl apply -f [./task-runner/task-runner.yaml](https://github.com/coder-society/kubernetes-with-telepresence/blob/master/task-runner/task-runner.yaml)

Verify that the pods are running:

$ kubectl get pods  
NAME                           READY     STATUS    RESTARTS   AGE  
echo-server-1203620631-mk089   1/1       Running   0          49m  
echo-server-1203620631-pv53n   1/1       Running   0          49m  
echo-server-1203620631-r035z   1/1       Running   0          49m  
task-runner-297154768-1btr3    1/1       Running   0          40m

Swap out the echo-server deployment and start the local node server which is exposed on port 3000:

$ telepresence --swap-deployment echo-server --expose 3000 --run node [echo-server/server.js](https://github.com/coder-society/kubernetes-with-telepresence/blob/master/echo-server/server.js)  
Echo Server listening on [http://localhost:3000](http://localhost:3000)  
Incoming request: 1502666001958  
Incoming request: 1502666004957  
Incoming request: 1502666007957

The server will now receive the traffic from the task-runner pod which is running inside the Kubernetes cluster.

Telepresence is an invaluable tool for developing and debugging applications running on Kubernetes. Its functionality to provide secure access to Kubernetes services can also be useful in other situations such as when running integration tests for your private Kubernetes services.

  • telepresence
  • kubernetes
ShareTweet
logoAlt

Content you want to read. By coders, for coders.

Right in your inbox.

By submitting you agree to us processing your personal data according to our privacy policy

Recommended

We use cookies 🍪 in order to provide you with the best browsing experience. The data collected by cookies is used to optimize the website. By continuing to browse the site you are agreeing to our use of cookies in accordance with our .