Setting up Dapr
Dapr is a runtime for every platform and every language. The focus of this book is on C# in .NET, used with Visual Studio Code (VS Code). The code snippets in the book can be appreciated by developers from any background, but nevertheless, you will get the most out of it from a .NET perspective.
The development environment I use is Windows 10, as you will be able to tell from the screenshots we use in the book. While the CLI, configuration, and files will be the same, if you need more details on how to perform a particular action on Linux or a macOS development machine, I encourage you to check the Dapr documentation at https://docs.dapr.io/.
Important note: Updated to production-ready version
The Dapr runtime reached the v1.0 production-ready version in February 2021, as announced in the Dapr blog: https://blog.dapr.io/posts/2021/02/17/announcing-dapr-v1.0/.
The samples and scripts in this book have been updated and tested with v1.1 of Dapr.
Docker
Dapr requires Docker locally on your development environment, therefore make sure you have it installed. If your development machine is Windows, Docker must be running in Linux container mode.
You can find detailed instructions for running Docker at https://docs.docker.com/install/.
The Dapr CLI
We will immediately start working with Dapr; therefore, you need to install all the necessary tools. The Dapr runtime and its tools can be found at https://github.com/dapr/cli.
On Windows, it is suggested to use the following command to install the CLI in the %USERPROFILE%\.dapr\
folder and add it to the user PATH
environment variable so that the tools can be found from the command line:
powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"
The previous command installs a release candidate (RC) version of the Dapr CLI. Please refer to https://docs.dapr.io/getting-started/install-dapr-cli/ for more details.
We still need to initialize Dapr on the development machine, which we will do later in this chapter.
.NET
To install .NET 5, please refer to https://dotnet.microsoft.com/download for the link to the latest binaries.
On a development machine, it makes sense to install the full SDK, which includes the runtime. Once the install is complete, open a new Command Prompt and run the dotnet --info
command. You should see the following output:
PS C:\Repos\dapr-samples\chapter01> dotnet --info .NET SDK (reflecting any global.json): Version: 5.0.202 Commit: db7cc87d51 Runtime Environment: OS Name: Windows OS Version: 10.0.19042 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.202\ Host (useful for support): Version: 5.0.5 Commit: 2f740adc14 …
This proves .NET has been recognized and the framework is working fine.
VS Code
VS Code is a great multiplatform source code editor by Microsoft. You can install it for free by following the instructions at https://code.visualstudio.com/docs/setup/windows.
The Dapr extension
Dapr has an extension for VS Code that helps with navigating the Dapr local environment and eases the debugging configuration—I highly recommend it. Please follow the instructions at https://docs.dapr.io/developing-applications/ides/vscode/.
Windows Terminal
I really love the new Windows Terminal (https://aka.ms/terminal) for its ease of use and configurability. In the following chapters, we will often have to run multiple commands and tools in parallel. Therefore, the tabs feature of Windows Terminal is just one of the reasons why I suggest you adopt it too.
Installing self-hosted Dapr
Dapr can be initialized in two modes: self-hosted (or standalone) and Kubernetes.
As it is intended to be used only for a development environment, the self-hosted mode locally installs Redis, the Dapr placement services, and Zipkin. The following command initializes Dapr on your local environment:
dapr init
In a local development environment, it might happen that the ports Dapr intends to use for Redis (for example) are already in use. In this case, you should identify which processes or containers are using the ports and change them accordingly.
Once you launch the init
command, this is the output you should expect:
PS C:\Repos\dapr-samples\chapter01> dapr init Making the jump to hyperspace... Downloading binaries and setting up components... Downloaded binaries and completed components set up. daprd binary has been installed to C:\Users\dabedin\.dapr\bin. dapr_placement container is running. dapr_redis container is running. dapr_zipkin container is running. Use `docker ps` to check running containers. Success! Dapr is up and running. To get started, go here: https://aka.ms/dapr-getting-started
To check your newly initialized Dapr environment, you can use docker ps
, as follows:
PS C:\Repos\dapr-samples\chapter01> docker ps --format "{{.ID}}: {{.Image}} - {{.Ports}} - {{.Names}}" 2082b7f0eda4: daprio/dapr - 0.0.0.0:6050->50005/tcp - dapr_placement 6c68d869cea7: redis - 0.0.0.0:6379->6379/tcp - dapr_redis e4c8eae6992d: openzipkin/zipkin - 9410/tcp, 0.0.0.0:9411->9411/tcp - dapr_zipkin
The output shows the Docker container running on my machine.
Installing Dapr on Kubernetes
Dapr is specifically intended to be executed on Kubernetes. From your development machine on which you have the Dapr CLI installed, you can set up Dapr on the Kubernetes cluster currently configured, as follows:
dapr init -k
Alternatively, you can install Dapr on Kubernetes with a Helm v3 chart. You can find more details at https://docs.dapr.io/getting-started/install-dapr-kubernetes/#install-with-helm-advanced.
Important note
If you intend to define a continuous integration/continuous deployment (CI/CD) pipeline that takes care of the Dapr installation on the Kubernetes cluster too, this can also work, although it is out of scope for the present setup.
To verify the installation completed successfully, execute this command:
kubectl get pods --namespace dapr-system
The command should display the pods in the dapr-system
namespace.
Updating the Dapr version
On a development Windows machine on which a previous version of Dapr was already present, this is the output of an update of the CLI:
PS C:\Repos\dapr-samples\chapter01> powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex" WARNING: Dapr is detected - c:\dapr\dapr.exe CLI version: 1.0.1 Runtime version: 1.0.0 Reinstalling Dapr... Creating c:\dapr directory Downloading https://api.github.com/repos/dapr/cli/releases/assets/34341976 ... Extracting c:\dapr\dapr_windows_amd64.zip... CLI version: 1.1.0 Runtime version: 1.0.0 Clean up c:\dapr\dapr_windows_amd64.zip... Try to add c:\dapr to User Path Environment variable... Skipping to add c:\dapr to User Path - … omitted … Dapr CLI is installed successfully. To get started with Dapr, please visit https://docs.dapr.io/getting-started/ . Ensure that Docker Desktop is set to Linux containers mode when you run Dapr in self hosted mode.
The process to update the Dapr runtime is similar to the initialization, but first, we will need to uninstall Dapr from your machine, as follows:
PS C:\Repos\dapr-samples\chapter01> dapr uninstall Removing Dapr from your machine... Removing directory: C… Removing container: dapr_placement Dapr has been removed successfully
After we execute dapr init
, checking the Dapr version, we can see it has now moved forward from 1.0 to 1.1 for both the CLI and the runtime, as illustrated in the following code snippet:
PS C:\Repos\dapr-samples\chapter01> dapr --version CLI version: 1.1.0 Runtime version: 1.1.1
Our Dapr test environment is up and running: we are now ready to try it with our first sample.