【问题标题】:How can I allow port-forwarding for a specific deployment in Kubernetes?如何为 Kubernetes 中的特定部署允许端口转发?
【发布时间】:2019-11-02 09:11:15
【问题描述】:

我正在尝试允许我组织中的一些用户将端口转发到我们在 Kubernetes 中的生产命名空间。但是,我不希望他们能够将端口转发到所有服务。我想限制对某些服务的访问。这可能吗?

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: allow-port-forward-for-deployment-a
rules:
- apiGroups: [""]
  resources: ["pods/portforward"]
  verbs: ["get", "list", "create"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: allow-port-forward-for-deployment-a
  namespace: production
subjects:
- kind: User
  name: "xyz@org.com"
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: allow-port-forward-for-deployment-a
  apiGroup: rbac.authorization.k8s.io

上述设置允许所有服务,但我不希望这样。

【问题讨论】:

    标签: kubernetes user-roles rbac


    【解决方案1】:

    我相信你做不到。 According to the docs

    对于某些请求,资源也可以通过名称来引用 resourceNames 列表。指定后,请求可以限制为 资源的单个实例。将主题限制为仅 “获取”和“更新”单个 configmap,你可以这样写:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      namespace: default
      name: configmap-updater
    rules:
    - apiGroups: [""]
      resources: ["configmaps"]
      resourceNames: ["my-configmap"]
      verbs: ["update", "get"]
    

    注意创建请求 不能受资源名称限制,因为对象名称未知 在授权时。另一个例外是 deletecollection。

    由于您想授予用户创建转发端口的权限,我认为您不能。

    【讨论】:

      【解决方案2】:

      假设用户已经可以访问您的kubernetes 集群和相关的namespace。他们可以简单地将本地端口端口转发到pod(资源)端口。

      你怎么能做到这一点? kubectl port-forward <POD_NAME> <LOCAL_PORT>:<POD_PORT>

      See Documentation

      引用文档 - kubectl port-forward 允许使用资源名称(例如 pod 名称)来选择匹配的 podport forward 到自 Kubernetes v1.10

      Refer this article if you wish, this nicely explains when you would need RBAC vs kubectl port-forward RBAC 可能仅在您希望 persongroup of people 仅用于 port-forward 以用于 kubernetes 集群中相关 namespace 中的任何服务时才有用。

      【讨论】:

        猜你喜欢
        • 2019-03-27
        • 2019-01-21
        • 1970-01-01
        • 2020-06-24
        • 2020-11-04
        • 2018-12-22
        • 1970-01-01
        • 1970-01-01
        • 2017-09-23
        相关资源
        最近更新 更多