问题:成本高、部署慢、浪费资源、硬件限制、不利于迁移扩展
虚拟化时代
优点:相对利用好资源,相对容易扩展
缺点:虚拟机太重了,占用较多的物理资源,移植性差,资源利用率低。
容器时代
Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.
Docker的优势和应用场景
what is Image
A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings
what is container
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliable from one computing environment to another.
Relation between image and container
Container image become container at runtime and in the case of Docker containers- images become containers when they run on Docker Engine
A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.
By contrast, a virtual machine runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.
Docker Engine is a client-server application with these major components:
Docker user a client-service architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daem can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate useing a REST API, over UNIX sockerts or a network interface.
虚拟环境准备:
VirtualBox-6.1.26-145957-Win
vagrant_2.2.18_x86_64
如果版本不对可能会导致无法创建虚拟机
01 进入centos7
vagrant ssh
02 卸载之前的docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
03 安装必要的依赖
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
04 设置docker仓库
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
镜像加速器:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://kgnevhxa.mirror.aliyuncs.com"]
}
EOF
05 安装docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
06 启动docker
sudo systemctl start docker
docker pull 拉取镜像到本地,镜像地址:hub.docker.com(默认)
docker run 根据某个镜像创建容器
-d 让容器在后台运行,其实就是一个进程
--name 给容器指定一个名字
-p 将容器的端口映射到宿主机的端口
--memory memory limit 如果不限制,会无限制的使用物理机资源
--cpu-shares 权重
docker logs -f 查看日志
docker exec -it 进入到某个容器中并交互式运行
docker ps 查询运行的容器
-a 查询运行容量历史记录
docker rmi -f 删除镜像
docker rm -f $(docker ps -aq) 删除全部容器
docker images 查看本地image列表
docker images ls 查看本地image列表
docker image rm imageid 删除镜像
docker stop/start container 停止/启动容器
docker ispect container 查看容器详情信息
docker stats 查看容器资源使用情况
01 创建tomcat容器
docker pull tomcat
docker run -d --name my-tomcat -p 9090:8080 tomcat
02 创建mysql容器
docker run -d --name my-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=jack123 --privileged mysql
do'c
03 进入到容器里面
docker exec -it containerid /bin/bash
04 查看资源
docker stats
FROM
指定基础镜像,例如ubuntu:14.04
FROM ubuntu:14.04
RUN
在镜像内部执行一些命令,例如安装软件,配置环境等
RUN groupadd -r mysql && useradd -r -g mysql mysql
ENV
设置变量值,ENV MYSQL_MAJOR5.7, 可以通过docker run --e key=value修改,后面可以直接使用${MYSQL_MAJOR}
ENV MYSQL_MAJOR 5.7
LABEL
设置镜像标签
LABEL emali="itcrazy2022@126.com"
LABEL name="itcrazy2022"
VOLUME
指定数据的挂在目录
VOLUME /var/lib/mysql
COPY
将主机的文件复制到镜像内,如果目录不存在,会自动创建所需要的目录。注意只是复制,不会提取和解压
COPY docker-entrypoint.sh /usr/local/bin
ADD
将主机的文件复制到镜像内,和COPY类似,只是ADD会对压缩文件进行提取和解压
ADD application.yml /etc/itcrazy2022
WORKDIR
指定镜像的工作目录,之后的命令都是基于此目录工作,若不存在则创建
WORKDID /usr/local
WORKDED tomcat
RUN touch test.txt
会在/usr/local/tomcat下创建test.txt文件
WORKDED /root add app.yml test/
会在/root/test下多出一个app.yml文件
CMD
容器启动的时候默认会执行的命令,若有多个CMD命令,则最后一个生效
CMD ["mysql"]
或
CMD mysql
ENTRYPOINT
和CMD的使用类似,不同的是docker run执行时,会覆盖CMD的命令,而ENTRYPOINT不会
EXPOSE
指定镜像要暴露的端口,启动镜像时,可以使用-p将该端口映射给宿主机
EXPORT 3306
创建一个Spring Boot项目
写一个controller
@RestController
public class DockerController {
@GetMapping("/dockerfile")
@ResponseBody String dockerfile() {
return "hello docker" ;
}
}
mvn clean package打成一个jar包
在target下找到"dockerfile-demo-0.0.1-SNAPSHOT.jar"
在docker环境中新建一个目录"first-dockerfile"
上传"dockerfile-demo-0.0.1-SNAPSHOT.jar"到该目录下,并且在此目录创建Dockerfile
创建Dockerfile文件,编写内容
FROM openjdk:8
MAINTAINER itcrazy2016
LABEL name="dockerfile-demo" version="1.0" author="itcrazy2016"
COPY dockerfile-demo-0.0.1-SNAPSHOT.jar dockerfile-image.jar
CMD ["java","-jar","dockerfile-image.jar"]
基于Dockerfile构建镜像
docker build -t test-docker-image
基于image创建container
docker run -d --name user01 -p 6666:8080 test-docker-image
查看启动日志
docker logs user01
宿主机上访问curl localhost:6666/dockerfile
hello docker
还可以再次启动一个
docker run -d --name user02 -p 8081:8080 test-docker-image
在docker机器上登录
docker login
输入用户名和密码
docker push itcrazy2022/test-docker-image[镜像名称要与docker id一致]
给image重命名,并删除原来的
docker tag test-docker-image itcrazy2018/test-docker-image
docker rmi -f test-docker-image
再次推送,刷新hub.docker.com后台,发现成功
别人下载,并且运行
docker pull itcrazy2018/test-docker-image
docker run -d --name user01 -p 6661:8080 itcrazy2018/test-docker-image
登录到阿里云docker仓库
sudo docker login --username=itcrazy2016@163.com registry.cn-hangzhou.aliyuncs.com
输入密码
创建命名空间,例如itcrazy2022
给image打tag
sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test- docker-image:v1.0
推送镜像到docker阿里云仓库
sudo docker push registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test-docker- image:v1.0
下载且运行
docker pull registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test-docker- image:v1.0
docker run -d --name user01 -p 6661:8080 registry.cn- hangzhou.aliyuncs.com/itcrazy2016/test-docker-image:v1.0
访问github上的harbor项目,并下载
https://github.com/goharbor/harbor
https://github.com/goharbor/harbor/releases
找一台安装了docker-compose[这个后面的课程会讲解],上传并解压
tar -zxvf xxx.tar.gz
进入到harbor目录
修改harbor.cfg文件,主要是ip地址的修改成当前机器的ip地址
同时也可以看到Harbor的密码,默认是Harbor12345
安装harbor
sh install.sh
浏览器访问,比如39.100.39.63,输入用户名和密码即可
https://github.com/weaveworks/scope
sudo curl -l git.io.scope -o /usr/local/bin/scope
sudo chmod a+x /usr/local/bin/scope
scope launch 39.100.39.63
# 停止scope
scope stop
# 同时监控两台机器,在两台机器中分别执行
scope launch ip1 ip2
Container是一种轻量化的虚拟化技术们不用模拟硬件创建虚拟机。
Docker是基于Linux Kernel的Namespace、CGroups、UnionFileSystem等技术封装成一种自定义容器格式,从而提供一套虚拟运行环境
Namespace:用来做隔离的,比如pid[进程]、net[网络]、mnu[挂载点]等
CGroups:Controller Groups用来做资源限制,比如内存和CPU等
Uniont file systems:用来做image和container分层
查看网卡命令
ip a
ip link show
ls /sys/class/net
网卡信息
eth0: mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:16:3e:21:4c:7c brd ff:ff:ff:ff:ff:ff
inet 172.29.12.105/20 brd 172.29.15.255 scope global dynamic noprefixroute eth0
valid_lft 307768225sec preferred_lft 307768225sec
inet6 fe80::216:3eff:fe21:4c7c/64 scope link
valid_lft forever preferred_lft forever
state: 网卡状态,UP/DOWN/UNKOWN
link/ether: MAC地址(每一个网卡都有一个唯一的MAC地址)
inet: 绑定的IP地址
网卡配置信息
/etc/systemconfig/network-scripts
网卡只有绑定IP地址后才能进行网络通信
//网卡绑定IP
ip addr add 192.168.0.100/24 dev eth0
//网卡删除绑定
ip addr delete 192.168.0.100/24 dev eth0
网卡的状态:UP/DOWN/UNKOWN
网卡启动与关闭
//重启网卡
service network restart / systemctl restart network
//启动/关闭
ifup/ifdown eth0 / ip link set eth0 up/down
在Linux上,网络的隔离是通过network namespace来管理的,不同的network namespace是相互隔离的
ip netns list # 查看当前机器上的namespace
ip netns add ns1 # 添加
ip netns delete ns1 # 删除
查看/操作namespace下的网卡
ip netns exec ns1 ip a #查看
ip netns exec ns1 ifup lo
ip netns exec ns1 ip link set lo up #启动网卡
Virtual Ethernet pair 是一个成对的端口,可以实现两个namespace网络的连接
ip link add veth-ns1 type veth peer name veth-ns2 # 创建一对link
ip link # 查看link情况
ip link set veth-ns1 netns ns1
ip link set veth-ns1 netns ns2 #将veth-ns2加入ns2
ip netns exec ns1 ip link set veth-ns1 up
ip netns exec ns2 ip link set veth-ns2 up # 启动网卡
实际上每个Container都会有自己的network namesaace,并且是独立的,他们之间的通信是通过Bridge(桥接)实现的。
# 创建一个network ,类型为bridge
docker network create tomcat-net
or
docker network create --subnet= 172.18.0.1/24 tomcat-net
# 查询已有的network
docker network ls
#查看tomcat-net详情
docker network inspect tomcat-net
# 在指定的network中创建容器
docker run -d --name custom-net-tomcat --network tomcat-net tomcat
# 查看custom-net-tomcat的网络信息
docker exec -it custom-net-tomcat ip a
# tomcat01 容器连接tomcat-net
docker network connect tomcat-net tomcat01
# 创建mysql的container
# "-v mysql01_volume:/var/lib/mysql"表示给上述的volume起一个能识别的名字
docker run -d --name mysql01 -v mysql01_volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD -p 3316:3306 mysql
# 查看volume
docker volume ls
# 持久化
docker volume inspect mysql01_volume
持久化验证
# 进入容器
docket exec -it mysql01 /bin/bash
#登录服务
mysql -uroot -p123
#创建测试数据库
create database db_test
#退出mysql 退出mysql container
#删除mysql容器
docker rm -f mysql01
#查看volume
docket volume ls
#新建一个mysql container,并指定使用“mysql01_volume”
docker run -d --name mysql01 -v mysql01_volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 mysql
# 创建tomcat容器
docket run -d --name tomcat01 -p 9090:8080 -v /tmp/test:/usr/local/tomcat/webapps/test tomcat
# 查看两个目录
centos: cd /tmp/test
tomcat容器:/usr/local/tomcat/webapps/test
# 在centos的/tmp/test中新建1.html文件,进入tomcat上也会存在这个文件。
01 拉取pxc镜像
docker pull percona/percona-xtradb-cluster:5.7.21
02 镜像重命名
docker tag percona/percona-xtradb-cluster:5.7.21 pxc
03 删除原来的镜像
docker rmi percona/percona-xtradb-cluster:5.7.21
04 创建单独的网段,给mysql集群使用
- docker network create --subnet=172.18.0.0/24 pxc-net
- docker network inspect pxc-net
- docker network rm pxc-net
05 创建和删除volume
创建:docker volume crerate --name v1
删除:docker volume rm v1
查看详情:docker volume inspect v1
06 创建单个pxc容器demo
[CLUSTER_NAME PXC容器名称]
[XTRABACKUP_PASSWORD 数据库同步需要使用的密码]
docker run -d -p 3301:3306
-v V1:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123
-e CLUSTER_NAME=PXC
-e XTRABACKUP_PASSWORD=123
--privileged
--name=node1
--net=pxc-net
--ip 172.18.0.2
pxc
07 搭建PXC[MYSQL]集群
- 准备3个数据卷
docker volumer create --name v1
docker volumer create --name v2
docker volumer create --name v3
- 运行3个PXC容器
docker run -d -p 3301:3306
-v v1:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123
-e CLUSTER_NAME=PXC
-e XTRABACKUP_PASSWORD=123
--privileged
--name=node1
--net=pxc-net
--ip 172.18.0.2
pxc
[CLUSTER_JOIN将该数据库加入到某个节点上组成集群]
docker run -d -p 3302:3306
-v v2:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123
-e CLUSTER_NAME=PXC
-e XTRABACKUP_PASSWORD=123
-e CLUSTER_JOIN=node1
--privileged
--name=node2
--net=pxc-net
--ip 172.18.0.3
pxc
docker run -d -p 3303:3306
-v v2:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123
-e CLUSTER_NAME=PXC
-e XTRABACKUP_PASSWORD=123
-e CLUSTER_JOIN=node1
--privileged
--name=node3
--net=pxc-net
--ip 172.18.0.4
pxc
数据库的负载均衡
01 拉取haproxy镜像
docker pull haproxy
02 创建haproxy配置文件,使用bind mounting的方式
touch /tmp/haproxy/haproxy.cfg
03 创建haproxy容器
docker run -d
-p 8888:8888
-p 3306:3306
-v /tmp/haproxy:/usr/local/etc/haproxy
--name haproxy01
--privileged
--net=pxc-net
haproxy
04 根据haproxy.cfg文件启动haproxy
docker exec -it haproxy01 bash
haproxy -f /usr/local/etc/haproxy/haproxy.cfg
05 在mysql数据库上创建用户,用于心跳检测
create user 'haproxy'@'%' identified by '';
[小技巧[如果创建失败,可以先输入一下命令]:
drop user 'haproxy'@'%';
flush privileges;
CREATE USER 'haproxy'@'%' IDENTIFIED BY '';
]
06 浏览器访问
http://centos_ip:8888/dbs_monitor
用户名密码都是:admin
07 datagrip连接haproxy01
ip:centos_ip
port:3306
user:root
password:123
haproxy.cfg
global
#工作目录,这边要和创建容器指定的目录对应
chroot /usr/local/etc/haproxy
#日志文件
log 127.0.0.1 local5
#守护进程运行
daemon
defaults
log global
mode http
#日志格式
option httplog
#日志中不记录负载均衡的心跳检测记录
option dontlognull
#连接超时(毫秒)
timeout connect 5000
#客户端超时(毫秒)
timeout client 50000
#服务器超时(毫秒)
timeout server 50000
#监控界面
listen admin_stats
#监控界面的访问的IP和端口
bind 0.0.0.0:8888
#访问协议
mode http
#URI相对地址
stats uri /dbs_monitor
#统计报告格式
stats realm Global\ statistics
#登陆帐户信息
stats auth admin:admin
#数据库负载均衡
listen proxy-mysql
#访问的IP和端口,haproxy开发的端口为3306
#假如有人访问haproxy的3306端口,则将请求转发给下面的数据库实例
bind 0.0.0.0:3306
#网络协议
mode tcp
#负载均衡算法(轮询算法)
#轮询算法:roundrobin
#权重算法:static-rr
#最少连接算法:leastconn
#请求源IP算法:source
balance roundrobin
#日志格式
option tcplog
#在MySQL中创建一个没有权限的haproxy用户,密码为空。
#Haproxy使用这个账户对MySQL数据库心跳检测
option mysql-check user haproxy
server MySQL_1 172.18.0.2:3306 check weight 1 maxconn 2000
server MySQL_2 172.18.0.3:3306 check weight 1 maxconn 2000
server MySQL_3 172.18.0.4:3306 check weight 1 maxconn 2000
#使用keepalive检测死链
option tcpka
docker run -d -p 3301:3306 -v v1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=123 --privileged --name=node1 --net=pxc-net --ip 172.18.0.2 pxc
docker run -d -p 3302:3306 -v v2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=123 -e CLUSTER_JOIN=node1 --privileged --name=node2 --net=pxc-net --ip 172.18.0.3 pxc
第一步:网络划分
# 创建网络
docker network create --subnet=172.19.0.0/24 pro-net
第二步:mysql
# 创建持久化空间
docker volume create v1
#运行mysql
docker run -d -p 3301:3306 -v v1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 --net=pro-net --ip 172.19.0.6 mysql
# 也可以直接使用mysql高可用集群,需要将haproxy的网络加载到pro-net中
docker network connect pro-net haproxy01
第三步:spring boot项目
# 在docker环境中新建一个目录"springboot-mybatis",上传jar包到该目录下
# 创建Dockerfile文档
FROM openjdk:8-jre-alpine
MAINTAINER itcrazy2016
LABEL name="springboot-mybatis" version="1.0" author="itcrazy2016"
COPY springboot-mybatis-0.0.1-SNAPSHOT.jar springboot-mybatis.jar
CMD ["java","-jar","springboot-mybatis.jar"]
# 基于Dockerfile构建镜像
docker build -t sbm-image .
# 基于image创建container
docker run -d --name sb01 -p 8801:8080 --net pro-net --ip=172.19.0.11 sbm-image
docker run -d --name sb02 -p 8802:8080 --net pro-net --ip=172.19.0.12 sbm-image
docker run -d --name sb03 -p 8803:8080 --net pro-net --ip=172.19.0.13 sbm-image
第四步:Nginx
# 下载nginx容器
docker pull nginx
# 启动容器
docker run -d -p 80:80 -v /tmp/nginx/nginx.conf:/etc/nginx/nginx.conf --net=pro-net --ip 172.19.0.10
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
location / {
proxy_pass http://balance;
}
}
upstream balance{
server 172.18.0.11:8080;
server 172.18.0.12:8080;
server 172.18.0.13:8080;
}
include /etc/nginx/conf.d/*.conf;
}
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# 查看版本
docker-compose version
# 根据yml创建service
docker-compose up
docker-compose -f xxx.ymal up -d
# 查看启动成功的service
docker-compose ps
docker ps
# 查看images
docker-compose images
# 停止/启动service
docker-compose stop/start
# 删除service,同时会删除network和volume
docker-compose down
# 进入service
docker-compose exec redis sh
ersion: '3'
services:
pxc-master:
image: pxc
container_name: node1
privileged: true
ports:
- 3301:3306
environment:
- MYSQL_ROOT_PASSWORD=123
- CLUSTER_NAME=PXC
- XTRABACKUP_PASSWORD=123
volumes:
- v1:/var/lib/mysql
networks:
pxc-network:
ipv4_address: 172.18.0.2
pxc-follow1:
image: pxc
container_name: node2
privileged: true
ports:
- 3302:3306
environment:
- MYSQL_ROOT_PASSWORD=123
- CLUSTER_NAME=PXC
- XTRABACKUP_PASSWORD=123
- CLUSTER_JOIN=node1
volumes:
- v2:/var/lib/mysql
networks:
pxc-network:
ipv4_address: 172.18.0.3
pxc-follow2:
image: pxc
container_name: node3
privileged: true
ports:
- 3303:3306
environment:
- MYSQL_ROOT_PASSWORD=123
- CLUSTER_NAME=PXC
- XTRABACKUP_PASSWORD=123
- CLUSTER_JOIN=node1
volumes:
- v3:/var/lib/mysql
networks:
pxc-network:
ipv4_address: 172.18.0.4
haproxy:
image: haproxy
container_name: haproxy
privileged: true
ports:
- 3306:3306
- 8888:8888
volumes:
- /tmp/haproxy:/usr/local/etc/haproxy
networks:
- pxc-network
networks:
pxc-network:
ipam:
driver: default
config:
- subnet: "172.18.0.0/24"
volumes:
v1:
v2:
v3:
version: '3'
services:
pxc-master:
image: pxc
container_name: node1
privileged: true
ports:
- 3301:3306
environment:
- MYSQL_ROOT_PASSWORD=123
- CLUSTER_NAME=PXC
- XTRABACKUP_PASSWORD=123
volumes:
- v1:/var/lib/mysql
networks:
pxc-net:
ipv4_address: 172.18.0.2
pxc-follow:
image: pxc
container_name: node2
privileged: true
ports:
- 3302:3306
environment:
- MYSQL_ROOT_PASSWORD=123
- CLUSTER_NAME=PXC
- XTRABACKUP_PASSWORD=123
- CLUSTER_JOIN=node1
volumes:
- v2:/var/lib/mysql
networks:
pxc-net:
ipv4_address: 172.18.0.3
haproxy:
image: haproxy
container_name: haproxy01
privileged: true
ports:
- 3306:3306
- 8888:8888
volumes:
- /tmp/haproxy:/usr/local/etc/haproxy
networks:
- pxc-net
- pro-net
springboot:
build: ./springboot-mysql
container_name: sbm
ports:
- 8801:8080
networks:
- pro-net
nginx:
image: nginx
container_name: nginx
ports:
- 80:80
volumes:
- /tmp/nginx/nginx.conf:/etc/nginx/nginx.conf
networks:
- pro-net
networks:
pxc-net:
ipam:
driver: default
config:
- subnet: "172.18.0.0/24"
pro-net:
ipam:
driver: default
config:
- subnet: "172.19.0.0/24"
volumes:
v1:![请添加图片描述](https://img-blog.csdnimg.cn/368a3dea44144b1ba3391c6266d1672f.png)
v2:
使用的时候要注意去掉ports,
docker-compose -f redis.yaml up --scale redis=5 -d
# 进入master
docker swarm init --advertise-addr=192.168.0.11
## 从执行结果中拿到worker noder需要执行的信息
docker swarm join --token SWMTKN-1-0a5ph4nehwdm9wzcmlbj2ckqqso38pkd238rprzwcoawabxtdq-arcpra6yzltedpafk3qyvv0y3 192.168.0.11:2377
# 进入worker
docker swarm join --token SWMTKN-1-0a5ph4nehwdm9wzcmlbj2ckqqso38pkd238rprzwcoawabxtdq-arcpra6yzltedpafk3qyvv0y3 192.168.0.11:2377
# 查看集群状态,master节点
docker node ls
# 节点类型转换,可以将worker提升为master,从而保证manager的高可用
docker node promote worker01-node
# 降级
docker node demote worker01-node
http://labs.play-with-docker.com
# 创建一个tomcat的service
docker service create --name my-tomcat tomcat
# 查询当前swarm的service
docker service ls
# 查看service的启动日志
docker service logs my-tomcat
# 查看service详情
docker service inspect my-tomcat
# 查看运行在哪一个node上
docker service ps my-tomcat
## 日志
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
u6o4mz4tj396 my-tomcat.1 tomcat:latest worker01-node Running Running 3 minutes ago
# 水平扩展service
docker service scale my-tomcat=3
## 如果某个node上的my-tomcat挂掉了,会自动扩展
wordpress + mysql案例
# 创建overlay网络,用于docker swarm中多机通信
【manager-node】
docker network create -d overlay my-overlay-net
docker network ls[此时work node查不到]
# 创建mysql的service
【manager-mode】
docker service --name mysql --mount type=volume,source=v1,destination=val/lib/mysql --env MYSQL_ROOT_PASSWORD=examplepass --env MYSQL_DATABASE=db_wordpress --network my-overlay-net mysql:5.6
# 创建wordpress的service
【manager-mode】
docker service create --name wordpress --env WORDPRESS_DB_USER=root --env WORDPRESS_DB_PASSWORD=examplepass --env WORDPRESS_DB_HOST=mysql:3306 --env WORDPRESS_DB_NAME=db_wordpress -p 8080:80 --network my-overlay-net wordpress
类似docker-compose
version: '3'
services:
wordpress:
image: wordpress
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
networks:
- ol-net
volumes:
- wordpress:/var/www/html
deploy:
mode: replicated
replicas: 3
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
update_config:
parallelism: 1
delay: 10s
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
networks:
- ol-net
deploy:
mode: global
placement:
constraints:
- node.role == manager
volumes:
wordpress:
db:
networks:
ol-net:
driver: overlay
# 根据service.ymal创建service
docker statck deploy -c service.yml my-service