1

As Kubernetes becomes the mainstream application container orchestration platform, its command line client kubectl has become the most commonly used tool for our daily deployment of applications and cluster maintenance.
kubectl provides powerful built-in self-commands to satisfy our operations on the cluster, such as get Get the resource objects in the cluster, proxy create proxy and so on , in addition to these built-in self-commands, kubectl also provides extensible capabilities, allowing us to install plugins written by ourselves or provided by the community to enhance our productivity using kubectl .

Here I will show you how to install kubectl extended plug-ins, as well as several community-provided plug-ins that I often use in my daily work.

Before installing and using the kubectl plugin, please ensure and install and configure the kubectl command line tool and the git tool.

krew

The first extension plug-in introduced is a plug-in developed by the krew -k8s special interest group for installation and management kubectl extension plug-in.

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

Install krew (on macOS/Linux):

  1. Execute in terminal (Bash or 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
    )
  2. Add $HOME/.krew/bin to the PATH environment variable, update your .bashrc or .zshrc file, and add the following line

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

Then restart your terminal.

  1. Test krew has been installed successfully

     ❯ 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
  2. (Optional) Set the krew alias

     alias krew='kubectl-krew'
    echo "alias krew='kubectl-krew'" >> ~/.alias

这样就安装完成了, krewkubectl ,而---14e4c363aa3614e3f0dc6cd62891c152 krew自己又是插件,所以之后可以krew来更新krew .

To install on Windows, please refer to https://krew.sigs.k8s.io/docs/user-guide/setup/install/ .

Example of use

  • List currently installed plugins

     ❯ krew list
    PLUGIN            VERSION
    krew              v0.4.3
  • Install the plugin (other plugins described below will be installed using this method)

     ❯ krew install [插件名]
  • Update the local plugin index (check for plugin updates)

     ❯ 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
  • upgrade plugin

     ❯ krew upgrade open-svc
    Updated the local copy of plugin index.
    Upgrading plugin: open-svc
  • remove plugin

     ❯ krew uninstall [插件名]

Krew-index maintains hundreds of plugins that can be installed directly through krew. Next, I will introduce several other plug-ins that I use every day kubectl .

example

Code: https://github.com/seredot/kubectl-example

install krew install example

example plugin can be used to quickly generate yaml file examples of k8s resource objects, such as

 ❯ 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

When we want to temporarily pass the resource declaration file to an unfamiliar application, we can use this plugin to generate a template.

fleet

Code: https://github.com/kubectl-plus/kcf

Install: krew install fleet

fleet plugin to quickly see an overview of the currently configured cluster

 ❯ 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

Code: https://github.com/corneliusweig/ketall

Install: krew install get-all

get-all plugin gets all the resource objects of the cluster (or a certain namespace)

 ❯ 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

Code: https://github.com/shibumi/kubectl-htpasswd

Installation: krew install htpasswd

htpasswd is a basic authentication password generator compatible with 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

This plugin is handy for creating basic authenticated Ingress for services.

images

Code: https://github.com/chenjiandongx/kubectl-images

Installation: krew install images

images Container image information used in the real namespace

 ❯ 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

Code: https://github.com/vladimirvivien/ktop

Installation: krew install ktop

ktop plugin is similar to the Linux Top tool to plug in the load of the k8s cluster

 ❯ 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%    
└ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─

Summarize

krew-index contains a large number of powerful extension plug-ins, covering RBAC management, resource management and other functions, it is not meaningful to list here. However, with krew , we can easily install the required extensions to improve our productivity in managing k8s clusters in our daily work.

refer to

Also posted on Mengz's Blog


梦哲
74 声望58 粉丝

寻找人生的意义!