信任域迁移
本任务向您展示如何在不更改授权策略的情况下从一个信任域迁移到另一个信任域。
在 Istio 1.4 中,我们引入了 alpha 功能来支持 信任域迁移 用于授权策略。这意味着如果 Istio 网格需要更改其 信任域,则不需要手动更改授权策略。在 Istio 中,如果 工作负载 运行在命名空间 foo
中,服务帐户为 bar
,系统的信任域为 my-td
,则该工作负载的身份为 spiffe://my-td/ns/foo/sa/bar
。默认情况下,Istio 网格信任域为 cluster.local
,除非您在安装期间指定它。
开始之前
在开始此任务之前,请执行以下操作
阅读 Istio 授权概念。
使用自定义信任域和启用双向 TLS 安装 Istio。
$ istioctl install --set profile=demo --set meshConfig.trustDomain=old-td
在
default
命名空间中部署 httpbin 示例,并在default
和curl-allow
命名空间中部署 curl 示例$ kubectl label namespace default istio-injection=enabled $ kubectl apply -f @samples/httpbin/httpbin.yaml@ $ kubectl apply -f @samples/curl/curl.yaml@ $ kubectl create namespace curl-allow $ kubectl label namespace curl-allow istio-injection=enabled $ kubectl apply -f @samples/curl/curl.yaml@ -n curl-allow
应用以下授权策略,拒绝对
httpbin
的所有请求,除了来自curl-allow
命名空间中的curl
的请求。$ kubectl apply -f - <<EOF apiVersion: security.istio.io/v1 kind: AuthorizationPolicy metadata: name: service-httpbin.default.svc.cluster.local namespace: default spec: rules: - from: - source: principals: - old-td/ns/curl-allow/sa/curl to: - operation: methods: - GET selector: matchLabels: app: httpbin --- EOF
请注意,授权策略可能需要几十秒才能传播到 sidecar。
验证对
httpbin
的请求来自default
命名空间中的curl
被拒绝。$ kubectl exec "$(kubectl get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 403
curl-allow
命名空间中的curl
被允许。$ kubectl exec "$(kubectl -n curl-allow get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -n curl-allow -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 200
在没有信任域别名的情况下迁移信任域
使用新的信任域安装 Istio。
$ istioctl install --set profile=demo --set meshConfig.trustDomain=new-td
重新部署 istiod 以获取信任域更改。
$ kubectl rollout restart deployment -n istio-system istiod
Istio 网格现在使用新的信任域
new-td
运行。重新部署
httpbin
和curl
应用程序以获取来自新的 Istio 控制平面的更改。$ kubectl delete pod --all
$ kubectl delete pod --all -n curl-allow
验证来自
default
命名空间中的curl
和curl-allow
命名空间中的curl
的对httpbin
的请求都被拒绝。$ kubectl exec "$(kubectl get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 403
$ kubectl exec "$(kubectl -n curl-allow get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -n curl-allow -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 403
这是因为我们指定了拒绝对
httpbin
的所有请求的授权策略,除了来自old-td/ns/curl-allow/sa/curl
身份的请求,这是curl-allow
命名空间中curl
应用程序的旧身份。当我们在上面迁移到新的信任域(即new-td
)时,此curl
应用程序的身份现在为new-td/ns/curl-allow/sa/curl
,与old-td/ns/curl-allow/sa/curl
不相同。因此,来自curl-allow
命名空间中curl
应用程序对httpbin
的请求在之前被允许,现在被拒绝。在 Istio 1.4 之前,唯一的方法是手动更改授权策略。在 Istio 1.4 中,我们引入了一种简单的方法,如下所示。
使用信任域别名迁移信任域
使用新的信任域和信任域别名安装 Istio。
$ cat <<EOF > ./td-installation.yaml apiVersion: install.istio.io/v1alpha1 kind: IstioOperator spec: meshConfig: trustDomain: new-td trustDomainAliases: - old-td EOF $ istioctl install --set profile=demo -f td-installation.yaml -y
在不更改授权策略的情况下,验证来自以下位置对
httpbin
的请求:default
命名空间中的curl
被拒绝。$ kubectl exec "$(kubectl get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 403
curl-allow
命名空间中的curl
被允许。$ kubectl exec "$(kubectl -n curl-allow get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -n curl-allow -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 200
最佳实践
从 Istio 1.4 开始,在编写授权策略时,您应该考虑在策略中使用值 cluster.local
作为信任域部分。例如,应该使用 cluster.local/ns/curl-allow/sa/curl
,而不是 old-td/ns/curl-allow/sa/curl
。请注意,在这种情况下,cluster.local
不是 Istio 网格信任域(信任域仍然是 old-td
)。但是,在授权策略中,cluster.local
是一个指向当前信任域(即 old-td
(以及后面的 new-td
)及其别名的指针。通过在授权策略中使用 cluster.local
,当您迁移到新的信任域时,Istio 会检测到这一点,并将新的信任域视为旧的信任域,而无需您包含别名。
清理
$ kubectl delete authorizationpolicy service-httpbin.default.svc.cluster.local
$ kubectl delete deploy httpbin; kubectl delete service httpbin; kubectl delete serviceaccount httpbin
$ kubectl delete deploy curl; kubectl delete service curl; kubectl delete serviceaccount curl
$ istioctl uninstall --purge -y
$ kubectl delete namespace curl-allow istio-system
$ rm ./td-installation.yaml