すこぶる.net

技術系備忘録など

kubernetes

kubectl syntax error near unexpected token

投稿日:

kubectl コマンドで syntax error が発生する

今回、MacOS 上で kubernetes を触ろうと、 kubectl をインストールして実行したところ以下のようなエラーに直面しました。

% kubectl
/usr/local/bin/kubectl: line 1: syntax error near unexpected token `<'
/usr/local/bin/kubectl: line 1: `<?xml version='1.0' encoding='UTF-8'?><Error><Code>NoSuchKey</Code><Message>The specified key does not exist.</Message></Error>'

検索して調べてみましたが、同じような問題を持っている方が少なく、いずれの方法も解決しませんでした。

疑った点としては、 zsh のバージョン、 zshrc の記載、 kubectl のバージョン、 kubectl auto-completion などです。

解決方法

同じ問題にあたった方の役に立つかはわかりませんが、以下の方法で問題は解消しました。

% brew remove kubectl
% brew install kubectl
==> Downloading https://homebrew.bintray.com/bottles/kubernetes-cli-1.14.0.mojave.bottle.tar.gz
Already downloaded: /Users/xkx/Library/Caches/Homebrew/downloads/878e8c574c331ba1da58211aee9706d791978fc28007672d7c46118f33271bdc--kubernetes-cli-1.14.0.mojave.bottle.tar.gz
==> Pouring kubernetes-cli-1.14.0.mojave.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/kubectl
Target /usr/local/bin/kubectl
already exists. You may want to remove it:
  rm '/usr/local/bin/kubectl'

To force the link and overwrite all conflicting files:
  brew link --overwrite kubernetes-cli

To list all files that would be deleted:
  brew link --overwrite --dry-run kubernetes-cli

Possible conflicting files are:
/usr/local/bin/kubectl
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/kubernetes-cli/1.14.0: 220 files, 47.9MB

% brew link --overwrite --dry-run kubernetes-cli
Would remove:
/usr/local/bin/kubectl

% brew link --overwrite kubernetes-cli
Linking /usr/local/Cellar/kubernetes-cli/1.14.0... 215 symlinks created

% kubectl
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
~省略~ 

上記は、kubectl をアンインストールして、インストールをやり直していますが、実際のところ関係ありません。問題は、インストールした /usr/local/Cellar/kubernetes-cli/1.14.0… ではなく、 /usr/local/bin/kubectl を参照していることでした。 which コマンドでパスを確認したときもおかしいなとは思っていたのですが、kubectl をアンインストールしてインストールし直したときの出力内容で気づくことができました。

余談ですが、 https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion を参考に自動補完機能をいれてやると作業がはかどります。

-kubernetes
-

執筆者: