Source: Cloud native laboratory, Click to view the original .
REVIEW : Kubernetes as native cloud era "operating system", it is familiar with and use per user (User) essential skills. If you are working on Kubernetes, you need the right tools and techniques to ensure the stable operation of high availability and workload Kubernetes cluster. This article will give you details of six practical Kubernetes open source tools, do not miss.
Foreword
With the development and evolution Kubernetes, one can tame its excesses from within. But some people are not willing to become dry and so Kubernetes easy to use and develop their own solutions to many common questions have been put into production Kubernetes encountered.
Here we will introduce some techniques to improve operational efficiency, and to name a few of the more useful open source Kubernetes tools that simplify Kubernetes in various ways, including streamlining the command line interaction, simplify application deployment and grammar.
kubectl autocomplete
kubectl
This command-line tool is very important, associated commands are also many, we can not remember so many commands, but also often wrong, so the auto-complete command is necessary, kubectl tool itself supports auto-fill full, simply set up what you can.
bash users
Most users are using a shell bash
, Linux system can be set by the following command:
$ echo "source <(kubectl completion bash)" >> ~/.bashrc
$ source ~/.bashrc
If you can not find auto-completion, you can try to install bash-completion
then refresh can!
zsh users
If you use a shell is the zsh
can be set by the following command:
$ echo "source <(kubectl completion zsh)" >> ~/.zshrc
$ source ~/.zshrc
Custom kubectl get output
kubectl get
Related Resources, the default output is kubectl built, generally we can use -o json
or -o yaml
view the complete resource information. But many times, the information we need to be concerned not exhaustive, so we need to customize the output of the column, you can use go-template
to achieve.
go-template
Golang is a template, you can refer to the instructions of the template .
For example, only want to see the acquisition of pods in each pod is uid
, 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 get pods of the result is List
the type of acquisition pods in items
this of value, and therefore need to traverse items, and will have {{range .items}}
. Then the selected content to be presented by the template, that is items each {{.metadata.uid}}
.
Special attention here to do a special deal, it is to {{end}}
the front line break, in order to insert a line break in the template.
Of course, if you feel that this treatment is not elegant, you can also use the printf
function, in which use \n
can be realized insert line breaks.
$ kubectl get pods --all-namespaces -o go-template --template='{{range .items}}{{printf "%s\n" .metadata.uid}}{{end}}'
Or it can be:
$ kubectl get pods --all-namespaces -o go-template --template='{{range .items}}{{.metadata.uid}}{{"\n"}}{{end}}'
With the fact printf
, you can easily achieve the output of the corresponding field, and can control their own style. For example, it can be:
$ 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|
...
The following two go-template advanced usage examples:
- range nesting
# 列出所有容器使用的镜像名
$ 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
# 列出所有不可调度节点的节点名与 IP
$ kubectl get no -o go-template='{{range .items}}{{if .spec.unschedulable}}{{.metadata.name}} {{.spec.externalID}}{{"\n"}}{{end}}{{end}}'
In addition to use go-template
, you can also use a comma-separated list of columns to print custom forms:
$ 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
a custom list of templates, the template parameter do not pass go, but written in a file, then you need to specify template
points 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 allows you to enter the equivalent of what the client Kubernetes interactive command session and provides background information automatically populated for each command, you do not have to type the kubectl to add a prefix for each command.
Alias generated kubectl
If you need to use kubectl and kubernetes api to interact frequently, using the alias will save you a lot of time, open source project kubectl-aliases can be generated programmatically kubectl alias, alias generation rule as follows:
Alias simple example
kd → kubectl describe
Senior alias example
kgdepallw → kubectl get deployment --all-namespaces --watch
Check the configuration file
If you manually write Kubernetes manifest file, the syntax is very difficult to check the manifest file, especially when you have several different versions of Kubernetes cluster, confirm that the configuration file syntax is correct even more difficult.
Kubeval is a tool to check Kubernetes YAML or JSON configuration file to support multiple versions Kubernetes that can help us solve a lot of trouble.
- Examples of Use
$ kubeval nginx.yaml
The document nginx.yaml contains an invalid Deployment
---> spec.replicas: Invalid type. Expected: integer, given: string
Simplify deployment defined Kubernetes
Many people have complained defined Kubernetes manifest file is too complex and lengthy. They are difficult to write and difficult to maintain, if we can simplify deployment definitions will greatly reduce maintenance difficulty.
Kedge provide simpler, more concise syntax, and then convert it to kedge Kubernetes manifest file.
- Examples of Use
Fanger Wei code scanning to add a small assistant, with 8000 native cloud enthusiasts discuss technology trends, advanced combat!
Into the group cipher: Company - Position - City