隨著 Kubernetes 成為主流的應用容器編排平臺,其命令列客戶端 kubectl
也成為了我們日常部署應用,維護叢集最常用的工具。 kubectl
自身提供了強大的內建自命令來滿足我們對叢集的操作,例如 get
獲取叢集內的資源物件,proxy
建立代理之類的,除了內建的這些自命令,kubectl
還提供了可擴充套件的能力,允許我們安裝自己編寫或者社群提供的外掛來增強我們使用 kubectl
的生產力。
這裡將給大家介紹如何在安裝 kubectl
擴充套件外掛,以及幾款我在日常工作中常用到的社群提供的外掛。
在安裝和使用 kubectl
外掛的之前,請確保以及安裝和配置好 kubectl
命令列工具和 git
工具。
krew
首先介紹的第一款擴充套件外掛就是 krew - k8s特別興趣小組開發的一款用於安裝和管理 kubectl
擴充套件外掛的外掛。
程式碼: https://github.com/kubernetes...
安裝 krew (在macOS/Linux上):
在終端執行(Bash或者Zsh)執行
( set -x; cd "$(mktemp -d)" && OS="$(uname | tr '[:upper:]' '[:lower:]')" && ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" && KREW="krew-${OS}_${ARCH}" && curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" && tar zxvf "${KREW}.tar.gz" && ./"${KREW}" install krew )
將 $HOME/.krew/bin 加入到 PATH 環境變數,更新你的 .bashrc 或者 .zshrc 檔案,新增下面一行
export PATH="${KREW_ROOT:-$HOME/.brew}/bin:$PATH"
然後重啟你的終端。
測試 krew 已經安裝成功
❯ k krew version OPTION VALUE GitTag v0.4.3 GitCommit dbfefa5 IndexURI https://github.com/kubernetes-sigs/krew-index.git BasePath /home/mengz/.krew IndexPath /home/mengz/.krew/index/default InstallPath /home/mengz/.krew/store BinPath /home/mengz/.krew/bin DetectedPlatform linux/amd64
(可選)設定 krew 別名
alias krew='kubectl-krew' echo "alias krew='kubectl-krew'" >> ~/.alias
這樣就安裝完成了,krew 是 kubectl
外掛管理器,而 krew
自己又是外掛,所以之後可以使用 krew
來更新 krew
。
在 Windows 上安裝,請參考 https://krew.sigs.k8s.io/docs... 。
使用示例
列出當前已安裝的外掛
❯ krew list PLUGIN VERSION krew v0.4.3
安裝外掛(下面介紹的其他外掛將使用這個方式安裝)
❯ krew install [外掛名]
更新本地外掛索引(檢視是否有外掛更新)
❯ krew update Updated the local copy of plugin index. New plugins available: * liqo * switch-config Upgrades available for installed plugins: * open-svc v2.5.2 -> v2.5.3 * rbac-tool v1.7.1 -> v1.8.0 * rolesum v1.5.1 -> v1.5.5
升級外掛
❯ krew upgrade open-svc Updated the local copy of plugin index. Upgrading plugin: open-svc
刪除外掛
❯ krew uninstall [外掛名]
krew-index 裡維護了上百款可通過 krew 直接安裝的外掛。接下來將介紹其他幾款我日常使用的 kubectl
外掛。
example
程式碼: https://github.com/seredot/ku...
安裝 krew install example
example
外掛可用於快速生成k8s資源物件的yaml檔案示例,例如
❯ k example deploy
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
當我們想臨時通過資源宣告檔案不熟應用的時候,可以使用該外掛來生成模板。
fleet
程式碼: https://github.com/kubectl-pl...
安裝: krew install fleet
fleet
外掛可快速檢視當前配置的叢集的概覽
❯ k fleet
CLUSTER VERSION NODES NAMESPACES PROVIDER API
homek8sc1 v1.23.8 3/3 4 ? https://k8sc1.mengz.lan:6443
api-sandbox-x8i5-p1-openshiftapps-com:6443 v1.23.5+9ce5071 ? ? ? https://api.sandbox.x8i5.p1.openshiftapps.com:6443
minikube v1.22.3 1/1 4 minikube https://192.168.64.3:8443
get-all
程式碼: https://github.com/corneliusw...
安裝: krew install get-all
get-all
外掛獲取叢集(或者某一個名字空間)的所有資源物件
❯ k get-all -n kube-system
NAME NAMESPACE AGE
configmap/calico-config kube-system 13d
configmap/coredns kube-system 13d
configmap/extension-apiserver-authentication kube-system 13d
configmap/kube-proxy kube-system 13d
configmap/kube-root-ca.crt kube-system 13d
configmap/kubeadm-config kube-system 13d
configmap/kubelet-config-1.23 kube-system 13d
endpoints/kube-dns kube-system 13d
...
htpasswd
程式碼: https://github.com/shibumi/ku...
安裝: krew install htpasswd
htpasswd
是 nginx-ingress 相容的基礎認證密碼生成器
❯ k htpasswd create aaa-basic-auth user1=user1password user2=user2password -o yaml --dry-run
apiVersion: v1
data:
auth: dXNlcjE6JDJhJDEwJDVNeEJGT3lEUEJYT0xkUldlblNWME91RGtZTzFQOElJNXJuRnh5blpUdC55L2FUUUNDYzJ1CnVzZXIyOiQyYSQxMCRVbFdHOG5NTU4zRGVpOC5GMmVRM3EuYWhxTENYZGtLYUJ1cXZzT3lEOGl0ODJRdU4zV1c1dQ==
kind: Secret
metadata:
creationTimestamp: null
name: aaa-basic-auth
namespace: default
type: Opaque
該外掛在為服務建立基礎認證的 Ingress 很方便。
images
程式碼: https://github.com/chenjiando...
安裝: krew install images
images
現實名字空間中使用的容器映象資訊
❯ k images
[Summary]: 1 namespaces, 2 pods, 2 containers and 1 different images
+------------------------+---------------+-------------------+
| PodName | ContainerName | ContainerImage |
+------------------------+---------------+-------------------+
| webapp-98f7444c5-8772w | nginx | nginx:1.21-alpine |
+------------------------+ + +
| webapp-98f7444c5-vsxr9 | | |
+------------------------+---------------+-------------------+
ktop
程式碼: https://github.com/vladimirvi...
安裝: krew install ktop
ktop
外掛是以類似 Linux Top 工具的方式來外掛k8s叢集的負載情況
❯ k ktop
┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
│ API server: https://k8sc1.mengz.lan:6443 Version: v1.23.8 context: admin@homek8sc1 User: kubernetes-admin namespace: (all) metrics: not connected v0.3.0
└ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
╔ ? Cluster Summary ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═
║ ptime: 13d Nodes: 3 Namespaces: 4 Pods: 15/15 (20 imgs) Deployments: 5/5 Sets: replicas 5, daemons 6, stateful 0 Jobs: 0 (cron: 0) PVs: 0 (0Gi) PVCs: 0 (0Gi)
║ PU: [||||||||||| ] 1600m/6000m (26.7% requested) Memory: [|| ] 1Gi/11Gi (2.5% requested)
╚ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═
┌ ? Nodes (3) ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
│ NAME STATUS AGE VERSION INT/EXT IPs OS/ARC PODS/IMGs DISK CPU MEM
│ homek8sc1-control Ready 13d v1.23.8 192.168.0.140/<none> Ubuntu 20.04.4 LTS/amd64 9/10 16Gi [|||||| ] 1100m/2000m (55%) [|| ] 1Gi/2Gi (13%)
│ homek8sc1-worker1 Ready 13d v1.23.8 192.168.0.141/<none> Ubuntu 20.04.4 LTS/amd64 3/5 16Gi [|| ] 250m/2000m (12%) [ ] 0Gi/5Gi (0%)
│ homek8sc1-worker2 Ready 13d v1.23.8 192.168.0.142/<none> Ubuntu 20.04.4 LTS/amd64 3/5 16Gi [|| ] 250m/2000m (12%) [ ] 0Gi/5Gi (0%)
│
└ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
┌ ? Pods (15) ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ AMESPACE POD READY STATUS RESTARTS AGE VOLS IP NODE CPU MEMORY
│ ube-system kube-controller-manager-homek8sc1-control 1/1 Running 1 13d 8/8 192.168.0.140 homek8sc1-control [| ] 200m 10.0% [ ] 0Gi 0.0%
│ ube-system kube-proxy-4c9nq 1/1 Running 2 13d 4/4 192.168.0.141 homek8sc1-worker1 [ ] 0m 0.0% [ ] 0Gi 0.0%
│ ube-system kube-proxy-4whcn 1/1 Running 1 13d 4/4 192.168.0.140 homek8sc1-control [ ] 0m 0.0% [ ] 0Gi 0.0%
│ ube-system kube-proxy-bz8lt 1/1 Running 3 13d 4/4 192.168.0.142 homek8sc1-worker2 [ ] 0m 0.0% [ ] 0Gi 0.0%
│ ube-system kube-scheduler-homek8sc1-control 1/1 Running 1 13d 1/1 192.168.0.140 homek8sc1-control [| ] 100m 5.0% [ ] 0Gi 0.0%
└ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
總結
krew-index 裡包含了大量功能強大的擴充套件外掛,涵蓋了 RBAC 管理,資源管理等功能,這裡沒法意義列出。不過有了 krew
,我們可以很方便的安裝需要的擴充套件,以在日常工作中提高我們管理k8s叢集的的生產力。
參考
同時釋出在 Mengz's Blog