1.安装docker环境,参考其他教程
2.初始化docker swarm
docker swarm init [option]3.创建网关 over-lay
docker network create -d overlay swarm-overlay4.登录私有docker私服
sudo docker login -u 'username' -p 'password' registry-vpc.cn-hangzhou.aliyuncs.com5.部署deploy
docker stack deploy -c docker-stack-deloy.yml --with-registry-auth {stackName}6.其他命令
# 查看服务运行情况
docker service ls
# 输出docker swarm service日志
docker service logs -f --tail=100 {serviceName}
# 查看所有堆栈
docker stack ls
# 查看stack内容器运行情况
docker stack ps {stackName}
# 删除指定镜像关联的Exited容器
sudo docker container rm $(sudo docker container ls -a|grep 'registry-vpc.cn-hangzhou.aliyuncs.com/xxxxx'|grep 'Exited'|awk '{print $1}')
# 删除未被容器使用关联指定镜像($GIT_COMMIT 为 jenkins中的session级别的环境变量 )
sudo docker rmi $(sudo docker image ls|grep 'registry-vpc.cn-hangzhou.aliyuncs.com/xxxxx'|grep -v $GIT_COMMIT|awk '{print $3}')
# 横向扩容/收缩
docker service scale {serviceName}=2
- 7.其他 docker-stack-deloy.yml 文件参考 (endpoint_mode 默认为 vip 模式)
version: "3.7"
services:
content-cloud-admin-test:
image: registry-vpc.cn-hangzhou.aliyuncs.com/xxx/xxx:xxxxx
ports:
- "8080:8080"
networks:
swarm-overlay: {}
#volumes:
# - /data/kong/data:/home/kong
volumes:
- ./logs/:/opt/xxx/admin/logs
environment:
- spring.profiles.active=test
- JAVA_OPTS=-Xmx512m -Xms512m -Xss256k -XX:MetaspaceSize=256M -XX:+DisableExplicitGC -XX:+PrintGCDetails -XX:TargetSurvivorRatio=80 -XX:-PrintAdaptiveSizePolicy -XX:+PrintGCDateStamps -XX:SurvivorRatio=8 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=java_heapdump.hprof -Xloggc:gc.log
deploy:
mode: replicated
replicas: 1
#endpoint_mode: dnsrr
endpoint_mode: vip
resources:
limits:
cpus: "1.0"
memory: 1024M
reservations:
cpus: "0.5"
memory: 512M
restart_policy:
condition: on-failure
delay: 5s
window: 60s
update_config:
parallelism: 1
delay: 10s
monitor: 60s
order: start-first
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080"]
interval: 40s
timeout: 5s
retries: 3
start_period: 90s
networks:
swarm-overlay:
driver: overlay
external: true
attachable: true