使用reset api 存取 Kubernetes

 ·  ☕ 3 


圖片來源

Kubernetes Cluster 中 API Server 是管理整個叢集的大腦, API-Server 一般執行在 Controller Plane (Master Node)上,維運人員通常會透過 CLI kubectl 工具去管理 Kubernetes。

但還有許多方式可以存取 Kubernetes API Server ,今天會建立一個特殊場景。有一個名為 Jean 的使用者,透過 Rest API 的方式存取 Kubernetes 的資源

向 Kubernetes API Server 發起請求通常需要有一個有權限的 ServiceAccount ,使用這個 ServiceAccount 通過 ClusterRole 、 Role 、 ClusterRoleBinding 、 RoleBinding 賦予操作相關資源的權限, 與 API Server 的請求基本上是基於 TLS ,所以 User 發起請求的時候還需要自帶憑證,當然我們也可以透過非安全方式存取 API Server , 但是不推薦在環境中使用,這邊就不再多做說明。

create user

create private key

為了簡單起見,本篇將 X.509 client certificates 與 OpenSSL 結合使用建立User:

  • Create a private key for jean:
1
openssl genrsa -out jean.key 2048

create CSR

建立一個證書籤名請求(CSR)

Without Group

1
2
3
openssl req -new -key jean.key \
-out jean.csr \
-subj "/CN=jean"

singup CSR

與 Kubernetes CA 簽署 CSR , Kubernetes CA 通常在 /etc/kubernetes/pki/ 間單的授權100天。

1
2
3
4
5
openssl x509 -req -in jean.csr \
-CA /etc/kubernetes/pki/ca.crt \
-CAkey /etc/kubernetes/pki/ca.key \
-CAcreateserial \
-out jean.crt -days 100

檢查一下當前資料夾的檔案,應該會有三個檔案分別是 jean.crtjean.csrjean.key

1
2
ls
jean.crt  jean.csr  jean.key

做完以上的步驟就簡單的建立了一個名為 Jean 的 user ,我們後續可以使用 rest api 或是 CLI kubectl 去存取 Kubernetes 資源。

access kubernetes api server

check API Server IP

我們需要先確認 Master 在哪裡,這邊有兩種方式都可以存取到 Kubernetes Master Node 的IP。

  1. master NIC IP
1
2
3
kubectl get node jason-test -o go-template --template='{{ (index .status.addresses 0).address }}'

172.18.0.5
  1. API Server Service IP
1
2
3
kubectl get svc kubernetes -o go-template --template='{{.spec.clusterIP}}'

10.96.0.1

第一種就是直接確認 master Node 的 IP ,第二種是可以透過 Kubernetes Service 上的 VIP 。

access pods list

例如我們可以透過 curl 指令直接存取 Master Node IP 上的 API Server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
curl https://172.18.0.5:6443/api/v1/namespaces/default/pods/ --cacert /etc/kubernetes/pki/ca.crt --cert ./jean.crt --key ./jean.key
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "pods is forbidden: User \"jean\" cannot list resource \"pods\" in API group \"\" in the namespace \"default\"",
  "reason": "Forbidden",
  "details": {
    "kind": "pods"
  },
  "code": 403
}

但這裡會發現無法存取 Pod resource in "" group 這邊是因為沒有針對 jean 去設定 RBAC 的存取範圍。

  • 新增一個讀取 default namespace 的 role
1
2
3
4
5
6
7
8
9
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: pod-reader
rules:
- apiGroups: [""] # "" indicates the core API group
  resources: ["pods"]
  verbs: ["get", "watch", "list"]
  • 幫 Jean 綁定有能力讀取 Pod 的角色
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apiVersion: rbac.authorization.k8s.io/v1
# This role binding allows "jean" to read pods in the "default" namespace.
# You need to already have a Role named "pod-reader" in that namespace.
kind: RoleBinding
metadata:
  name: read-pods
  namespace: default
subjects:
# You can specify more than one "subject"
- kind: User
  name: jane # "name" is case sensitive
  apiGroup: rbac.authorization.k8s.io
roleRef:
  # "roleRef" specifies the binding to a Role / ClusterRole
  kind: Role #this must be Role or ClusterRole
  name: pod-reader # this must match the name of the Role or ClusterRole you wish to bind to
  apiGroup: rbac.authorization.k8s.io

完成以上綁定的動作之後 Jean 就有了讀取 default namespaces 的權限了

reaccess kubernetes api server

一樣透過 curl 指令存取 Kubernetes api server ,最後透過 jq 幫忙整理輸出的內容。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
curl -s https://172.18.0.5:6443/api/v1/namespaces/default/pods/ --cacert /etc/kubernetes/pki/ca.crt --cert ./jean.crt --key ./jean.key | jq -r '.items[].metadata.name'

greetings-run-1598345882182-pod-c7vbz
h2c-client
h2c-server-78bbb8f665-qz7kw
hello-run-1598334732648-pod-ckvgk
hello-run-1598342326047-pod-5hbj7
hello-run-1598342412495-pod-kbcsp
hello-run-1598342606769-pod-xj6wd
hello-run-1598344900394-pod-nc95n
print-date-run-1598433322555-pod-m9lqf

小結

其實大多數的方法 例如透過 client-go 撰寫程式去存取 Kubernetes 資源又或是透過 CLI kubectl 去存取 Kubernetes 狀態,底層都是透過 Reset api ,學習怎麼使用 Reset api 去讀取資料也是一個很有用的方法

像是 python client 如果沒有更新的話,我們可以使用 reset api 的方式取得當前 Kubernetes 資源近一步去修改。


Meng Ze Li
Meng Ze Li
Kubernetes / DevOps / Backend