一、简介
Dapr 分布式应用程序运行时是一种可移植、事件驱动的运行时,使任何开发人员都可以轻松构建在云和边缘运行的弹性、无状态和有状态应用程序,并支持多种语言和开发框架。
本文记录基于kubernetes部署dapr应用。
二、环境准备
1、kubernetes
安装采用kubeadm安装,可参见官网文档
2、hmle
curl -O https://get.helm.sh/helm-v3.12.1-linux-amd64.tar.gz
tar -zxvf helm-v3.12.1-linux-amd64.tar.gz
chmond +7 helm
cp helm /usr/local/bin
3、dapr cli (可选)
curl -O https://github.com/dapr/cli/releases/download/v1.11.0/dapr_linux_amd64.tar.gz
tar -zxvf dapr_linux_amd64.tar.gz
chmond +7 dapr
cp dapr /usr/local/bin
三、初始化Dapr
helm repo add dapr https://dapr.github.io/helm-charts/
helm repo update
helm install dapr dapr/dapr -n dapr-system --create-namespace
#也可以直接使用一下cli命令安装
#dapr init --kubernetes --wait
#查看
#dapr status -k
四、安装组件
statestore组件
1、安装redis
这里使用的是redis作为staestore存储,并安装至redis-system命名空间
注:需要kubernetes存在默认的storageClass存储类
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm install redis bitnami/redis -n redis-system --create-namespace
#根据输出结果提示获取host及密码,如下
#host:redis-master.redis-system.svc.cluster.local
echo $(kubectl get secret --namespace redis-system redis -o jsonpath="{.data.redis-password}" | base64 -d)
2、添加statestore组件
vi dapr-statestore.yaml
kubectl apply -f dapr-statestore.yaml
dapr-statestore.yaml
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
version: v1
metadata:
# These settings will work out of the box if you use `helm install
# bitnami/redis`. If you have your own setup, replace
# `redis-master:6379` with your own Redis master address, and the
# Redis password with your own Secret's name. For more information,
# see https://docs.dapr.io/operations/components/component-secrets .
- name: redisHost
#根据安装redis的输出信息进行替换
value: redis-master.redis-system.svc.cluster.local:6379
- name: redisPassword
secretKeyRef:
name: redis
#不能替换,会自动获取kubernetes密文
key: redis-password
auth:
secretStore: kubernetes
3、添加redis密码
因为我这里redis安装命名空间和组件命名空间不同,所以需要在添加组件的命名空间添加redis密码密文,以便daprd能够正常加载组件
kubectl get secret redis -n redis-system -o yaml >secret-redis.yaml
vi secret-redis.yaml
kubectl apply -f secret-redis.yaml
#查看
kubectl get secret
secret-redis.yaml
apiVersion: v1
data:
redis-password: MDdHMnYyTUR5RQ==
kind: Secret
metadata:
annotations:
meta.helm.sh/release-name: redis
meta.helm.sh/release-namespace: redis-system
creationTimestamp: "2023-07-01T09:30:06Z"
labels:
app.kubernetes.io/instance: redis
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: redis
helm.sh/chart: redis-17.11.6
name: redis
#替换或删除命名空间
namespace: default
resourceVersion: "85040"
uid: 17b2d512-ce67-4503-b19d-05fb7409ad21
type: Opaque
注:1)、组件使用也存在命名空间
2)、配置auth:secretStore使用kubernetes密文,需要在应用的命名空间添加对应密文,否则daprd无法加载组件
五、部署应用
参照官网操作state,部署dapr-counter.yaml
vi dapr-counter.yaml
kubectl apply -f dapr-statestore.yaml
dapr-counter.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: counterapp
labels:
app: counter
spec:
replicas: 1
selector:
matchLabels:
app: counter
template:
metadata:
labels:
app: counter
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "counterapp"
dapr.io/enable-api-logging: "true"
spec:
containers:
- name: counter
image: registry.cn-hangzhou.aliyuncs.com/vectorluo/dapr-counter:v1.1
#收缩至0停止应用
kubectl scale deployment counterapp --replicas=0
Comments NOTHING