Building Cloud-Native Services with Dapr, Go, and Kubernetes — Part 1

Vladimir Vivien
5 min readMay 28, 2024

Getting Started with Dapr and Kubernetes

This series explores how to get started with Dapr to build distributed Go services deployed on Kubernetes.

The source code for the series is hosted at
github.com/vladimirvivien/dapr-examples

Introduction to Dapr

The Distributed Application Runtime (Dapr) is a Cloud Native Computing Foundation (CNCF) project that exposes a set of tools and API abstractions for creating distributed applications. Dapr can be best described as having two main characteristics:

  • A collection of standard components to build highly distributed and resilient service-based applications using your choice of language
  • A portable runtime that makes it easy to deploy your applications in different environments including local, bare metal/VM, containerized, Kubernetes, or cloud managed

Dapr building blocks and components

A Dapr application is comprised of loosely-coupled service components that can communicate either directly via HTTP / gRPC or using a message-passing scheme via a pub/sub mechanism.

The Dapr SDK provides a collection of standard components that encapsulate best practices for building and deploying distributed service-based applications as depicted below (source dapir.io):

Source: dapr.io

You can use Dapr components for the followings:

  • To build publish/subscribe event-base applications
  • Create HTTP / gRPC based services
  • Deploy secret management with your application
  • Automatically bind to data input sources and output sinks
  • Plugin storage providers for application statement management
  • Deploy scalable processes using the Actor pattern

Because I initially started this blog post many months ago, the previous figure is now outdated. The Dapr community has added even more building blocks since then, as shown below.

Source dapr.io/docs/concepts/overview

Additionally Dapr comes with more building blocks:

  • A distributed lock to synchronize data access across multiple services
  • Use workflows for long-running, multi-service processes
  • A cryptographic component to encrypt / decrypt data across services

For further detail on all of Dapr components, read here.

The Dapr sidecar architecture

One attractive aspect of Dapr is that your service code can be developed completely decoupled from the Dapr runtime. You can test your service code using your native programming language’s tools and libraries.

The Dapr runtime runs as a separate process known as a sidecar. The sidecar is the heart of the Dapr runtime. Beside exposing the Dapr API, part of what it does is to proxy communication between deployed components.

Source: Dapr.io side car concept

When a request is sent to an app/service, that request is first routed to the Dapr sidecar (attached to the service), then the request is forwarded to the service’s process. Conversely, when the service needs to send data out, it sends it to the Dapr sidecar which routes it to the proper component.

Getting started with Go + Dapr + Kubernetes

Before you can build Dapr-managed Go applications to run on Kubernetes, you must have the following prerequisites:

You can learn more about Kubernetes and Dapr requirements here.

Install Go

This blog post uses Go for its example. Next, follow instructions here to install Go on your local machine if you don’t already have it.

Create a Kubernetes cluster

You will need to create a Kubernetes cluster to run your applications for this series. You can use any Kubernetes environment at your disposal, however for this example we will use Kind.

If you choose to use Kind, you will first need to install Docker (or a compatible OCI engine) to host and run your OCI-compliant Kubernetes images and your applications.

Once you have Kind installed, create a simple cluster named dapr-cluster using the example cluster config in the GitHub repository:

kind create cluster --config ./configs/kind-cluster.yaml --name dapr-cluster

Setup Dapr on Kubernetes

Dapr makes it easy to install its control-plane components on a Kubernetes cluster. The first step to running Dapr is to install its tools locally:

Next, you can use the dapr command-line tool (or Helm charts) to install the Dapr control-plane components on a running Kubernetes cluster:

dapr int --kubernetes

You can verify the deployment of the Dapr control plane components on Kubernetes using the kubectl command shown below:

$> kubectl get pods -n dapr-system
NAME READY STATUS RESTARTS AGE
dapr-dashboard-5858b7d9d8-9l85x 1/1 Running 1 (15d ago) 15d
dapr-operator-5799f9765b-qjm4w 1/1 Running 5 (15d ago) 15d
dapr-placement-server-0 1/1 Running 1 (15d ago) 15d
dapr-sentry-597bf4cdf9-c9tts 1/1 Running 2 (15d ago) 15d
dapr-sidecar-injector-568c99f5b8-rqlfz 1/1 Running 5 (15d ago) 15d 9m5s

Alternatively, you can use the dapr command line tool to retrieve a curated list of the same components:

$> dapr status --kubernetes
NAME NAMESPACE HEALTHY STATUS REPLICAS VERSION AGE CREATED
dapr-placement-server dapr-system True Running 1 1.13.2 15d 2024-04-27 09:07.15
dapr-dashboard dapr-system True Running 1 0.14.0 15d 2024-04-27 09:07.17
dapr-operator dapr-system True Running 1 1.13.2 15d 2024-04-27 09:07.15
dapr-sidecar-injector dapr-system True Running 1 1.13.2 15d 2024-04-27 09:07.15
dapr-sentry dapr-system True Running 1 1.13.2 15d 2024-04-27 09:07.15

The output above shows that your environment is ready to run Dapr-managed distributed applications.

Next step

This post provided walked through of Dapr and showed how to set it up with a local Kubernetes environment. In the next post, we will use the Dapr building blocks and components to create a new application.

References

--

--