第23关 Kubectl插件
大约 4 分钟
让K8s管理变得轻松! Kubectl插件助你管理K8s如虎添翼
创建插件
使用以下示例来帮助您熟悉编写和使用kubectl插件:
# 用任何你熟悉的开发语言,创建一个可执行的文件,可以是二进制文件,也可以是脚本
# 注意可执行文件的名称前面要以 "kubectl-" 开头,例如:"kubectl-hello"
cat ./kubectl-hello
#!/bin/bash
echo "hello world"
# 给文件添加可执行权限
chmod +x ./kubectl-hello
# 移动文件到系统默认的可执行目录 PATH
sudo mv ./kubectl-hello /usr/local/bin
# 这样我们就安装好了一个 kubectl 插件.
# 查看可用的所有插件`kubectl`,我们可以使用`kubectl plugin list`子命令:
# kubectl plugin list
Unable to read directory "/snap/bin" from your PATH: open /snap/bin: no such file or directory. Skipping...
The following compatible plugins are available:
/usr/local/bin/kubectl-hello
# 执行相应的插件
# kubectl hello
hello world卸载插件
# 要卸载一个插件,我们只需要直接删除这个可执行文件即可
sudo rm /usr/local/bin/kubectl-hello创建插件
下面来手写一个k8s节点事件查看的插件:
# cat ./kubectl-nodeck
#!/bin/bash
check_k8s_resource(){
for ip in `kubectl get node|grep -wv SchedulingDisabled|awk 'NR!=1{print $1}'`;do echo "============================ [ $ip ] ============================";kubectl describe node $ip|tail -7;done
}
check_k8s_resource#!/bin/bash
check_k8s_resource() {
for node_name in $(kubectl get node | grep -wv SchedulingDisabled | awk 'NR!=1{print $1}'); do
echo "============================ [ $node_name ] ============================"
kubectl describe node $node_name | grep -wA30 'Events:'
done
}
check_k8s_resourcegrep -wv SchedulingDisabled:grep命令用于在输入中搜索包含特定文本的行。
在这里,-w选项表示只匹配整个单词,-v选项表示只匹配不包含指定文本的行。
因此,grep -wv SchedulingDisabled会过滤掉包含"SchedulingDisabled"文本的行。
awk 'NR!=1{print $1}':awk是一个用于处理文本文件的强大工具。
在这个命令中,NR!=1表示忽略第一行(标题行),然后打印每行的第一个字段kubectl describe node 10.0.1.201|grep -wA30 'Events:'
-w 全词匹配
A30 30行Bash shell中,
!$是一个特殊的变量,它代表了上一个命令的最后一个参数。这对于快速重复或修改前一个命令的某个部分非常有用
# chmod +x ./kubectl-nodecheck
# mv ./kubectl-nodecheck /usr/local/bin/
# kubectl plugin list
Unable to read directory "/snap/bin" from your PATH: open /snap/bin: no such file or directory. Skipping...
The following compatible plugins are available:
/usr/local/bin/kubectl-hello
/usr/local/bin/kubectl-nodecheck
# kubectl nodecheck
============================ [ 10.0.1.203 ] ============================
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Starting 17m kube-proxy
Normal Starting 18m kubelet Starting kubelet.
Warning InvalidDiskCapacity 18m kubelet invalid capacity 0 on image filesystem
Normal NodeAllocatableEnforced 18m kubelet Updated Node Allocatable limit across pods
Normal NodeHasSufficientMemory 17m (x5 over 18m) kubelet Node 10.0.1.203 status is now: NodeHasSufficientMemory
Normal NodeHasNoDiskPressure 17m (x5 over 18m) kubelet Node 10.0.1.203 status is now: NodeHasNoDiskPressure
Normal NodeHasSufficientPID 17m (x5 over 18m) kubelet Node 10.0.1.203 status is now: NodeHasSufficientPID
Warning Rebooted 17m kubelet Node 10.0.1.203 has been rebooted, boot id: 9622ba08-ee58-4eb6-9c50-b01f74439d33
Normal RegisteredNode 17m node-controller Node 10.0.1.203 event: Registered Node 10.0.1.203 in Controller
============================ [ 10.0.1.204 ] ============================
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Starting 17m kube-proxy
Normal Starting 18m kubelet Starting kubelet.
Warning InvalidDiskCapacity 18m kubelet invalid capacity 0 on image filesystem
Normal NodeAllocatableEnforced 18m kubelet Updated Node Allocatable limit across pods
Normal NodeHasNoDiskPressure 18m (x7 over 18m) kubelet Node 10.0.1.204 status is now: NodeHasNoDiskPressure
Normal NodeHasSufficientPID 18m (x7 over 18m) kubelet Node 10.0.1.204 status is now: NodeHasSufficientPID
Normal NodeHasSufficientMemory 18m (x8 over 18m) kubelet Node 10.0.1.204 status is now: NodeHasSufficientMemory
Normal RegisteredNode 17m node-controller Node 10.0.1.204 event: Registered Node 10.0.1.204 in Controller