跳到内容

安装 Security-Guard

这里展示了如何在 Knative 中安装 Security-Guard。Security-Guard 是 knative-Serving 的增强功能,需要在 Knative-Serving 成功安装后进行安装。

使用 Security-Guard 需要您的集群使用增强型 queue-proxy 镜像。

此外,Security-Guard 包含用于自动学习每个服务的 Guardian 的自动化。自动学习需要您在 kubernetes 集群上部署 guard-serviceguard-service 应该安装在 knative-serving 命名空间中。

在生产环境中,您通常也希望启用 TLS 和 Token 支持来保护 queue-proxy 与 guard-service 的通信,如下所述。

在您开始之前

在安装 Security-Guard 之前,请了解关于 Security-Guard 的信息

安装步骤

要开始本教程,在安装 Knative Serving 后,请运行以下步骤来替换您的 queue-proxy 镜像并部署 guard-service

  1. 使用 git clone git@github.com:knative-extensions/security-guard.git 克隆 Security-Guard 仓库

  2. 执行 cd security-guard

  3. 运行 ko apply -Rf ./config

使用已发布的镜像更新您的系统以启用 Security-Guard

  1. 在 config-features ConfigMap 中将名为 queueproxy.mount-podinfo 的功能设置为 allowed

    一种简单的方法是使用

    kubectl apply -f https://raw.githubusercontent.com/knative-extensions/security-guard/release-0.4/config/deploy/config-features.yaml
    
  2. 在 config-deployment ConfigMap 中将部署参数 queue-sidecar-image 设置为 gcr.io/knative-releases/knative.dev/security-guard/cmd/queue

    一种简单的方法是使用

    kubectl apply -f https://github.com/knative-extensions/security-guard/releases/download/v0.4.0/queue-proxy.yaml
    
  3. 使用以下命令将必要的 Security-Guard 资源添加到您的集群

    kubectl apply -f https://raw.githubusercontent.com/knative-extensions/security-guard/release-0.4/config/resources/gateAccount.yaml
    kubectl apply -f https://raw.githubusercontent.com/knative-extensions/security-guard/release-0.4/config/resources/serviceAccount.yaml
    kubectl apply -f https://raw.githubusercontent.com/knative-extensions/security-guard/release-0.4/config/resources/guardiansCrd.yaml
    
  4. 在您的系统上部署 guard-service 以启用微规则的自动学习。

    一种简单的方法是使用

    kubectl apply -f https://github.com/knative-extensions/security-guard/releases/download/v0.4.0/guard-service.yaml
    

注意

以下示例展示了使用 kourier ingress 的情况,在使用 istio 或 contour 进行安装时,请做出必要的更改。

使用 Knative Operator 安装 Security-Guard 和 Serving(使用 Kourier)的示例脚本。

kubectl apply --filename - <<EOF
apiVersion: v1
kind: Namespace
metadata:
  name: knative-serving
---
apiVersion: operator.knative.dev/v1beta1
kind: KnativeServing
metadata:
  name: knative-serving
  namespace: knative-serving
spec:
  security:
    securityGuard:
      enabled: true
  ingress:
    kourier:
      enabled: true
  config:
    network:
      ingress.class: "kourier.ingress.networking.knative.dev"
EOF

kubectl apply -f https://raw.githubusercontent.com/knative-extensions/security-guard/release-0.4/config/resources/gateAccount.yaml

每个命名空间的设置

为了在命名空间中部署受 guard 保护的服务,请在使用的每个命名空间上为 guard-gate 提供必要的权限

kubectl apply -f https://raw.githubusercontent.com/knative-extensions/security-guard/release-0.4/config/resources/gateAccount.yaml

其他生产配置

建议使用以下方法之一来保护 queue-proxy 与 guard-service 之间的通信

  1. guard-service 的环境中添加 GUARD_SERVICE_TLS=true 以启用 TLS 和使用 Knative 发行证书的服务器端身份验证。guard-service 将使用 knative-serving 命名空间的 knative-serving-certs 密钥中的密钥。

  2. guard-service 的环境中添加 GUARD_SERVICE_AUTH=true 以启用使用令牌的客户端身份验证

  3. knative-serving 命名空间的 config-deployment 配置映射中的 queue-sidecar-rootca 参数设置为 knative-serving-certs 密钥的 ca-cert.pem 密钥中定义的公钥。这将告知 queue-proxy 使用 TLS 并批准 guard-service 证书。

  4. knative-serving 命名空间的 config-deployment 配置映射中设置 queue-sidecar-token-audiences = "guard-service"。这将为每个 queue-proxy 实例生成一个具有 guard-service 访问者的令牌。

使用以下脚本在 guard-service 中设置 TLS 和令牌支持

echo "Add TLS and Tokens to guard-service"
kubectl patch deployment guard-service -n knative-serving -p '{"spec":{"template":{"spec":{"containers":[{"name":"guard-service","env":[{"name": "GUARD_SERVICE_TLS", "value": "true"}, {"name": "GUARD_SERVICE_AUTH", "value": "true"}]}]}}}}'

使用以下脚本在 guard-gates 中设置 TLS 和令牌支持

echo "Copy the certificate to a temporary file"
ROOTCA="$(mktemp)"
FILENAME=`basename $ROOTCA`
kubectl get secret -n knative-serving knative-serving-certs -o json| jq -r '.data."ca-cert.pem"' | base64 -d >  $ROOTCA

echo "Get the certificate in a configmap friendly form"
CERT=`kubectl create cm config-deployment --from-file $ROOTCA -o json --dry-run=client |jq .data.\"$FILENAME\"`

echo "Add TLS and Tokens to config-deployment configmap"
kubectl patch cm config-deployment -n knative-serving -p '{"data":{"queue-sidecar-token-audiences": "guard-service", "queue-sidecar-rootca": '"$CERT"'}}'

echo "cleanup"
rm  $ROOTCA

使用以下脚本读取 guard-service 和 guard-gates 的 TLS 和令牌设置

echo "Results:"
kubectl get cm config-deployment -n knative-serving -o json|jq '.data'
kubectl get deployment guard-service -n knative-serving -o json|jq .spec.template.spec.containers[0].env

使用以下脚本取消设置 guard-service 中的 TLS 和令牌支持

echo "Remove TLS and Tokens from  guard-service deployment"
kubectl patch deployment guard-service -n knative-serving -p '{"spec":{"template":{"spec":{"containers":[{"name":"guard-service","env":[{"name": "GUARD_SERVICE_TLS", "value": "false"}, {"name": "GUARD_SERVICE_AUTH", "value": "false"}]}]}}}}'

使用以下脚本取消设置 guard-gates 中的 TLS 和令牌支持

echo "Remove TLS and Tokens from config-deployment configmap"
kubectl patch cm config-deployment -n knative-serving -p '{"data":{"queue-sidecar-token-audiences": "", "queue-sidecar-rootca": ""}}'

注意

以下示例展示了使用 kourier ingress 的情况,在使用 istio 或 contour 进行安装时,请做出必要的更改。

使用 Knative Operator 安装 Security-Guard(使用 TLS)和 Serving(使用 Kourier)的示例脚本。

kubectl apply --filename - <<EOF
apiVersion: v1
kind: Namespace
metadata:
  name: knative-serving
---
apiVersion: operator.knative.dev/v1beta1
kind: KnativeServing
metadata:
  name: knative-serving
  namespace: knative-serving
EOF

echo "Waiting for secret to be created (CTRL-C to exit)"
PEM=""
while [[ -z $PEM ]]
do
  echo -n "."
  sleep 1
  DOC=`kubectl get secret -n knative-serving knative-serving-certs -o json 2> /dev/null`
  PEM=`echo $DOC | jq -r '.data."ca-cert.pem"'`
done
echo " Secret found!"

echo "Copy the certificate to file"
ROOTCA="$(mktemp)"
FILENAME=`basename $ROOTCA`
echo $PEM | base64 -d >  $ROOTCA

echo "Create a temporary config-deployment configmap with the certificate"
CERT=`kubectl create cm config-deployment --from-file $ROOTCA -o json --dry-run=client |jq .data.\"$FILENAME\"`

echo "cleanup"
rm $ROOTCA

kubectl apply --filename - <<EOF
apiVersion: operator.knative.dev/v1beta1
kind: KnativeServing
metadata:
  name: knative-serving
  namespace: knative-serving
spec:
  deployments:
  - name: guard-service
    env:
    - container: guard-service
      envVars:
      - name: GUARD_SERVICE_TLS
        value: "true"
      - name: GUARD_SERVICE_AUTH
        value: "true"
  security:
    securityGuard:
      enabled: true
  ingress:
    kourier:
      enabled: true
  config:
    network:
      ingress.class: "kourier.ingress.networking.knative.dev"
    deployment:
      queue-sidecar-rootca: ${CERT}
      queue-sidecar-token-audiences: guard-service
EOF

我们使用分析和 Cookie 来了解网站流量。有关您使用我们网站的信息将与 Google 共享,用于该目的。 了解更多。