天天看點

kubernetes安全之Polaris

一 polaris簡介

Fairwinds' Polaris 讓您的叢集順利航行。它運作各種檢查以確定使用最佳實踐配置 Kubernetes pod 和控制器,進而幫助您避免将來出現問題。

Polaris 可以在三種不同的模式下運作:

  • 作為儀表闆,您可以稽核叢集内運作的内容。
  • 作為準入控制器,您可以自動拒絕不符合組織政策的工作負載。
  • 作為指令行工具,您可以測試本地 YAML 檔案,例如作為 CI/CD 流程的一部分。
kubernetes安全之Polaris

二 部署Polaris

2.1 DashBoard部署

Polaris 儀表闆可以使用 kubectl 或 Helm 安裝在叢集上。它也可以在本地運作,使用存儲在您的 KUBECONFIG 中的憑證連接配接到您的叢集。

儀表闆是了解叢集或基礎設施即代碼中的哪些工作負載不符合最佳實踐的好方法。

2.1.1 kubectl安裝

kubectl apply -f https://github.com/fairwindsops/polaris/releases/latest/download/dashboard.yaml
kubectl port-forward --namespace polaris svc/polaris-dashboard 8080:80      

2.1.2 Helm安裝

helm repo add fairwinds-stable https://charts.fairwinds.com/stable
helm upgrade --install polaris fairwinds-stable/polaris --namespace polaris --create-namespace
kubectl port-forward --namespace polaris svc/polaris-dashboard --address 0.0.0.0 8080:80      

2.1.3 Local Binary

您需要為儀表闆設定有效的 KUBECONFIG 才能連接配接到您的叢集。 二進制版本可以從釋出頁面下載下傳(打開新視窗),也可以使用 Homebrew 安裝(打開新視窗):

brew tap reactiveops/tap
brew install reactiveops/tap/polaris
polaris dashboard --port 8080      

您還可以将儀表闆指向本地檔案系統,而不是實時叢集:

polaris dashboard --port 8080 --audit-path=./deploy/      

圖像界面

kubernetes安全之Polaris

檢視容器配置異常

kubernetes安全之Polaris

2.2 Admission Controller

Polaris 可以作為一個接納控制器運作,作為一個确認的網絡挂鈎。它接受與儀表闆相同的配置,并可以運作相同的驗證。Webhook 将拒絕觸發危險級别檢查的任何工作負載。這表明了 Polaris 更大的目标,不僅僅是通過儀表闆可見性來鼓勵更好的配置,而是通過這個 webhook 來實際執行它。請注意,Polaris 不會改變您的工作負載,隻會阻止不符合配置政策的工作負載。

2.2.1 安裝

使用helm進行部署

helm repo add fairwinds-stable https://charts.fairwinds.com/stable
helm upgrade --install polaris fairwinds-stable/polaris --namespace polaris --create-namespace \
  --set webhook.enable=true --set dashboard.enable=false      

2.3 Infrastructure as Code

想在一個地方看到所有檢測業務的結果嗎?

檢視 Fairwinds Insights (打開新視窗) Polaris 可以在指令行上用于稽核存儲在 YAML 檔案中的本地 Kubernetes 清單。

這對于針對作為 CI/CD 管道一部分的基礎設施代碼運作 Polaris 特别有幫助。使用可用的指令行标志,如果您的 Polaris 得分下降到某個門檻值以下,或者出現任何危險級别的問題,将導緻 CI/CD 失敗

三 檢查項目

3.1 Security

容器需要遵循安全規則

key default description

​hostIPCSet​

​danger​

Fails when ​

​hostIPC​

​ attribute is configured.

​hostPIDSet​

​danger​

Fails when ​

​hostPID​

​ attribute is configured.

​notReadOnlyRootFilesystem​

​warning​

Fails when ​

​securityContext.readOnlyRootFilesystem​

​ is not true.

​privilegeEscalationAllowed​

​danger​

Fails when ​

​securityContext.allowPrivilegeEscalation​

​ is true.

​runAsRootAllowed​

​warning​

Fails when ​

​securityContext.runAsNonRoot​

​ is not true.

​runAsPrivileged​

​danger​

Fails when ​

​securityContext.privileged​

​ is true.

​insecureCapabilities​

​warning​

Fails when ​

​securityContext.capabilities​

​ includes one of the capabilities ​​listed here(opens new window)​​

​dangerousCapabilities​

​danger​

Fails when ​

​securityContext.capabilities​

​ includes one of the capabilities ​​listed here(opens new window)​​

​hostNetworkSet​

​warning​

Fails when ​

​hostNetwork​

​ attribute is configured.

​hostPortSet​

​warning​

Fails when ​

​hostPort​

​ attribute is configured.

​tlsSettingsMissing​

​warning​

Fails when an Ingress lacks TLS settings.

確定 Kubernetes 的工作負載安全是整個叢集安全的重要組成部分。總體目标應該是確定容器以盡可能少的特權運作。這包括避免使用權限提升檔案,不使用帶有根使用者的容器,不給主機網絡提供過多的通路權限,以及盡可能使用隻讀檔案系統。

啟用 hostNetwork 屬性運作的 pod 将可以通路環回裝置,監聽本地主機上的服務,并且可以用來窺探同一節點上其他吊艙的網絡活動。在某些情況下,需要将 hostNetwork 設定為 true,比如部署一個像 Flannel 這樣的網絡插件。

在容器上設定 hostPort 屬性将確定在它部署到的每個節點上的特定端口上都可以通路它。不幸的是,當它被指定時,它限制了一個 pod 可以在叢集中實際排程的位置。

3.2 Efficiency

這些檢查確定配置了 CPU 和記憶體設定,以便 Kubernetes 能夠有效地排程您的工作負載。

為在 Kubernetes 運作的容器配置資源請求和限制是一個重要的最佳實踐。設定适當的資源請求将確定所有應用程式具有足夠的計算資源。設定适當的資源限制将確定應用程式不會消耗太多資源。

key default description

​cpuRequestsMissing​

​warning​

Fails when ​

​resources.requests.cpu​

​ attribute is not configured.

​memoryRequestsMissing​

​warning​

Fails when ​

​resources.requests.memory​

​ attribute is not configured.

​cpuLimitsMissing​

​warning​

Fails when ​

​resources.limits.cpu​

​ attribute is not configured.

​memoryLimitsMissing​

​warning​

Fails when ​

​resources.limits.memory​

​ attribute is not configured.

3.3 Reliability

這些檢查有助于確定您的工作負載始終可用,并且正在運作正确的映像。

key default description

​readinessProbeMissing​

​warning​

Fails when a readiness probe is not configured for a pod.

​livenessProbeMissing​

​warning​

Fails when a liveness probe is not configured for a pod.

​tagNotSpecified​

​danger​

Fails when an image tag is either not specified or ​

​latest​

​.

​pullPolicyNotAlways​

​warning​

Fails when an image pull policy is not ​

​always​

​.

​priorityClassNotSet​

​ignore​

Fails when a priorityClassName is not set for a pod.

​deploymentMissingReplicas​

​warning​

Fails when there is only one replica for a deployment.

​missingPodDisruptionBudget​

​ignore​

四 自定義檢測

4.1 禁用quay.io

checks:
  imageRegistry: warning

customChecks:
  imageRegistry:
    successMessage: Image comes from allowed registries
    failureMessage: Image should not be from disallowed registry
    category: Security
    target: Container
    schema:
      '$schema': http://json-schema.org/draft-07/schema
      type: object
      properties:
        image:
          type: string
          not:
            pattern: ^quay.io      
kubernetes安全之Polaris

4.2 檢測CPU和記憶體

customChecks:
  resourceLimits:
    containers:
      exclude:
      - initContainer
    successMessage: Resource limits are within the required range
    failureMessage: Resource limits should be within the required range
    category: Resources
    target: Container
    schema:
      '$schema': http://json-schema.org/draft-07/schema
      type: object
      required:
      - resources
      properties:
        resources:
          type: object
          required:
          - limits
          properties:
            limits:
              type: object
              required:
              - memory
              - cpu
              properties:
                memory:
                  type: string
                  resourceMinimum: 100M
                  resourceMaximum: 6G
                cpu:
                  type: string
                  resourceMinimum: 100m
                  resourceMaximum: "2"      

五 其他

參考連結

  • ​​polaris.docs.fairwinds.com/​​
  • ​​github.com/FairwindsOp…​​