如何使用RBAC API在Kubernetes中创建限制为命名空间的用户/组?

时间:2016-12-24 00:59:43

标签: kubernetes client-certificates rbac kubectl

问题

我想在开发组中向许多不同的开发人员(不同的主题)颁发证书,并让他们都有权在dev命名空间内创建和修改内容,但不要触摸它之外的任何东西,并且绝对不会在它之外看到秘密。我怀疑我在下面的第2步中创建的角色,角色绑定等是不正确的,有人可以建议更正吗?

尝试

  1. 使用API​​服务器标记部署Kubernetes以支持" RBAC,AlwaysAllow"授权模式,设置RBAC超级用户,并通过--runtime-config启用RBAC API。
  2. 创建了命名空间,角色和角色绑定,其意图是:(a)服务帐户和系统组件可以有效地仍然具有" AlwaysAllow"访问,以及(b)组dev中的任何实体都可以使用this YAML file访问名称空间dev中的任何内容。 注意:此链接的内容已更改,请参阅我在问题底部工作的YAML文件。
  3. 更新了Kubernetes,只允许" RBAC"授权模式。
  4. 生成的客户端TLS数据,其中证书主题标记(对于openssl)为-subj "/CN=example-dev@kubernetes.click/O=dev"
  5. this template之后生成了一个kubeconfig文件。
  6. 实际结果

    运行时遇到以下错误:kubectl -v 8 --kubeconfig=/tmp/dev-kube-config.yml create -f /tmp/busybox.yml

    I1219 16:12:37.584657   44323 loader.go:354] Config loaded from file /tmp/dev-kube-config.yml
    I1219 16:12:37.585953   44323 round_trippers.go:296] GET https://api.kubernetes.click/api
    I1219 16:12:37.585968   44323 round_trippers.go:303] Request Headers:
    I1219 16:12:37.585983   44323 round_trippers.go:306]     Accept: application/json, */*
    I1219 16:12:37.585991   44323 round_trippers.go:306]     User-Agent: kubectl/v1.5.1+82450d0 (    darwin/amd64) kubernetes/82450d0
    I1219 16:12:38.148994   44323 round_trippers.go:321] Response Status: 403 Forbidden in 562     milliseconds
    I1219 16:12:38.149056   44323 round_trippers.go:324] Response Headers:
    I1219 16:12:38.149070   44323 round_trippers.go:327]     Content-Type: text/plain; charset=utf-    8
    I1219 16:12:38.149081   44323 round_trippers.go:327]     Content-Length: 17
    I1219 16:12:38.149091   44323 round_trippers.go:327]     Date: Tue, 20 Dec 2016 00:12:38 GMT
    I1219 16:12:38.149190   44323 request.go:904] Response Body: Forbidden: "/api"
    I1219 16:12:38.149249   44323 request.go:995] Response Body: "Forbidden: \"/api\""
    I1219 16:12:38.149567   44323 request.go:1151] body was not decodable (unable to check for     Status): Object 'Kind' is missing in 'Forbidden: "/api"'
    ...
    I1219 16:12:38.820672   44323 round_trippers.go:296] GET https://api.kubernetes.    click/swaggerapi/api/v1
    I1219 16:12:38.820702   44323 round_trippers.go:303] Request Headers:
    I1219 16:12:38.820717   44323 round_trippers.go:306]     User-Agent: kubectl/v1.5.1+82450d0 (    darwin/amd64) kubernetes/82450d0
    I1219 16:12:38.820731   44323 round_trippers.go:306]     Accept: application/json, */*
    I1219 16:12:38.902256   44323 round_trippers.go:321] Response Status: 403 Forbidden in 81     milliseconds
    I1219 16:12:38.902306   44323 round_trippers.go:324] Response Headers:
    I1219 16:12:38.902327   44323 round_trippers.go:327]     Content-Type: text/plain; charset=utf-    8
    I1219 16:12:38.902345   44323 round_trippers.go:327]     Content-Length: 31
    I1219 16:12:38.902363   44323 round_trippers.go:327]     Date: Tue, 20 Dec 2016 00:12:38 GMT
    I1219 16:12:38.902456   44323 request.go:904] Response Body: Forbidden: "/swaggerapi/api/v1"
    I1219 16:12:38.902512   44323 request.go:995] Response Body: "Forbidden:     \"/swaggerapi/api/v1\""
    F1219 16:12:38.903025   44323 helpers.go:116] error: error validating "/tmp/busybox.yml": error validating data: the server does not allow access to the requested resource; if you choose to ignore these errors, turn validation off with --validate=false
    

    预期结果

    预计会在dev命名空间中创建busybox pod。

    其他详细信息:

    • $ kubectl version

      Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.1+82450d0", GitCommit:"82450d03cb057bab0950214ef122b67c83fb11df", GitTreeState:"not a git tree", BuildDate:"2016-12-14T04:09:31Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"darwin/amd64"}
      Server Version: version.Info{Major:"1", Minor:"4", GitVersion:"v1.4.6", GitCommit:"e569a27d02001e343cb68086bc06d47804f62af6", GitTreeState:"clean", BuildDate:"2016-11-12T05:16:27Z", GoVersion:"go1.6.3", Compiler:"gc", Platform:"linux/amd64"}
      
    • GitHub问题:https://github.com/kubernetes/kubernetes/issues/38997

    • 邮件列表帖子:https://groups.google.com/forum/#!topic/kubernetes-dev/6TBTu1AC2L8

    编辑:基于答案评论的工作解决方案

    根据Jordan的回答,我升级到Kubernetes v1.5.1,然后得到以下两个YAML文件来构建命名空间和所有正确的RBAC资源,以便一切按预期工作:

    system-access.yml(因为开箱即用的群集角色和群集角色绑定似乎不起作用):

    kind: ClusterRoleBinding
    apiVersion: rbac.authorization.k8s.io/v1alpha1
    metadata:
      name: system:node--kubelet
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: system:node
    subjects:
    - kind: User
      name: kubelet
    ---
    kind: ClusterRoleBinding
    apiVersion: rbac.authorization.k8s.io/v1alpha1
    metadata:
      name: cluster-admin--kube-system:default
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: cluster-admin
    subjects:
    - kind: ServiceAccount
      name: default
      namespace: kube-system
    ---
    kind: ClusterRoleBinding
    apiVersion: rbac.authorization.k8s.io/v1alpha1
    metadata:
      name: system:node-proxier--kube-proxy
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: system:node-proxier
    subjects:
    - kind: User
      name: kube-proxy
    

    dev-access.yml

    kind: Namespace
    apiVersion: v1
    metadata:
      name: dev
    ---
    kind: Role
    apiVersion: rbac.authorization.k8s.io/v1alpha1
    metadata:
      namespace: dev
      name: dev-all
    rules:
      - apiGroups: ["*"]
        resources: ["*"]
        verbs: ["*"]
    ---
    kind: RoleBinding
    apiVersion: rbac.authorization.k8s.io/v1alpha1
    metadata:
      name: dev-role-dev-all-members
      namespace: dev
    subjects:
      - kind: Group
        name: dev
      - kind: Group
        name: system:serviceaccounts:dev
    roleRef:
      kind: Role
      name: dev-all
      apiGroup: "rbac.authorization.k8s.io"
    

1 个答案:

答案 0 :(得分:4)

首先,您需要允许访问kubectl用于API发现和验证的URL(swagger,API组列表和资源类型等)。

最简单的方法是加载默认引导程序cluster rolescluster role bindings

kubectl create -f https://raw.githubusercontent.com/kubernetes/kubernetes/master/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/testdata/cluster-roles.yaml
kubectl create -f https://raw.githubusercontent.com/kubernetes/kubernetes/master/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/testdata/cluster-role-bindings.yaml

这将创建一个system:discovery ClusterRole并将所有用户(经过身份验证和未经身份验证的)绑定到它,允许他们访问swagger和AP​​I组信息。

其次,您不应在all群集角色绑定中包含开发服务帐户。这将允许该服务帐户(以及访问包含dev服务帐户凭据的dev命名空间中的机密的任何人)群集范围访问