如何将用户添加到 Kubernetes(kubectl)?

How to Add Users to Kubernetes (kubectl)?

我已经使用 kops 在 AWS 上创建了一个 Kubernetes 集群,并且可以在我的本地计算机上通过 kubectl 成功管理它。

我可以使用 kubectl config view 查看当前配置,也可以直接访问 ~/.kube/config 的存储状态,例如:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: REDACTED
    server: https://api.{CLUSTER_NAME}
  name: {CLUSTER_NAME}
contexts:
- context:
    cluster: {CLUSTER_NAME}
    user: {CLUSTER_NAME}
  name: {CLUSTER_NAME}
current-context: {CLUSTER_NAME}
kind: Config
preferences: {}
users:
- name: {CLUSTER_NAME}
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED
    password: REDACTED
    username: admin
- name: {CLUSTER_NAME}-basic-auth
  user:
    password: REDACTED
    username: admin

我需要让其他用户也能进行管理。 user guide 描述了如何在另一台用户机器上定义这些,但没有描述如何在集群本身内实际创建用户的凭据。你是怎么做到的?

此外,仅共享 cluster.certificate-authority-data 安全吗?

你说:

I need to enable other users to also administer.

但是根据 documentation

Normal users are assumed to be managed by an outside, independent service. An admin distributing private keys, a user store like Keystone or Google Accounts, even a file with a list of usernames and passwords. In this regard, Kubernetes does not have objects which represent normal user accounts. Regular users cannot be added to a cluster through an API call.

您必须为此使用第三方工具。

== 编辑 ==

一种解决方案是在 kubeconfig file. From the documentation 中手动创建用户条目:

# create kubeconfig entry
$ kubectl config set-cluster $CLUSTER_NICK \
    --server=https://1.1.1.1 \
    --certificate-authority=/path/to/apiserver/ca_file \
    --embed-certs=true \
    # Or if tls not needed, replace --certificate-authority and --embed-certs with
    --insecure-skip-tls-verify=true \
    --kubeconfig=/path/to/standalone/.kube/config

# create user entry
$ kubectl config set-credentials $USER_NICK \
    # bearer token credentials, generated on kube master
    --token=$token \
    # use either username|password or token, not both
    --username=$username \
    --password=$password \
    --client-certificate=/path/to/crt_file \
    --client-key=/path/to/key_file \
    --embed-certs=true \
    --kubeconfig=/path/to/standalone/.kube/config

# create context entry
$ kubectl config set-context $CONTEXT_NAME \
    --cluster=$CLUSTER_NICK \
    --user=$USER_NICK \
    --kubeconfig=/path/to/standalone/.kube/config

有关身份验证的完整概述,请参阅 Authentication and Authorization

上的官方 Kubernetes 文档

对于用户,理想情况下您使用 Kubernetes (OpenID Connect) 的身份提供程序。

如果您在 GKE/ACS 上,您将与相应的身份和访问管理框架集成

如果你self-host kubernetes(使用kops时就是这种情况),你可以使用coreos/dex to integrate with LDAP / OAuth2 identity providers - a good reference is this detailed 2 part SSO for Kubernetes文章。

kops (1.10+) 现在有 built-in authentication support 如果您在 AWS 上,它可以简化与作为身份提供商的 AWS IAM 的集成。

对于 Dex,有一些开源的 cli 客户端如下:

如果您正在寻找一种快速简便(不是最安全且在漫长的 运行 中易于管理)的入门方式,您可能会滥用 serviceaccounts - 有 2 个选项用于专门控制访问的策略。 (见下文)

注意自 1.6 起强烈推荐基于角色的访问控制!此答案不包括 RBAC 设置

编辑:很棒,但已过时(2017-2018),Bitnami 在 User setup with RBAC 上的指南也可用。

启用服务帐户访问权限的步骤是(取决于您的集群配置是否包括 RBAC 或 ABAC 策略,这些帐户可能具有完全的管理员权限!):

编辑Here is a bash script to automate Service Account creation - see below steps

  1. 为用户创建服务帐户 Alice

    kubectl create sa alice
    
  2. 获取相关秘密

    secret=$(kubectl get sa alice -o json | jq -r .secrets[].name)
    
  3. 从秘密中获取 ca.crt(使用 OSX base64-D 标志进行解码)

    kubectl get secret $secret -o json | jq -r '.data["ca.crt"]' | base64 -D > ca.crt
    
  4. 从秘密中获取服务帐户令牌

    user_token=$(kubectl get secret $secret -o json | jq -r '.data["token"]' | base64 -D)
    
  5. 从您的 kubectl 配置(current-context、服务器..)获取信息

    # get current context
    c=$(kubectl config current-context)
    
    # get cluster name of context
    name=$(kubectl config get-contexts $c | awk '{print }' | tail -n 1)
    
    # get endpoint of current context 
    endpoint=$(kubectl config view -o jsonpath="{.clusters[?(@.name == \"$name\")].cluster.server}")
    
  6. 在新机器上,按照以下步骤操作(鉴于上面检索到的 ca.cert$endpoint 信息:

    1. 安装kubectl

       brew install kubectl
      
    2. 设置集群(运行在存放ca.crt的目录中)

       kubectl config set-cluster cluster-staging \
         --embed-certs=true \
         --server=$endpoint \
         --certificate-authority=./ca.crt
      
    3. 设置用户凭据

       kubectl config set-credentials alice-staging --token=$user_token
      
    4. 定义alice用户与staging集群的组合

       kubectl config set-context alice-staging \
         --cluster=cluster-staging \
         --user=alice-staging \
         --namespace=alice
      
    5. 将用户current-context切换为alice-staging

       kubectl config use-context alice-staging
      

使用策略控制用户访问(使用 ABAC), you need to create a policy 文件(例如):

{
  "apiVersion": "abac.authorization.kubernetes.io/v1beta1",
  "kind": "Policy",
  "spec": {
    "user": "system:serviceaccount:default:alice",
    "namespace": "default",
    "resource": "*",
    "readonly": true
  }
}

在每个主节点上配置此 policy.json 并将 --authorization-mode=ABAC --authorization-policy-file=/path/to/policy.json 标志添加到 API 服务器

这将允许 Alice(通过她的服务帐户)仅对默认命名空间中的所有资源拥有只读权限。

bitnami 指南对我有用,即使你使用 minikube。最重要的是你的集群支持 RBAC。 https://docs.bitnami.com/kubernetes/how-to/configure-rbac-in-your-kubernetes-cluster/