How to add automatic completion commands for docker and kubectl on Mac

How to add automatic completion commands for docker and kubectl on Mac

Introduction to kubectl

kubectl is a command line tool for operating k8s clusters. It is installed on the master node of k8s. kubectl looks for a file named config in the $HOME/.kube directory. You can specify other kubeconfig files by setting the Kubeconfig environment variable or setting --kubeconfig. Kubectl can add, delete, modify and query various resources in the k8s cluster by interacting with the apiserver. Next, we will introduce the kubectl syntax, command line operations, and common examples. Details on the command, its arguments, and its subcommands can be found in the kubectl reference documentation.

kubectl syntax

The kubectl syntax format is as follows, which can be executed on the master node of the k8s cluster:

kubectl [command] [TYPE] [NAME] [flags]

1 Introduction

Automatic command completion is a very useful feature, especially when the command has a lot of parameters. Apparently, docker/kubectl is such a command. Let's see how to do command completion.

2 View the currently used shell

Check shell supported by the current system:

$ cat /etc/shells 
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

Several ways to view the current shell :

# View environment variables$ echo $SHELL
/bin/bash

# Print process name $ echo $0
-bash

# View the process $ ps
  PID TTY TIME CMD
66162 ttys005 0:00.68 -bash

# Directly type a non-existent command, the error message will show $ pkslow
-bash: pkslow: command not found

3 Add autocompletion for kubectl

Use the command kubectl completion -h to get help:

Check the bash version:

$ bash -version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Copyright (C) 2007 Free Software Foundation, Inc.

Install bash-completion :

brew install bash-completion

Copy kubectl completion bash to the completion directory:

$ kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl

See the statements to add to .bash_prfile :

$ brew info bash-completion
==> Caveats
Add the following line to your ~/.bash_profile:
  [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

Just add the above [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh" to .bash_profile file.

After completing the above steps, enable .bash_profile and you can use it. When using a command, use Tab to complete the command. If the prefix is ​​not uniquely identified, all options will be listed:

$ kubectl get se
secrets serviceaccounts services

4 Add auto-completion for docker commands

With the previous foundation, it is much simpler:

$ cd /usr/local/etc/bash_completion.d
$ ln -s /Applications/Docker.app/Contents/Resources/etc/docker.bash-completion
$ ln -s /Applications/Docker.app/Contents/Resources/etc/docker-machine.bash-completion
$ ln -s /Applications/Docker.app/Contents/Resources/etc/docker-compose.bash-completion

Reopen a terminal and you can use it.

5 Conclusion

For complex commands, it is still necessary to add automatic command completion, which can improve efficiency and reduce errors.

This is the end of this article about adding auto-completion commands for docker and kubectl on Mac. For more information about auto-completion commands for docker and kubectl, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of get commands and usage examples in kubectl
  • Summary of kubectl operation commands and usage instructions

<<:  How to query data from multiple unrelated tables and paging in Mysql

>>:  How to Apply for Web Design Jobs

Recommend

HTML tutorial, understanding the optgroup element

Select the category selection. After testing, IE ...

MySQL master-slave principle and configuration details

MySQL master-slave configuration and principle, f...

How to manage multiple projects on CentOS SVN server

One demand Generally speaking, a company has mult...

How to implement hot deployment and hot start in Eclipse/tomcat

1. Hot deployment: It means redeploying the entir...

CSS border adds four corners implementation code

1.html <div class="loginbody"> &l...

Web Design Principles of Hyperlinks

<br />Related articles: 9 practical tips for...

Native js implements a minesweeper game with custom difficulty

This article example shares the specific code of ...

HTML unordered list bullet points using images CSS writing

Create an HTML page with an unordered list of at l...

Solution to the problem of English letters not wrapping in Firefox

The layout of text has some formatting requiremen...

Detailed tutorial on docker-compose deployment and configuration of Jenkins

Docker-compose deployment configuration jenkins 1...

Solve the problem of garbled Chinese characters in Mysql5.7

When using MySQL 5.7, you will find that garbled ...

MySQL 5.7.20 free installation version configuration method graphic tutorial

I have seen many relevant tutorials on the Intern...