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

Detailed explanation of the use of the <meta> tag in HTML

In the web pages we make, if we want more people ...

Learning Vue instructions

Table of contents 1. v-text (v-instruction name =...

CSS complete parallax scrolling effect

1. What is Parallax scrolling refers to the movem...

Specific use of useRef in React

I believe that people who have experience with Re...

CentOS 6 Compile and install ZLMediaKit analysis

Install ZLMediaKit on centos6 The author of ZLMed...

Detailed explanation of the use of React list bar and shopping cart components

This article example shares the specific code of ...

Detailed tutorial on installing phpMyAdmin on Ubuntu 18.04

We will install phpMyAdmin to work with Apache on...

Using Docker+jenkins+python3 environment to build a super detailed tutorial

Preface: After the automation is written, it need...

HTTP header information interpretation and analysis (detailed summary)

HTTP Header Explanation 1. Accept: Tells the web s...

Modify the boot time of grub in ubuntu

The online search to modify the grub startup time...

Keepalived+Nginx+Tomcat sample code to implement high-availability Web cluster

Keepalived+Nginx+Tomcat to achieve high availabil...

Example code for implementing beautiful clock animation effects with CSS

I'm looking for a job!!! Advance preparation:...