taxin's notes

読書、勉強メモ etc.

Kubernetes the hard way (GCP版) #10 (リモートアクセス用のkubectl設定)

Kubernetes the hard way (GCP版)の続きです。

前回の記事はこちら taxintt.hatenablog.com

Chap10. Configuring kubectl for Remote Access(リモートアクセス用のkubectl設定)

このチャプターでは、ユーザーがリモートからKubernetesクラスターにアクセスする際に利用するkubectlコマンドの設定を行います。
(ハンズオンの手順も少ないので、今回の記事は比較的短めです。)

Kubernetesの設定ファイル(Adminユーザー用)

ユーザー用のクライアント証明書と鍵ファイルを指定してkubeconfigファイル(接続設定ファイル)を作成します。
kubeconfigファイルの作成時に実行しているコマンドの内容については、Chap5の内容を参考にして頂ければと思います。

taxintt.hatenablog.com

以前のチャプターで作成した証明書・鍵ファイルを用いてkubectl config set-clusterコマンドで通信先を--serverとして指定して、Adminユーザーとしての権限を付与しています。

~/w/k/h/04 ❯❯❯ >{
  KUBERNETES_PUBLIC_ADDRESS=$(gcloud compute addresses describe kubernetes-the-hard-way \
    --region $(gcloud config get-value compute/region) \
    --format 'value(address)')

  kubectl config set-cluster kubernetes-the-hard-way \
    --certificate-authority=ca.pem \
    --embed-certs=true \
    --server=https://${KUBERNETES_PUBLIC_ADDRESS}:6443

  kubectl config set-credentials admin \
    --client-certificate=admin.pem \
    --client-key=admin-key.pem

  kubectl config set-context kubernetes-the-hard-way \
    --cluster=kubernetes-the-hard-way \
    --user=admin

  kubectl config use-context kubernetes-the-hard-way
}
Cluster "kubernetes-the-hard-way" set.
User "admin" set.
Context "kubernetes-the-hard-way" created.
Switched to context "kubernetes-the-hard-way".

検証1

kubectlコマンドの設定が正常にできたかを確認するために、リモートからコマンドを実行します。 kubectl get componentstatusesコマンドを実行して、手順内の想定では下記のような表示になるはずです。

NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok
scheduler            Healthy   ok
etcd-1               Healthy   {"health":"true"}
etcd-2               Healthy   {"health":"true"}
etcd-0               Healthy   {"health":"true"}


しかし、実際に実行したところ下記のような結果になりました。

サーバ側のversion情報を取得できなかったので、Kind(Kubernetes in Docker)で別のクラスタを用意して同じコマンドを実行した際には正常に結果が取得できていました。
そのため、特定のversionのKubernetesクラスターに依存する問題かと考えられます。

github.com

~/w/k/h/04 ❯❯❯ kubectl get componentstatuses
NAME                 AGE
scheduler            <unknown>
controller-manager   <unknown>
etcd-2               <unknown>
etcd-1               <unknown>
etcd-0               <unknown>

~/w/k/h/04 ❯❯❯ kubectl version                                   
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.2", GitCommit:"59603c6e503c87169aea6106f57b9f242f64df89", GitTreeState:"clean", BuildDate:"2020-01-18T23:30:10Z", GoVersion:"go1.13.5", Compiler:"gc", Platform:"darwin/amd64"}
Unable to connect to the server: dial tcp 35.233.237.51:6443: i/o timeout


調べてみたところ、KubernetesのServer versionによってはkubectl get componentstatusesコマンドの出力結果が正常に表示されないケースがあるようです。

github.com

今回は、Chap10の内容をまとめました。