1. 基于 Volume 互联
1.1 存储 Driver
-
Aufs: Docker最早支持的driver,但它只是Linux内核的一个补丁集。
-
Device Mapper: Linux2.6 内核提供的一种从逻辑设备到物理设备的映射框架机制,时LVM2的核心,支持块级别的copy on write特性。
-
VFS: 虚拟文件系统,每层都是一个单独的目录,如果新增一个child层,则需要将父级层镜像文件一并复制到新目录,不支持copy on write特性
-
Btrfs: 速度快,采用btrfs的文件系统的快照能力来实现layer分层功能。缺点是还不够成熟。
-
Overlay: 当前最新的文件驱动
1.2 不指定挂载目录
默认放在容器的**_data**目录下
$ docker run --rm -it -v /data ubuntu /bin/bash
root@c33db6442eeb:/# df
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 19475088 8221656 10241108 45% /
tmpfs 65536 0 65536 0% /dev
tmpfs 1000948 0 1000948 0% /sys/fs/cgroup
shm 65536 0 65536 0% /dev/shm
/dev/mapper/ubuntu--vg-ubuntu--lv 19475088 8221656 10241108 45% /data
tmpfs 1000948 0 1000948 0% /proc/asound
tmpfs 1000948 0 1000948 0% /proc/acpi
tmpfs 1000948 0 1000948 0% /proc/scsi
tmpfs 1000948 0 1000948 0% /sys/firmware
$ docker inspect c33db6442eeb
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/c3de425847ca3026927f29d8e6cdc9bde2360861f6e94bc6249a13589494fafe-init/diff:/var/lib/docker/overlay2/135cb45c9b61b0b6bd7ea119ee0ab2b30ccf9695e8197d635922f72a5cc007b5/diff",
"MergedDir": "/var/lib/docker/overlay2/c3de425847ca3026927f29d8e6cdc9bde2360861f6e94bc6249a13589494fafe/merged",
"UpperDir": "/var/lib/docker/overlay2/c3de425847ca3026927f29d8e6cdc9bde2360861f6e94bc6249a13589494fafe/diff",
"WorkDir": "/var/lib/docker/overlay2/c3de425847ca3026927f29d8e6cdc9bde2360861f6e94bc6249a13589494fafe/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "1018b771cefd508a5b610314a5e8599535895f72a0c1b731e17bfbb57217ecd9",
"Source": "/var/lib/docker/volumes/1018b771cefd508a5b610314a5e8599535895f72a0c1b731e17bfbb57217ecd9/_data",
"Destination": "/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
1.3 指定挂载目录
$ mkdir -p /mnt/data # 宿主机上创建目录 (SRC:DEST)
$ docker run --rm -it -v /mnt/data:/data ubuntu /bin/bash
root@b15e7b09953e:/# df
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 19475088 8221792 10240972 45% /
tmpfs 65536 0 65536 0% /dev
tmpfs 1000948 0 1000948 0% /sys/fs/cgroup
shm 65536 0 65536 0% /dev/shm
/dev/mapper/ubuntu--vg-ubuntu--lv 19475088 8221796 10240968 45% /data
tmpfs 1000948 0 1000948 0% /proc/asound
tmpfs 1000948 0 1000948 0% /proc/acpi
tmpfs 1000948 0 1000948 0% /proc/scsi
tmpfs 1000948 0 1000948 0% /sys/firmware
$ docker inspect b15e7b09953e
"Mounts": [
{
"Type": "bind",
"Source": "/mnt/data",
"Destination": "/data",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
1.4 基于数据容器的单主机互联
数据容器:只提供数据的容器,业务容器连接到该数据容器,实现数据共享
# 共享 --volumes-from
$ docker run --rm -it --privileged=true --volumes-from=b15e7b09953e ubuntu /bin/sh
$ docker inspect e8fd1836a90f
"Mounts": [
{
"Type": "bind",
"Source": "/mnt/data",
"Destination": "/data",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
2. 基于 Link 互联
docker 启动参数 --icc=false
, 关闭互联
--link name:alias
: 可以通过一个固定的name来和另一个容器通信,link机制方便了容器去发现其它的容器并且可以安全的传递一些连接信息给其它的容器。
$ docker run --name=mysql-server -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.28
# link 网络
$ docker run --rm --link=mysql-server:db -it nginx /bin/bash
root@dbbef909d582:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 db aedbae5bebb9 mysql-server
172.17.0.3 dbbef909d582
root@dbbef909d582:/# apt install mariadb-client
root@dbbef909d582:/# mysql -h mysql-server -p
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.003 sec)
3. 基于 Network 互联
3.1 端口映射
$ docker run --rm -p 8306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.28
$ ps -ef | grep docker-proxy
root 75228 62168 0 20:14 ? 00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8306 -container-ip 172.17.0.2 -container-port 3306
3.2 宿主机网络
$ docker run --rm --net=host -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.28
$ mysql -h 127.0.0.1 -P 3306 -p
3.3 容器共用一个IP网络
$ docker run --rm --name=mysql-server -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.28
$ docker exec -it mysql-server cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 09af02aab502
# 与 mysql 共用网络
$ docker run --rm --name nginx-server --net=container:mysql-server -d nginx
$ docker exec -it nginx-server cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 09af02aab502
4. 实例:安装 MySQL 主从数据库
4.1 创建配置文件
mkdir -p /mysql_data/conf
mkdir -p /mysql_data/master
mkdir -p /mysql_data/slave
# 主节点配置
cat > /mysql_data/conf/master.conf <<EOF
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
log_bin = log # 开启二进制日志,用于从节点的历史复制回放
collation-server = utf8_unicode_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8
server_id = 1 # 需保证主库和从库的server_id不同
replicate-do-db=fileserver # 需要复制的数据库名,复制多个数据库时,重复设置即可
EOF
# 从节点配置
cat > /mysql_data/conf/slave.conf <<EOF
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
log_bin = log # 开启二进制日志,用于从节点的历史复制回放
collation-server = utf8_unicode_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8
server_id = 2 # 需保证主库和从库的server_id不同
replicate-do-db=fileserver # 需要复制的数据库名,复制多个数据库时,重复设置即可
EOF
4.2 启动MYSQL容器
# 主节点
docker run -d --name mysql-master -p 13306:3306 \
-v /mysql_data/conf/master.conf:/etc/mysql/mysql.conf.d/mysqld.cnf \
-v /mysql_data/master:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
# 从节点
docker run -d --name mysql-slave -p 13307:3306 \
-v /mysql_data/conf/slave.conf:/etc/mysql/mysql.conf.d/mysqld.cnf \
-v /mysql_data/slave:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
4.3 宿主机安装 MYSQL 客户端
# 卸载 mariadb 组件
$ yum list installed | grep -i mariadb
mariadb-libs.x86_64 1:5.5.65-1.el7 @anaconda
$ yum remove -y mariadb-libs
# 安装 mysql repo
yum -y install http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
# 安装客户端
yum search mysql-community
yum install -y mysql-community-client
4.4 配置同步信息
4.4.1 主节点
# 不要使用localhost,使用本机公网IP
mysql -uroot -h 192.168.31.60 -P13306 -p
# 授权slave节点登录
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'slave';
mysql> flush privileges;
mysql> create database utime default character set utf8mb4;
mysql> show master status\G
*************************** 1. row ***************************
File: log.000001
Position: 582
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
4.4.2 从节点
mysql -uroot -h 192.168.31.60 -P13307 -p # 不要使用localhost,使用本机公网IP
mysql> stop slave;
mysql> create database utime default character set utf8mb4;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.31.60', MASTER_PORT=13306, MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='log.000001', MASTER_LOG_POS=627;
mysql> start slave;
mysql> show slave status\G