签发一个kubeconfig文件的详细流程
大致步骤:
- cfssl证书
- 定义集群
- 定义用户
- 定义上下文
- 绑定角色
cfssl证书
ca-config.json cfssl的配置文件
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
],
"expiry": "876000h"
}
}
}
}
user-csr.json 当前证书签发内容
{
"CN": "linjb",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "BeiJing",
"L": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes user-csr.json | cfssljson -bare user
[root@node1 practice]# ls -l user*
-rw-r--r-- 1 root root 993 10月 28 17:17 user.csr
-rw-r--r-- 1 root root 217 10月 28 16:59 user-csr.json
-rw------- 1 root root 1679 10月 28 17:17 user-key.pem
-rw-r--r-- 1 root root 1294 10月 28 17:17 user.pem
定义集群
kubectl config set-cluster linjb --server=https://127.0.0.1:8443 --certificate-authority=ca.pem --embed-certs=true --kubeconfig=linjb.kubeconfig
定义用户
kubectl config set-credentials linjb --client-certificate=user.pem --client-key=user-key.pem --embed-certs=true --kubeconfig=linjb.kubeconfig
定义上下文
kubectl config set-context linjb --cluster=linjb --user=linjb --kubeconfig=linjb.kubeconfig
使用上下文
kubectl config view --kubeconfig=linjb.kubeconfig
创建rolebinding
kubectl create rolebinding linjb-binding --clusterrole=admin --user=linjb
##user=linjb为user-csr.json文件中的CN
##clusterrole=admin为集群中已存在的角色
完整的kubeconfig文件
linjb.kubeconfig
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURvRENDQW9pZ0F3SUJBZ0lVR2ZMckUrRHJNZWZud20zejd1a3h1alFJOGIwd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1p6RUxNQWtHQTFVRUJoTUNRMDR4RURBT0JnTlZ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KS29aSWh2Y05BUUVMQlFBRApnZ0VCQUp1V092UE8xRUQybkY4YTRTWWFwaUFvbDJKVWNSN0RVR01pZ1hhYk9LcWVXV3ZTUWtLc2o3MnlRR1lTCjBFcEVwUlRTK1M5ZTRkS3UzQVN1cjhncU1SWTROMG1KelQvMFdJSzZZZlFXWU5pVDgwR2swakltbVJSWnZqRzkKYWVOT2J5R3hWSC9jTmdFaGZ5THJTZGQ1MkVHODNFNXRNU1RTekRmWUpWaFk4b2lmTGd0MFpXdU9CaXpQK3JNUApGdXQzeGhlWlR5M1RMMjJ2NGxYQkNmSmZ4bFJOVXZUWkNMalFKNFV2VUNpYlR0NGkzbUFtNzUyeG1MVjV1MzQ0ClJEV0EvZ1VFcG8zOVNmU2QxaWZJZVN4UUFyWkJxM3lzUDhPQStWZ1Nic3VDemVrTEsxMkNIeWx5c3pzZSs5S0MKc2s3V1FETjNVVmF4eW96V3JXVlpoVEZFdlFVPQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
server: https://127.0.0.1:8443
name: linjb
contexts:
- context:
cluster: linjb
user: linjb
name: linjb
current-context: linjb
kind: Config
preferences: {}
users:
- name: linjb
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQxakNDQXI2Z0F3SUJBZ0lVWmE2YzdnTW11Z2lGR2sxTC95VElBaVlUcDFRd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1p6RUxNQWtHQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdUQjBKbGFVcHBibWN4RURBT0JnTlZCQWNUQjBKbAphVXBwYm1jeEREQUtCZ05WQkFvVEEyczRjekVPTUF3R0ExVUVDeE1GWVcxeWIyMHhGakFVQmdOVkJBTVREV3QxClltVnlibVYwWlhNdFkyRXdJQmNOTWpFeE1ESTVNREkxTVRBd1doZ1BNakV5TVRFd01EVXdNalV4TURCYU1HQXgKQ3pBSkJnTlZCQVlUQWtOT01SQXdEZ1lEVlFRSUV3ZENa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