共计 6347 个字符,预计需要花费 16 分钟才能阅读完成。
随着 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 集群的的生产力。
参考
- https://kubernetes.io/docs/ta…
- https://krew.sigs.k8s.io/docs…
同时公布在 Mengz’s Blog