[原创]Dapr入门教程之二:Dapr的安装
安装Dapr的CLI
在安装Dapr runtime之前需要先安装Dapr的CLI。有两个原因,第一,如果你以后不是用Helm来安装Dapr的话,需要用Dapr CLI来执行dapr init。第二,即使你不用Dapr CLI,而是用Helm来安装,以后查看Dapr系统状态的时候还是要用到Dapr CLI,比如查看Runtime的状态(dapr status -k),查看Components状态(dapr components -k),查看Configurations状态(dapr configurations -k),甚至启动Dashboard的时候还是要执行(dapr dashboard -k)。你可以在很多OS上安装Dapr CLI,比如Windows,Linux,Mac OS,我用的是Windows10系统,可以选择在Windows上直接装,也可以在WSL(Windows Subsystem for Linux)上装,这里选择在WSL上安装(没有WSL的话在windows商店里选一个免费的Linux装上。我选的是Ubuntu)。参考官方文档How-To: Install Dapr CLI,在Ubuntu里执行:
$ wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash Your system is linux_amd64 Installing Dapr CLI... Getting the latest Dapr CLI... Installing v0.11.0 Dapr CLI... Downloading https://github.com/dapr/cli/releases/download/v0.11.0/dapr_linux_amd64.tar.gz ... [sudo] password for ubuntu: dapr installed into /usr/local/bin successfully. CLI version: 0.11.0 Runtime version: n/a To get started with Dapr, please visit https://docs.dapr.io/getting-started/
安装完成。看看版本号:
$ dapr --version CLI version: 0.11.0 Runtime version: n/a
CLI是0.11.0版的,Runtime因为还没装,所以显示n/a。
因为接下来要安装1.0.0-rc.2的Runtime,1.x相对于0.x版有了重大的改进(比如多Instance模式),为了版本的统一性(低版本的CLI操作高版本的Runtime可能会出问题),把CLI升级到v1.0.0-rc.2版的吧。
curl -LO https://github.com/dapr/cli/releases/download/v1.0.0-rc.2/dapr_linux_amd64.tar.gz tar -xzf dapr_linux_amd64.tar.gz sudo cp dapr /usr/local/bin/dapr
再看一下版本:
$ dapr --version CLI version: 1.0.0-rc.2 Runtime version: n/a
CLI安装完毕。接下来安装Runtime。
安装Dapr的Runtime
这里有两种选择,一个是Standalone mode,一个是Kubernetes mode。我们两个都试一下。
安装Docker Engine
Dapr Runtime安装的前提条件是要先安装Docker。不然安装Dapr的时候会报"could not connect to Docker. Docker may not be installed or running"的错。这里参考Docker的官方文档Install Docker Engine on Ubuntu来安装Docker Engine。
$ sudo apt-get update $ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - $ sudo apt-key fingerprint 0EBFCD88 $ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" $ sudo apt-get update $ sudo apt-get install docker-ce docker-ce-cli containerd.io
安装完成。把docker的service启动:
$ sudo service docker start
看看service是否启动:
$ service docker status * Docker is running
OK,Docker Engine安装完成。
安装Standalone版的Dapr Runtime
用dapr init来安装单机版,可以指定版本号。目前最新的是1.0.0-rc.2。
$ dapr init --runtime-version=1.0.0-rc.2 ⌛ Making the jump to hyperspace... ←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑↗ Downloading binaries and setting up components... Dapr runtime installed to /root/.dapr/bin, you may run the following to add it to your path if you want to run daprd directly: export PATH=$PATH:/root/.dapr/bin →↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←↑→↓←✅ Downloaded binaries and completed components set up. ℹ️ daprd binary has been installed to /root/.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 export PATH=$PATH:/root/.dapr/bin
完成。看看都装了什么东西。
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 293b479ed26c openzipkin/zipkin "start-zipkin" 5 minutes ago Up 4 minutes (healthy) 9410/tcp, 0.0.0.0:9411->9411/tcp dapr_zipkin a07696221fd2 daprio/dapr "./placement" 5 minutes ago Up 4 minutes 0.0.0.0:50005->50005/tcp dapr_placement c475c415332b redis "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:6379->6379/tcp dapr_redis
跑起来三个Container。一个dapr_zipkin,一个dapr_placement,一个dapr_redis。
看看版本号:
$ dapr --version CLI version: 1.0.0-rc.2 Runtime version: 1.0.0-rc.2
至此单机版的Dapr Runtime安装完成。
dapr list一下看看,什么也没有。因为我们还没有启动App。
安装Kubernetes版的Dapr Runtime
安装之前需要先有Kubernetes环境,minikube,AWS托管的EKS,Azure托管的AKS,GCP托管的GKE等等。这里选择在本地安装minikube。
参考官方文档
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 $ sudo install minikube-linux-amd64 /usr/local/bin/minikube
看看版本:
$ minikube version minikube version: v1.16.0 commit: 9f1e482427589ff8451c4723b6ba53bb9742fbb1
先执行
$ sudo usermod -aG docker $USER && newgrp docker
不然minikube启动的时候会报错。然后启动minikube环境:
$ minikube start 😄 minikube v1.16.0 on Ubuntu 20.04 ✨ Automatically selected the docker driver 👍 Starting control plane node minikube in cluster minikube 🚜 Pulling base image ... 💾 Downloading Kubernetes v1.20.0 preload ... > preloaded-images-k8s-v8-v1....: 491.00 MiB / 491.00 MiB 100.00% 2.56 MiB 🔥 Creating docker container (CPUs=2, Memory=3100MB) ... 🐳 Preparing Kubernetes v1.20.0 on Docker 20.10.0 ... ▪ Generating certificates and keys ... ▪ Booting up control plane ... ▪ Configuring RBAC rules ... 🔎 Verifying Kubernetes components... 🌟 Enabled addons: storage-provisioner, default-storageclass 💡 kubectl not found. If you need it, try: 'minikube kubectl -- get pods -A' 🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
看看docker ps的结果:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cc47e6e56d32 gcr.io/k8s-minikube/kicbase:v0.0.15-snapshot4 "/usr/local/bin/entr…" 4 minutes ago Up 4 minutes 127.0.0.1:49156->22/tcp, 127.0.0.1:49155->2376/tcp, 127.0.0.1:49154->5000/tcp, 127.0.0.1:49153->8443/tcp minikube fedf3508c0f4 daprio/dapr:1.0.0-rc.2 "./placement" 43 minutes ago Up 43 minutes 0.0.0.0:50005->50005/tcp dapr_placement c52f4a72abc4 redis "docker-entrypoint.s…" 44 minutes ago Up 44 minutes 0.0.0.0:6379->6379/tcp dapr_redis af7b8dd7dcf3 openzipkin/zipkin "start-zipkin" 44 minutes ago Up 44 minutes (healthy) 9410/tcp, 0.0.0.0:9411->9411/tcp dapr_zipkin
多了个minikube的Container。
接下来安装kubectl。参考官方文档:
$ curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl" $ chmod +x ./kubectl $ sudo mv ./kubectl /usr/local/bin/kubectl
看看版本:
$ kubectl version --client Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.1", GitCommit:"c4d752765b3bbac2237bf87cf0b1c2e307844666", GitTreeState:"clean", BuildDate:"2020-12-18T12:09:25Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
安装Dapr Runtime可以用Dapr CLI也可以用Helm。我们选择用Helm来安装。参考官方文档:
$ curl https://baltocdn.com/helm/signing.asc | sudo apt-key add - $ sudo apt-get install apt-transport-https --yes $ echo "deb https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list $ sudo apt-get update $ sudo apt-get install helm
看一下版本:
$ helm version version.BuildInfo{Version:"v3.4.2", GitCommit:"23dd3af5e19a02d4f4baa5b2f242645a1a3af629", GitTreeState:"clean", GoVersion:"go1.14.13"}
终于可以安装Dapr了。这里指定目前最新的版本号1.0.0-rc.2。
$ helm repo add dapr https://dapr.github.io/helm-charts/ $ helm repo update $ kubectl create namespace dapr-system $ helm install dapr dapr/dapr --version 1.0.0-rc.2 --namespace dapr-system
看一下版本号:
$ dapr status -k NAME NAMESPACE HEALTHY STATUS REPLICAS VERSION AGE CREATED dapr-dashboard dapr-system True Running 1 0.5.0 1m 2020-12-26 02:22.08 dapr-sidecar-injector dapr-system True Running 1 1.0.0-rc.2 1m 2020-12-26 02:22.08 dapr-sentry dapr-system True Running 1 1.0.0-rc.2 1m 2020-12-26 02:22.08 dapr-operator dapr-system True Running 1 1.0.0-rc.2 1m 2020-12-26 02:22.08 dapr-placement-server dapr-system True Running 1 1.0.0-rc.2 1m 2020-12-26 02:22.08
我们看看启动了什么Pod:
$ kubectl get pods -n dapr-system NAME READY STATUS RESTARTS AGE dapr-dashboard-6f749469dd-z7hzx 1/1 Running 0 3m45s dapr-operator-699cd79686-nndhd 1/1 Running 0 3m45s dapr-placement-server-0 1/1 Running 0 3m45s dapr-sentry-7c4fb54fb7-xsx5q 1/1 Running 0 3m45s dapr-sidecar-injector-6bdbc588fc-tzpm4 1/1 Running 0 3m45s
启动了5个Pod,dapr-dashboard,dapr-operator,dapr-placement,dapr-sentry,dapr-sidecar-injector。
其中dapr-dashboard负责显示Dapr Dashboard,dapr-operator负责管理Dapr Component的更新和Kubernetes Service Endpoint(State stores,pub/sub等),dapr-sidecar-injector负责将Dapr的sidecar注入带Dapr注释的Pod(如果App的Deploy里有dapr.io/app-id, dapr.io/app-port之类的annotation,就会被注入Dapr sidecar。这时你会发现Pod里有两个Container)。dapr-placement用于Actor,创建将Actor实例映射到Pod的映射表。dapr-sentry负责管理服务之间的mTLS并充当证书颁发机构。
把Dapr Dashboard起来看看:
$ dapr dashboard -k
效果和"kubectl port-forward svc/dapr-dashboard 8080:8080 -n dapr-system"是一样的。
在浏览器里输入http://localhost:8080。
恭喜,Dapr Runtime终于安装完成了!
本章就到这里。下一章我们来部署一个Dapr的微服务的例子。