Docker Swarm - chanandrew96/MyLearning GitHub Wiki

Updated on 2023-08-10

參考資料:

Prerequisites

  • 確認你的裝置上已安裝Docker Desktop
  • 查看docker swarm 是否已active (執行docker system info並查看輸出是否帶有Swarm: active)
    • 使用docker swarm init來開啟Docker Swarm
    • 當前的Node將會以Manager Node加入

介紹

  • 使用Swarm後,所有的Workload都會以服務(Services)的形式存在
  • 在有Swarm的物件(Object)都可以被stack files所描述及使用YAML來建立或清除Swarm環境

YAML File範例

version: '3.7'

services:
  bb-app:
    image: getting-started
    ports:
      - "8000:3000"

上面的YAML檔案會新建一個service的物件
在上面的Container中我們會使用getting-started的Image
8000:3000會要求Swarm將所有送到8000 port的traffic轉至3000 port

Kubernetes Services和Swarm Services所指的並非同一樣東西

部署你的程式到Swarm

要使用YAML檔案來部署你的程式到Swarm,請執行docker stack deploy -c [YAML FILE.yaml] [APP NAME]
查看已部署的所有Services docker service ls

移除已部署的程式

執行docker stack rm [APP NAME]來移除已部署的程式

在Windows上使用Ubuntu的Docker Container

Reference: How To Install Docker on Ubuntu 20.04 and 22.04

  • docker pull ubuntu提取Ubuntu Image
  • docker run -it ubuntu使用Ubuntu Image新建並運行Container
    • 設定Ubuntu的SSH Port
      緊記Image的名稱必須放於所有Arguments的後方
      docker run -it -p {host_IP}:{host_port}:{container_ssh_port} ubuntu

為Ubuntu Container安裝SSH元件

Reference: Install & Setup for SSH
Reference: Create User

* 進入Ubuntu Container
docker exec -it {containerID_or_name} bash
* 安裝ssh
apt-get update && apt-get install -y ssh
* 重啟Container後,重啟SSH
/etc/init.d/ssh restart
* 新建一個User用於SSH
sudo useradd -s /bin/bash -m {user}
sudo passwd {user}

使用ssh連接 ssh {user}@{IP address} -p {port_number}

為Ubuntu Container安裝Docker元件

由於Ubuntu Image中沒有帶有docker元件,我們需要自行安裝

  • 查看sudo是否已安裝 find /etc/sudoers.d
* Update apt
apt-get update
* Install sudo
apt-get install sudo
* Update the Package Repository
sudo apt update
* Install Prerequisite Packages
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
* Add GPG Key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker-archive-keyring.gpg
* Add Docker Repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
* Specify Installation Source
apt-cache policy docker-ce
* Install Docker
sudo apt install docker-ce -y
* Check Docker Status
sudo systemctl status docker

安裝net-tools以查看IP Address

sudo apt install net-tools

上手Container

以當前的Container新建為一個Image

Reference

  • 停止運行當前的Container
    docker stop {container_name}
  • Commit你的Container
    docerk commit {container_name} {image_name}

查看所有Container

* List all running containers
docker ps
docker container ls 
* List all containers (Included container not running)
docker ps -a
docker container ls -a

啟動一個已存在的Container

docker start -ai [ID]

Networking for Container

每個Container在預設狀況下都給予了一個獨立的網絡,不能與Host或其他Container交流

查看現有的Network

下面的這個命令會列出所有存在的Network及其對應的Network Drive & Scope
docker network ls

使用Host Networking

透過docker network ls我們應該可以看到有一個host的Network,我們可以設定Container使用這個網絡以連接到Host的網絡

* Start container with specific network
docker run --net=host <image-name>
* Update the network use
docker network connect <network-name> <container-name>

Services on Container

查看當前Container上正在運行的服務

sudo service --status-all

Services運作

* Start Service
sudo service <service-name> start
* Restart
sudo service <service-name> restart
* Status
sudo service <service-name> status

Visualizer

在Docker Hub上提供了一個參考項目 - Docker Swarm Visualizer可以用於提供一個網頁用於顯示當前的Docker Swarm中所使用的節點及在各節點上運行中的程式

docker pull dockersamples/visualizer

透過提供的界面可以更直觀地知道Docker Swarm當前的運行狀態
Docker Swarm Visualizer

查看當前Docker Swarm網絡中的所有節點

docker node ls

Reference

Deploy a stack to a swarm
docker swarm和compose 的使用(阿里)

⚠️ **GitHub.com Fallback** ⚠️