如何拒绝在kubernetes码头注册?

7qhs6swi  于 2023-02-21  发布在  Kubernetes
关注(0)|答案(3)|浏览(136)

我想拒绝所有码头注册,除了我自己的一个。我正在寻找一个码头注册和他们的形象某种政策。
例如,我的注册表名是registry.my.com。我想让kubernetes只从registry.my.com拉/运行映像,所以:

image: prometheus:2.6.1

或任何其他应被拒绝,而:

image: registry.my.com/prometheus:2.6.1

不应该。
有办法做到吗?

8nuwlpux

8nuwlpux1#

Admission Controllers是你要找的。
准入控制器拦截操作以验证在api服务器提交操作之前应该发生什么。
一个例子是ImagePolicyWebhook,它是一个准入控制器,用于拦截Image操作以验证它是应该被允许还是拒绝。
它将使用如下有效负载调用REST端点:

{  
  "apiVersion":"imagepolicy.k8s.io/v1alpha1",
  "kind":"ImageReview",
  "spec":{  
    "containers":[  
      {  
        "image":"myrepo/myimage:v1"
      },
      {  
        "image":"myrepo/myimage@sha256:beb6bd6a68f114c1dc2ea4b28db81bdf91de202a9014972bec5e4d9171d90ed"
      }
    ],
    "annotations":[  
      "mycluster.image-policy.k8s.io/ticket-1234": "break-glass"
    ],
    "namespace":"mynamespace"
  }
}

API回答为允许

{
  "apiVersion": "imagepolicy.k8s.io/v1alpha1",
  "kind": "ImageReview",
  "status": {
    "allowed": true
  }
}

拒绝

{
  "apiVersion": "imagepolicy.k8s.io/v1alpha1",
  "kind": "ImageReview",
  "status": {
    "allowed": false,
    "reason": "image currently blacklisted"
  }
}

端点可以是Lambda函数或在集群中运行的容器。
这个github repo github.com/flavio/kube-image-bouncer实现了一个示例,使用ImagePolicyWebhook拒绝使用标签“Latest”的容器。
还有一个选项是在启动时使用标志 registry-whitelist 来传递一个逗号分隔的允许注册表列表,ValidatingAdmissionWebhook将使用该列表来验证注册表是否被列入白名单。

另一个备选方案是项目Open Policy Agent [OPA]。
OPA是一个灵活的引擎,用于根据规则创建策略以匹配资源,并根据这些表达式的结果做出决策。它是一个变异和验证webhook,上面提到的准入控制器会调用它来匹配Kubernetes API服务器请求。总之,操作的工作方式与上面描述的类似。唯一的区别是规则是作为配置而不是代码来编写的。2上面的重写器使用OPA的例子类似于:

package admission

import data.k8s.matches

deny[{
    "id": "container-image-whitelist",  # identifies type of violation
    "resource": {
        "kind": "pods",                 # identifies kind of resource
        "namespace": namespace,         # identifies namespace of resource
        "name": name                    # identifies name of resource
    },
    "resolution": {"message": msg},     # provides human-readable message to display
}] {
    matches[["pods", namespace, name, matched_pod]]
    container = matched_pod.spec.containers[_]
    not re_match("^registry.acmecorp.com/.+$", container.image) # The actual validation
    msg := sprintf("invalid container registry image %q", [container.image])
}

上述内容可转化为:* 拒绝容器映像与以下注册表不匹配的任何pod registry.acmecorp.com*

vuktfyat

vuktfyat2#

目前还不能用一个命令来启用或禁用,但有一些准入控制器可以使用。
如果您使用的是redhat平台,并且只在RHEL上运行docker或kubernetes节点,并将RHEL docker作为容器运行时,则可以将那里的注册表列入白名单。

将Docker注册表列入白名单

您可以指定Docker注册中心的白名单,以便精心设计一组可供OpenShift容器平台用户下载的映像和模板。您可以将这组经过精心设计的映像和模板放置在一个或多个Docker注册中心中,然后添加到白名单中。使用白名单时,OpenShift容器平台中仅可访问指定的注册中心,默认情况下,所有其他注册中心将被拒绝访问。
要配置白名单:
编辑/etc/sysconfig/docker文件以阻止所有注册表:

BLOCK_REGISTRY='--block-registry=all'

您可能需要取消注解BLOCK_REGISTRY行。
在同一文件中,添加要允许访问的注册表:

ADD_REGISTRY='--add-registry=<registry1> --add-registry=<registry2>'
Allowing Access to Registries
ADD_REGISTRY='--add-registry=registry.access.redhat.com'

还有一个github项目:
https://github.com/flavio/kube-image-bouncer
你可以用它来列出注册表白名单。我认为注册表白名单已经在它里面实现了,你只需要在你要运行二进制文件的时候给它提供白名单。

b1uwtaje

b1uwtaje3#

如果你正在处理Azure管理的AKS集群,你可以使用Azure策略。这里是一个总结。我在我的博客文章中写了更详细的内容,可以在here中找到。
激活订阅上的Policy Insights资源提供程序

az provider register --namespace Microsoft.PolicyInsights

启用AKS Azure策略加载项

az aks enable-addons --addons azure-policy --name <cluster> --resource-group rg-demo

分配一个仅允许该使用情形的内置策略

# Define parameters for Azure Policy
$param = @{
    "effect" = "deny";
    "excludedNamespaces" = "kube-system", "gatekeeper-system", "azure-arc", "playground";
    "allowedContainerImagesRegex" = "myregistry\.azurecr\.io\/.+$";
}

# Set a name and display name for the assignment 
$name = 'restrict-container-registries'

# Retrieve the Azure Policy object 
$policy = Get-AzPolicyDefinition -Name 'febd0533-8e55-448f-b837-bd0e06f16469'

# Retrieve the resource group for scope assignment 
$scope = Get-AzResourceGroup -Name rg-demo 

# Assign the policy 
New-AzPolicyAssignment -DisplayName $name -name $name -Scope $scope.ResourceId -PolicyDefinition $policy -PolicyParameterObject $param

有几件事值得注意:

  • 安装附加组件,为您安装网关守护设备
  • 应用策略可能需要20分钟的时间
  • 我特意排除了名称空间playground,仅用于演示

相关问题