Service Preview has been replaced by Telepresence, these docs will remain as a historical reference. Learn more about Telepresence or go to the quick start guide.
Service Preview Tutorial
When Service Preview is used, incoming requests get routed by Ambassador to a Traffic Agent, which then routes traffic to the microservice. When a request meets a specific criteria (e.g., it has a specific HTTP header value), the Traffic Agent will route that request to the microservice running locally. The following video shows Service Preview in more detail:
Quick Start
Service Preview creates a connection between your local environment and the cluster. These connections are managed through the Traffic Manager, which is deployed in your cluster, and the edgectl
daemon, which runs in your local environment.
To get started with Service Preview, you'll need to download and install the edgectl
client.
Service Preview should already by installed in your cluster before starting this quick start. See the installation instructions for information on how to install Service Preview.
Intercepting Traffic
One of the main use cases of Service Preview is to intercept certain requests to services in your Kubernetes cluster and route them to your laptop instead.
Intercept with an HTTP header
Make sure sure that the
Hello
is installed. See the installation instructions.$ kubectl get svc,deployNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/hello ClusterIP 10.4.28.14 <none> 80/TCP 6m18sservice/kubernetes ClusterIP 10.4.16.1 <none> 443/TCP 25mNAME READY UP-TO-DATE AVAILABLE AGEdeployment.extensions/hello 1/1 1 1 6m18sLaunch a local service on your laptop. If you were debugging the
Hello
service, you might run a local copy in your debugger. In this example, we will start an arbitrary service on port 9000.# using Python$ python3 -m http.server 9000 &Serving HTTP on :: port 9000 (http://[::]:9000/) ...Make sure you are connected to the cluster to set up outbound connectivity and check that you can access the
Hello
service in the cluster withcurl
.$ edgectl connectAlready connected$ edgectl statusConnectedContext: default (https://localhost:6443)Proxy: ON (networking to the cluster is enabled)Interceptable: 1 deploymentsIntercepts: 0 total, 0 local$ curl -L helloHello, world!Set up an intercept. In this example, we’ll capture requests that have the
x-dev
header set to $USER.$ edgectl intercept availFound 1 interceptable deployment(s):1. hello in namespace default$ edgectl intercept listNo intercepts$ edgectl intercept add hello -n example -m x-dev=$USER -t localhost:9000Using deployment hello in namespace defaultAdded intercept "example"$ edgectl intercept list1. exampleIntercepting requests to hello when- x-dev: ark3and redirecting them to localhost:9000$ curl -L helloHello, world!$ curl -L -H x-dev:$USER hello<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Directory listing for /</title></head><body><h1>Directory listing for /</h1><hr><ul></ul><hr></body></html>As you can see, the second request, which includes the specified
x-dev
header, is served by the local server.Next, remove the intercept to restore normal operation.
$ edgectl intercept remove exampleRemoved intercept "example"$ curl -L -H x-dev:$USER helloHello, world!Requests are no longer intercepted.
Intercept with a Preview URL
Now let's set up an intercept with a preview URL.
Create or edit an existing
Host
resource to enable Preview URLs---apiVersion: getambassador.io/v2kind: Hostmetadata:name: preview-hostspec:hostname: {{AMBASSADOR_IP_OR_DOMAIN_NAME}}# [...]previewUrl:enabled: truetype: PathReplace
{{AMBASSADOR_IP_OR_DOMAIN_NAME}}
with the IP address or domain name of your Ambassador service and apply it withkubectl
kubectl apply -f preview-hostRefresh the edgectl connection for it to detect the new
Host
$ edgectl disconnectDisconnected$ edgectl connectConnecting to traffic manager in namespace ambassador...Connected to context k3s-default (https://172.20.0.3:6443)Now add an intercept and give it a try.
$ edgectl intercept availFound 1 interceptable deployment(s):1. hello in namespace default$ edgectl intercept listNo intercepts$ edgectl intercept add hello -n example-url -t 9000Using deployment hello in namespace defaultAdded intercept "example-url"Share a preview of your changes with anyone by visitinghttps://staging.example.com/.ambassador/service-preview/251b550a-66e4-47f3-aa5e-97801b4037a8/$ edgectl intercept list1. example-url(preview URL available)Intercepting requests to hello when- x-service-preview: 251b550a-66e4-47f3-aa5e-97801b4037a8and redirecting them to 127.0.0.1:9000Share a preview of your changes with anyone by visitinghttps://staging.example.com/.ambassador/service-preview/251b550a-66e4-47f3-aa5e-97801b4037a8/$ curl https://staging.example.com/hello/Hello, world!$ curl https://staging.example.com/.ambassador/service-preview/251b550a-66e4-47f3-aa5e-97801b4037a8/hello/<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Directory listing for /</title></head><body><h1>Directory listing for /</h1><hr><ul></ul><hr></body></html>As you can see, the second request, which uses the preview URL, is served by the local server.
Remove the intercept to restore normal operation.
$ edgectl intercept remove example-urlRemoved intercept "example-url"$ curl https://staging.example.com/.ambassador/service-preview/0efb6d52-9ddc-410d-8717-8db58bac2088/hello/Hello, world!Requests are no longer intercepted.
Outbound Services
Service Preview bridges your local and cluster DNS. This allows for the use case of using Service Preview as a debug tool for interacting with services in your cluster.
Make sure sure that the
Hello
service is installed. See the installation instructions.$ kubectl get svc,deployNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/hello ClusterIP 10.4.28.14 <none> 80/TCP 6m18sservice/kubernetes ClusterIP 10.4.16.1 <none> 443/TCP 25mNAME READY UP-TO-DATE AVAILABLE AGEdeployment.extensions/hello 1/1 1 1 6m18sMake sure you are still connected to the cluster.
$ edgectl connectAlready connected$ edgectl statusConnectedContext: default (https://34.72.18.227)Proxy: ON (networking to the cluster is enabled)Interceptable: 1 deploymentsIntercepts: 0 total, 0 local$ curl -L helloHello, world!
You are now able to connect to services directly from your laptop, as demonstrated by the curl
command above.
When you’re done working with this cluster, disconnect.
$ edgectl disconnectDisconnected$ edgectl statusNot connected
What's Next?
Multiple intercepts of the same deployment can run at the same time too. You can direct them to the same machine, allowing you to “or” together intercept conditions. Also, multiple developers can intercept the same deployment simultaneously. As long as their match patterns don’t collide, they don’t need to worry about disrupting one another.
Questions?
We’re here to help. If you have questions, join our Slack or contact us.