Dapr + .NET Core實戰(十四)虛擬機器叢集部署 mDNS + Consul

chester·chen發表於2021-10-17

前面我們說了在單機模式下和K8S叢集下的Dapr實戰,這次我們來看看如何在不使用K8S的情況下,在一個傳統的虛擬機器叢集裡來部署Dapr。

1.環境準備

我們準備兩臺centos7虛擬機器

Dapr1:192.168.43.131

Dapr2:192.168.43.132

2.Dapr CLI安裝

分別在Dapr1和Dapr2兩臺虛機上安裝Dapr CLI

方式1 wget命令安裝

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

方式2 二進位制方式手動安裝
從https://github.com/dapr/cli/releases下載Dapr腳手架,並解壓後放入/usr/local/bin目錄

tar -xzvf dapr_linux_amd64.tar.gz

mv dapr /usr/local/bin

3.Dapr初始化

在Dapr1和Dapr2兩臺虛機上初始化,不依賴Docker的情況下課通過指定--slim選項來啟動

dapr init --slim

初始化好之後檢視~/.dapr資料夾下的資料夾與檔案,其中bin資料夾存放著daprd,dashboard,placement三個可執行檔案

[root@localhost ~]# ll ~/.dapr
總用量 4
drwxrwxrwx 3 root root  64 10月 15 10:37 bin
drwxrwxrwx 2 root root  29 10月 15 15:33 components
-rw-r--r-- 1 root root 224 10月 15 18:34 config.yaml
[root@localhost ~]# ll ~/.dapr/bin/
總用量 135632
-rwxrwxrwx 1 root root 94117888 10月 15 11:08 daprd
-rwxrwxrwx 1 root root 30007296 10月 15 11:08 dashboard
-rwxrwxrwx 1 root root 14761984 10月 15 11:07 placement
drwxr-xr-x 3 root root       18 10月 15 11:08 web

4.部署專案

我們仍然採用之前的FrontEnd和BackEnd專案,在Dapr1與Dapr2兩臺機器上分別部署BackEnd,使其實現負載

 釋出FrontEnd和BackEnd,選擇完全釋出模式,這樣我們不需要安裝dotnet core執行時即可直接執行FrontEnd與BackeEnd,並選擇linux-x64目標執行時

 釋出完成後,將BackEnd上傳到Dapr1和Dapr2兩臺機器動;將FrontEnd上傳到Dapr2

5.執行BackEnd和FrontEnd

Dapr1上和Dapr2上啟動BackEnd

chmod 777 /usr/local/src/backend/BackEnd
dapr run --dapr-http-port 3511 --app-port 5000 --app-id backend  /usr/local/src/backend/BackEnd

Dapr2上啟動FrontEnd

chmod 777 /usr/local/src/frontend/FrontEnd
dapr run --dapr-http-port 3501 --app-port 5001  --app-id frontend  /usr/local/src/frontend/FrontEnd

6.呼叫FrontEnd介面Dapr/ip驗證

發現每次呼叫獲取的ip不一致,已經通過mDNS實現負載

 

7.自定義元件

我們在Dapr1和Dapr2兩臺機器上的~/.dapr/components資料夾中分別新建檔案statestore.yaml

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: statestore
spec:
  type: state.redis
  version: v1
  metadata:
  - name: redisHost
    value: 192.168.43.102:6379
  - name: redisPassword
    value: "123456"
  - name: actorStateStore
    value: "true"

重新啟動FrontEnd,呼叫api/State介面,發現元件設定成功

8.Actors使用

因為我們初始化時指定了--slim選項,這種模式下placement需要我們手動啟動,我們啟動FrontEnd中的~/.dapr/bin/placement

[root@localhost ~]# ~/.dapr/bin/placement
INFO[0000] starting Dapr Placement Service -- version 1.4.3 -- commit a8ee30180e1183e2a2e4d00c283448af6d73d0d0  instance=localhost.localdomain scope=dapr.placement type=log ver=unknown
INFO[0000] log level set to: info                        instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3
INFO[0000] metrics server started on :9090/              instance=localhost.localdomain scope=dapr.metrics type=log ver=1.4.3
INFO[0000] Raft server is starting on 127.0.0.1:8201...  instance=localhost.localdomain scope=dapr.placement.raft type=log ver=1.4.3
INFO[0000] placement service started on port 50005       instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3
INFO[0000] Healthz server is listening on :8080          instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3
INFO[0001] cluster leadership acquired                   instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3
INFO[0001] leader is established.                        instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3
INFO[0004] Start disseminating tables. memberUpdateCount: 1, streams: 1, targets: 1, table generation: 1  instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3
INFO[0004] Completed dissemination. memberUpdateCount: 1, streams: 1, targets: 1, table generation: 1  instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3

重新啟動FrontEnd與BackEnd,並呼叫api/ActorsClient/123,Actors環境啟動成功

9.Consul名字解析元件

前面的負載我們使用了mDNS,官方還為我們提供了Consul名字解析元件,我們新建一個虛擬機器Consul(192.168.43.133)來安裝Consul

1.下載
wget https://releases.hashicorp.com/consul/1.3.0/consul_1.3.0_linux_amd64.zip
2.解壓
unzip  consul_1.3.0_linux_amd64.zip
3.拷貝到/usr/local/bin
mv consul /usr/local/bin
4.驗證
consul
5.啟動
consul agent -dev -ui -node=consul-dev -client=192.168.43.133
6.訪問192.168.43.133:8500

 

 下面修改Dapr1和Dapr2機器上的~/.dapr/config.yaml配置,讓其使用Consul來解析名字

apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
  name: daprConfig
spec:
  nameResolution:
    component: "consul"
    configuration:
      client:
        address: "192.168.43.133:8500"
      selfRegister: true

重啟FrontEnd與BackEnd並呼叫api/Dapr/ip介面,發現只能呼叫到後啟動的BackEnd,無法實現負載

 

 針對這個問題我像dapr官方提了issue,靜等回覆

https://github.com/dapr/components-contrib/issues/1199

相關文章