Kubernetes环境Dapr部署与应用

发布于最后编辑于全文约1388字,阅读时间约为4分钟。

Kubernetes Helm Sidecar Dapr
Dapr Dashboard
Dapr Dashboard
更新记录
  • 2021-11-20

    • Dapr Runtime版本由v1.4.3更新至v1.5.0

  • 2022-04-30

    • Dapr Runtime版本由v1.5.0更新至v1.7.2

    • Redis版本由v6.2.6更新至v7.0.0

  • 2022-05-20

    • Dapr Runtime版本由v1.7.2更新至v1.7.3

  • 2022-06-23

    • Dapr Runtime版本由v1.7.3更新至v1.7.4

    • Redis版本由v7.0.0更新至v7.0.2

  • 2022-07-09

    • Dapr Runtime版本由v1.7.4更新至v1.8.0

  • 2022-07-24

    • Dapr Runtime版本由v1.8.0更新至v1.8.2

  • 2022-08-14

    • Dapr Runtime版本由v1.8.2更新至v1.8.4

    • Redis版本由v7.0.2更新至v7.0.4

  • 2022-10-16

    • Dapr Runtime版本由v1.8.4更新至v1.9.0

    • 新增Dapr CLI部署Dapr Runtime说明;

    • Redis版本由v7.0.4更新至v7.0.5

  • 2022-11-12

    • Dapr Runtime版本由v1.9.0更新至v1.9.3

  • 2022-12-08

    • Dapr Runtime版本由v1.9.3更新至v1.9.5

  • 2023-02-09

    • Dapr Runtime版本由v1.9.5更新至v1.9.6

  • 2023-02-18

    • Dapr CLI版本由v1.9.1更新至v1.10.0

    • Dapr Runtime版本由v1.9.6更新至v1.10.0

    • Redis版本由v7.0.5更新至v7.0.8

  • 2023-03-03

    • Dapr Runtime版本由v1.10.0更新至v1.10.2

  • 2023-03-22

    • Dapr Runtime版本由v1.10.2更新至v1.10.4

  • 2023-04-14

    • Dapr Runtime版本由v1.10.4更新至v1.10.5

    • Redis版本由v7.0.8更新至v7.0.10

  • 2023-06-02

    • Dapr Runtime版本由v1.10.5更新至v1.10.7

    • Redis版本由v7.0.10更新至v7.0.11

  • 2023-06-24

    • Dapr CLI版本由v1.10.0更新至v1.11.0

    • Dapr Runtime版本由v1.10.7更新至v1.11.1

  • 2023-07-24

    • Dapr Runtime版本由v1.11.1更新至v1.11.2

    • Redis版本由v7.0.11更新至v7.0.12

  • 2023-09-01

    • Dapr Runtime版本由v1.11.2更新至v1.11.3

  • 2024-01-01

    • Dapr CLI版本由v1.11.0更新至v1.12.0

    • Dapr Runtime版本由v1.11.3更新至v1.12.2

    • Redis版本由v7.0.12更新至v7.2.3

  • 2024-03-31

    • Dapr CLI版本由v1.12.0更新至v1.13.0

    • Dapr Runtime版本由v1.12.2更新至v1.13.1

  • 2025-03-16

    • Dapr CLI版本由v1.13.0更新至v1.15.0

    • Dapr Runtime版本由v1.13.1更新至v1.15.3

    • Redis版本由v7.2.3更新至v7.4.2

  • 2025-05-26

    • Dapr CLI版本由v1.15.0更新至v1.15.1

    • Dapr Runtime版本由v1.15.3更新至v1.15.5

    • Redis版本由v7.4.2更新至v7.4.3

  • 2026-04-05

    • Dapr CLI版本由v1.15.1更新至v1.17.0

    • Dapr Runtime版本由v1.15.5更新至v1.17.3

    • Redis版本由v7.4.3更新至v8.6.2

概述

本文用于整理基于Kubernetes环境的Dapr部署与应用,并基于Redis部署状态管理发布/订阅组件。

随着各相关组件版本的更新,笔者将在验证通过后对本文进行补充和更新,请参考更新记录

本次演练环境为Kubernetes集群环境,环境配置可参考笔者另一篇笔记《Kubernetes集群部署笔记》。

本次演练使用Traefik作为Ingress Controller实现,环境配置可参考笔者另一篇笔记《Kubernetes环境Traefik部署与应用》。

组件版本

配置过程

部署Dapr运行时

本次演练中将Dapr Runtime部署至dapr-system命名空间,可根据需要替换。

根据需要选择使用Dapr CLI或Helm部署运行时组件。

  • 使用Dapr CLI部署

    安装Dapr CLI

    curl -sL https://github.com/dapr/cli/releases/download/v1.17.0/dapr_linux_amd64.tar.gz | tar xzf - dapr
    sudo mv ./dapr /usr/local/bin/dapr
    sudo chown root:root /usr/local/bin/dapr
    sudo chmod 755 /usr/local/bin/dapr

    安装运行时组件。

    # --kubernetes                       部署至Kubernetes集群
    # --namespace dapr-system            指定命名空间
    # --enable-ha                        启用高可用模式
    # --runtime-version 1.17.3           指定Dapr Runtime版本
    # --set image.registry=ghcr.io/dapr  指定镜像仓库地址
    dapr init --kubernetes --namespace dapr-system --enable-ha --runtime-version 1.17.3 --set image.registry=ghcr.io/dapr

    查看Dapr Runtime部署状态。

    dapr status --kubernetes
  • 使用Helm部署(高级)

    添加Helm仓库。

    helm repo add dapr https://dapr.github.io/helm-charts/
    helm repo update dapr

    安装运行时组件。

    # --namespace dapr-system   指定命名空间
    # global.ha.enabled=true    启用高可用模式
    # global.ha.replicaCount=3  设置高可用模式下运行时组件的副本数
    helm upgrade --install --namespace dapr-system --create-namespace \
      --set global.ha.enabled=true \
      --set global.ha.replicaCount=3 \
      dapr dapr/dapr --version 1.17.3

    安装Dapr Dashboard(可选)。

    # --namespace dapr-system   指定命名空间
    helm upgrade --install --namespace dapr-system \
      --set image.registry=ghcr.io/dapr \
      dapr-dashboard dapr/dapr-dashboard --version 0.15.0

配置Dapr Dashboard(可选)

  • 创建TLS证书Secret

    从已准备好的证书key文件和crt文件创建Secret

    kubectl create secret tls local-choral-io-tls -n dapr-system --key=local.choral.io.key --cert=local.choral.io.crt
  • 配置BasicAuth认证

    首先,创建一个用于保存用户名和密码的Secret,其中的users字段内容可使用htpasswd工具生成。本次演练中,认证usernamepassword都是admin

    cat <<EOF | kubectl apply -f - > /dev/null
    apiVersion: v1
    kind: Secret
    metadata:
      name: dapr-basicauth-secret
      namespace: dapr-system
    data:
      users: |2 # htpasswd -nb admin admin | openssl base64
        YWRtaW46e1NIQX0wRFBpS3VOSXJyVm1EOElVQ3V3MWhReE5xWmM9Cg==
    EOF

    创建一个Traefik中间件,用于对请求启用BasicAuth认证。

    cat <<EOF | kubectl apply -f - > /dev/null
    apiVersion: traefik.io/v1alpha1
    kind: Middleware
    metadata:
      name: dapr-basicauth
      namespace: dapr-system
    spec:
      basicAuth:
        realm: traefik.local.choral.io
        secret: dapr-basicauth-secret
    EOF
  • 配置dapr-dashboard服务的入口规则

    使用IngressRoute配置入口规则。

    cat <<EOF | kubectl apply -f - > /dev/null
    apiVersion: traefik.io/v1alpha1
    kind: IngressRoute
    metadata:
      name: dapr-dashboard
      namespace: dapr-system
    spec:
      entryPoints:
        - websecure
      routes:
        - match: Host(\`dapr.local.choral.io\`)
          kind: Rule
          services:
            - name: dapr-dashboard
              kind: Service
              port: 8080
          middlewares:
            - name: dapr-basicauth
      tls:
        secretName: local-choral-io-tls
    EOF

部署Redis实例

首先,部署一个用于演练的Redis实例。由于需要使用Redis Streams功能,要求Redis实例版本高于5.0.0

该实例仅用于演练环境,不可用于生产环境

  • 部署Redis实例(可选)

    本次演练中将Redis实例部署至data-redis命名空间,可根据需要替换。

    cat <<EOF | kubectl apply -f - > /dev/null
    apiVersion: v1
    kind: Namespace
    metadata:
      name: data-redis
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: redis-data
      namespace: data-redis
    spec:
      storageClassName: local-path
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 2Gi
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: redis
      namespace: data-redis
    spec:
      selector:
        matchLabels:
          app: redis
      strategy:
        type: Recreate
      template:
        metadata:
          labels:
            app: redis
        spec:
          containers:
            - image: redis:8.6.2-alpine
              name: redis
              ports:
                - containerPort: 6379
                  name: redis
              volumeMounts:
                - name: redis-persistent-storage
                  mountPath: /data
              resources:
                limits:
                  memory: 128Mi
                  cpu: 50m
          volumes:
            - name: redis-persistent-storage
              persistentVolumeClaim:
                claimName: redis-data
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: redis-headless
      namespace: data-redis
    spec:
      type: ClusterIP
      clusterIP: None
      selector:
        app: redis
      ports:
        - port: 6379
    EOF

创建Dapr组件

  • 创建发布/订阅组件

    cat <<EOF | kubectl apply -f - > /dev/null
    apiVersion: dapr.io/v1alpha1
    kind: Component
    metadata:
      name: pubsub
      namespace: apps-choral
    spec:
      type: pubsub.redis
      version: v1
      metadata:
      - name: redisHost
        value: redis-headless.data-redis:6379
      - name: redisPassword
        value: ""
      - name: redisDB
        value: 9
    EOF
  • 创建状态管理组件

    cat <<EOF | kubectl apply -f - > /dev/null
    apiVersion: dapr.io/v1alpha1
    kind: Component
    metadata:
      name: statestore
      namespace: apps-choral
    spec:
      type: state.redis
      version: v1
      metadata:
      - name: redisHost
        value: redis-headless.data-redis:6379
      - name: redisPassword
        value: ""
      - name: redisDB
        value: 9
    EOF

验证组件状态

  • 创建示例应用

    cat <<EOF | kubectl apply -f - > /dev/null
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: dapr-demo
      namespace: apps-choral
    spec:
      selector:
        matchLabels:
          app: dapr-demo
      strategy:
        type: Recreate
      replicas: 1
      template:
        metadata:
          labels:
            app: dapr-demo
          annotations:
            dapr.io/enabled: "true"      # 启用Dapr集成
            dapr.io/app-id: "dapr-demo"  # Dapr应用标识
            dapr.io/app-port: "80"       # 应用访问端口
        spec:
          containers:
            - image: nginx:alpine
              imagePullPolicy: IfNotPresent
              name: dapr-demo
              ports:
                - containerPort: 80
              resources:
                limits:
                  memory: 128Mi
                  cpu: 200m
    EOF

    打开示例应用Shell。本节中所有命令需要在示例应用的Shell中执行。

    kubectl exec -it $(kubectl get pods -n apps-choral --selector=app=dapr-demo -o jsonpath="{.items[0].metadata.name}") -n apps-choral -- sh
  • 健康检查元数据

    # 访问健康检查端点
    curl http://localhost:3500/v1.0/healthz -w "%{http_code}\n"
    # 204
    
    # 访问获取元数据端点
    curl http://localhost:3500/v1.0/metadata
    # {"id":"dapr-demo","runtimeVersion":"1.17.3","enabledFeatures":["SchedulerReminders"],"components":[{"name":"kubernetes","type":"secretstores.kubernetes","version":"v1"},{"name":"statestore","type":"state.redis","version":"v1","capabilities":["ETAG","TRANSACTIONAL","TTL","ACTOR"]},{"name":"pubsub","type":"pubsub.redis","version":"v1"}],"extended":{"daprRuntimeVersion":"1.17.3"},"appConnectionProperties":{"port":80,"protocol":"http","channelAddress":"127.0.0.1"},"actorRuntime":{"runtimeStatus":"RUNNING","hostReady":true,"placement":"placement: connected"},"scheduler":{"connected_addresses":["10.244.2.215:50006","10.244.1.41:50006","10.244.3.227:50006"]}}
  • 验证状态管理组件

    # 保存一组状态
    curl -X POST http://localhost:3500/v1.0/state/statestore -H "Content-Type: application/json" -d '[{ "key": "counter", "value": 1, "etag": "1" }]'
    
    # 读取某个状态
    curl http://localhost:3500/v1.0/state/statestore/counter -H "Content-Type: application/json" -v -w "\n"
    # < Etag: 1
    # 1
    
    # 删除某个状态
    curl -X DELETE -H "If-Match: 1" http://localhost:3500/v1.0/state/statestore/counter
  • 验证发布/订阅组件

    curl -X POST http://localhost:3500/v1.0/publish/pubsub/orderStatus -H "Content-Type: application/json" -d '{"id": 1, "status": "completed"}' -w "%{http_code}\n"
    # 204

参考资料