k8s如何生成一个完整的kubeconfig文件
k8s如何生成一个完整的kubeconfig文件
1. kubeconfig
1.1 简介
Kubernetes 使用YAML 文件存储
kubectl
的集群身份验证信息。kubeconfig
包含 kubectl 在运行命令时引用的上下文列表。默认情况下,该文件为 $HOME/.kube/config,也可以使用–kubeconfig参数指定kubeconfig文件
1.2 组成
一个完整的kubeconfig文件包含clusters、contexts、users、current-context以及一些元信息组成,一个kubeconfig文件可以定义多个cluster、多个context以及多个user,可以实现一个config管理多套k8s集群的目的
clusters
用来配置k8s的连接信息,可以有多个,每个cluster需要指定集群的api-server地址以及证书
contexts
用来配置k8s的连接信息,可以有多个,每个cluster需要指定集群的api-server地址以及证书
users
用来配置k8s的上下文信息,可以有多个,每个context需要指定属于哪个集群,以及使用哪个用户进行认证
current-context
指定当前kubectl使用的上下文,使用kubectl config usecontext context-name命令可以切换上下文
示例文件
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM2VENDQWRHZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQ0FYRFRJME1ERXdOREF5TlRreU5Gb1lEekl4TWpNeE1qRXhNREkxT1RJMFdqQVZNUk13RVFZRApWUVFERXdwcmRXSmxjbTVsZEdWek1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBCjV4ZzFLNllmN0laa0xraHNCdUx5SCs3M212WTF4SjZrUmxuMnErckpGeksyVE1zNmM2R1poZG1GR2V4TTFyNysKMndyOGRpcUlmRGxvYXBBczUvcWQxZGovS1loUGdWNVgvcW16RFpEcG1xaTNhMCsrRkdXUDNDNE5DdklvMkY2QgpnVlpMRWozdWh4dGVhSUhxNG51MEQxaUJkeFU1MXJnaG0rNElyZFdwWDZQL2U4Uyt3ZG5LeHBlRUVid2lBK1JUCnJFZXJJd1VlcS95RFg1a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UklQWVpycUJ6NnJwMlA1QzlyYXptTmFKbDV4YwpyMjZnZ2lGbkRuM0ZBMWw2SVRkQW5HTUNIYVVMNDVyWDhLbnkySU1iZlZSMmtmRlBjL0NpQkN6U3JGZWJqbldUClZCSjNJNHh4T2gvaldpdDlrT2piWXYySzVlK3l2Zk9TQ1hXWkNEZz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
server: https://xxx.xxx.xxx:6443
name: xxx.xxx.xxx
contexts:
- context:
cluster: xxx.xxx.xxx
user: admin2
name: xxx.xxx.xxx
- context:
cluster: xxx.xxx.xxx
user: admin
name: xxx.xxx.xxx
current-context: xxx.xxx.xxx
kind: Config
preferences: {}
users:
- name: admin
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN1akNDQWFJQ0NRRC92aXBtTUNUYmR6QU5CZ2txaGtpRzl3MEJBUXNGQURBVk1STXdFUVlEVlFRREV3cHIKZFdKbGNtNWxkR1Z6TUI0WERUSTBNRFF3T0RBek1EWTBOMW9YRFRJMU1EUXdPREF6TURZME4xb3dLVEVPTUF3RwpBMVVFQXd3RllXUnRhVzR4RnpBVkJnTlZCQW9NRG5ONWMzUmxiVHB0WVhOMFpYSnpNSUlCSWpBTkJna3Foa2lHCjl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF0NGp4blpSMEdjZW82K0x1ZFQ0NUlWSVlMODRiSCtCaTZudkkKY0xVNjg1ZjNhdWVGODcyd0NpWEpJQWZrRXBIZkFsRS9qdU1EaFNSY2Z4VHNqUlpNK1poMlJMclBUejdhdTROcApKaHo2NFdoeWV1T1RZWEIwb2hyemtaVU84RjRqTkFnakhNNDg1OG1OaGNTMW9lVjk3bFBZbWhRU3BQSmMyL3BQClNiaEJLWmFWU3RHdnU2NGtUaFdPQ3I5RU5IOVJTTHB4QlZja0EvaGV2WmNFVitmekd3SjhTN2ExUEpzazlyV08KaU40R2t5cnI3SmMwQlQ2OCt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UJ2YUpvaWVrdm9BeEt5eDlKOEExUWlYMUJhZ0JSU0VFRS9IRzkxV1IwTjNqRXVNVm1FeERncllhdDc2N0ZDTgpLNkhhYW9ETjN5OG5qQ0FMQ2NmVSt0czN6RWp5bjZnY1BZcjhydGdGaEF5QkRNbHg2MTZGMnc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=
一般来说kubelet会使用一个默认的kubeconfig文件,也及时/root/.kube/config
文件,如果是使用kubeadm
安装的集群,其实也就是复制的/etc/kubernetes/admin.conf
文件,那么如何自己生成一个kubeconfig文件呢??
要生成一个完整的kubeconfig文件,一般包括如下几个步骤
1. 生成私钥
使用openssh生成一个2048位的私钥
openssl genrsa -out user1.key 2048
2. 生成证书请求文件(csr)
通过私钥生成csr文件,后面通过csr文件想kubernetes请求证书
注意-subj
中的CN
相当于用户名,o
相当于用户组
在k8s中有两类用户,一类是由Kubernetes 管理的service account,另一类是普通用户,对于普通用户k8s是没有存储用户信息的,当像k8s请求资源时,会解析证书中的用户和用户组然后通过RCBA进行鉴权
参考: https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/rbac/#discovery-roles
openssl req -new -key user1.key -out user1.csr -subj "/CN=user1/O=group1"
3. 申请证书
通过csr文件向k8s请求证书
openssl x509 -req -in user1.csr \
-CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key -CAcreateserial \
-out user1.crt -days 365
4. 添加clusters
向kubeconfig文件中配置clusters信息,可配置多个
--server
:kube-apiserver
的地址-certificate-authority
: k8s的ca文件--embed-certs
: 将证书嵌入到kubeconfig文件中,方便客户端认证
kubectl --kubeconfig=kubeconfig.config config \
set-cluster demo1 --server=https://xxx.xxx.xxx:6443 \
--embed-certs --certificate-authority=/etc/kubernetes/pki/ca.crt
5. 添加user
向kubeconfig文件中配置用户信息,可配置多个
--client-key
:第一步的私钥文件-client-certificate
: 第三步的公钥--embed-certs
: 将证书嵌入到kubeconfig文件中,方便客户端认证
kubectl --kubeconfig=kubeconfig.config config \
set-credentials user1 \
--client-key=user1.key --embed-certs --client-certificate=user1.crt
6. 添加context
向kubeconfig文件中配置上下文信息,可配置多个
--user
:用户名--cluster
: 集群名
kubectl --kubeconfig=kubeconfig.config config \
set-context ctx-demo1 --user=user1 --cluster=demo1
7. 设置current-context
设置当前使用的k8s上下文
kubectl --kubeconfig=kubeconfig.config config use-context ctx-demo1
下面是一个自动生成kubeconfig的简单脚本
#!/bin/bash
work_dir=$(
cd $(dirname $0)
pwd
)
read -p "输入用户名: " username
read -p "输入用户组(默认system:masters): " group
if [ -z "$username" ];then
echo -e "\033[31m用户名不能为空\033[0m"
exit -1
fi
if [ -z "$group" ];then
group="system:masters"
fi
echo -e "\033[32m用户名:${username},用户组:${group}\033[0m"
root_path=${work_dir}/${username}
mkdir -p $root_path
private_key_path="${root_path}/${username}.key"
openssl genrsa -out ${private_key_path} 2048
echo -e "\033[32m私钥生成成功: ${private_key_path}\033[0m"
csr_path="${root_path}/${username}.csr"
openssl req -new -key ${private_key_path} -out $csr_path -subj "/CN=${username}/O=${group}" > /dev/null
echo -e "\033[32m证书请求文件生成成功: ${csr_path}\033[0m"
crt_path="${root_path}/${username}.crt"
openssl x509 -req -in ${csr_path} -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key -CAcreateserial -out ${crt_path} -days 365
echo -e "\033[32m证书生成成功: ${crt_path}\033[0m"
config_path="${root_path}/${username}.config"
master_ip=` kubectl get nodes -owide --no-headers | grep control-plane,master | awk -F " " '{print $6}'`
cluster_name="k8s-${master_ip}"
context_name="k8s-${master_ip}"
kubectl --kubeconfig=$config_path config set-cluster $cluster_name --server=https://${master_ip}:6443 --embed-certs --certificate-authority=/etc/kubernetes/pki/ca.crt
kubectl --kubeconfig=$config_path config set-credentials $username --client-key=$private_key_path --embed-certs --client-certificate=$crt_path
kubectl --kubeconfig=$config_path config set-context $context_name --user=$username --cluster=${cluster_name}
kubectl --kubeconfig=$config_path config use-context $context_name
echo -e "\033[32mconfig生成成功: ${config_path}\033[0m"
至此一个完整的kubeconfig文件就已经生成好了,但是如果只做上面的这些操作是没有任何意义的,因为在生成csr文件的时候我们使用的用户是user1,用户组是group1,如果在k8s集群中没有为user1或group1绑定角色,通过这个kubeconfig文件去访问k8s资源时会报如下错误:
这是没有为对应的用户组或者用户创建rolebind或者clusterrolebind绑定权限,这个问题如何解决,在后面的k8s RBAC中再介绍k8s RBAC权限控制
完结,撒花~~~~~
更多推荐
所有评论(0)