維護人 | 日期 | 內容 |
---|---|---|
yanke679 | 2019-4-23 | 第一版 |
k8s 中有2種用戶,通常用戶
和serviceAccount
。node
pod
訪問apiserver
。每一個pod
都有一個 default serviceAccount。一個完整的訪問k8s api流程shell
+-----------------------------------------------------------------------------------------------------------+
| |
| +---------------------------------------------------------------------------+ +--------+ |
| | | | | |
| +--------+ | +------------------+ +----------------+ +--------------+ +------+ | | | |
| | | | | | | | | Admission | | | | | | |
| | Client +------> | Authentication +-> | Authorization +-> | Control +-> |Logic | +--> | Others | |
| | | | | | | | | | | | | | | |
| +--------+ | +------------------+ +----------------+ +--------------+ +------+ | | | |
| | | | | |
| | | | | |
| | Kube-apiserver | | | |
| +---------------------------------------------------------------------------+ +--------+ |
| |
+-----------------------------------------------------------------------------------------------------------+
複製代碼
如下操做以 root 帳號在node
上執行。json
證書是k8s中用於認證(Authentication)的具體機制。api
# 若是有配置環境變量,kubectl會讀取變量定義的config文件
$ env | grep -i KUBECONFIG
# kubectl 默認 config 文件
$ ls -lh ~/.kube/config
-rw------- 1 root root 6.2K Apr 8 2018 /root/.kube/config
$
# 查看 kubectl 配置的上下文(context)
# 注意看 user name,client-certificate-data。
# 留意 cluster 下的 server,以及 context 下的 namespace,等下生產證書是須要用到。
$ kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: REDACTED
server: https://10.25.65.209:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
namespace: szd-dev-f0109307
user: kubectl
name: kubernetes
current-context: kubernetes
kind: Config
preferences: {}
users:
- name: kubectl
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
複製代碼
# 從 config 文件截取 client-certificate-data,放到一個文件中。
cat ~/.kube/config --> 找到 當前 user 對應的 client-certificate-data。
# 此處保存到以下文件:
$ pwd
/root/szd-dev-f0109307_kubectl
$ ls
kubectl_client-certificate-data.txt
$ cat kubectl_client-certificate-data.txt
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQxRENDQXJ5Z0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clM1WWk0ZTZiVTJ3QjJUV20Kcnd2NXQrNGd6WkNZcHJMaThzcDA1R2xHdWdOVjFpRGg4aDZNTVg2ZnNkMjZqRjB5ekdxQzhidDRCV3AzR2Uxagp2S012RlFXYTlqQzNpV3dOSGdCZGNueVhyNitNYkRsegotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
$
複製代碼
# 解碼查看客戶端證書狀態,能夠看到 Not After : Apr 8 09:30:00 2019 GMT,已通過期。
# 注意 subject 中 CN 對應 k8s 中 role/clusterrole 的 user name;O 對應 group name。
$ base64 -d ./kubectl_client-certificate-data.txt | openssl x509 -text -noout
Certificate:
Data:
......
Signature Algorithm: sha256WithRSAEncryption
Issuer: C=CN, ST=BeiJing, L=BeiJing, O=k8s, OU=System, CN=kubernetes
Validity
Not Before: Apr 8 09:30:00 2018 GMT
Not After : Apr 8 09:30:00 2019 GMT
Subject: C=CN, ST=BeiJing, L=BeiJing, O=k8s, OU=System, CN=kubectl
Subject Public Key Info:
......
$
複製代碼
1. 生成客戶端ssl私鑰
2. 使用私鑰生成證書請求
3. 經過管理員給予的 ca 證書、ca key給客戶端簽名,生產客戶端訪問用的證書,簽名方式爲 x509。
`/etc/kubernetes/ssl`下的文件不要隨意更改,惟一一個能改的是`ca-config.json`中的`expiry`,8760h 爲 365 天。
複製代碼
$ cat ca-config.json
{
"signing": {
"default": {
"expiry": "8760h"
},
"profiles": {
"kubernetes": {
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
],
"expiry": "8760h"
}
}
}
複製代碼
# 從剛纔 kubectl config view 獲取到當前集羣的 server,即 master_vip,以及 namespace。
MASTER_VIP='10.25.65.209'
NAMESPACE='szd-dev-f0109307'
# 生成客戶端證書請求以及配置證書的步驟封裝在如下shell腳本中,給它傳參數執行便可。
cd /opt/k8s/node/scripts && sh ./k8s-node-rbac ${MASTER_VIP} ${NAMESPACE}
複製代碼
參照 `2. 查看 kubectl 客戶端證書狀態`。
能夠看到證書狀態已經更新。
複製代碼
$ base64 -d kubectl_client-certificate-data.txt | openssl x509 -text -noout
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
......
Signature Algorithm: sha256WithRSAEncryption
Issuer: C=CN, ST=BeiJing, L=BeiJing, O=k8s, OU=System, CN=kubernetes
Validity
Not Before: Apr 23 04:55:00 2019 GMT
Not After : Apr 22 04:55:00 2020 GMT
Subject: C=CN, ST=BeiJing, L=BeiJing, O=k8s, OU=System, CN=kubectl
複製代碼