1. 认识Dapr集群组件

1.1. 架构

1.2. Dapr组件的作用

kubectl get pods

  • dapr-dashboard:              仪表盘,查看集群运行情况
  • dapr-operator:                  管理组件(state stores, pub/subs, etc.)
  • dapr-placement-server:    将actor实例映射到pods
  • dapr-sentry:                      管理服务之间的mTLS并做为证书颁发机构
  • dapr-sidecar-injector:       Dapr 注入 annotated pods(一个pod里有app和daprd两个container)

1.3. Dapr边车注解

 annotations:
    dapr.io/enabled: "true" # 允许注入daprd作为service01的的sidecar
    dapr.io/app-id: "service01"
    dapr.io/app-port: "80"

2. 请参考上一篇文章创建基础工程

https://shuaihj.blog.csdn.net/article/details/122670268icon-default.png?t=M1H3https://shuaihj.blog.csdn.net/article/details/122670268

3. 上传Service01和Service02的Docker镜像

3.1. 登录docker hub

https://hub.docker.com/
docker login

3.2. 给service01和service02打镜像

docker build -t netcorecore/service01:v1 -f ./Service01/Dockerfile .
docker build -t netcorecore/service02:v1 -f ./Service02/Dockerfile .

3.3. 上传Service01和service02的镜像

docker push netcorecore/service01:v1
docker push netcorecore/service02:v1

3.4. 查看上传结果

4. 创建Service01的部署文件: service01.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service01
  labels:
    app: service01
spec:
  replicas: 2 # 2个副本
  selector:
    matchLabels:
      app: service01
  template:
    metadata:
      labels:
        app: service01
      annotations:
        dapr.io/enabled: "true" # 开启Dapr边车
        dapr.io/app-id: "service01"
        dapr.io/app-port: "80"
    spec:
      containers:
      - name: service01
        image: docker.io/netcorecore/service01:v1

5. 创建Service02的部署文件: service02.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service02
  labels:
    app: service02
spec:
  replicas: 1
  selector:
    matchLabels:
      app: service02
  template:
    metadata:
      labels:
        app: service02
      annotations:
        dapr.io/enabled: "true"  # 开启Dapr边车
        dapr.io/app-id: "service02"
        dapr.io/app-port: "80"
    spec:
      containers:
      - name: service02
        image: docker.io/netcorecore/service02:v1        
---
apiVersion: v1
kind: Service
metadata:
  name: service02
spec:
  type: NodePort
  selector:
    app: service02
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30001

6. 部署到k8s集群

kubectl apply -f service01.yaml
kubectl apply -f service02.yaml

7. 查看部署结果

kubectl get service

8. 访问k8s集群,验证负载均衡效果

http://localhost:30001/api/hello
  • 刷新两次页面,验证负载均衡效果

9. 验证service02是否和dapr sidecar共同存在于一个pod中

9.1. 查看pod

kubectl get pods

9.2. 查看service02的pod中的container

kubectl describe pod service02-69b5c9fcc6-t5r8l

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐