当前位置:网站首页>Expand your kubecl function

Expand your kubecl function

2022-07-04 23:28:00 InfoQ

With  
Kubernetes
  Become a mainstream application container orchestration platform , Its command line client  
kubectl
  It has also become our daily deployment Application , The most commonly used tool for maintaining clusters .

kubectl
  It provides powerful built-in self commands to meet our cluster operations , for example  
get
  Get the resource object in the cluster ,
proxy
  Create proxy and so on , In addition to these built-in self commands ,
kubectl
  It also provides scalable capabilities , Allow us to install plug-ins written by ourselves or provided by the community to enhance our use  
kubectl
  The productivity of .

Here we will introduce how to install  
kubectl
  Extension plug-in , And several plug-ins provided by the community that I often use in my daily work .

In the installation and use of  
kubectl
  Before plug-in , Please make sure that it is well installed and configured  
kubectl
  Command line tools and  
git
  Tools .

krew

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

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

install  
krew
 ( stay macOS/Linux On ):

  • Execute at the terminal (Bash perhaps Zsh) perform

(
 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
)

  • take  
    $HOME/.krew/bin
      Add to  
    PATH
      environment variable , Update your  
    .bashrc
      perhaps  
    .zshrc
      file , Add the following line

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

Then restart your terminal .

  • test  
    krew
      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

  • ( Optional ) Set up  krew  Alias

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

This completes the installation ,
krew
  yes  
kubectl
  Plug in Manager , and  
krew
  I am a plug-in again , So you can use  
krew
  To update  
krew
.

stay  Windows  Installation on , Please refer to  https://krew.sigs.k8s.io/docs/user-guide/setup/install/ .

Examples of use

  • List the currently installed plug-ins

* krew list
PLUGIN VERSION
krew v0.4.3

  • Installing a plug-in ( Other plug-ins described below will be installed in this way )

* krew install [ The plugin name ]

  • Update the local plug-in index ( Check whether there are plug-in 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 plug-ins

* krew upgrade open-svc
Updated the local copy of plugin index.
Upgrading plugin: open-svc

  • Delete plug-ins

* krew uninstall [ The plugin name ]

krew-index
  There are hundreds of models that can be passed  krew  Directly installed plug-ins . Next, I will introduce several other products that I use everyday  
kubectl
  plug-in unit .

example

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

install  
krew install example

example
  Plug ins can be used to quickly generate k8s Of the resource object yaml File example , for example

* 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 declare that we are unfamiliar with the application through the resource file , You can use this plug-in to generate templates .

fleet

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

install : 
krew install fleet

fleet
  The plug-in can quickly view the 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
  Plug in get cluster ( Or a namespace ) All resource objects for

* 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

install : 
krew install htpasswd

htpasswd
  yes  nginx-ingress  Compatible basic authentication password generator

* 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

The plug-in is creating basic authentication for services  Ingress  Very convenient .

images

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

install : 
krew install images

images
  Container image information used in real namespaces

* 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

install : 
krew install ktop

ktop
  The plug-in is similar to  Linux Top  Tools to plug-ins k8s The load of the cluster

* k ktop
┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
│ API server: https://k8sc1.mengz.lan:6443 Version: v1.23.8 context: [email protected] 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% 
└ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─

summary

krew-index
  It contains a large number of powerful extensions , covers  RBAC  management , Resource management and other functions , There is no point in listing . But there is.  
krew
 , We can easily install the required extensions , To improve our management in our daily work k8s Productivity of clusters .

Reference resources
  • https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins/
  • https://krew.sigs.k8s.io/docs/user-guide/quickstart/

Also published in  [Mengz's Blog](https://blog.mengz.dev/posts/k8s/extend-kubectl-with-plugins/)
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041927463590.html