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

MySQL uses the truncate command to quickly clear all tables in a database

1. Execute the select statement first to generate...

Implementation of navigation bar and drop-down menu in CSS

1. CSS Navigation Bar (1) Function of the navigat...

Detailed installation and use of docker-compose

Docker Compose is a Docker tool for defining and ...

Understanding and usage scenarios of ES6 extension operators

Table of contents 1. Replace the apply method, ge...

Detailed example of HTML element blocking Flash

Copy code The code is as follows: wmode parameter...

Debian virtual machine created by VirtualBox shares files with Windows host

the term: 1. VM: Virtual Machine step: 1. Downloa...

A brief discussion on whether CSS animation will be blocked by JS

The animation part of CSS will be blocked by JS, ...

HTML+CSS to achieve surround reflection loading effect

This article mainly introduces the implementation...

A brief discussion on the CSS overflow mechanism

Why do you need to learn CSS overflow mechanism i...

JavaScript function call, apply and bind method case study

Summarize 1. Similarities Both can change the int...

Mysql solves the database N+1 query problem

Introduction In orm frameworks, such as hibernate...

MySQL database operations and data types

Table of contents 1. Database Operation 1.1 Displ...

MySQL 5.7.17 installation and configuration tutorial under CentOS6.9

CentOS6.9 installs Mysql5.7 for your reference, t...