1 krew
krew是kubectl命令行的插件管理器,可以和yum一样,查找,安装,更新插件,让插件使用起来更方便。
2 安装
从github下载最新的安装包,然后解压安装,
(
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
)
然后需要将krew路径添加到PATH环境变量中,可以放到对应用户home目录下.bash_profile
echo 'export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"' >> ~/.bash_profile
3 使用
- 查找插件
[root@master ~]# kubectl krew search
NAME DESCRIPTION INSTALLED
access-matrix Show an RBAC access matrix for server resources no
accurate Manage Accurate, a multi-tenancy controller no
advise-policy Suggests PodSecurityPolicies and OPA Policies f... no
advise-psp Suggests PodSecurityPolicies for cluster. no
...
- 安装插件
[root@master ~]# kubectl krew install get-all
Updated the local copy of plugin index.
Updated the local copy of plugin index "kvaps".
Installing plugin: get-all
Installed plugin: get-all
\
| Use this plugin:
| kubectl get-all
| Documentation:
| https://github.com/corneliusweig/ketall
/
WARNING: You installed plugin "get-all" from the krew-index plugin repository.
These plugins are not audited for security by the Krew maintainers.
Run them at your own risk.
- 更新插件
[root@master ~]# kubectl krew upgrade
Updated the local copy of plugin index.
Updated the local copy of plugin index "kvaps".
Upgrading plugin: get-all
Skipping plugin get-all, it is already on the newest version
Upgrading plugin: krew
Skipping plugin krew, it is already on the newest version
- 使用插件
[root@master ~]# kubectl get-all -n kyverno
NAME NAMESPACE AGE
configmap/istio-ca-root-cert kyverno 55d
configmap/kube-root-ca.crt kyverno 55d
secret/default-token-b5k8b kyverno 55d
secret/kyverno-svc.kyverno.svc.kyverno-tls-ca kyverno 55d
secret/kyverno-svc.kyverno.svc.kyverno-tls-pair kyverno 55d
serviceaccount/default kyverno 55d
lease.coordination.k8s.io/kyverno kyverno 55d
lease.coordination.k8s.io/kyvernopre kyverno 55d
lease.coordination.k8s.io/kyvernopre-lock kyverno 55d
lease.coordination.k8s.io/webhook-register kyverno 55d
- 卸载插件
[root@master ~]# kubectl krew uninstall get-all
Uninstalled plugin: get-all
4 常用插件
- get-all 可以列出某个namespace下所有资源,是真的所有资源,和kubectl get all不同
[root@master ~]# kubectl get all -n kyverno
No resources found in kyverno namespace.
[root@master ~]# kubectl get-all -n kyverno
NAME NAMESPACE AGE
configmap/istio-ca-root-cert kyverno 55d
configmap/kube-root-ca.crt kyverno 55d
secret/default-token-b5k8b kyverno 55d
secret/kyverno-svc.kyverno.svc.kyverno-tls-ca kyverno 55d
secret/kyverno-svc.kyverno.svc.kyverno-tls-pair kyverno 55d
serviceaccount/default kyverno 55d
lease.coordination.k8s.io/kyverno kyverno 55d
lease.coordination.k8s.io/kyvernopre kyverno 55d
lease.coordination.k8s.io/kyvernopre-lock kyverno 55d
lease.coordination.k8s.io/webhook-register kyverno 55d
-
ctx: 用于切换context
-
ns: 用于切换namespace
-
node-shell: 用于获得node节点的root shell,从而进行一些debug,参考——《K8S node-shell》
更多插件可以去这里探索,https://krew.sigs.k8s.io/plugins/
5 备注
krew安装的命令会放到用户home目录的.krew目录下,如果实在不想使用命令时还要打个kubect,可以直接使用对应二进制命令,比如ns/ctx,我就喜欢直接使用二进制命令,
[root@master store]# pwd
/root/.krew/store
[root@master store]# ls
ctx get-all krew node-shell ns
参考文档:
- https://krew.sigs.k8s.io/
- https://krew.sigs.k8s.io/docs/user-guide/setup/install/
- https://krew.sigs.k8s.io/docs/user-guide/quickstart/
- https://krew.sigs.k8s.io/plugins/