跳至內容

安裝 Security-Guard

這裡我們展示如何在 Knative 中安裝 Security-Guard。Security-Guard 是 knative-Serving 的增強功能,需要在成功安裝 Knative-Serving 之後安裝。

使用 Security-Guard 需要您的叢集使用增強的 queue-proxy 映像。

此外,Security-Guard 還包含自動化功能,可自動學習每個服務的 Guardian。自動學習需要您在 Kubernetes 叢集上部署 `guard-service`。`guard-service` 應安裝在 `knative-serving` 命名空間中。

在生產環境中,您通常也會希望啟用 TLS 和權杖支援,以保護 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 入口的情況,在使用 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_TLS=true` 新增至 `guard-service` 的環境,以啟用 TLS 和伺服器端身份驗證,使用 Knative 發出的憑證。`guard-service` 將使用 `knative-serving` 命名空間的 `knative-serving-certs` secret 中的金鑰。

  2. 將 `GUARD_SERVICE_AUTH=true` 新增至 `guard-service` 的環境,以啟用使用權杖的用戶端身份驗證

  3. 將 `knative-serving` 命名空間中 `config-deployment` configmap 的 `queue-sidecar-rootca` 參數設定為 `knative-serving` 命名空間的 `knative-serving-certs` secret 中 `ca-cert.pem` 金鑰下定義的公鑰。這將通知 queue-proxy 使用 TLS 並批准 guard-service 憑證。

  4. 在 `knative-serving` 命名空間的 `config-deployment` configmap 中設定 `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 入口的情況,在使用 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 分享以達到此目的。了解更多。