Go & Kubernetes: Rapidly Developing Golang Microservices
Tags: go, kubernetes, microservices, telepresence
Published: 2021-03-03 19:06:55


### Build a cloud development environment with Telepresence & Golang

Kubernetes is a container orchestration platform that enables its users to deploy and scale their microservice applications at any scale: from one service to thousands of services. Unleashing the power of Kubernetes is often more complicated than it may initially seem — the learning curve for application developers is particularly steep. Knowing what to do is just half the battle, then you have to choose the best tools to do the job. So how do Go developers create a development workflow on Kubernetes that is fast and effective?

Application developers face two unique challenges when trying to create productive development workflows on Kubernetes.

 1. Most development workflows are optimized for local development, and Kubernetes applications are designed to be native to the cloud.

 2. As Kubernetes applications evolve into complex microservice architectures, the development environments also become more complex as every microservice adds additional dependencies. These services quickly start to need more resources than are available in your typical local development environment.

In this tutorial, we’ll set up a development environment for Kubernetes and make a change to a Golang microservice. Normally to develop locally, we’d have to wait for a container build, push to registry and deploy to see the effect of our code change. Instead, we’ll use [Telepresence](http://www.getambassador.io/products/telepresence/) to see the results of our change instantly.

## Step 1: Deploy a Sample Microservices Application

For our example, we’ll make code changes to a Go service running between a resource-intensive Java service and a large datastore. We’ll start by deploying a sample microservice application consisting of 3 services:

* **VeryLargeJavaService**: A memory-intensive service written in Java that generates the front-end graphics and web pages for our application

* **DataProcessingService**: A Golang service that manages requests for information between the two services.

* **VeryLargeDataStore**: A large datastore service that contains the sample data for our Edgey Corp store.

Note: We’ve called these VeryLarge services to emphasize the fact that your local environment may not have enough CPU and RAM, or you may just not want to pay for all that extra overhead for every developer.

![](https://cdn-images-1.medium.com/max/3200/0*7J_48_5o8juPX5E6)

In this architecture diagram, you’ll notice that requests from users are routed through an ingress controller to our services. For simplicity’s sake, we’ll skip the step of [deploying an ingress controller](https://www.getambassador.io/docs/latest/topics/install/install-ambassador-oss/#kubernetes-yaml) in this tutorial. If you’re ready to use Telepresence in your own setup and need a simple way to set up an ingress controller, we recommend checking out the [Ambassador Edge Stack](https://www.getambassador.io/products/edge-stack/) which can be easily configured with the [K8s Initializer](https://app.getambassador.io/initializer).

Let’s deploy the sample application to your Kubernetes cluster:

```bash
    kubectl apply -f [https://raw.githubusercontent.com/datawire/edgey-corp-go/main/k8s-config/edgey-corp-web-app-no-mapping.yaml](https://raw.githubusercontent.com/datawire/edgey-corp-go/main/k8s-config/edgey-corp-web-app-no-mapping.yaml)
```

## Step 2: Set up your local Go development environment

We’ll need a local development environment so that we can edit the `DataProcessingService` service. As you can see in the architecture diagram above, the `DataProcessingService` is dependent on both the `VeryLargeJavaService` and the `VeryLargeDataStore`, so in order to make a change to this service, we’ll have to interact with these other services as well. Let’s get started!

 1. Clone the repository for this application from GitHub.

```bash
    git clone [https://github.com/datawire/edgey-corp-go.git](https://github.com/datawire/edgey-corp-go.git)
```

2. Change directories into the DataProcessingService

```bash
    cd edgey-corp-go/DataProcessingService
```

3. Start the Go server:

```bash
    go build main.go && ./main
```

4. See your service running!

```bash
    10:23:41 app | Welcome to the DataProcessingGoService!
```

5. In another terminal window, curl localhost:3000/color to see that the service is returning blue.

```bash
    $ curl localhost:3000/color

    “blue”
```

## Step 3: Rapid Development with Telepresence

Instead of waiting for a container image to build, pushed to a repository, and deployed to our Kubernetes cluster we are going to use Telepresence, an open source Cloud Native Computing Foundation project. Telepresence creates a bidirectional network connection between your local development and the Kubernetes cluster to enable [fast, efficient Kubernetes development](https://www.getambassador.io/use-case/local-kubernetes-development/).

 1. Download Telepresence (~60MB):

```bash
    # Mac OS X
    sudo curl -fL [https://app.getambassador.io/download/tel2/darwin/amd64/latest/telepresence](https://app.getambassador.io/download/tel2/darwin/amd64/latest/telepresence) -o /usr/local/bin/telepresence

    #Linux
    sudo curl -fL https://app.getambassador.io/download/tel2/linux/amd64/latest/telepresence -o /usr/local/bin/telepresence
```

2. Make the binary executable

```bash
    $ sudo chmod a+x /usr/local/bin/telepresence
```

3. Test Telepresence by connecting to the remote cluster

```bash
    $ telepresence connect
```

4. Send a request to the Kubernetes API server:

```bash
    $ curl -ik [https://kubernetes.default.svc.cluster.local](https://kubernetes.default.svc.cluster.local)

    HTTP/1.1 401 Unauthorized
    Cache-Control: no-cache, private
    Content-Type: application/json
    Www-Authenticate: Basic realm="kubernetes-master"
    Date: Tue, 09 Feb 2021 23:21:51 GMT
```

Great! You’ve successfully configured Telepresence. Right now, Telepresence is intercepting the request you’re making to the Kubernetes API server, and routing over its direct connection to the cluster instead of over the Internet.

## Step 4: Intercept Your Golang Service

An intercept is a routing rule for Telepresence. We can create an intercept to route traffic intended for the `DataProcessingService` in the cluster and instead route all of the traffic to the *local* version of the `DataProcessingService` running on port 3000.

 1. Create the intercept

```bash
    telepresence intercept dataprocessingservice — port 3000
```

2. Access the application directly with Telepresence. Visit [http://verylargejavaservice:8080](http://verylargejavaservice:8080). Again, Telepresence is intercepting requests from your browser and routing them directly to the Kubernetes cluster.

 1. Now, we’ll make a code change. Open edgey-corp-go/DataProcessingService/main.go and change the value of the color variable from blue to orange. Save the file, stop the previous server instance and start it again with go build main.go && ./main.

 2. Reload the page in your browser and see how the color has changed from blue to orange!

That’s it! With Telepresence we saw how quickly we can go from editing a local service to seeing how these changes will look when deployed with the larger application. When you compare it to our original process of building and deploying a container after every change, it’s very easy to see how much time you can save especially as we make more complex changes or run even larger services.

## Learn More about Telepresence

Today, we’ve learned how to use Telepresence to rapidly iterate on a Golang microservice running in Kubernetes. Now, instead of waiting for slow local development processes, we can iterate quickly with an instant feedback loop and a productive cloud native development environment.

If you want to learn more about Telepresence, check out the following resources:

* [Read the docs](https://www.getambassador.io/docs/latest/telepresence/quick-start/qs-go/)

* Watch the [demo video](https://www.youtube.com/watch?v=W_a3aErN3NU)

* Read more about [Intercepts](https://www.getambassador.io/docs/latest/telepresence/howtos/intercepts/#intercepts)

* Learn about [Preview URLs](https://www.getambassador.io/docs/pre-release/telepresence/howtos/preview-urls/#collaboration-with-preview-urls) for easy collaboration with teammates

* [Join our Slack channel](https://d6e.co/slack) to connect with the Telepresence community

In our next tutorial, we’ll use Telepresence to set up a local Kubernetes development environment and then use Delve to set breakpoints and debug a broken service. To be notified when more tutorials are available, make sure to [check out our website](http://www.getambassador.io) or [follow us on Twitter](http://www.twitter.com/ambassadorlabs).