Several common tools make it easier to work on Kubernetes clusters

I wrote an article before This paper introduces tools to accelerate cloud native Java development.

In fact, there are many operations on the cluster in daily work. Today I will introduce the tools I use.

kubectl-alias

The most frequently used tool, I modified it a little and added StatefulSet support.

This is mine https://github.com/addozhang/kubectl-aliases , based on https://github.com/ahmetb/kubectl-aliases.

For example, kgpoojson xxx is equivalent to kubectl get pod xxx -o json for outputting json of a pod.

combination jq Better use effect 😂.

Grammatical interpretation

  • k=kubectl
    • sys=--namespace kube-system
  • commands:
    • g=get
    • d=describe
    • rm=delete
    • a:apply -f
    • ak:apply -k
    • k:kustomize
    • ex: exec -i -t
    • lo: logs -f
  • resources:
    • po=pod, dep=deployment, ing=ingress, svc=service, cm=configmap, sec=secret,ns=namespace, no=node
  • flags:
    • output format: oyaml, ojson, owide
    • all: --all or --all-namespaces depending on the command
    • sl: --show-labels
    • w=-w/--watch
  • value flags (should be at the end):
    • n=-n/--namespace
    • f=-f/--filename
    • l=-l/--selector

kubectx + kubens

Look here

kubectx is used for fast switching between different clusters. If you use kubectl, you need to:

# context list
kubectl config current-context 
# Setting context
kubectl config use-context coffee

kubens is a tool for quickly switching between different namespace s. In kubectl, you need to:

# namespace list
kbuectl get ns
# kubectl config set-context --current --namespace=kube-system

k9s

Yes, only one more than k8s 😂.

k9s It provides editing interaction between terminal UI and Kubernetes cluster. I often use, such as:

  • F configure port forwarding
  • l output pod log
  • e modify resource object
  • s pod terminal interaction mode
  • Output resource objects in y yaml mode
  • d describe resource object
  • ctrl+d delete pod

Start mode

# Specify namespace run
k9s -n mycoolns
# Specify context to run
k9s --context coolCtx
# Read only mode operation
k9s --readonly

Type a question mark You can open the shortcut guide.

stern

stern It can be used to log multiple pods on the tail cluster and multiple containers in the pod. Different pods and containers are distinguished by different colors to facilitate debug ging.

For example, use the command stern - L tier = control plane - n Kube system to output the log of the control plane (Tier = control plane) pod under the Kube system namespace.

Command line options

Tail multiple pods and containers from Kubernetes

Usage:
  stern pod-query [flags]

Flags:
  -A, --all-namespaces             If present, tail across all namespaces. A specific namespace is ignored even if specified with --namespace.
      --color string               Color output. Can be 'always', 'never', or 'auto' (default "auto")
      --completion string          Outputs stern command-line completion code for the specified shell. Can be 'bash' or 'zsh'
  -c, --container string           Container name when multiple containers in pod (default ".*")
      --container-state string     If present, tail containers with status in running, waiting or terminated. Default to running. (default "running")
      --context string             Kubernetes context to use. Default to current context configured in kubeconfig.
  -e, --exclude strings            Regex of log lines to exclude
  -E, --exclude-container string   Exclude a Container name
      --field-selector string      Selector (field query) to filter on. If present, default to ".*" for the pod-query.
  -h, --help                       help for stern
  -i, --include strings            Regex of log lines to include
      --init-containers            Include or exclude init containers (default true)
      --kubeconfig string          Path to kubeconfig file to use
  -n, --namespace strings          Kubernetes namespace to use. Default to namespace configured in Kubernetes context. To specify multiple namespaces, repeat this or set comma-separated value.
  -o, --output string              Specify predefined template. Currently support: [default, raw, json] (default "default")
  -l, --selector string            Selector (label query) to filter on. If present, default to ".*" for the pod-query.
  -s, --since duration             Return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to 48h.
      --tail int                   The number of lines from the end of the logs to show. Defaults to -1, showing all logs. (default -1)
      --template string            Template to use for log lines, leave empty to use --output flag
  -t, --timestamps                 Print timestamps
      --timezone string            Set timestamps to specific timezone (default "Local")
  -v, --version                    Print the version and exit

Lens

Lens It is an IDE used to control Kubernetes. It is open source and free.

It eliminates the complexity of cluster operation, provides real-time observability, facilitates troubleshooting, supports multi system desktop clients, and is compatible with a variety of clusters.

Infra App

Infra App Similar to Lens, the UI is better than Lens, but the function is much weaker, similar to Lens's read-only mode.

The difference between the free version and the paid version lies in the number of clusters supported. The free version supports only one cluster.

kubefwd

kubefwd , this has been installed, but it has been used only a few times, because the access between applications does not go through the service, but it will be used occasionally when doing experiments.

kubefwd is a command-line tool used to forward all or part of the pod s in the specified namespace in Kubernetes. kubefwd forwards the service to be accessed using the local loopback IP address and uses the same port as the service. kubefwd will temporarily add the domain entry of the service to the / etc/hosts file.

After starting kubefwd, you can use the service name and port to access the corresponding application locally as in the Kubernetes cluster.

summary

Making good use of tools can improve efficiency, but it is not indispensable.

If you have other tools, please leave a message.

The article is unified in the official account of the cloud.

Added by dgray on Mon, 24 Jan 2022 03:23:16 +0200