The author choosesScience and Technology Education FundAs aWrite for DOnationsPart of the program accepts donations.
Introduction to the
Application developers building microservices on Kubernetes often run into two main problems that slow them down.
- Slow feedback loops. Once the code changes have been made, they must be deployed to Kubernetes for testing. This requires container building, pushing to the container registry, and deploying to Kubernetes. This adds a few minutes to each code iteration.
- There is not enough memory or CPU locally. Developers try to speed up the feedback loop by running Kubernetes locally with miniKube or similar tools. However, resource-demanding applications quickly outstrip locally available computing and memory.
Telepresence is a cloud native computing Foundation project for fast, efficient development on Kubernetes. With Telepresence, you can run your services locally and your other applications in the cloud. Telepresence establishes a two-way network connection between your Kubernetes cluster and your local workstation. This way, your services running locally can communicate with services in the cluster and vice versa. This enables you to use the computing and memory resources of the cluster without having to go through a full deployment cycle for each change.
In this tutorial, you will configure Telepresence on your local machine running MacOS to work with the Kubernetes cluster. You will intercept traffic to your cluster and redirect it to your local environment.
The premise condition
To complete this tutorial, you will need.
-
A Kubernetes cluster, such as DigitalOcean Kubernetes. We will use DigitalOcean Kubernetes in this tutorial, but you can also use an existing Kubernetes cluster (local or cloud).
-
Kubectl, installed on your workstation and configured to connect to the Kubernetes cluster.
-
A local development environment for Node.js. You can follow how to install Node.js and create a local development environment.
Step 1 – Install Telepresence
In this step, you will install Telepresence and connect it to your Kubernetes cluster. First, make sure you have Kubectl configured and that you can connect to your Kubernetes cluster from your local workstation. Use the get services command to check the status of your cluster.
kubectl get services
Copy the code
The output will look like this, listing the IP address of your own cluster.
OutputNAME TYPE cluster-ip external-ip PORT(S) AGE kubernetes ClusterIP 10.245.0.1 < None > 443/TCP 116mCopy the code
Next you will install Telepresence locally. Telepresence is a single binary file.
Use curl to download the latest binary file for MacOS (about 60MB).
sudo curl -fL https://app.getambassador.io/download/tel2/darwin/amd64/latest/telepresence -o /usr/local/bin/telepresence
Copy the code
Then use chmod to make the binary executable.
sudo chmod a+x /usr/local/bin/telepresence
Copy the code
Now that you have Telepresence installed locally, you can verify that it works by connecting to your Kubernetes cluster.
telepresence connect
Copy the code
You should see the output below.
Output
Launching Telepresence Daemon v2.2.0 (api v3)
Connecting to traffic manager...
Connected to context default (https://<cluster public URL)
Copy the code
If Telepresence is not connected, please check your Kubectl configuration.
Use the status command to connect to the Kubernetes API server to verify that Telepresence is working properly.
telepresence status
Copy the code
You should see the following output. Telepresence Proxy: ON Indicates that a Telepresence Proxy has been configured to access services ON the cluster.
Output Root Daemon: Running Version: v2.2.0 (API 3) Primary DNS: "" Fallback DNS: "" User Daemon: Running Version: V2.2.0 (API 3) Ambassador Cloud: Logged Out Status: Connected Kubernetes Server: https://e5488ea3-6455-4fc7-be25-09d1d90bde82.k8s.ondigitalocean.com Kubernetes context: your_kubernetes_context Telepresence proxy: ON (networking to the cluster is enabled) Intercepts : 0 totalCopy the code
When you use TelePresence Connect, on the server side, TelePresence creates a namespace called Ambassador and runs a traffic manager. On the client, Telepresence has configured DNS for local access to the remote server. This means you don’t need to use Kubectl port-forward to manually configure access to local services. When you access a remote service, DNS resolves to a specific IP address. See the Telepresence architecture documentation for more details.
Now you can connect to a remote Kubernetes cluster from your local workstation just as the Kubernetes cluster runs on your laptop. Next, you’ll try out a sample application.
Step 2 — Add a Sample Node.js application
In this step, you’ll use a simple Node.js application to simulate a complex service running on a Kubernetes cluster. Instead of creating the file locally, you will access it from DockerHub and deploy it to your cluster from there. This file is called hello-node and returns a text string.
var http = require('http');
var handleRequest = function(request, response) {
console.log('Received request for URL: ' + request.url);
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello, Node! ');
response.end();
};
http.createServer(handleRequest).listen(9001);
console.log('Use curl
:9001 to access this server... '
);
Copy the code
Use the kubectl create Deployment command to create a deployment called Hello Node.
Kubectl create deployment hello-node --image=docommunity/hello-node:1.0Copy the code
You should see the output below.
Outputdeployment.apps/hello-node created
Copy the code
Use the get pod command to confirm that deployment has occurred and that the application is now running on the cluster.
kubectl get pod
Copy the code
The output will show that READY is 1/1.
OutputNAME READY STATUS RESTARTS AGE
hello-node-86b49779bf-9zqvn 1/1 Running 0 11s
Copy the code
Use the Expose Deployment command to make the application available on port 9001.
kubectl expose deployment hello-node --type=LoadBalancer --port=9001
Copy the code
The output will look something like this.
Outputservice/hello-node exposed
Copy the code
Use the kubectl get SVC command to check whether the load balancer is running.
kubectl get svc
Copy the code
The output will look like this, with your own IP address.
OutputNAME TYPE cluster-ip external-ip PORT(S) AGE hello-node LoadBalancer 10.245.75.48 < Pending > 9001:30682/TCP 4s Kubernetes ClusterIP 10.245.0.1 < None > 443/TCP 6dCopy the code
If you are using local Kubernetes without LoadBalancer support, the external IP value of LoadBalancer will always be displayed as
Next, verify that the application is running by accessing the load balancer with curl.
curl <ip-address>:9001
Copy the code
If you are not running a load balancer, you can use curl to access the service directly.
curl <servicename>.<namespace>:9001
Copy the code
The output will look something like this.
OutputHello, Node!
Copy the code
Next, use the telepresence connect command to connect the Telepresence to the cluster.
telepresence connect
Copy the code
This allows you to access all remote services as if they were local, so you can access services by name.
curl hello-node.default:9001
Copy the code
You will receive the same response as when you access the service through its IP.
OutputHello, Node!
Copy the code
The service is already up and running on the cluster, and you can access it remotely. If you make any changes to the Hello-node. js application, you need to take the following steps.
- Modify the application.
- Rebuild the container image.
- Push it to the container registry.
- Deploy to Kubernetes.
This is a big step. You can use tools (automated pipes, such as Skaffold) to reduce manual work. But the steps themselves are inescapable.
Now you will build another version of our Hello-Node app and test it using Telepresence without having to build a container image or push it to a registry or even deploy it to Kubernetes.
Step 3 – Run the new version of the service locally
In this step, you will modify the existing Hello-Node application on your local machine. You will then use the Telepresence interceptor to send traffic to the local version _. Interceptors redirect traffic for your cluster to the local version of the service, so you can continue to work in your development environment.
Create a new file containing the modified version of the sample application.
nano hello-node-v2.js
Copy the code
Add the following code to the new file.
hello-node-v2.js
var http = require('http');
var handleRequest = function(request, response) {
console.log('Received request for URL: ' + request.url);
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello, Node V2! ');
response.end();
};
http.createServer(handleRequest).listen(9001);
Copy the code
Save and exit the file.
Start the service with Node.
node hello-node-v2.js
Copy the code
Leave the service running, then open a new terminal window and access the service.
curl localhost:9001
Copy the code
The output will look something like this.
OutputHello, Node V2!
Copy the code
However, this service only runs locally. If you’re trying to access a remote server, it’s currently running version 1 of Hello-Node. To solve this problem, you need to enable an interceptor to route all traffic in the cluster to the local version of the Hello-Node service.
Use the intercept command to set up the intercept.
telepresence intercept hello-node --port 9001
Copy the code
The output will look something like this.
OutputUsing deployment hello-node intercepted Intercept name : hello-node State : ACTIVE Destination : 127.0.0.1:9001 Volume Mount Error: SSHFS is not installed on your local machine Intercepting: all TCP connectionsCopy the code
Use the status command to check that interception has been set up correctly.
telepresence status
Copy the code
The output will look something like this.
OutputRoot Daemon: Running Version: v2.2.0 (API 3) Primary DNS: "" Fallback DNS: "" User Daemon: Running Version: V2.2.0 (API 3) Ambassador Cloud: Logged Out Status: Connected Kubernetes Server: https://e5488ea3-6455-4fc7-be25-09d1d90bde82.k8s.ondigitalocean.com Kubernetes context: <your_kubernetes_context> Telepresence proxy: ON (networking to the cluster is enabled) Intercepts : 1 total hello-node: user@contextCopy the code
Now use curl to access the remote service as before.
curl hello-node.default:9001
Copy the code
The output will look something like this.
OutputHello, Node V2!
Copy the code
Any message sent to a service on the cluster is now redirected to the local service. This is useful during development because you can avoid a deployment cycle (build, push, deploy) for each individual change to your code.
conclusion
In this tutorial, you have Telepresence installed on your local machine and demonstrated how to make code changes in your local environment without having to deploy to Kubernetes with each change. For more tutorials and information on Telepresence, see the Telepresence documentation.