LoginSignup
0
0

More than 3 years have passed since last update.

kubernetes the hard way on PC (12.kubectl )

Last updated at Posted at 2020-08-02

初めに

kubernetes the hard way を PC でやってみる」の12回目、「kubectl」についてです。( 目次

今回、この一連の記事内では、リモートから kubectl コマンドを実行していないので
今回の作業はなくてもOKではあります。
ただ、せっかくなので、 KVM ホストノード (sakura) から kubectl でアクセスできるように構成します。

kubernetes the hard way では、 Google Cloud の Front end load balancer 経由でアクセスさせていますが、
今回はオンプレで Load Balancer を構成する気力が無かったので、
名前解決でアクセスし分けられるように構成しておき、
もしいずれ Load Balancer を実装することがあれば、名前解決を Load Balancer の IP に変えればよい、という形で構成します。

49.png

やることは以下の通りです。
今回は KVM ホストノードで実施しています

  • 名前解決の設定
  • kubectl バイナリの配置
  • 秘密鍵・証明書の取得
  • kubernetes の configuration ファイルの作成
  • アクセス検証

名前解決の設定

ホストノード (sakura) の /etc/hosts に k8sapi という名前解決を設定します。
下記の例では k8smaster0 を指していますが、 k8smaster0,1,2 どれでも OK です。
(※ロードバランサーが設定されている場合は当然そこを指します)

# cat /etc/hosts
...(略)
192.168.199.200         k8smaster0 k8sapi
192.168.199.201         k8smaster1
192.168.199.202         k8smaster2

kubectl バイナリの配置

第10回で、 kubernetes の各種バイナリを取得していますが、
その中から kubectl を ホストノードの /usr/local/bin に配置します。

[root@sakura /]# which kubectl
/usr/local/bin/kubectl

秘密鍵・証明書の取得

kubectl で利用する秘密鍵や証明書を取得し、作業用ディレクトリに配置します。

# pwd
/root

# ls
admin-key.pem  admin.pem  ca.pem

kubernetes の configuration ファイルの作成

configuration ファイルを作成します。
kubernetes the hard way では、 set-cluster コマンドの際には --embed-certs オプションを指定していますが
set-credential、set-context の際には --embed-certs を付けていません。
(つけていない場合、config ファイルの証明書・秘密鍵部分にはファイルのパスが指定されます。
--embed-certs を付けると証明書等の内容が取り込まれます)

なぜ kubernetes the hard way では --embed-certs を付けたり付けなかったりしているのかわかりませんが
ここではすべて付けて試してみます。)

# kubectl config set-cluster kubernetes-the-hard-way \
>     --certificate-authority=ca.pem \
>     --embed-certs=true \
>     --server=https://k8sapi:6443
Cluster "kubernetes-the-hard-way" set.

(※ --server オプションでは、名前解決設定した k8sapi を指定しています)

参考に、ですが、 この時点で $HOME/.kube ディレクトリと、その中の config ファイルが作成されます。

[root@sakura ~]# cd .kube/
[root@sakura .kube]# ls
config
[root@sakura .kube]# ls -l
合計 4
-rw------- 1 root root 1965  8月  2 19:50 config
# cat config
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRU...(略)...==
    server: https://k8sapi:6443
  name: kubernetes-the-hard-way
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []

certificate-authority-date や server に設定が入っていますが、
contexts, users には何も入っていませんね。

次に、 credential 設定コマンドを実行します。
(kubernetes the hard way とは違い、 --embed-certs=true を付けています。

# kubectl config set-credentials admin \
>     --client-certificate=admin.pem \
>     --client-key=admin-key.pem \
>     --embed-certs=true
User "admin" set.

この際なので、config ファイルも見ておきましょう。

# cat config
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1...(略)...==
    server: https://k8sapi:6443
  name: kubernetes-the-hard-way
contexts: []
current-context: ""
kind: Config
preferences: {}
users:
- name: admin
  user:
    client-certificate-data: LS0tLS1...(略)...0tLS0K
    client-key-data: LS0tLS1...(略)...==

user 部分に admin の証明書等の情報が入りましたね。
なお、 --embed-certs=true を入れていないと、下記のようになります。

users:
- name: admin
  user:
    client-certificate: /root/admin.pem
    client-key: /root/admin-key.pem

次に、 context 設定コマンドを実行します。

# kubectl config set-context kubernetes-the-hard-way \
>   --cluster=kubernetes-the-hard-way \
>   --user=admin
Context "kubernetes-the-hard-way" created.

config ファイルです。(一部)

# cat config
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1...(略)...==
    server: https://k8sapi:6443
  name: kubernetes-the-hard-way
contexts:
- context:
    cluster: kubernetes-the-hard-way
    user: admin
  name: kubernetes-the-hard-way
current-context: ""
kind: Config
preferences: {}
users:
- name: admin
...(略)

context 部分に、 context 名とユーザー名が入りましたね。

最後に、利用する context を設定します。

# kubectl config use-context kubernetes-the-hard-way
Switched to context "kubernetes-the-hard-way".

config ファイルに current-context が入りました。

contexts:
- context:
    cluster: kubernetes-the-hard-way
    user: admin
  name: kubernetes-the-hard-way
current-context: kubernetes-the-hard-way

さて、最後に kubectl config view を見てみましょう。

# kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://k8sapi:6443
  name: kubernetes-the-hard-way
contexts:
- context:
    cluster: kubernetes-the-hard-way
    user: admin
  name: kubernetes-the-hard-way
current-context: kubernetes-the-hard-way
kind: Config
preferences: {}
users:
- name: admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

証明書等の部分は DATA+OMITTED、REDACTED となっていて見えないですが、
その他の情報は確認できますね。

検証

では kubectl コマンドが利用できるかどうか確認してみます。
kubernetes the hard way では get component status を実施していますが、
v1.16 ではエラーになるので、代わりに get node で確認します。

# kubectl get nodes -o wide
NAME         STATUS   ROLES    AGE   VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE           KERNEL-VERSION     CONTAINER-RUNTIME
k8sworker0   Ready    <none>   85d   v1.16.9   192.168.199.210   <none>        Ubuntu 20.04 LTS   5.4.0-40-generic   containerd://1.3.4
k8sworker1   Ready    <none>   85d   v1.16.9   192.168.199.211   <none>        Ubuntu 20.04 LTS   5.4.0-40-generic   containerd://1.3.4
k8sworker2   Ready    <none>   85d   v1.16.9   192.168.199.212   <none>        Ubuntu 20.04 LTS   5.4.0-42-generic   containerd://1.3.4

無事に見えました。(なお、 ホームディレクトリに配置した 各種証明書等のファイルを消しても問題ありません。)
--embed-certs を付けていない場合は当然エラーになります。

# kubectl get nodes
Error in configuration:
* unable to read client-cert /root/admin.pem for admin due to open /root/admin.pem: no such file or directory
* unable to read client-key /root/admin-key.pem for admin due to open /root/admin-key.pem: no such file or directory

今回はここまでとして、次回は Provisioning Pod Network Routes の部分を実施します。

11.Worker Nodes
目次
13.Pod Network


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0