背景
在企业中,出于安全考虑对外部服务的访问是被严格管控的,一般都会默认禁止对外部服务的访问。但是有时系统不可避免的会依赖外部服务,安全团队就会将这些外部服务加入到信任白名单中,并限制只有某些访问源才可以访问这些外部服务。通常做法是安全团队收集访问源的 IP 地址,将其加入到允许的源名单中,所有来自这些 IP 地址的对外部服务的访问都被放行,否则被拒绝。或者,安全团队通过设置网络规则,某些设备上发出的对外请求才会被允许,其他设备上的则都被拒绝。
在 Kubernetes 的环境中,pod 的 IP 是“快消品”,一旦 pod 销毁其 IP 地址被回收并在将来会分配给其他的 pod。这就给安全团队增加了管理的难度。
服务网格将应用容器的流量拦截到 sidecar proxy 中,由 sidecar proxy 对流量进行处理,那就可以由 sidecar 来管理对外部服务的网络。这篇文章就来为大家介绍如何使用服务网格的流量管理功能来轻松管理外部服务的访问。
实现
对外部服务的流量管理有两种方式,一种由 sidecar 直接将请求发往外部服务,这种我们称之为 边车透传;另外一种是 sidecar 先将流量转发给出口网关(Egress Gateway),然后出口网关完成到外部服务的转发。
使用出口网关虽然从链路上来看多了一跳,但是提供了统一的出口管理。安全团队可以在固定的设备上设置网络规则,来允许对外部服务的访问。再通过节点选择器,当出口网关调度到这些设备上。两种方式各有优劣,需要根据具体场景来选择。
对于流量的处理模式也分为两种:宽松模式 和 策略模式。前者会放行所有流量,而后者只会放行配置的访问源和目标的流量。
接下来,我们通过示例进行演示。
环境准备
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
复制代码
下载 osm-edge CLI
system=$(uname -s | tr [:upper:] [:lower:])arch=$(dpkg --print-architecture)release=v1.2.0curl -L https://github.com/flomesh-io/osm-edge/releases/download/${release}/osm-edge-${release}-${system}-${arch}.tar.gz | tar -vxzf -./${system}-${arch}/osm versioncp ./${system}-${arch}/osm /usr/local/bin/
复制代码
安装 osm-edge
export osm_namespace=osm-system export osm_mesh_name=osm
osm install \ --mesh-name "$osm_mesh_name" \ --osm-namespace "$osm_namespace" \ --set=osm.enableEgress=false \ --set=osm.image.pullPolicy=Always
复制代码
部署示例应用
kubectl create namespace curlosm namespace add curlkubectl apply -n curl -f https://raw.githubusercontent.com/flomesh-io/osm-edge-docs/main/manifests/samples/curl/curl.yaml
复制代码
测试
集群外的第三方服务,我们使用 httpbin.org 和 httpstat.us。执行下面的命令,在应用 curl 的 pod 中发送请求到 httpbin.org。
kubectl exec "$(kubectl get pod -n curl -l app=curl -o jsonpath='{.items..metadata.name}')" -n curl -c curl -- curl -sI http://httpbin.org:80/get
复制代码
会收到下面的错误,说明请求无法发送到集群外。
command terminated with exit code 7
复制代码
演示
边车透传
宽松模式
执行下面的命令,确保出口宽松模式开启。
kubectl patch meshconfig osm-mesh-config -n "$osm_namespace" -p '{"spec":{"traffic":{"enableEgress":true}}}' --type=merge
复制代码
发送请求进行测试。
kubectl exec "$(kubectl get pod -n curl -l app=curl -o jsonpath='{.items..metadata.name}')" -n curl -c curl -- curl -sI http://httpbin.org:80/get http://httpstat.us/200
复制代码
两个服务的请求都收到了 200 OK 的响应,说明在宽松模式所有对外部服务的访问都被允许。
HTTP/1.1 200 OKdate: Thu, 10 Nov 2022 03:02:02 GMTcontent-type: application/jsoncontent-length: 571server: pipyaccess-control-allow-origin: *access-control-allow-credentials: truex-pipy-upstream-service-time: 1411connection: keep-alive
HTTP/1.1 200 OKcontent-length: 6content-type: text/plaindate: Thu, 10 Nov 2022 03:02:02 GMTserver: pipyset-cookie: ARRAffinity=8704f50f7b5106506d91f9a9bf4b765528533ff3a17e44c2bee3d021b099959c;Path=/;HttpOnly;Domain=httpstat.usrequest-context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53x-pipy-upstream-service-time: 360connection: keep-alive
复制代码
策略模式
出口策略模式只有在禁用出口宽松模式的情况下才生效,执行下面的命令来禁用出口宽松模式。
kubectl patch meshconfig osm-mesh-config -n "$osm_namespace" -p '{"spec":{"traffic":{"enableEgress":false}}}' --type=merge
复制代码
在出口策略中,通过 spec.resources 指定访问源使用的 serviceaccount;spec.hosts 和 spec.ports 配置了目标的主机名和端口。sidecar 只会转发发送到配置中的目标的请求。比如下面的
应用出口策略。
kubectl apply -f - <<EOFkind: EgressapiVersion: policy.openservicemesh.io/v1alpha1metadata: name: httpbin-80 namespace: curlspec: sources: - kind: ServiceAccount name: curl namespace: curl hosts: - httpbin.org ports: - number: 80 protocol: httpEOF
复制代码
上面的策略中,我们只配置了 httpbin.org ,尝试向两个服务发送请求来测试出口策略
kubectl exec "$(kubectl get pod -n curl -l app=curl -o jsonpath='{.items..metadata.name}')" -n curl -c curl -- curl -sI http://httpbin.org:80/get http://httpstat.us/200
复制代码
从结果上可以看到,http://httpbin.org 的访问正常,而对 httpstat.us 的访问收到 403 Forbidden 的响应,被 sidecar 禁止。
HTTP/1.1 200 OKdate: Thu, 10 Nov 2022 03:01:03 GMTcontent-type: application/jsoncontent-length: 571server: pipyaccess-control-allow-origin: *access-control-allow-credentials: truex-pipy-upstream-service-time: 947connection: keep-alive
HTTP/1.1 403 Forbiddencontent-length: 13connection: keep-alive
复制代码
执行命令 kubectl delete egress httpbin-80 -n curl 清理策略。
出口网关
部署出口网关
kubectl apply -n egress-gateway -f https://raw.githubusercontent.com/flomesh-io/osm-edge-docs/main/manifests/egress/egress-gateway.yamlosm namespace add egress-gatewaykubectl annotate namespace egress-gateway openservicemesh.io/sidecar-injection=disabled --overwrite
复制代码
有了网关之后,我们需要添加一条全局的出口策略。spec.gateway 对 sidecar 声明出口流量可以转发到命名空间 egress-gateway 下的 Service global-egress-gateway。
kubectl apply -f - <<EOFkind: EgressGatewayapiVersion: policy.openservicemesh.io/v1alpha1metadata: name: global-egress-gateway namespace: egress-gatewayspec: global: - service: global-egress-gateway namespace: egress-gatewayEOF
复制代码
宽松模式
既然是宽松模式,需要执行下面的命令来开启宽松模式。
kubectl patch meshconfig osm-mesh-config -n "$osm_namespace" -p '{"spec":{"traffic":{"enableEgress":true}}}' --type=merge
复制代码
尝试向两个服务发送请求。
kubectl exec "$(kubectl get pod -n curl -l app=curl -o jsonpath='{.items..metadata.name}')" -n curl -c curl -- curl -sI http://httpbin.org:80/get http://httpstat.us/200
复制代码
两个服务的访问都收到 200 OK 的响应,发送成功。
HTTP/1.1 200 OKDate: Thu, 10 Nov 2022 08:15:32 GMTContent-Type: application/jsonContent-Length: 258Connection: keep-aliveServer: gunicorn/19.9.0Access-Control-Allow-Origin: *Access-Control-Allow-Credentials: true
HTTP/1.1 200 OKContent-Length: 6Content-Type: text/plainDate: Thu, 10 Nov 2022 08:15:32 GMTServer: KestrelSet-Cookie: ARRAffinity=8704f50f7b5106506d91f9a9bf4b765528533ff3a17e44c2bee3d021b099959c;Path=/;HttpOnly;Domain=httpstat.usRequest-Context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53
复制代码
请求发送成功还不够,我们还需要确认下请求是从出口网关出去的。出口网关在日志中记录了请求和响应的信息,只需要查看出口网关日志中是否上面的请求。执行下面命令检索出口网关的日志。
kubectl logs -n egress-gateway "$(kubectl get pod -n egress-gateway -l app=global-egress-gateway -o jsonpath='{.items..metadata.name}')" | grep httpbin.org | jq
复制代码
从命令的输出结果可以看到出口网关转发了请求,并记录了请求的相关信息,包括请求时间、访问源的地址、主机名、路径、方法等。
{ "connection_id": "f34e0b0aa1034190", "request_time": "2022-11-10T08:15:31.864Z", "source_address": "10.42.0.17", "source_port": 36856, "host": "httpbin.org", "path": "/get", "method": "HEAD"}
复制代码
假如说,只想放行 httpbin.org 的请求,应该如何处理呢?出口网关同样支持策略模式。
策略模式
执行策略模式,首先要禁用出口宽松模式。
kubectl patch meshconfig osm-mesh-config -n "$osm_namespace" -p '{"spec":{"traffic":{"enableEgress":false}}}' --type=merge
复制代码
与边车透传时的策略一样,在配置中指定请求源的 serviceaccount,以及目的服务的 host 和 port。
kubectl apply -f - <<EOFkind: EgressapiVersion: policy.openservicemesh.io/v1alpha1metadata: name: httpbin-80 namespace: curlspec: sources: - kind: ServiceAccount name: curl namespace: curl hosts: - httpbin.org ports: - number: 80 protocol: httpEOF
复制代码
发送测试请求
kubectl exec "$(kubectl get pod -n curl -l app=curl -o jsonpath='{.items..metadata.name}')" -n curl -c curl -- curl -sI http://httpbin.org:80/get http://httpstat.us/200
复制代码
如我们所期望的,只有 httpbin.org 的服务才被放行。
HTTP/1.1 200 OKdate: Thu, 10 Nov 2022 09:17:25 GMTcontent-type: application/jsoncontent-length: 572server: pipyaccess-control-allow-origin: *access-control-allow-credentials: truex-pipy-upstream-service-time: 419connection: keep-alive
HTTP/1.1 403 Forbiddencontent-length: 13connection: keep-alive
复制代码
总结
服务网格为出口流量提供了非常灵活的管理方式和部署模型,允许我们以云原生的方式对出口流量进行管理。每种都各有优缺点,可根据具体场景和需求灵活选择。
评论