vm.gowatana.jp

NEOにほんごVMware(仮)

vSphere with Tanzu ラボ環境構築。Part-11: kubectl のダウンロード

vSphere with Tanzu を体験するためのラボ環境構築をしたので、kubectl で接続して、コンテナ を Kubernetes の Pod(vSphere Pod)として起動してみます。

スーパーバイザー クラスタに作成した名前空間の配下の管理は、基本的には vSphere 専用の kubectl を利用します。

 

前回はこちら。

 

一連の投稿のまとめはこちら。

 

今回の内容です。

 

クライアント環境について

今回は、kubectl を実行するクライアントとして Linux(VMware Photon OS 3.0)を利用しています。

gowatana [ ~ ]$ cat /etc/photon-release
VMware Photon OS 3.0
PHOTON_BUILD_NUMBER=49d932d

 

Photon OS はデフォルトのパッケージが少ないので、あらかじめ root ユーザで、次の RPM(2つ)を追加インストールしておきます。

  • unzip: kubectl の zip ファイル(vsphere-plugin.zip)の解凍のため。
  • photon-checksum-generator: ハッシュ値確認のため(sha256sum コマンド)

 

RPM パッケージは、tdnf(もしくは yum)コマンドでインストールします。

root@vm01 [ ~ ]# tdnf install -y unzip photon-checksum-generator

 

kubectl のダウンロード ページ

まず、vSphere 専用の kubectl をダウンロードします。

この環境のスーパーバイザー クラスタには、名前空間「lab-ns-01」、「lab-ns-02」を作成してあります。ダウンロード サイトには、「名前空間」のサマリ タブにある「開く」リンクからアクセスできます。

f:id:gowatana:20201116081937p:plain

 

「Kubernetes CLI Tools」ページが開けます。ちなみに、このサイトのアドレスは、Supervisor Control Plane VM (のアドレスをメンバにしている NSX-T の LB の VIP)のものです。

Web ブラウザのアドレスバーを見ると、今回の IP アドレスは、192.168.70.33 になっています。このあとの kubectl での接続先も、この IP アドレスです。

f:id:gowatana:20201116081958p:plain

 

ダウンロードサイトでは、Windows / Linux / Mac OS 用の kubectl と、それぞれの SHA256 ハッシュ値のファイルが提供されます。

f:id:gowatana:20201116082019p:plain

 

OS ごとの kubectl の利用手順も、このページに記載されています。

f:id:gowatana:20201116082038p:plain

 

kubectl のダウンロード ~ ログイン

「Kubernetes CLI Tools」ページにある「CLI PLUGIN LINUX」と「Checksum CLI plugin Linux」から、右クリック メニューなどでダウンロード URL を取得しておきます。そして、今回 kubectl を実行する Linux からダウンロードします。 

 

kubectl の zip ファイル(vsphere-plugin.zip)を、curl でダウンロードします。

gowatana [ ~ ]$ curl -k -L -O https://192.168.70.33/wcp/plugin/linux-amd64/vsphere-plugin.zip

 

あわせて、ハッシュ値のファイル(sha256sum.txt)をダウンロードします。

gowatana [ ~ ]$ curl -k -L -O https://192.168.70.33/wcp/plugin/linux-amd64/sha256sum.txt

 

2つのファイルをダウンロードしたら、SHA256 のハッシュ値でファイルに問題がなそうか確認しておきます。

Photon OS 3.0 では、「Kubernetes CLI Tools」ページとは少し手順が異なり、冒頭での photon-checksum-generator のインストールが必要です。とりあえず実行してみるだけであれば、ハッシュ値確認は省略しても大丈夫です。

gowatana [ ~ ]$ ls
sha256sum.txt  vsphere-plugin.zip
gowatana [ ~ ]$ tdnf install -y photon-checksum-generator
gowatana [ ~ ]$ sha256sum --check sha256sum.txt < vsphere-plugin.zip
vsphere-plugin.zip: OK

 

zunzip でファイルを解凍すると、中に kubectl と、スーパーバイザー クラスタに接続するための kubectl プラグイン(kubectl-vsphere)が入っています。

gowatana [ ~ ]$ unzip vsphere-plugin.zip
Archive:  vsphere-plugin.zip
   creating: bin/
  inflating: bin/kubectl-vsphere
  inflating: bin/kubectl

 

kubectl に PATH を通しておきます。

gowatana [ ~ ]$ export PATH=$(pwd)/bin:$PATH
gowatana [ ~ ]$ which kubectl
/home/gowatana/bin/kubectl

 

今回の kubectl のバージョンです。

gowatana [ ~ ]$ kubectl version --client
Client Version: version.Info{Major:"1", Minor:"17+", GitVersion:"v1.17.4-2+a00aae1e6a4a69", GitCommit:"a00aae1e6a4a698595445ec86aab1502a495c1ce", GitTreeState:"clean", BuildDate:"2020-04-22T11:35:29Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"linux/amd64"}

 

kubectl で Bash の Tab キー補完を利用するためのコマンドラインを実行しておきます。

gowatana [ ~ ]$ source <(kubectl completion bash)

 

ちなみに、Photon OS ではなく RHEL / CentOS などを利用する場合、この Bash 補完機能を利用するためには、bash-completion のインストールと再ログインが必要だったりします。

[root@centos7 ~]# yum install -y bash-completion

 

ダウンロードした kubectl では、プラグインにより、一般的な kubectl にはない「kubectl vsphere ~」といったコマンドが実行できます。

gowatana [ ~ ]$ kubectl vsphere --help
vSphere Plugin for kubectl.

Usage:
  kubectl-vsphere [command]

Available Commands:
  help        Help about any command
  login       Authenticate user with vCenter Namespaces
  logout      Destroys current sessions with all vCenter Namespaces clusters.
  version     Prints the version of the plugin.

Flags:
  -h, --help                     help for kubectl-vsphere
      --request-timeout string   Request timeout for HTTP client.
  -v, --verbose int              Print verbose logging information.

Use "kubectl-vsphere [command] --help" for more information about a command.

 

「kubectl vsphere login」で、Supervisor Control VM のアドレス宛にログインします。

gowatana [ ~ ]$ kubectl vsphere login --help
Authenticate user with vCenter Namespaces:
To access Kubernetes, you must first authenticate against vCenter Namespaces.
You must pass the address of the vCenter Namspaces server and the username of
the user to authenticate, and will be prompted to provide further credentials.

Usage:
  kubectl-vsphere login [flags]

Examples:
kubectl vsphere login --vsphere-username user@domain --server=https://10.0.1.10

Flags:
  -h, --help                                        help for login
      --insecure-skip-tls-verify                    Skip certificate verification (this is insecure).
      --server string                               Address of the server to authenticate against.
      --tanzu-kubernetes-cluster-name string        Name of the Tanzu Kubernetes cluster to login to.
      --tanzu-kubernetes-cluster-namespace string   Namespace in which the Tanzu Kubernetes cluster resides.
  -u, --vsphere-username string                     Username to authenticate.

Global Flags:
      --request-timeout string   Request timeout for HTTP client.
  -v, --verbose int              Print verbose logging information.

 

スーパーバイザー クラスタへのログイン

kubectl で、スーパーバイザー クラスタにログインします。

  • 接続先は vCenter のアドレスではなく、Supervisor Control VM の VIP アドレス「192.168.70.33」です。これは、さきほどの 「Kubernetes CLI Tools」ページと同じアドレスになるはずです。
  • 証明書エラーを回避するため、「--insecure-skip-tls-verify」を指定しています。
  • 今回は、vCenter の名前空間で特にアクセス権限を付与していないので、デフォルトでアクセス可能になっている「administrator@vsphere.local」ユーザでログインします。

kubectl vsphere login を実行すると、次のようになります。

gowatana [ ~ ]$ kubectl vsphere login --insecure-skip-tls-verify --server=192.168.70.33

Username: administrator@vsphere.local ★ユーザ名を入力
Password: ★パスワード入力
Logged in successfully.

You have access to the following contexts:
   192.168.70.33
   lab-ns-01
   lab-ns-02

If the context you wish to use is not in this list, you may need to try
logging in again later, or contact your cluster administrator.

To change context, use `kubectl config use-context <workload name>`

 

利用可能な Context が表示されるので、名前空間「lab-ns-01」にアクセスできる、同名のコンテキストに切り替えます。

gowatana [ ~ ]$ kubectl config use-context lab-ns-01
Switched to context "lab-ns-01".

 

つづく。