安装 krew

背景

kubectl支持通过kubectl-xx这种方式来扩展,而krew用来管理这些插件

下载包

https://github.com/kubernetes-sigs/krew/releases

下载连接
https://github.com/kubernetes-sigs/krew/releases/download/v0.4.4/krew-linux_amd64.tar.gz

# 下载软件
wget https://github.com/kubernetes-sigs/krew/releases/download/v0.4.4/krew-linux_amd64.tar.gz
# 解压
tar -xvf krew-linux_amd64.tar.gz
# 安装
[root@master softs]# ./krew-linux_amd64 install krew
WARNING: To be able to run kubectl plugins, you need to add
the following to your ~/.bash_profile or ~/.bashrc:

    export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"

and restart your shell.

Adding "default" plugin index from https://github.com/kubernetes-sigs/krew-index.git.
Updated the local copy of plugin index.
Installing plugin: krew
Installed plugin: krew
\
 | Use this plugin:
 |      kubectl krew
 | Documentation:
 |      https://krew.sigs.k8s.io/
 | Caveats:
 | \
 |  | krew is now installed! To start using kubectl plugins, you need to add
 |  | krew's installation directory to your PATH:
 |  |
 |  |   * macOS/Linux:
 |  |     - Add the following to your ~/.bashrc or ~/.zshrc:
 |  |         export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
 |  |     - Restart your shell.
 |  |
 |  |   * Windows: Add %USERPROFILE%\.krew\bin to your PATH environment variable
 |  |
 |  | To list krew commands and to get help, run:
 |  |   $ kubectl krew
 |  | For a full list of available plugins, run:
 |  |   $ kubectl krew search
 |  |
 |  | You can find documentation at
 |  |   https://krew.sigs.k8s.io/docs/user-guide/quickstart/.
 | /
/

#查看是否安装成功
[root@master ~]# cd ${HOME}/.krew/bin
[root@master bin]# ls
kubectl-krew
[root@master bin]# ./kubectl-krew -h
krew is the kubectl plugin manager.
You can invoke krew through kubectl: "kubectl krew [command]..."

在$HOME/.zshrc或者$HOME/.bashrc中添加

vim ~/.bashrc
alias k=kubectl
source <(kubectl completion bash) # 命令行提示
source <(minikube completion bash)
source <(kubeadmin completion bash)
export PATH=${PATH}:${HOME}/.krew/bin # 加入这一行

//刷新 bashrc
source ~/.bashrc

kubectl常用插件:

  • ctx
  • ns
  • neat
  • images
  • node-shell
kubectl krew install ns
kubectl krew install ctx

kubectl krew install neat
  k get po -o yaml|k neat
kubectl krew install node-shell
  kubectl node-shell instance-i
kubectl krew install images
  kubectl images -A

以下是执行记录

# 安装ns
[docker@haima root]$ kubectl krew install ns
Updated the local copy of plugin index.
Installing plugin: ns
Installed plugin: ns
\
 | Use this plugin:
 |      kubectl ns
 | Documentation:
 |      https://github.com/ahmetb/kubectx
 | Caveats:
 | \
 |  | If fzf is installed on your machine, you can interactively choose
 |  | between the entries using the arrow keys, or by fuzzy searching
 |  | as you type.
 | /
/

# 安装ctx
[docker@haima root]$ kubectl krew install ctx
Updated the local copy of plugin index.
Installing plugin: ctx
Installed plugin: ctx
\
 | Use this plugin:
 |      kubectl ctx
 | Documentation:
 |      https://github.com/ahmetb/kubectx
 | Caveats:
 | \
 |  | If fzf is installed on your machine, you can interactively choose
 |  | between the entries using the arrow keys, or by fuzzy searching
 |  | as you type.
 |  | See https://github.com/ahmetb/kubectx for customization and details.
 | /
/
WARNING: You installed plugin "ctx" from the krew-index plugin repository.
   These plugins are not audited for security by the Krew maintainers.
   Run them at your own risk.

ns 插件

操作 命名空间

k ns kube-system

# 查看帮助
[root@master ~]# kubectl ns -h
USAGE:
  kubectl ns                    : list the namespaces in the current context
  kubectl ns <NAME>             : change the active namespace of current context
  kubectl ns -                  : switch to the previous namespace in this context
  kubectl ns -c, --current      : show the current namespace
  kubectl ns -h,--help          : show this message

# 查看空间
[docker@haima root]$ k get ns
NAME                   STATUS   AGE
default                Active   6d1h
kube-node-lease        Active   6d1h
kube-public            Active   6d1h
kube-system            Active   6d1h
kubernetes-dashboard   Active   178m
您在 /var/spool/mail/root 中有新邮件
[root@master ~]# kubectl ns
default
kube-flannel
kube-node-lease
kube-public
kube-system
# 切换空间
[docker@haima root]$ k ns kube-system
Context "minikube" modified.
Active namespace is "kube-system".

images 插件

查看帮助

[root@master ~]# kubectl images -h
Show container images used in the cluster.

Usage:
  kubectl-images [podname-regex] [flags]

Examples:
  # display a table of all images in current namespace using podName/containerName/containerImage as columns.
  kubectl images

  # display images info in yaml format
  kubectl images -oy

  # display a table of images that match 'nginx' podname regex in 'dev' namespace using podName/containerImage as columns.
  kubectl images -n dev nginx -c 1,2

Flags:
  -A, --all-namespaces         if present, list images in all namespaces.
  -c, --columns string         specify the columns to display, separated by comma. [0:Namespace, 1:PodName, 2:ContainerName, 3:ContainerImage, 4:ImagePullPolicy, 5:ImageSize] (default "1,2,3")
  -C, --context string         The name of the kubeconfig context to use.
  -h, --help                   help for kubectl-images
  -k, --kubeconfig string      path to the kubeconfig file to use for CLI requests.
  -n, --namespace string       if present, list images in the specified namespace only. Use current namespace as fallback.
  -o, --output-format string   output format. [json(j)|table(t)|yaml(y)] (default "table")
  -u, --unique                 Unique images group by namespace/container/images/pullPolicy.
      --version                version for kubectl-images

显示集群中使用的容器镜像

[root@master bin]#   kubectl images -A
[Summary]: 2 namespaces, 12 pods, 18 containers and 8 different images
+--------------------------------+---------------------------+--------------------------------------------------------------+
|              Pod               |         Container         |                            Image                             |
+--------------------------------+---------------------------+--------------------------------------------------------------+
| kube-flannel-ds-74wph          | kube-flannel              | harbor.top/google_containers/flannel:v0.22.1                 |
+                                +---------------------------+--------------------------------------------------------------+
|                                | (init) install-cni-plugin | harbor.top/google_containers/flannel-cni-plugin:v1.2.0       |
+                                +---------------------------+--------------------------------------------------------------+
|                                | (init) install-cni        | harbor.top/google_containers/flannel:v0.22.1                 |
+--------------------------------+---------------------------+                                                              +
| kube-flannel-ds-9gz7p          | kube-flannel              |                                                              |
+                                +---------------------------+--------------------------------------------------------------+
|                                | (init) install-cni-plugin | harbor.top/google_containers/flannel-cni-plugin:v1.2.0       |
+                                +---------------------------+--------------------------------------------------------------+
|                                | (init) install-cni        | harbor.top/google_containers/flannel:v0.22.1                 |
+--------------------------------+---------------------------+                                                              +
| kube-flannel-ds-vfhrc          | kube-flannel              |                                                              |
+                                +---------------------------+--------------------------------------------------------------+
|                                | (init) install-cni-plugin | harbor.top/google_containers/flannel-cni-plugin:v1.2.0       |
+                                +---------------------------+--------------------------------------------------------------+
|                                | (init) install-cni        | harbor.top/google_containers/flannel:v0.22.1                 |
+--------------------------------+---------------------------+--------------------------------------------------------------+
| coredns-7d5ccbb94b-rqt7g       | coredns                   | harbor.top/google_containers/coredns:v1.10.1                 |
+--------------------------------+                           +                                                              +
| coredns-7d5ccbb94b-w6scb       |                           |                                                              |
+--------------------------------+---------------------------+--------------------------------------------------------------+
| etcd-master                    | etcd                      | harbor.top/google_containers/etcd:3.5.7-0                    |
+--------------------------------+---------------------------+--------------------------------------------------------------+
| kube-apiserver-master          | kube-apiserver            | harbor.top/google_containers/kube-apiserver:v1.27.3          |
+--------------------------------+---------------------------+--------------------------------------------------------------+
| kube-controller-manager-master | kube-controller-manager   | harbor.top/google_containers/kube-controller-manager:v1.27.3 |
+--------------------------------+---------------------------+--------------------------------------------------------------+
| kube-proxy-gqv8c               | kube-proxy                | harbor.top/google_containers/kube-proxy:v1.27.3              |
+--------------------------------+                           +                                                              +
| kube-proxy-rgbpl               |                           |                                                              |
+--------------------------------+                           +                                                              +
| kube-proxy-w9rdp               |                           |                                                              |
+--------------------------------+---------------------------+--------------------------------------------------------------+
| kube-scheduler-master          | kube-scheduler            | harbor.top/google_containers/kube-scheduler:v1.27.3          |
+--------------------------------+---------------------------+--------------------------------------------------------------+

查看所有node 信息

kubectl top node #查看所有node
kubectl top node 节点名称  # 查看指定节点资源
kubectl top pos

报错:

Error from server (NotFound): the server could not find the requested resour

解决:

下载地址:
https://github.com/kubernetes-sigs/metrics-server/releasesv

 wget  https://github.com/kubernetes-sigs/metrics-server/archive/refs/tags/metrics-server-helm-chart-3.10.0.tar.gz
tar xvf metrics-server-helm-chart-3.10.0.tar.gz 
cd metrics-server-helm-chart-3.10.0

参考下面文档:
http://www.manongjc.com/detail/18-seporcyffrxjtfs.html

更多插件说明

[kubectl 实用插件](K8S 实用工具之四 - kubectl 实用插件)
https://mp.weixin.qq.com/s/X7RmXER0bT9m524kTBCyDg

作者:海马  创建时间:2023-08-13 07:39
最后编辑:海马  更新时间:2024-08-03 21:41