13

Ok, I installed kubectl in the following way on my Mac: 1) installed gcloud using homebrew 2) installed kubectl using gcloud components install.

I want to run a shell script that calls kubectl directly. However, I get an error. $ kubectl version -bash: kubectl: command not found

I expected gcloud components install to set path variables so that I can call kubectl. Looks like that has not happened. I searched for kubectl in my mac but was not able to find it.

How can I get kubectl to work from command line?

Yuva Athur
  • 131
  • 1
  • 4
  • Just to doublecheck: Did you execute `gcloud components install kubectl` as the [documentation states](https://cloud.google.com/kubernetes-engine/docs/quickstart) (the last parameter is missing in your question)? Alternatively it may work installing the kubernetes cli yourself: https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-with-homebrew-on-macos ? – Capricorn Jul 19 '18 at 18:43
  • 1
    Yes, I executed gcloud components install kubectl . I was hoping to not have several instances of kubectl on my machine. – Yuva Athur Jul 19 '18 at 21:45

4 Answers4

16

Short answer:

On macOS, you may need to add a symlink: sudo ln /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin/kubectl /usr/local/bin/kubectl

Long answer:

I believe this is caused by installing kubectl via homebrew, then via gcloud, and then uninstalling the homebrew managed tool. homebrew will remove its symlink but gcloud doesn't add it back even when you reinstall kubectl.

To see if this is affecting you on macOS:

  • See if gcloud has installed kubectl: gcloud info | grep -i kubectl

    • If you are having the problem I am, I'd expect to see the output look something like this:
        kubectl: [2019.05.31]
      Kubectl on PATH: [False]
      
    • When working you should see something like this:
        kubectl: [2019.05.31]
      Kubectl on PATH: [/usr/local/bin/kubectl]
        /usr/local/bin/kubectl
      
  • Check for the symlink: ls -la /usr/local/bin | grep -i google-cloud-sdk. That will show your links to google cloud binaries.

    • If kubectl isn't on the list then run sudo ln /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin/kubectl /usr/local/bin/kubectl
Mark
  • 606
  • 7
  • 12
1

The gcloud info command will tell you if and where kubectl is installed.

Per https://kubernetes.io/docs/tasks/tools/install-kubectl/, you can install kubectl with brew install kubernetes-cli. Alternatively, you can install the Google Cloud SDK per https://cloud.google.com/sdk/docs/quickstart-macos, and then install kubectl with gcloud components install kubectl.

  • 1
    I did the second - installed using [gcloud components install kubectl]. After this step, I am wondering how to get calls to kubectl work since bash informs me that command is not found. – Yuva Athur Jul 19 '18 at 21:47
  • Can you share the output of `gcoud info` (with any sensitive data removed)? – Harrison Gregg Jul 22 '18 at 02:48
  • I decided to `brew install kubernetes-cli`. However, I did run `gcloud info` that returns `Installation Root: [/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk] Installed Components: kubectl: [] gsutil: [4.33] bq: [2.0.34] core: [2018.07.16] Cloud SDK on PATH: [False] Kubectl on PATH: [/usr/local/bin/kubectl] `. – Yuva Athur Jul 23 '18 at 19:37
0

Just to update and add more clarity around this:

If you did not install kubectl via Homebrew but chose to do

gcloud components install kubectl

instead then the binary is installed inside the bin folder of your gcloud install folder. Even if your gcloud bin folder is already on your shell's PATH it wont see kubectl right away unless you start a new shell or run hash -r (bash / zsh) to tell your shell to discard its cached paths.

Eno
  • 10,730
  • 18
  • 53
  • 86
0

Thanks to @mark, I was able to sort this out, but I feel modifying the $PATH is a better approach. I also found that the Caskroom folder was in a different spot. So I recommend adding this to the relevant rc file:

export PATH="`brew --prefix`/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin:$PATH"
nomadic_squirrel
  • 614
  • 7
  • 21