Super practical! Six Open Source Tools K8s Developers Must Know

Keywords: Kubernetes shell JSON Nginx

Source: Yunnan Native Laboratory, Click to view the original text.

Guide: Kubernetes, as the "operating system" of the cloud native era, is familiar with and used as a necessary skill for every User. If you are working on Kubernetes, you need the right tools and techniques to ensure that the Kubernetes cluster is highly available and the workload runs steadily. This article will give you a detailed introduction to six practical open source tools for Kubernetes. Don't miss them.

Preface

With the development and evolution of Kubernetes, people can tame its uncontrolled behavior from the inside. But some people are reluctant to wait for Kubernetes to become easy to use, and have developed their own solutions for many common problems encountered in Kubernetes already in production.

Here we will introduce some techniques to improve operational efficiency, and list several useful open source Kubernetes tools that simplify Kubernetes in various ways, including simplifying command-line interaction, simplifying application deployment syntax, and so on.

kubectl automatic completion

Kubectl is a very important command-line tool. There are many commands related to it. We can't remember so many commands, and we often make mistakes. So it is necessary to complete the commands automatically. The kubectl tool itself supports automatic completion. It only needs a simple setup.

bash user

Most users'shell s use bash, and Linux systems can be set up with the following commands:

$ echo "source <(kubectl completion bash)" >> ~/.bashrc
$ source ~/.bashrc

If you find that it can't be automatically repaired, you can try to install bash-completion and refresh it!

zsh users

If the shell you use is zsh, you can set it up with the following commands:

$ echo "source <(kubectl completion zsh)" >> ~/.zshrc
$ source ~/.zshrc

Custom kubectl get output

kubectl get related resources, the default output is built-in kubectl, generally we can also use - o json or - o yaml to view its complete resource information. But most of the time, the information we need to care about is not comprehensive, so we need to customize the output columns, so we can use go-template to implement.

go-template is a kind of template of golang, which can be referred to. Relevant description of template.

For example, if you just want to see the uid of each pod in the acquired pods, you can use the following command:

$ kubectl get pods --all-namespaces -o go-template='{{range .items}}{{.metadata.uid}}
{{end}}'
2ea418d4-533e-11e8-b722-005056a1bc83
7178b8bf-4e93-11e8-8175-005056a1bc83
a0341475-5338-11e8-b722-005056a1bc83
...

Because the return result of get pods is a List type, the pods obtained are all in the value of items, so you need to traverse items, and you have {range. items}. Then, the template is used to select the content that needs to be displayed, which is {metadata.uid}} for each item.

It's important to note that a special treatment is to wrap lines before {{end} so that newline characters can be inserted into the template.

Of course, if you feel that this is not elegant, you can also use the printf function, where the use of n can achieve the insertion of newline characters.

$ kubectl get pods --all-namespaces -o go-template --template='{{range .items}}{{printf "%s\n" .metadata.uid}}{{end}}'

Or it could be:

$ kubectl get pods --all-namespaces -o go-template --template='{{range .items}}{{.metadata.uid}}{{"\n"}}{{end}}'

In fact, with printf, it is easy to realize the output of corresponding fields, and the style can be controlled by itself. For example:

$ kubectl get pods --all-namespaces -o go-template --template='{{range .items}}{{printf "|%-20s|%-50s|%-30s|\n" .metadata.namespace .metadata.name .metadata.uid}}{{end}}'
|default             |details-v1-64b86cd49-85vks                        |2e7a2a66-533e-11e8-b722-005056a1bc83|
|default             |productpage-v1-84f77f8747-7tkwb                   |2eb4e840-533e-11e8-b722-005056a1bc83|
|default             |ratings-v1-5f46655b57-qlrxp                       |2e89f981-533e-11e8-b722-005056a1bc83|
...

Here are two examples of advanced use of go-template:

  • range nesting
# List the mirror names used by all containers
$ kubectl get pods --all-namespaces -o go-template --template='{{range .items}}{{range .spec.containers}}{{printf "%s\n" .image}}{{end}}{{end}}'
istio/examples-bookinfo-details-v1:1.5.0
istio/examples-bookinfo-productpage-v1:1.5.0
istio/examples-bookinfo-ratings-v1:1.5.0
...

  • Conditional Judgment
# List the node names and IP of all non-schedulable nodes
$ kubectl get no -o go-template='{{range .items}}{{if .spec.unschedulable}}{{.metadata.name}} {{.spec.externalID}}{{"\n"}}{{end}}{{end}}'

In addition to using go-template, comma-separated custom column lists can be used to print tables:

$ kubectl -n kube-system get pods coredns-64b597b598-7547d -o custom-columns=NAME:.metadata.name,hostip:.status.hostIP
NAME                       hostip
coredns-64b597b598-7547d   192.168.123.250

You can also use go-template-file to customize the template list. Instead of passing in the template through parameters, the template is written as a file, and then you need to specify the template to point to the file.

$ cat > test.tmpl << EOF 
NAME                      HOSTIP
metadata.name       status.hostIP
EOF
$ kubectl -n kube-system get pods coredns-64b597b598-7547d -o custom-columns-file=test.tmpl
NAME                       HOSTIP
coredns-64b597b598-7547d   192.168.123.250

Interactive Kubernetes Client

Kube-prompt It allows you to enter something equivalent to an interactive command session on the Kubernetes client and provides automatically populated background information for each command. You don't have to type kubectl to prefix each command.

Generate kubectl aliases

If you need to interact frequently with kubectl and kubernetes api, using aliases will save you a lot of time, open source projects. kubectl-aliases The kubectl aliases can be generated programmatically. The rules of alias generation are as follows:

  • Simple Alias Example

kd → kubectl describe

  • Examples of high-level names

kgdepallw → kubectl get deployment --all-namespaces --watch

Check configuration file

If you write the Kubernetes manifest file manually, it's difficult to check the syntax of the manifest file, especially when you have multiple versions of the Kubernetes cluster, it's even more difficult to verify that the syntax of the configuration file is correct.

Kubeval It is a tool for verifying Kubernetes YAML or JSON configuration files. It supports multiple versions of Kubernetes and can help us solve many problems.

  • Use examples
$ kubeval nginx.yaml
The document nginx.yaml contains an invalid Deployment
---> spec.replicas: Invalid type. Expected: integer, given: string

Simplified Kubernetes deployment definition

Many people complain that the definition of Kubernetes manifest file is too complex and lengthy. They are difficult to write and maintain, and simplifying deployment definitions greatly reduces maintenance.

Kedge Provide a simpler and more concise grammar, and then kedge converts it into a Kubernetes manifest file.

  • Use examples

Scan down the two-dimensional code to add a small assistant, with 8000 cloud native enthusiasts to discuss technical trends, practical progress! Entry code: company-post-city

Posted by HSM on Mon, 09 Sep 2019 03:41:12 -0700