こんにちは、ナナオです。
前回の記事でk3sで複数のコントロールプレーンの構築ができました。
今回はまた2台ほど新たにPCをセッティングしたので、ワーカーを追加していきたいと思います。
なお、OSはすべてUbuntu Serverを使用しています。
ワーカーの追加
各ノードにアクセスし、以下のコマンドを実行します。
curl -sfL https://get.k3s.io | K3S_URL=https://<サーバーのIP>:6443 K3S_TOKEN=<トークン> sh -
トークンはコントロールプレーンノードの/var/lib/rancher/k3s/server/node-tokenに格納されているものを使用します。
コマンドの実行に成功すると、kubectl get nodeの結果に追加したワーカーが追加されているはずです。
❯ kubectl get node
NAME STATUS ROLES AGE VERSION
mouse1 Ready control-plane,etcd 43h v1.34.3+k3s1
nanaonuc6caysserver Ready control-plane,etcd 43h v1.34.3+k3s1
nuc2 Ready <none> 62s v1.34.3+k3s1 <- 追加されている!!
thinkcentre1 Ready control-plane,etcd 45h v1.34.3+k3s1
ということで、もう一台にも同じコマンドを実行してあげます。
❯ kubectl get node
NAME STATUS ROLES AGE VERSION
mouse1 Ready control-plane,etcd 43h v1.34.3+k3s1
nanaonuc6caysserver Ready control-plane,etcd 43h v1.34.3+k3s1
nuc2 Ready <none> 3m43s v1.34.3+k3s1
thinkcentle2 Ready <none> 41s v1.34.3+k3s1
thinkcentre1 Ready control-plane,etcd 45h v1.34.3+k3s1
…はい、コントロールプレーンの追加に比べたら驚くほど簡単にセットアップできました。
ただ、このままだと自宅に構築したDockerイメージを格納するプライベートレジストリが使えません。
NASにDockerイメージは格納しているのですが、NASのアクセスに必要なNFSクライアントがまだワーカーノードにインストールされていないからです。
この辺の手順はAnsibleのプレイブックとして自動化しているので、ここに追加しましょう。
hosts.yamlは以下のようにします。
---
all:
vars:
ansible_user: username
ansible_password: password
children:
control-plane:
hosts:
thinkcentle1:
ansible_host: 192.168.0.14
nuc1:
ansible_host: 192.168.0.15
mouse1:
ansible_host: 192.168.0.16
agent:
hosts:
nuc2:
ansible_host: 192.168.0.17
thinkcentle2:
ansible_host: 192.168.0.18
また、プレイブックの修正を行います。
ワーカーノードではk3s-agentサービスを再起動するようにしましょう。
- hosts: all
become: yes
vars:
# デフォルトのサービス名を定義(基本はk3s-agentとする)
k3s_service_name: k3s-agent
tasks:
# control-planeグループに属している場合だけ、変数の中身を書き換える
- name: サービス名の設定(Control Plane用)
set_fact:
k3s_service_name: k3s
when: "'control-plane' in group_names"
- name: nfs-commonのインストール
apt:
name: nfs-common
state: present
update_cache: yes
- name: /etc/rancher/k3s ディレクトリの作成
file:
path: /etc/rancher/k3s
state: directory
mode: '0755'
- name: registries.yaml の配置
copy:
src: ./registries.yaml
dest: /etc/rancher/k3s/registries.yaml
owner: root
group: root
mode: '0644'
notify: restart_k3s_service
handlers:
- name: restart_k3s_service
systemd:
name: "{{ k3s_service_name }}" # 変数を使って動的に切り替え
state: restarted
ignore_errors: yes
実行します。
❯ ansible-playbook -i hosts.yaml setup.yaml -K
BECOME password:
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [all] *************************************************************************************************
TASK [Gathering Facts] *************************************************************************************
[WARNING]: Host 'thinkcentle2' is using the discovered Python interpreter at '/usr/bin/python3.12', but future installation of another Python interpreter could cause a different interpreter to be discovered. See https://docs.ansible.com/ansible-core/2.20/reference_appendices/interpreter_discovery.html for more information.
ok: [thinkcentle2]
[WARNING]: Host 'thinkcentle1' is using the discovered Python interpreter at '/usr/bin/python3.12', but future installation of another Python interpreter could cause a different interpreter to be discovered. See https://docs.ansible.com/ansible-core/2.20/reference_appendices/interpreter_discovery.html for more information.
ok: [thinkcentle1]
[WARNING]: Host 'mouse1' is using the discovered Python interpreter at '/usr/bin/python3.12', but future installation of another Python interpreter could cause a different interpreter to be discovered. See https://docs.ansible.com/ansible-core/2.20/reference_appendices/interpreter_discovery.html for more information.
ok: [mouse1]
[WARNING]: Host 'nuc1' is using the discovered Python interpreter at '/usr/bin/python3.12', but future installation of another Python interpreter could cause a different interpreter to be discovered. See https://docs.ansible.com/ansible-core/2.20/reference_appendices/interpreter_discovery.html for more information.
ok: [nuc1]
[WARNING]: Host 'nuc2' is using the discovered Python interpreter at '/usr/bin/python3.12', but future installation of another Python interpreter could cause a different interpreter to be discovered. See https://docs.ansible.com/ansible-core/2.20/reference_appendices/interpreter_discovery.html for more information.
ok: [nuc2]
TASK [サービス名の設定(Control Plane用)] *****************************************************************
ok: [thinkcentle1]
ok: [nuc1]
ok: [mouse1]
skipping: [nuc2]
skipping: [thinkcentle2]
TASK [nfs-commonのインストール] ****************************************************************************
ok: [thinkcentle1]
ok: [mouse1]
ok: [nuc1]
changed: [thinkcentle2]
changed: [nuc2]
TASK [/etc/rancher/k3s ディレクトリの作成] *****************************************************************
changed: [thinkcentle2]
ok: [thinkcentle1]
ok: [mouse1]
ok: [nuc1]
changed: [nuc2]
TASK [registries.yaml の配置] ******************************************************************************
ok: [thinkcentle1]
ok: [mouse1]
ok: [nuc1]
changed: [thinkcentle2]
changed: [nuc2]
RUNNING HANDLER [restart_k3s_service] **********************************************************************
changed: [thinkcentle2]
changed: [nuc2]
PLAY RECAP *************************************************************************************************
mouse1 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
nuc1 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
nuc2 : ok=5 changed=4 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
thinkcentle1 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
thinkcentle2 : ok=5 changed=4 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
無事成功しました。
感想
ワーカーノードはかなり簡単に追加できてよかったです。
これで更に便利なk8sライフを送れそうです。