Skip to content

Commit 87e3291

Browse files
authored
Update install-kubectl.md to use tabs for the different Operating Systems (#26682)
* top-level tags and change to included files * revert content to main windows page * include gcloud and update toc * add leading blank line * add text to avoid YAML error from Netlify * remove YAML delimiter error * removed redundant weight key * add front matter and toc shortcode * test to see if toc is fixed in included page * update macos toc to show difference * use static toc instead of shortcode * uniqueness for gcloud install headers * Move to model suggested in #26682 (comment) * correct link to included/install-kubectl-gcloud.md * correction of links to per-OS pages * try a different way to use path to per-OS links * correction of brainfart * remove .md from link destination for per-OS pages * modify how auto-competion steps are displayed * remove redundant file * correction of include - change % to < * remove closing tag as not needed * try and hide this directory from the ToC on the left * remove erroneous "include=" * addressing feedback: #26682 (comment) * addressing feedback: #26682 (comment) * addressing feedback: #26682 (comment) * addressing feedback: #26682 (comment) * #26682 (comment) * consistent verb usage as per feedback: #26682 (comment) * update redirects as existing page is being deleted
1 parent b43aafe commit 87e3291

13 files changed

+769
-645
lines changed

content/en/docs/tasks/tools/_index.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ no_list: true
77

88
## kubectl
99

10-
The Kubernetes command-line tool, `kubectl`, allows you to run commands against
11-
Kubernetes clusters. You can use `kubectl` to deploy applications, inspect and
12-
manage cluster resources, and view logs.
13-
14-
See [Install and Set Up `kubectl`](/docs/tasks/tools/install-kubectl/) for
15-
information about how to download and install `kubectl` and set it up for
16-
accessing your cluster.
10+
<!-- overview -->
11+
The Kubernetes command-line tool, [kubectl](/docs/reference/kubectl/kubectl/), allows
12+
you to run commands against Kubernetes clusters.
13+
You can use kubectl to deploy applications, inspect and manage cluster resources,
14+
and view logs. For more information including a complete list of kubectl operations, see the
15+
[`kubectl` reference documentation](/docs/reference/kubectl/).
1716

18-
<a class="btn btn-primary" href="/docs/tasks/tools/install-kubectl/" role="button" aria-label="View kubectl Install and Set Up Guide">View kubectl Install and Set Up Guide</a>
17+
kubectl is installable on a variety of Linux platforms, macOS and Windows.
18+
Find your preferred operating system below.
1919

20-
You can also read the
21-
[`kubectl` reference documentation](/docs/reference/kubectl/).
20+
- [Install kubectl on Linux](install-kubectl-linux)
21+
- [Install kubectl on macOS](install-kubectl-macos)
22+
- [Install kubectl on Windows](install-kubectl-windows)
2223

2324
## kind
2425

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: "Tools Included"
3+
description: "Snippets to be included in the main kubectl-installs-*.md pages."
4+
headless: true
5+
toc_hide: true
6+
---
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: "gcloud kubectl install"
3+
description: "How to install kubectl with gcloud snippet for inclusion in each OS-specific tab."
4+
headless: true
5+
---
6+
7+
You can install kubectl as part of the Google Cloud SDK.
8+
9+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/).
10+
11+
1. Run the `kubectl` installation command:
12+
13+
```shell
14+
gcloud components install kubectl
15+
```
16+
17+
1. Test to ensure the version you installed is up-to-date:
18+
19+
```shell
20+
kubectl version --client
21+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: "What's next?"
3+
description: "What's next after installing kubectl."
4+
headless: true
5+
---
6+
7+
* [Install Minikube](https://minikube.sigs.k8s.io/docs/start/)
8+
* See the [getting started guides](/docs/setup/) for more about creating clusters.
9+
* [Learn how to launch and expose your application.](/docs/tasks/access-application-cluster/service-access-application-cluster/)
10+
* If you need access to a cluster you didn't create, see the
11+
[Sharing Cluster Access document](/docs/tasks/access-application-cluster/configure-access-multiple-clusters/).
12+
* Read the [kubectl reference docs](/docs/reference/kubectl/kubectl/)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: "bash auto-completion on Linux"
3+
description: "Some optional configuration for bash auto-completion on Linux."
4+
headless: true
5+
---
6+
7+
### Introduction
8+
9+
The kubectl completion script for Bash can be generated with the command `kubectl completion bash`. Sourcing the completion script in your shell enables kubectl autocompletion.
10+
11+
However, the completion script depends on [**bash-completion**](https://github.com/scop/bash-completion), which means that you have to install this software first (you can test if you have bash-completion already installed by running `type _init_completion`).
12+
13+
### Install bash-completion
14+
15+
bash-completion is provided by many package managers (see [here](https://github.com/scop/bash-completion#installation)). You can install it with `apt-get install bash-completion` or `yum install bash-completion`, etc.
16+
17+
The above commands create `/usr/share/bash-completion/bash_completion`, which is the main script of bash-completion. Depending on your package manager, you have to manually source this file in your `~/.bashrc` file.
18+
19+
To find out, reload your shell and run `type _init_completion`. If the command succeeds, you're already set, otherwise add the following to your `~/.bashrc` file:
20+
21+
```bash
22+
source /usr/share/bash-completion/bash_completion
23+
```
24+
25+
Reload your shell and verify that bash-completion is correctly installed by typing `type _init_completion`.
26+
27+
### Enable kubectl autocompletion
28+
29+
You now need to ensure that the kubectl completion script gets sourced in all your shell sessions. There are two ways in which you can do this:
30+
31+
- Source the completion script in your `~/.bashrc` file:
32+
33+
```bash
34+
echo 'source <(kubectl completion bash)' >>~/.bashrc
35+
```
36+
37+
- Add the completion script to the `/etc/bash_completion.d` directory:
38+
39+
```bash
40+
kubectl completion bash >/etc/bash_completion.d/kubectl
41+
```
42+
43+
If you have an alias for kubectl, you can extend shell completion to work with that alias:
44+
45+
```bash
46+
echo 'alias k=kubectl' >>~/.bashrc
47+
echo 'complete -F __start_kubectl k' >>~/.bashrc
48+
```
49+
50+
{{< note >}}
51+
bash-completion sources all completion scripts in `/etc/bash_completion.d`.
52+
{{< /note >}}
53+
54+
Both approaches are equivalent. After reloading your shell, kubectl autocompletion should be working.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: "bash auto-completion on macOS"
3+
description: "Some optional configuration for bash auto-completion on macOS."
4+
headless: true
5+
---
6+
7+
### Introduction
8+
9+
The kubectl completion script for Bash can be generated with `kubectl completion bash`. Sourcing this script in your shell enables kubectl completion.
10+
11+
However, the kubectl completion script depends on [**bash-completion**](https://github.com/scop/bash-completion) which you thus have to previously install.
12+
13+
{{< warning>}}
14+
There are two versions of bash-completion, v1 and v2. V1 is for Bash 3.2 (which is the default on macOS), and v2 is for Bash 4.1+. The kubectl completion script **doesn't work** correctly with bash-completion v1 and Bash 3.2. It requires **bash-completion v2** and **Bash 4.1+**. Thus, to be able to correctly use kubectl completion on macOS, you have to install and use Bash 4.1+ ([*instructions*](https://itnext.io/upgrading-bash-on-macos-7138bd1066ba)). The following instructions assume that you use Bash 4.1+ (that is, any Bash version of 4.1 or newer).
15+
{{< /warning >}}
16+
17+
### Upgrade Bash
18+
19+
The instructions here assume you use Bash 4.1+. You can check your Bash's version by running:
20+
21+
```bash
22+
echo $BASH_VERSION
23+
```
24+
25+
If it is too old, you can install/upgrade it using Homebrew:
26+
27+
```bash
28+
brew install bash
29+
```
30+
31+
Reload your shell and verify that the desired version is being used:
32+
33+
```bash
34+
echo $BASH_VERSION $SHELL
35+
```
36+
37+
Homebrew usually installs it at `/usr/local/bin/bash`.
38+
39+
### Install bash-completion
40+
41+
{{< note >}}
42+
As mentioned, these instructions assume you use Bash 4.1+, which means you will install bash-completion v2 (in contrast to Bash 3.2 and bash-completion v1, in which case kubectl completion won't work).
43+
{{< /note >}}
44+
45+
You can test if you have bash-completion v2 already installed with `type _init_completion`. If not, you can install it with Homebrew:
46+
47+
```bash
48+
brew install bash-completion@2
49+
```
50+
51+
As stated in the output of this command, add the following to your `~/.bash_profile` file:
52+
53+
```bash
54+
export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
55+
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
56+
```
57+
58+
Reload your shell and verify that bash-completion v2 is correctly installed with `type _init_completion`.
59+
60+
### Enable kubectl autocompletion
61+
62+
You now have to ensure that the kubectl completion script gets sourced in all your shell sessions. There are multiple ways to achieve this:
63+
64+
- Source the completion script in your `~/.bash_profile` file:
65+
66+
```bash
67+
echo 'source <(kubectl completion bash)' >>~/.bash_profile
68+
```
69+
70+
- Add the completion script to the `/usr/local/etc/bash_completion.d` directory:
71+
72+
```bash
73+
kubectl completion bash >/usr/local/etc/bash_completion.d/kubectl
74+
```
75+
76+
- If you have an alias for kubectl, you can extend shell completion to work with that alias:
77+
78+
```bash
79+
echo 'alias k=kubectl' >>~/.bash_profile
80+
echo 'complete -F __start_kubectl k' >>~/.bash_profile
81+
```
82+
83+
- If you installed kubectl with Homebrew (as explained [above](#install-with-homebrew-on-macos)), then the kubectl completion script should already be in `/usr/local/etc/bash_completion.d/kubectl`. In that case, you don't need to do anything.
84+
85+
{{< note >}}
86+
The Homebrew installation of bash-completion v2 sources all the files in the `BASH_COMPLETION_COMPAT_DIR` directory, that's why the latter two methods work.
87+
{{< /note >}}
88+
89+
In any case, after reloading your shell, kubectl completion should be working.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: "zsh auto-completion"
3+
description: "Some optional configuration for zsh auto-completion."
4+
headless: true
5+
---
6+
7+
The kubectl completion script for Zsh can be generated with the command `kubectl completion zsh`. Sourcing the completion script in your shell enables kubectl autocompletion.
8+
9+
To do so in all your shell sessions, add the following to your `~/.zshrc` file:
10+
11+
```zsh
12+
source <(kubectl completion zsh)
13+
```
14+
15+
If you have an alias for kubectl, you can extend shell completion to work with that alias:
16+
17+
```zsh
18+
echo 'alias k=kubectl' >>~/.zshrc
19+
echo 'complete -F __start_kubectl k' >>~/.zshrc
20+
```
21+
22+
After reloading your shell, kubectl autocompletion should be working.
23+
24+
If you get an error like `complete:13: command not found: compdef`, then add the following to the beginning of your `~/.zshrc` file:
25+
26+
```zsh
27+
autoload -Uz compinit
28+
compinit
29+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: "verify kubectl install"
3+
description: "How to verify kubectl."
4+
headless: true
5+
---
6+
7+
In order for kubectl to find and access a Kubernetes cluster, it needs a
8+
[kubeconfig file](/docs/concepts/configuration/organize-cluster-access-kubeconfig/),
9+
which is created automatically when you create a cluster using
10+
[kube-up.sh](https://github.com/kubernetes/kubernetes/blob/master/cluster/kube-up.sh)
11+
or successfully deploy a Minikube cluster.
12+
By default, kubectl configuration is located at `~/.kube/config`.
13+
14+
Check that kubectl is properly configured by getting the cluster state:
15+
16+
```shell
17+
kubectl cluster-info
18+
```
19+
20+
If you see a URL response, kubectl is correctly configured to access your cluster.
21+
22+
If you see a message similar to the following, kubectl is not configured correctly or is not able to connect to a Kubernetes cluster.
23+
24+
```
25+
The connection to the server <server-name:port> was refused - did you specify the right host or port?
26+
```
27+
28+
For example, if you are intending to run a Kubernetes cluster on your laptop (locally), you will need a tool like Minikube to be installed first and then re-run the commands stated above.
29+
30+
If kubectl cluster-info returns the url response but you can't access your cluster, to check whether it is configured properly, use:
31+
32+
```shell
33+
kubectl cluster-info dump
34+
```

0 commit comments

Comments
 (0)