はじめに
Docker Swarm は、Docker ホストのクラスタを単一のシステムとして管理できる強力なオーケストレーションツールです。Swarm では、1 つのノードがリーダーとして指定され、クラスタの状態を管理し、決定を行います。ただし、非リーダーノードで管理コマンドを実行する必要がある場合があります。このチュートリアルでは、Docker Swarm でリーダーノードを特定し、非リーダーノードで管理コマンドを実行する方法を説明します。
Docker Swarm アーキテクチャの理解
Docker Swarm は、Docker ホストのクラスタ(「スウォーム」と呼ばれる)を管理するためのコンテナオーケストレーションツールです。Docker Swarm では、マネージャーノードとワーカーノードの 2 種類のノードがあります。
マネージャーノード
マネージャーノードは、スウォーム全体の管理を担当します。それらは、以下のタスクを処理します。
- コンテナのスケジューリングとデプロイ
- スウォームの所望の状態の維持
- スウォームとのインタラクションのための API の提供
マネージャーノードはコンテナを実行することもできますが、その主な役割はスウォームの管理です。
ワーカーノード
ワーカーノードは、実際のコンテナの実行を担当します。それらはマネージャーノードからの指示を受け取り、実行します。ワーカーノードは、クラスタのスケールアップに合わせて、必要に応じてスウォームに追加または削除できます。
graph LR
subgraph Docker Swarm
Manager[マネージャーノード] --- Worker[ワーカーノード]
Manager --- Worker
end
Docker Swarm アーキテクチャは、複数のマネージャーノードがスウォームの一部になることを可能にすることで、高可用性とフォールトトレランスを確保します。このように、1 つのマネージャーノードが障害を起こした場合でも、残りのマネージャーノードを使用してスウォームは動作を続けることができます。
Docker Swarm におけるリーダーノードの特定
Docker Swarm では、マネージャーノードのうちの 1 つがリーダーとして選出されます。リーダーノードは、すべての重要な決定を行い、他のマネージャーノードの動作を調整する役割を担います。
リーダーノードの確認
Docker Swarm でリーダーノードを特定するには、以下のコマンドを使用できます。
docker node ls
このコマンドは、スウォーム内のすべてのノードをリストし、リーダーノードは * 記号でマークされます。
あるいは、特定のノードの詳細情報を取得するために docker node inspect コマンドを使用できます。
docker node inspect < node-id > --pretty
これにより、ノードの役割、状態、リーダーであるかどうかを含むその他の関連情報が表示されます。
リーダー選出の仕組み
Docker Swarm は、マネージャーノード間でリーダーを選出するために Raft コンセンサスアルゴリズムを使用します。リーダーは、決定を行い、他のマネージャーノードの動作を調整する責任があります。
現在のリーダーノードが障害を起こしたり、利用できなくなったりした場合、残りのマネージャーノードから新しいリーダーが自動的に選出されます。これにより、マネージャーノードが障害を起こしても、スウォームが動作を続けることができます。
graph LR
subgraph Docker Swarm
Manager1[マネージャーノード 1] --- Manager2[マネージャーノード 2]
Manager1 --- Manager3[マネージャーノード 3]
Manager2 --- Manager3
Manager1 -- リーダー --> Manager2
Manager2 -- フォロワー --> Manager3
end
Running Admin Commands on Non-Leader Nodes
In a Docker Swarm, the leader node is responsible for making all the critical decisions and coordinating the actions of the other manager nodes. However, there may be situations where you need to run administrative commands on the non-leader nodes, such as for troubleshooting or maintenance purposes.
Accessing Non-Leader Nodes
To access a non-leader node in your Docker Swarm, you can use the docker node ssh command. This command allows you to establish an SSH connection to a specific node in the swarm.
docker node ssh <node-id>
Replace <node-id> with the ID of the non-leader node you want to access.
Running Admin Commands
Once you have established an SSH connection to the non-leader node, you can run administrative commands as needed. Some common admin commands you might want to run include:
- Checking the node's status and role:
docker node inspect < node-id > --pretty - Viewing the node's logs:
docker node logs <node-id> - Executing a command on the node:
docker exec -it <container-id> <command>
Keep in mind that while you can run admin commands on non-leader nodes, any changes or actions you take will not affect the overall state of the swarm. The leader node is still responsible for maintaining the desired state of the cluster.
Limitations
It's important to note that running admin commands on non-leader nodes has some limitations. For example, you cannot use the docker service or docker stack commands on non-leader nodes, as these commands require access to the swarm's state, which is managed by the leader node.
If you need to perform actions that require access to the swarm's state, you should do so on the leader node or use the docker node exec command to execute the command on the leader node from a non-leader node.
非リーダーノードでの管理コマンドの実行
Docker Swarm では、リーダーノードがすべての重要な決定を行い、他のマネージャーノードの動作を調整する役割を担います。ただし、トラブルシューティングやメンテナンスのために、非リーダーノード上で管理コマンドを実行する必要がある場合があります。
非リーダーノードへのアクセス
Docker Swarm で非リーダーノードにアクセスするには、docker node ssh コマンドを使用できます。このコマンドは、スウォーム内の特定のノードに SSH 接続を確立します。
docker node ssh <node-id>
<node-id> を、アクセスしたい非リーダーノードの ID に置き換えます。
管理コマンドの実行
非リーダーノードへの SSH 接続が確立されたら、必要に応じて管理コマンドを実行できます。実行したい一般的な管理コマンドを以下に示します。
- ノードの状態と役割の確認:
docker node inspect < node-id > --pretty - ノードのログの表示:
docker node logs <node-id> - ノード上でコマンドを実行:
docker exec -it <container-id> <command>
非リーダーノード上で管理コマンドを実行できますが、実行する変更や操作は、スウォーム全体の状態には影響しません。リーダーノードは、クラスタの所望の状態を維持する責任を依然として担っています。
制限事項
非リーダーノード上で管理コマンドを実行する場合、いくつかの制限があります。たとえば、docker service や docker stack コマンドは、リーダーノードが管理するスウォームの状態にアクセスする必要があるため、非リーダーノード上では使用できません。
スウォームの状態にアクセスする必要がある操作を実行する必要がある場合は、リーダーノード上で実行するか、非リーダーノードからリーダーノード上で docker node exec コマンドを使用して実行する必要があります。



