使用 Ingres 访问 Dapr 应用

在 上一篇 文章中分享了分布式运行时 Dapr 的使用,在示例中将状态存储能力分离到 Dapr 运行时中,应用通过 Dapr API 来使用该能力。这篇文章将介绍如何通过 Ingress Controller(入口控制器)来访问 Dapr 应用。

方案

如何公开 Dapr 应用的访问,方案有两种:

  1. 像传统用法一样,配置应用的 Service 作为后端,由入口控制器直接将流量转发到应用容器,简单说就是支持自动配置的 L7 负载均衡器。
  2. 不直接访问应用容器,而是通过 Daprd 运行时来访问。这时,我们就需要将入口控制器也声明为 Dapr 应用,为其注入 Daprd 运行时容器。此时创建入口规则指向的后端,则是 Ingress 的 Dapr Service。.

两种方案各有优劣,前者架构简单;后者虽然引入 Daprd 容器,架构复杂,消耗了更多的资源,但也因此可以使用 Dapr 的服务治理能力,如超时、重试、访问控制等等。

接下来我们将通过示例来分别验证两种方案。

使用 Ingres 访问 Dapr 应用

演示

前置条件

  • helm
  • dapr cli
export INSTALL_K3S_VERSION=v1.23.8+k3s2curl -sfL https://get.k3s.io | sh -s - --disable traefik --write-kubeconfig-mode 644 --write-kubeconfig ~/.kube/config

安装 Dapr

dapr init --kubernetes --wait

安装入口控制器

这里使用 Flomesh 的入口控制器。通过 valus.yaml 为入口控制器添加 dapr 相关的注解。这里需要注意由于默认情况下 Daprd 运行时监听的端口是绑定在回环地址上的,而 Ingress 向后端转发请求时使用的是 Pod IP,因此需要注解 dapr.io/sidecar-listen-addresses: "[::],0.0.0.0" 让 Daprd 运行时可以接收来自 Pod IP 的请求。

# values.yamlfsm:  ingress:    podAnnotations:      dapr.io/sidecar-listen-addresses: "[::],0.0.0.0"      dapr.io/enabled: "true"      dapr.io/app-id: "ingress"      dapr.io/app-port: "8000"      dapr.io/enable-api-logging: "true"      dapr.io/config: "ingressconfig"
helm repo add fsm https://charts.flomesh.iohelm repo updatehelm install \  --namespace flomesh \  --create-namespace \  --version=0.2.1-alpha.3 \  -f values.yaml \  fsm fsm/fsm

部署示例应用

示例应用我们使用 kennethreitz/httpbin,为其添加 dapr 相关的注解。

kubectl create ns httpbinkubectl apply -n httpbin -f - <<EOFapiVersion: apps/v1kind: Deploymentmetadata:  labels:    app: httpbin  name: httpbinspec:  replicas: 1  selector:    matchLabels:      app: httpbin  strategy: {}  template:    metadata:      annotations:        dapr.io/enabled: "true"        dapr.io/app-id: "httpbin"        dapr.io/app-port: "80"        dapr.io/enable-api-logging: "true"        dapr.io/config: "httpbinconfig"      labels:        app: httpbin    spec:      containers:      - image: kennethreitz/httpbin        name: httpbin        resources: {}---apiVersion: v1kind: Servicemetadata:  labels:    app: httpbin  name: httpbin  namespace: httpbinspec:  ports:  - port: 80    protocol: TCP    targetPort: 80  selector:    app: httpbinEOF

方案 1

参考 Flomesh Ingress 的 文档[1],创建入口规则使用 Service httpbin 作为后端。

kubectl apply -n httpbin -f - <<EOFapiVersion: networking.k8s.io/v1kind: Ingressmetadata:  name: httpbin  annotations:    pipy.ingress.kubernetes.io/rewrite-target-from: ^/httpbin/?    pipy.ingress.kubernetes.io/rewrite-target-to: /spec:  ingressClassName: pipy  rules:  - http:      paths:      - backend:          service:            name: httpbin            port:              number: 80        path: /httpbin        pathType: PrefixEOF

使用主机 IP 地址和入口控制器的 80 端口来访问 /httpbin/get,由于上面配置了路径重写,最终请求 /get 将会被发送到目标 Pod。

curl HOST_IP:80/httpbin/get{  "args": {},  "headers": {    "Accept": "*/*",    "Connection": "keep-alive",    "Content-Length": "0",    "Host": "20.239.95.81:80",    "User-Agent": "curl/7.86.0"  },  "origin": "10.42.0.18",  "url": "http://20.239.95.81:80/get"}

方案 2

方案 2 也同样需要配置入口规则,只是这次后端服务配置的入口控制器的 Dapr Service,通过 kubectl get svc -n flomesh 就可以找到名字带有 -dapr 后缀的 Service

kubectl apply -n flomesh -f - <<EOFapiVersion: networking.k8s.io/v1kind: Ingressmetadata:  name: dapr  annotations:    pipy.ingress.kubernetes.io/rewrite-target-from: ^/dapr/?    pipy.ingress.kubernetes.io/rewrite-target-to: /spec:  ingressClassName: pipy  rules:  - http:      paths:      - backend:          service:            name: ingress-dapr            port:              number: 80        path: /dapr        pathType: PrefixEOF

仍然是访问 httpbin 的路径 /get,但是访问方式要遵循 Dapr 服务调用的 API[2]:通过请求头指定 dapr-app-id 为 httpbin.httpbin。由于入口控制器和目标应用不在同一命名空间下,在应用 id 需要带上其命名空间。

同样是成功返回,但是可以看到最终应用收到请求头部会多了一些信息,这些信息都是来自 Daprd 运行时,比如 Dapr-Caller-App-Id"Dapr-Callee-App-Id 标识表示请求的发起方和接收方;Forwarded 标识了发起请求的主机名。如果开启了 tracing,还会有链路相关的信息(本示例中并未开启)。

curl HOST_IP:80/dapr/get -H 'dapr-app-id:httpbin.httpbin'{  "args": {},  "headers": {    "Accept": "*/*",    "Accept-Encoding": "gzip",    "Connection": "keep-alive",    "Dapr-App-Id": "httpbin.httpbin",    "Dapr-Callee-App-Id": "httpbin",    "Dapr-Caller-App-Id": "ingress",    "Forwarded": "for=10.42.0.18;by=10.42.0.18;host=fsm-ingress-pipy-864d8d9c76-4kb7r",    "Host": "127.0.0.1:80",    "Proto": "HTTP/1.1",    "Protomajor": "1",    "Protominor": "1",    "Referer": "",    "Traceparent": "00-00000000000000000000000000000000-0000000000000000-00",    "User-Agent": "curl/7.86.0",    "X-Forwarded-Host": "fsm-ingress-pipy-864d8d9c76-4kb7r"  },  "origin": "10.42.0.18",  "url": "http://fsm-ingress-pipy-864d8d9c76-4kb7r/get"}

访问控制

文章开头提到使用 方案 2 虽然带来了延迟增加了复杂度,但是可以使用 Dapr 的服务治理能力。这里以 Dapr 的访问控制功能[3] 为例。

不知道大家注意到没,在部署入口控制器和示例应用时,有个注解 dapr.io/config: xxx。这个注解是为应用的 Daprd 运行时指定配置,运行时启动时会从名为 xxx 的 Configuration 资源读取配置。

执行下面的命令为 httpbin 应用设置访问控制规则:默认拒绝所有请求,只允许 flomesh 命名空间下的 id 为 ingress 的应用通过 GET 方式访问路径 /get。更多访问控制配置,可以参考 Dapr 访问控制官方文档[4]

kubectl apply -n httpbin -f - <<EOFapiVersion: dapr.io/v1alpha1kind: Configurationmetadata:  name: httpbinconfigspec:  accessControl:    defaultAction: deny    trustDomain: "public"    policies:    - appId: ingress      defaultAction: deny      trustDomain: 'public'      namespace: "flomesh"      operations:      - name: /get        httpVerb: ['GET']        action: allowEOF

应用配置之后,需要重启应用。

kubectl rollout restart deploy httpbin -n httpbin

等到重启完成,再访问 /get 路径,正常响应。

curl HOST_IP:80/dapr/get -H 'dapr-app-id:httpbin.httpbin'{  "args": {},  "headers": {    "Accept": "*/*",    "Accept-Encoding": "gzip",    "Connection": "keep-alive",    "Dapr-App-Id": "httpbin.httpbin",    "Dapr-Callee-App-Id": "httpbin",    "Dapr-Caller-App-Id": "ingress",    "Forwarded": "for=10.42.0.40;by=10.42.0.40;host=fsm-ingress-pipy-864d8d9c76-jctcx",    "Host": "127.0.0.1:80",    "Proto": "HTTP/1.1",    "Protomajor": "1",    "Protominor": "1",    "Referer": "",    "Traceparent": "00-00000000000000000000000000000000-0000000000000000-00",    "User-Agent": "curl/7.86.0",    "X-Forwarded-Host": "fsm-ingress-pipy-864d8d9c76-jctcx"  },  "origin": "10.42.0.40",  "url": "http://fsm-ingress-pipy-864d8d9c76-jctcx/get"}

但是,如果是访问路径 /headers,会收到下面的响应:被禁止访问 /headers

curl HOST_IP:80/dapr/headers -H 'dapr-app-id:httpbin.httpbin'{  "errorCode": "ERR_DIRECT_INVOKE",  "message": "fail to invoke, id: httpbin.httpbin, err: rpc error: code = PermissionDenied desc = access control policy has denied access to appid: ingress operation: headers verb: GET"}

总结

文中使用的两种入口方案各有优缺点,方案 1 架构简单,也是我们常用的方案;方案 2 中相当于将入口控制器声明为 Dapr 应用,实际上暴露的是 Dapr 的 API,在实现上更像是一个全局的应用运行时。