K9s is much faster than using Kubectl alone to manage K8s clusters. Today we will use an article to quickly improve the efficiency of cluster management.

Use K9S-Kubernetes CLI to manage K8s cluster

K9s is a terminal based UI that interacts with the Kubernetes cluster. K9s is designed to simplify the process of navigating, observing, and managing deployed applications. K9s continuously monitors Kubernetes changes and provides follow-up commands to interact with observed resources.

Characteristics of the

  • Track the real-time activity of resources running in the Kubernetes cluster.
  • Handles Kubernetes standard resources and custom resource definitions.
  • Track real-time metrics related to resources such as PODS, containers, and nodes.
  • Provides standard cluster management commands, such as log, scaling, port-forward, restart…
  • Customize command shortcuts through aliases and hotkeys.
  • Support for extending the K9s plug-in to create your own cluster commands.
  • Powerful filtering mode: Allows users to drill down and view resources associated with their workloads.
  • Error amplification: Dive directly into the cluster resource problem.
  • Customizable appearance
    • Define your own terminal appearance through K9s skins.
    • Customize/arrange columns to display by resource.
  • Provides toggling to view minimal or complete resource definitions
  • Provide an overview of cluster resources through the Trap and XRay views.
  • RBAC
    • Support for viewing RBAC rules, such as clusters/roles and their associated bindings.
    • Reverse lookup asserts what users/groups or ServiceAccount can do on a cluster.
  • You can benchmark HTTP Services/Pods directly from K9s built-in benchmarks to see how your application is performing and adjust resource requests/limits accordingly.
  • K9s provides easy traversal of Kubernetes resources and their associated resources.

The installation

K9s is available for Linux, macOS, and Windows platforms. Please refer to the official K9s documentation directly, because it is very simple and I won’t go into details here.

The command

K9s is a CLI for viewing and managing the Kubernetes cluster. Next, let’s learn how to use it and feel its convenience and power.

The help command

K9s help: Lists all available CLI options

# k9s help
K9s is a CLI to view and manage your Kubernetes clusters.

Usage:
  k9s [flags]
  k9s [command]

Available Commands:
  helpHelp about any command
  infoPrint configuration info
  version Print version/build info

Flags:
  -A, --all-namespaces Launch K9s in all namespaces
  --as string  Username to impersonate for the operation
  --as-group stringArray   Group to impersonate for the operation
  --certificate-authority string   Path to a cert file for the certificate authority
  --client-certificate string  Path to a client certificate file for TLS
  --client-key string  Path to a client key file for TLS
  --cluster string The name of the kubeconfig cluster to use
  -c, --command string Specify the default command to view when the application launches
  --context string The name of the kubeconfig context to use
  --demo   Enable demo mode to show keyboard commands
  --headless   Turn K9s header off
  -h, --help   help for k9s
  --insecure-skip-tls-verify   If true, the server's caCertFile will not be checked for validity
  --kubeconfig string  Path to the kubeconfig file to use for CLI requests
  -l, --logLevel stringSpecify a log level (info, warn, debug, error, fatal, panic, trace) (default "info")
  -n, --namespace string   If present, the namespace scope for this CLI request
  --readonly   Disable all commands that modify the cluster
  -r, --refresh intSpecify the default refresh rate as an integer (sec) (default 2)
  --request-timeout string The length of time to wait before giving up on a single server request
  --token string   Bearer token for authentication to the API server
  --user stringThe name of the kubeconfig user to use

Use "k9s [command] --help" for more information about a command.
Copy the code

K9s info: Get information about the K9S runtime (logs, configuration, etc.)

# k9s info
 ____  __.________       
|    |/ _/   __   \______
|      < \____    /  ___/
|    |  \   /    /\___ \ 
|____|__ \ /____//____  >
        \/            \/ 

Configuration:   /root/.k9s/config.yml
Logs:            /tmp/k9s-root.log
Screen Dumps:    /tmp/k9s-screens-root
Copy the code

K9s Version: View the k9S installation version

# k9s version ____ __.________ | |/ _/ __ \______ | < \____ / ___/ | | \ / /\___ \ |____|__ \ /____//____ > \/ \/ Version: 0.19.7 Commit: 46 c2f31249b3b67a16659614bde179c481a547de Date: 2020-05-23 T17:18:49 ZCopy the code

Start the command

K9s: Starts k9S

# Start k9s k9s # Run k9s k9s -n myCoolns # Run k9s and start k9s in pod view via the 'pod command' # start k9s k9s in a context other than the default KubeConfig --context coolCtx # Start K9s in read-only mode - Disable all modify commands (delete,create, etc.) K9s --readonlyCopy the code

shortcuts

K9s uses aliases to locate most K8s resources. The official K9S key bindings are posted here.


Ctrl a: Displays aliases for all available resources.

Above introduced k9S features mentioned, K9S for us to provide a lot of shortcut keys and alias, through this shortcut key we can quickly view K8s resource alias (abbreviation), help us easy resources, improve work efficiency.


?: Displays the command console shortcut keys provided by K9s.

Esc: Return/exit.

Let’s go back to the original POD screen and see that I have two POD resources under the default namespace:

/: Enters the filtering mode

Enter /custom to filter the resource in the current view and find all pods whose names contain custom strings:

:: Enters the CLI mode

Allows you to enter any resource/resource alias you want to view. For example :deploy, : SVC…… Enter ns to view all namespaces in the current cluster.







:cm namespace
:cj namespace



:cm zhangsan
zhangsan
configmap


d y l e: respectively correspond todescribe yaml logs edit

View resource description information, YAML files and logs, modify the resource information, we play K8S are very familiar, there is no need to expand the details too much.

Ctrl z: Quickly locate resource errors

Ctrl d: Deletes the selected K8s resource

Ctrl k: Deletes resources without prompting

:q Ctrl c: quit K9s

It’s too cool! You only need to pay very little learning cost (K9s only need to read this article I organized, enough in the work), can greatly improve the work efficiency, once and for all.

The configuration file

K9s saves its configuration file in $HOME/.k9s/config.yml in the.k9s directory of the HOME directory. We can store K9s current default startup view and namespace information by modifying the fields in the file. Below I list the contents in the configuration file and the meanings and functions of each item. Later, you can modify the values according to your own requirements.

# $HOME/.k9s/config.yml k9s: # # The default value 2secS refreshRate: 2 # is set to true to hide the K9s header. Default false headless: false # Indicates whether modification commands such as delete/kill/edit are disabled. The default value is false readOnly: false # Toggle icon display because not all terminals support these characters. NoIcons: false # Log configuration Logger: # define the number of lines to be returned. The default 100 tail: 200 # defines the total number of log lines allowed in the view. Default 1000 buffer: 500 # represents the distance returned in seconds to the log timeline. The default value is 5 minutes (seconds) sinceSeconds: 300 # Goes into full screen mode when displaying the log. Default value: false fullScreenLogs: false # Toggle log newlines. Default value false textWrap: false # Toggle log line timestamp information. The default value false showTime: false # indicates the current KUBE context. Default: currentContext currentContext: minikube # represents the current kube cluster. The default is the current context cluster currentCluster: Minikube # persist based on namespace and view preference of each cluster. clusters: cluster1: namespace: active: coolio favorites: - cassandra - default view: active: po featureGates: # Toggle nodeshell support. Allow K9s to be encapsulated in nodes if necessary. The default is false. NodeShell: false # Provide shellPod enable custom gate function shellPod: # Shell Pod image to use. Image: killerAdmin # To boot into the namespace of Shell Pod. Namespace: Resource limits set by Fred # on shell containers. Limits: CPU: 100m memory: 100Mi # IP address used to enable port forwarding. PortForwardAddress: 1.2.3.4 Cluster2: Namespace: Active: All Favorites: -all-kube-system-default view: active: DPCopy the code

Custom command alias

Now that we’ve seen it, it’s time to show you some real K9s action. Now that we know the basics of K9s, we can customize our own K9s commands! Let’s go ahead and customize one. In $HOME/.k9s, the same directory as config. Yml, create a file named alias.yml:

# $HOME /. K9s/alias. Yml alias: # using pp as Pod alias pp: v1 / pods # use the abbreviation of CRB as ClusterRoleBinding CRB: . Rbac authorization. K8s. IO/v1 / clusterrolebindings # use short for cr as ClusterRole cr: . Rbac authorization. K8s. IO/v1 / clusterroles # using dep as Deployments alias dep: Apps/v1 / deployments # use Fred as custom resource CRD Frederick alias Fred: acme. IO/v1alpha1 / fredericksCopy the code

Aliases/abbreviations/abbreviations mean the same thing, just to make it easier to understand.

Customize shortcut keys

Entering command mode and entering a resource name or alias can be cumbersome for browsing frequently accessed resources. Using HotKeys, K9s can be configured to quickly navigate to our K8s resources. Also, we can create a file named /hotkey.yml in the $HOME/.k9s directory to customize our own shortcut keys:

# $HOME/.k9s/hotkey.yml Hotkey: # Click shift-0 to navigate to pod view Shift-0: shortCut: Shift-0 Description: Viewing Pods Command: Pods # Click shift-0 to navigate to deployments View Shift-1: shortCut: Shift-1 Description: View deployments Command: Dp # Click shift-0 to navigate to the Xray deployments view Shift-2: shortCut: Shift-2 Description: Xray Deployments Command: Xray deployCopy the code

In $HOME/. K9s, you can create a plugin.yml file named plugin.yml. It is better to teach people to fish than to teach them to fish.

Tt’s important to work smarter, not harder.

Recommended reading

  • Here’s what K9s says about RBAC.
  • K9s project on Github is very comprehensive and of high quality.
  • There is also an English introduction to K9s, Speed Up Administration of Kubernetes Clusters with K9s, which explains why K9s is used and how it speeds up Kubernetes cluster management.