Docker Swarm - chanandrew96/MyLearning GitHub Wiki
Updated on 2023-08-10
- 確認你的裝置上已安裝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環境
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所指的並非同一樣東西
要使用YAML檔案來部署你的程式到Swarm,請執行docker stack deploy -c [YAML FILE.yaml] [APP NAME]
查看已部署的所有Services docker service ls
執行docker stack rm [APP NAME]
來移除已部署的程式
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的SSH Port
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 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
sudo apt install net-tools
- 停止運行當前的Container
docker stop {container_name}
- Commit你的Container
docerk commit {container_name} {image_name}
* List all running containers
docker ps
docker container ls
* List all containers (Included container not running)
docker ps -a
docker container ls -a
docker start -ai [ID]
每個Container在預設狀況下都給予了一個獨立的網絡,不能與Host或其他Container交流
下面的這個命令會列出所有存在的Network及其對應的Network Drive & Scope
docker network ls
透過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>
sudo service --status-all
* Start Service
sudo service <service-name> start
* Restart
sudo service <service-name> restart
* Status
sudo service <service-name> status
在Docker Hub上提供了一個參考項目 - Docker Swarm Visualizer可以用於提供一個網頁用於顯示當前的Docker Swarm中所使用的節點及在各節點上運行中的程式
docker pull dockersamples/visualizer
透過提供的界面可以更直觀地知道Docker Swarm當前的運行狀態
docker node ls