启用 GKE Fleet Config Management
使用开源 Open Policy Agent Gatekeeper
需求背景
随着越来越多的企业客户采用混合和多云策略,随着工作负载在环境中的分布,集中的安全和治理变得越来越重要。例如:运维部门希望通过 K8S Manifest 文件只能创建内部负载均衡器(ILB)而禁止其他团队创建外部负载均衡器(NLB);或者需要遵循企业内部的域名规范来限制 K8S 服务命名规范;亦或者控制 namespace 中资源使用量等。针对这些需求,如果使用 GCP IAM 来进行设置时,只能控制 GCP 资源的访问权限,而无法控制 K8S 中细粒度的资源。
所以接下来,我们来介绍一下如何通过 Policy Controller 来满足这些类型的需求,以及如何使用它来帮助确保 Kubernetes 集群和工作负载以安全且合规的方式运行。Policy Controller 支持为 Kubernetes 集群应用和实施可编程政策。这些政策作为一种“保障措施”,可帮助集群和舰队的最佳实践、安全性和合规性管理。
使用 Anthos Policy Controller
Anthos 是我们的云中心容器平台,可以在任何地方一致地运行现代应用程序并具备可扩展性。Anthos Config Management(ACM)自动化了 Kubernetes 集群的策略和安全性,并由 Config Sync、Config Controller 和 Policy Controller 组成。
Config Sync 将集群状态与一个或多个 Git 存储库协调一致。Config Controller 是一个托管服务,允许管理员以声明性方式管理 Google Cloud Platform(GCP)资源。作为ACM的重要组成部分,Policy Controller 可以强制执行可完全编程的策略来保护集群。这些策略充当“护栏”,防止任何更改违反安全、操作或合规性控制。Policy Controller 可以帮助开发人员快速、安全地发布代码,加速应用程序现代化的进程。
我们非常高兴地宣布推出全新的内置 Policy Controller Dashboard,这是一个强大的工具,可轻松管理和监视应用于您的集群群集的策略护栏。有了 Policy Controller Dashboard,平台和安全管理员可以:
● 获取关于应用于集群群集的所有策略的状态的一目了然的视图,包括强制执行状态(dryrun 或 enforced)
● 通过参考每个违规行为的推荐意见,轻松解决策略违规行为问题
● 获得对群集资源合规性状态的可见性
Policy Controller Dashboard 旨在易于使用和直观,使各技能水平的用户轻松管理和监视其群集群集的违规行为。它允许您集中查看策略违规行为并在必要时采取行动。
该仪表板还可以显示哪些资源受特定策略的影响,并可以提供具有建议性的建议,以解决问题。
安装步骤
注:本实验略去了创建 GKE 集群并启用 Workload Identify 的过程,只演示了在已有的 GKE 集群上安装和使用 Policy Controller。另外,如果需要进行多集群的 Policy 管理,需采用 Config Sync 和 Config Controller 等功能,请参考其他相关文档,本文暂不讨论。
启用 GKE Fleet Config Management
gcloud beta container fleet config-management enable |
注册 GKE 集群到 Fleet
gcloud container fleet memberships register dpv2-test --gke-cluster=us-central1-c/dpv2-test --enable-workload-identity
Membership [projects/flius-vpc-2/locations/global/memberships/dpv2-test] already registered with the cluster [//container.googleapis.com/projects/flius-vpc-2/locations/us-central1-c/clusters/dpv2-test] in the Fleet. Finished registering to the Fleet. |
查询 Fleet 成员状态
gcloud container fleet memberships list
NAME EXTERNAL_ID LOCATION dpv2-test 87fa0be9-8889-41dd-ab61-aa8c17c67d5e global dpv2-test-2 dfa14252-ad46-41ff-904d-bc6035996c2d global |
安装 Policy Controller
vim apply-spec.yaml #这个例子中没有安装ConfigSync applySpecVersion: 1 spec: policyController: # Set to true to install and enable Policy Controller enabled: true templateLibraryInstalled: true auditIntervalSeconds: 60
gcloud beta container fleet config-management apply \\ --membership=dpv2-test \\ --config=apply-spec.yaml |
注:您也可以选择在UI上配置和启用 Policy Controller。
安装完 Policy Controller 之后,检查集群已经正确进行配置。
查询 Fleet Config Management 状态。
gcloud beta container fleet config-management status |
安装之后,一共有 4 个 system pod。
了解 Constraints
我们提供了几十种预定义的 Constraint 模版,来满足您各种不同的安全治理的需求,并提供了相关的例子和说明文档,您可以作为最佳实践来采用它们。具体请参见:
https://cloud.google.com/anthos-config-management/docs/latest/reference/constraint-template-library
例如,我们希望禁止创建将工作负载公开给外部 IP 的已知资源。这包括 Istio 网关资源和 Kubernetes Ingress 资源。除非满足以下条件,否则 Kubernetes 服务也不允许创建:Google Cloud 中 LoadBalancer 类型的任何 Service 都必须具有 "cloud.google.com/load-balancer-type": "Internal" 注释,即只允许内部负载均衡器而不允许外部负载均衡器。我们可以这样做:
首先定义限制,创建 constraints.yaml。
apiVersion: constraints.gatekeeper.sh/v1beta1 kind: K8sNoExternalServices metadata: name: no-external spec: parameters: cloudPlatform: GCP internalCIDRs: - 10.0.0.0/8 |
应用到集群。
kubectl apply -f constraints.yaml |
验证 Constraints
创建一个 service,类型是 NLB。
apiVersion: v1 kind: Service metadata: annotations: cloud.google.com/l4-rbs: "enabled" labels: app: nginx name: nginx spec: ports: - name: http port: 80 protocol: TCP targetPort: 80 selector: app: nginx sessionAffinity: None type: LoadBalancer |
当进行 kubectl apply service.yaml 时,你将会看到如下的错误:
Error from server (Forbidden): error when creating "service-nlb.yaml": admission webhook "validation.gatekeeper.sh" denied the request: [no-external] Creating services of type LoadBalancer without Internal annotation or not setting service.beta.kubernetes.io/aws-load-balancer-internal to true is not allowed |
使用开源 Open Policy Agent Gatekeeper
您还可以使用开源的 Open Policy Agent GateKeeper 项目来管理您的安全策略,推荐使用在小规模集群和非生产环境中,当然也需要提醒 OSS 项目后期需要自行维护,且没有预定义的 policy library。参考资料:https://open-policy-agent.github.io/gatekeeper/website/docs/install
安装步骤
kubectl create clusterrolebinding cluster-admin-binding \\ --clusterrole cluster-admin \\ --user liufan@google.com
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper/master/deploy/gatekeeper.yaml |
安装之后,可以看见一共有 4 个 pod。
安装 gatekeeper-library。
git clone https://github.com/open-policy-agent/gatekeeper-library.git
cd gatekeeper-library/library/general/requiredannotations
# 修改template.yaml
violation[{"msg": msg, "details": {"missing_annotations": missing}}] { provided := {annotation | input.review.object.metadata.annotations[annotation]} required := {annotation | annotation := input.parameters.annotations[_].key} missing := required - provided count(missing) > 0 input.review.object.spec.type == "LoadBalancer" # 只针对LoadBalancer Type msg := sprintf("you must provide annotation(s): %v", [missing]) }
kubectl apply -f template.yaml |
安装 constraints。
#vim constraints.yaml apiVersion: constraints.gatekeeper.sh/v1beta1 kind: K8sRequiredAnnotations metadata: name: all-must-have-certain-set-of-annotations spec: match: kinds: - apiGroups: - "" kinds: - Service parameters: annotations: - allowedRegex: "Internal" key: networking.gke.io/load-balancer-type message: "All services must be networking.gke.io/load-balancer-type: `Internal` annotations."
kubectl apply -f constraints.yaml |
验证 Constraints
尝试创建一个 service 为 NLB,将会看见如下错误:
#vim service.yaml apiVersion: v1 kind: Service metadata: annotations: cloud.google.com/l4-rbs: "enabled" labels: app: nginx name: nginx spec: ports: - name: http port: 80 protocol: TCP targetPort: 80 selector: app: nginx sessionAffinity: None type: LoadBalancer
kubectl apply -f service.yaml
Error from server (Forbidden): error when creating "service.yaml": admission webhook "validation.gatekeeper.sh" denied the request: [all-must-have-certain-set-of-annotations] you must provide annotation(s): {"networking.gke.io/load-balancer-type"} |
总结
本文介绍了通过 Policy Controller 来精细化管理您在 Kubernetes 中的资源,在 GCP 上我们提供了预定义的 Constraints 模版来作为最佳实践,帮助您轻松地进行安全和治理您的 GKE 集群。
文章信息
相关推荐
