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

时间:2017-02-10 23:31:55

标签: kubernetes kubectl kops

我已使用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

是否安全

3 个答案:

答案 0 :(得分:81)

有关身份验证的完整概述,请参阅AuthenticationAuthorization上的官方Kubernetes文档

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

如果您使用的是GKE / ACS,则需要与各自的身份和访问管理框架集成

如果您自行托管kubernetes(使用kops时就是这种情况),您可以使用coreos/dex与LDAP / OAuth2身份提供程序集成 - 这个详细的2部分SSO for Kubernetes是一个很好的参考制品

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

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

如果您正在寻找快速简便(从长远来看不是最安全且易于管理)的方式入门,您可以滥用serviceaccounts - 使用2个专用策略选项来控制访问。 (见下文)

注意,强烈建议使用1.6基于角色的访问控制!此答案不包括RBAC设置

编辑: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获取秘密(使用带有base64标志的OSX -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配置中获取信息(当前上下文,服务器..)

    # get current context
    c=`kubectl config current-context`
    
    # get cluster name of context
    name=`kubectl config get-contexts $c | awk '{print $3}' | 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用户与登台集群的组合

      kubectl config set-context alice-staging \
        --cluster=cluster-staging \
        --user=alice-staging \
        --namespace=alice
      
    5. 将当前上下文切换为用户的alice-staging

      kubectl config use-context alice-staging
      
  7. 要使用策略控制用户访问(使用ABAC),您需要创建policy文件(例如):

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

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

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

答案 1 :(得分:1)

你说:

  

我需要让其他用户也能管理。

但根据documentation

  

假设普通用户由外部独立服务管理。管理员分发私钥,像Keystone或Google帐户这样的用户商店,甚至包含用户名和密码列表的文件。在这方面,Kubernetes没有代表普通用户帐户的对象。无法通过API调用将常规用户添加到群集中。

您必须使用第三方工具。

==编辑==

一种解决方案可能是在kubeconfig file中手动创建用户条目。来自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

答案 2 :(得分:0)

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