dapr 应用开发 | 环境配置

1 Dapr 简单介绍

Dapr CLI 允许您在上本地开发环境中运行 Dapr 框架,并支持 MacOS、Linux 和 Windows。在几秒钟内安装 Dapr,并快速开始编码。

The Dapr CLI allows you to get started with Dapr running locally on your dev box and support MacOS, Linux and Windows. Install Dapr in seconds and start coding quickly.

Dapr 框架的语言支持:任何语言,任何地方(云/Cloud 《=》端/Edge)

使用 Dapr 的公司:

2 环境搭建

2.1 安装 Dockers /Prerequisites 先决条件(可选项,不是必须)

默认情况下,在初始化期间 Dapr CLI 将安装 Dapr 二进制文件以及设置一个开发人员环境,以帮助您轻松地启动 Dapr。这个环境使用 Docker 容器,因此需要安装 Docker。如果您希望在没有这个环境和对 Docker 没有依赖性的情况下运行 Dapr,那么在安装 CLI 之后,请确保按照说明使用 slim init 初始化 Dapr。

Note, if you are a new user, it is strongly recommended to intall Docker and use the regular init command.

注意,如果您是一个新用户,强烈建议使用所有的 Docker 并使用常规 init 命令。

安装 docker 注意事项:

  • Note: On Windows, Docker must be running in Linux Containers mode
  • 注意: 在 Windows 上,Docker 必须在 Linux 容器模式下运行(建议使用WSL2)

2.2 安装 Dapr CLI(必须)

OS 平台 Dapr CLI 安装说明
Windows

#将最新的 windows Dapr CLI 安装到 c: Dapr (默认安装 C 盘根路径),然后将这个目录添加到 User PATH 环境变量。使用 -DaprRoot [path] 更改默认的安装目录

powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"

Linux

#将最新的 linux Dapr CLI 安装到 /usr/local/bin

wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash

MacOS

#将最新的 darwin Dapr CLI 安装到 /usr/local/bin

curl -fsSL https://raw.githubusercontent.com/dapr/cli/master/install/install.sh | /bin/bash

From the Binary Releases (二进制方式安装)

Dapr CLI 的每个版本都包含各种操作系统和体系结构,这些二进制版本可以手动下载和安装。

  1. 下载 Dapr CLI;
  2. 解压包 (e.g. dapr_linux_amd64.tar.gz, dapr_windows_amd64.zip)
  3. 把它移动到你想要的位置
  • 适用于 Linux/MacOS -/usr/local/bin
  • 对于 Windows,创建一个目录并将其添加到系统 [PATH] 中并通过编辑系统环境变量将此目录添加到 [PATH] 中

2.3 安装 Runtime 

Dapr 支持本地主机自托管  (self-hosted) 和 on Kubenetes 模式托管;

2.3.1 本地主机自托管模式 (self-hosted)

在本机安装Dapr运行时分为两种模式,一种是完整模式,一种是简易模式:

  • 完整模式】:需要 Docker 环境,运行 dapr init ,会自动下载 Dapr 的运行时 daprd ,并在 Docker 中启动3个容器:
  1. 【dapr_placement】:Image为“daprio/dapr”,用于Dapr的Actor应用的注册
  2. 【dapr_zipkin】:Image为“openzipkin/zipkin”,用于分布式跟踪的处理
  3. 【dapr_redis】:Image为“redis”,用于状态存储和发布订阅处理

对于 Linux 用户,如果使用 sudo 运行 docker cmds,则需要使用 sudo dapr init 

注意: 要查看 Dapr 是否已经成功安装,请从命令提示符运行 docker ps 命令,并检查 daprio/Dapr:latest、 dapr_redis 和 dapr_zipkin 容器映像是否都在运行。

这一步创建了以下缺省值:

  1. 组件文件夹,稍后在 除非 对于 Linux/MacOS,默认组件文件夹路径为 对于 Windows 系统来说就是这样 %USERPROFILE%\.dapr\components.
  2. 组件文件夹中的组件文件 及statestore.yaml.
  3. 默认配置文件 适用于 Linux/MacOS 或 Windows 启用追踪 调用。可以用 标志dapr run.
  • 简易模式】:不需要 Docker 环境,运行 dapr init --slim,同样会自动下载 Dapr 的运行时 daprd,并下载placement服务的二进制文件。

注意: 当使用 -- slim 标志初始化 Dapr 时,只安装 Dapr 运行时二进制文件和 placement service 二进制文件。创建一个没有默认配置文件的空默认组件文件夹。在 dapr run 期间,用户应该使用 -- components-path 指向包含自定义配置文件的组件目录,或者将这些文件放在默认目录中。对于 Linux/MacOS,默认组件目录路径是【$HOME/.dapr/components】,对于 Windows 它是【%USERPROFILE%\.dapr\components】。

两种模式下,有些配置也有所不同:

  • 完整模式:会在 Linux/MacOS 的 【$HOME/.dapr/components】目录或 Windows 的【%USERPROFILE%\.dapr\components】目录下面创建3个默认组件配置文件:pubsub.yaml 使用 redis 容器作为发布订阅组件、statestore.yaml 使用 redis 容器作为状态存储组件和 zipkin.yaml 把分布式跟踪的信息导出到 zipkin 容器;并添加 Linux/MacOS 的【$HOME/.dapr/config.yaml】或 Windows 的【%USERPROFILE%\.dapr\config.yaml】的默认配置文件,重点配置了分布式跟踪的采样率为1。

  • 简易模式:虽然会创建默认 components 目录,但是不会创建默认组件配置文件。后续运行的时候,你需要自行创建默认组件配置文件或者指定服务的自定义组件配置文件。

=》安装特定的运行时版本 | Install a specific runtime version 

您可以使用 Dapr init --runtime-version 安装或升级到 Dapr 运行时的特定版本。您可以在 Dapr Release 中找到版本列表。

# Install v1.0.0 runtime
$ dapr init --runtime-version 1.0.0

# Check the versions of CLI and runtime
$ dapr --version
CLI version: v1.0.0
Runtime version: v1.0.0

 =》安装到特定的 Docker 网络 | Install to a specific Docker network

您可以将 Dapr 运行时安装到特定的 Docker 网络中,以便将其与本地机器隔离开来(例如,从 Docker 容器中使用 Dapr)。

# Create Docker network
$ docker network create dapr-network

# Install Dapr to the network
$ dapr init --network dapr-network

 注意: 当安装到特定的 Docker 网络时,您需要将 --placement-host-address 参数添加到 dapr,以便在该网络内的任何容器中运行命令。

2.3.2 托管 on Kubenetes 模式

The init command will install Dapr to a Kubernetes cluster. For more advanced use cases, use our Helm Chart.

Init 命令将把 Dapr 安装到 Kubernetes 集群。

Note: The default namespace is dapr-system. The installation will appear under the name dapr for Helm

注意: 默认名称空间是 dapr-system。安装将以 Helm 的名称 dapr 出现

dapr init -k

提供 Helm values | Supplying Helm values 

# 可以使用 --set 标志设置所有可用的 Helm values | All available Helm Chart values can be set by using the --set flag:
$ dapr init -k --set global.tag=1.0.0 --set dapr_operator.logLevel=error  

# 安装到自定义命名空间 | Installing to a custom namespace
$ dapr init -k -n my-namespace

# 安装时使用高可用的控制平面配置 | Installing with a highly avaialable control plane config 
$ dapr init -k --enable-ha=true

# 安装时禁用 mTLS | Installing with mTLS disabled 
$ dapr init -k --enable-mtls=false 

Dapr 在 k8s 上面的升级 | Upgrade Dapr on Kubernetes 

要执行 Dapr 控制平面的零停机时间升级 | To perform a zero downtime upgrade of the Dapr control plane:

# dapr 升级指定 runtime 版本
$ dapr upgrade -k --runtime-version=1.0.0

Note: do not use the dapr upgrade command if you're upgrading from 0.x versions of Dapr

注意: 如果您从 Dapr 的0.x 版本升级,请不要使用 Dapr 升级命令

# 可以使用 --set 标志设置所有可用的 Helm values | All available Helm Chart values can be set by using the --set flag:
$ dapr upgrade -k --runtime-version=1.0.0 --set global.tag=my-tag --set dapr_operator.logLevel=error 

3 卸载 Dapr 环境

3.1 本地主机自托管模式 (self-hosted) 卸载

在独立模式下卸载 Dapr | Uninstall Dapr in a standalone mode

Uninstalling will remove daprd binary and the placement container (if installed with Docker or the placement binary if not).

卸载将删除 daprd 二进制文件和布局容器(如果安装了 Docker 或布局二进制文件,如果没有)。

$ dapr uninstall

For Linux users, if you run your docker cmds with sudo, you need to use "sudo dapr uninstall" to remove the containers.

对于 Linux 用户,如果使用 sudo 运行 docker cmds,则需要使用“ sudo dapr uninstall”来删除容器。

The command above won't remove the redis or zipkin containers by default in case you were using it for other purposes. It will also not remove the default dapr folder that was created on dapr init. To remove all the containers (placement, redis, zipkin) and also the default dapr folder created on init run:

上面的命令默认情况下不会删除 redis 或 zipkin 容器,以防您将其用于其他目的。它也不会移除在 dapr init 上创建的默认 dapr 文件夹。要删除所有的容器(placement、 redis、 zipkin)以及在 init 上创建的默认 dapr 文件夹,请运行:

$ dapr uninstall --all

The above command can also be run when Dapr has been installed in a non-docker environment, it will only remove the installed binaries and the default dapr folder in that case.

当 Dapr 安装在非 docker 环境中时,也可以运行上面的命令,在这种情况下,它只会删除已安装的二进制文件和默认的 Dapr 文件夹。

NB: The dapr uninstall command will always try to remove the placement binary/service and will throw an error is not able to.

注意: dapr uninstall 命令将总是尝试删除 placement binary/service,并将抛出一个无法执行的错误。

You should always run a dapr uninstall before running another dapr init.

在运行另一个 dapr init 之前,应该始终运行 dapr 卸载。

从特定的 Docker 网络卸载 Dapr | Uninstall Dapr from a specific Docker network 

If previously installed to a specific Docker network, Dapr can be uninstalled with the --network argument:

如果以前安装到特定的 Docker 网络,Dapr 可以通过-network 参数卸载:

$ dapr uninstall --network dapr-network

3.2 托管 on Kubenetes 模式卸载

卸载 Kubernetes 上的 Dapr | Uninstall Dapr on Kubernetes

To remove Dapr from your Kubernetes cluster, use the uninstall command with --kubernetes flag or the -k shorthand.

要从您的 Kubernetes 集群中删除 Dapr,可以使用 uninstall 命令 -- Kubernetes 标志或 -k 简写。

dapr uninstall --kubernetes //卸载Kubernetes中的测试环境
helm uninstall dapr -n dapr-system //卸载Kubernetes中的生产环境

至此,Dapr的开发、测试和生产环境就配置完成了,接下来就是进入到开发阶段了。

参考资料:

猜你喜欢

转载自blog.csdn.net/ChaITSimpleLove/article/details/114379578