Docker
1. Docker概述
docker思想来源于集装箱,核心思想:隔离,将多个应用打包装箱,每个箱子都是互相隔离的
docker是基于Go语言开发的,开源项目。
虚拟化技术
缺点:资源占用多、冗余步骤多、启动慢
容器化技术
容器化技术不是模拟一个完整的OS
Docker与虚拟化技术的不同:
- 传统虚拟机需要虚拟硬件,运行一个完整的OS,之后运行App
- 容器内的应用直接运行在宿主机,容器没有自己的内核,没有虚拟硬件
- 每个容器之间相互隔离,都有自己的文件系统,互不影响
DevOps 开发、运维
-
应用更加快捷的交付和部署
传统:很多帮助文档,需要根据文档安装环境,复杂
Docker:将开发的应用与环境一起打包成镜像,之后进行发布,就可以一键运行
-
更便捷的升级和扩缩容
项目打包为一个镜像,这个镜像就可以增删
-
更简单的系统运维
在容器化之后,开发和测试环境高度一致
-
更高效的计算和资源利用
Docker是内核级别的虚拟化,可以在一个物理机上运行多个容器,这样就可以使服务器的性能发挥到极致。
2. Docker安装
2.1 基本组成
镜像(image)
docker镜像就像是一个模板,可以通过此模板来创建容器,tomcat镜像---->run---->tomcat01容器(提供服务),而且可以通过这个镜像创建多个容器,最终项目运行在这个容器中。
容器(container)
Docker利用容器技术,独立运行一个或者一组应用,容器是通过镜像创建的。
启动、停止、删除、基本命令
可以将这个容器理解为一个小型的Linux系统
仓库(repository)
存放镜像的地方
公有仓库和私有仓库
Docker Hub、阿里云…都有容器服务,需要配置镜像加速
2.2 安装docker
环境查看
# 系统内核是3.10以上的
[root@summer ~]# uname -r
3.10.0-1062.18.1.el7.x86_64
# 系统版本
[root@summer ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
安装
https://docs.docker.com/engine/install/centos/
# 1. 清除之前的版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2.安装需要的软件包
yum install -y yum-utils
# 3.设置stable镜像仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 4.更新yum软件包索引
yum makecache fast
# 5.安装Docker-CE 社区版
yum -y install docker-ce docker-ce-cli containerd.io
# 6.启动docker
systemctl start docker
# 7.测试
docker version
docker run hello-world
docker images
卸载
# 1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
# 2. 删除资源
rm -rf /var/lib/docker
2.3 配置阿里云镜像加速
-
登录阿里云,进入镜像服务
-
找到镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://qjiedbun.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
2.4 Run 的流程
2.5 底层原理
Docker是一个CS模式的系统,Docker的守护进程运行在主机上,通过socket从客户端访问。Docker-server接收Docker-client的指令,就会执行这个命令。
Docker为什么比VM快?
-
Docker有比虚拟机更少的抽象层
-
Docker利用的是宿主机的内核,VM需要的是Guest OS
新建一个容器的时候,docker不需要和虚拟机一样加载一个OS内核,避免引导。虚拟机需要加载Guest OS,需要分钟级别的时间,而docker是利用宿主机的OS,省略了这个复杂的过程,需要秒级的时间。
3. Docker命令
3.1 帮助命令
docker version # 显示docker的信息
docker info # 显示docker的系统信息 含镜像和容器数量
dokcer --help # 帮助
3.2 镜像命令
docker images 查看本机所有的镜像
[root@summer ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 9 months ago 13.3kB
# REPOSITORY 镜像的仓库源 # TAG镜像的标签 # IMAGE ID 镜像id # CREATED 镜像创建时间 # SIZE 镜像大小
# 可选项
docker images -a
-a # 列出所有的镜像
docker images -q
-q # 只显示镜像的id
docker search 搜索镜像
[root@summer ~]# docker search mysql
NAME DESCRIPTION STARS UTOMATED
mysql MySQL is a widely used, open-source relation… 12752
# 可选项 通过收藏来过滤
docker search mysql -f=STARS=3000 # 搜索出的镜像收藏大于3000
docker pull 下载镜像
[root@summer ~]# docker pull mysql # 下载mysql镜像
Using default tag: latest # 默认使用最新版
latest: Pulling from library/mysql
72a69066d2fe: Pull complete # 分层下载 docker image的核心
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
docker pull mysql == docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@summer ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists # 上边已经下载过了,这里说已经存在
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@summer ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 6 months ago 448MB
mysql latest 3218b38490ce 6 months ago 516MB
hello-world latest feb5d9fea6a5 9 months ago 13.3kB
docker rmi 删除镜像
[root@summer ~]# docker rmi -f 3218b38490ce # 根据镜像id删除镜像
[root@summer ~]# docker rmi -f id id id id id id # 根据多个镜像id删除镜像
[root@summer ~]# docker rmi -f $(docker images -aq) # 删除所有的镜像 $(docker images -aq)得到所有的镜像
3.3 容器命令
有了镜像才可以创建容器
docker pull centos
新建容器并启动
docker run [可选参数] image
# 可选参数
--name="name" # 设置容器名字
-d # 后台运行
-it # 使用交互方式运行,进入容器查看内容
-p # 指定容器的端口 -p 8080
# -p 主机端口:容器端口 (常用) / -p 主机IP:主机端口:容器端口 / -p 容器端口 / 容器端口
-P # 随机指定端口
# 启动并进入容器
[root@summer ~]# docker run -it centos /bin/bash
# 查看容器内的centos
[root@d49174fa1a95 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
# 退出容器
[root@d49174fa1a95 /]# exit
列出所有运行的容器
# docker ps 列出当前正在运行的容器
[root@summer /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# docker ps -a 列出当前正在运行的 + 已经执行过的容器
# docker ps -n=3 列出最近执行的3个容器
# docker ps -q 只显示容器的编号
[root@summer /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d49174fa1a95 centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago beautiful_lamarr
e9ed88804f84 feb5d9fea6a5 "/hello" 28 hours ago Exited (0) 28 hours ago wonderful_gauss
退出容器
exit # 直接停止容器并退出
Ctrl + p + q # 容器不停止退出
[root@summer /]# docker run -it centos /bin/bash
[root@d764ed07e3c9 /]# [root@summer /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d764ed07e3c9 centos "/bin/bash" 20 seconds ago Up 20 seconds cranky_noyce
删除容器
docker rm 容器id # 删除指定的容器 不能删除正在运行的容器 强制删除 docker rm -f 容器id
docker rm -f $(docker ps -aq) # 删除所有
docker ps -a -q |xargs docker rm # 删除所有容器
启动和停止容器
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在执行的容器
docker kill 容器id # 强制停止当前容器
3.4 其他命令
后台启动容器
# docker run -d centos 容器名
[root@summer /]# docker run -d centos # 后台启动容器
# 当后台启动容器后,此时使用docker ps 发现并没有启动的容器,因为docker要求如果没有前台进程使用,后台就会停止
查看日志
[root@summer /]# docker run -d centos /bin/sh -c "while true;do echo hello;sleep 1;done"
18b16e0b8737399c200bd976fcdc256ac41f4ae568b32328f6b3a37d199cb6bf
[root@summer /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18b16e0b8737 centos "/bin/sh -c 'while t…" 6 seconds ago Up 5 seconds boring_dirac
[root@summer /]# docker logs -ft --tail 10 18b16e0b8737 # 显示指定行数(10条)的日志
2022-06-21T08:33:48.061129786Z hello
2022-06-21T08:33:49.062926733Z hello
2022-06-21T08:33:50.064704484Z hello
2022-06-21T08:33:51.066600393Z hello
2022-06-21T08:33:52.068816136Z hello
2022-06-21T08:33:53.070784213Z hello
2022-06-21T08:33:54.072612454Z hello
2022-06-21T08:33:55.074587258Z hello
2022-06-21T08:33:56.076494828Z hello
2022-06-21T08:33:57.078247385Z hello
2022-06-21T08:33:58.080320482Z hello
2022-06-21T08:33:59.082192594Z hello
查看容器中的进程信息
[root@summer /]# docker top 18b16e0b8737
UID PID PPID C STIME TTY TIME CMD
root 1281 1261 0 16:27 ? 00:00:00 /bin/sh -c while true;do echo hello;sleep 1;done
root 3317 1281 0 16:44 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看容器元数据
[root@summer /]# docker inspect 18b16e0b8737
[
{
"Id": "18b16e0b8737399c200bd976fcdc256ac41f4ae568b32328f6b3a37d199cb6bf",
"Created": "2022-06-21T08:27:36.104218525Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo hello;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 1281,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-06-21T08:27:36.347672878Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/18b16e0b8737399c200bd976fcdc256ac41f4ae568b32328f6b3a37d199cb6bf/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/18b16e0b8737399c200bd976fcdc256ac41f4ae568b32328f6b3a37d199cb6bf/hostname",
"HostsPath": "/var/lib/docker/containers/18b16e0b8737399c200bd976fcdc256ac41f4ae568b32328f6b3a37d199cb6bf/hosts",
"LogPath": "/var/lib/docker/containers/18b16e0b8737399c200bd976fcdc256ac41f4ae568b32328f6b3a37d199cb6bf/18b16e0b8737399c200bd976fcdc256ac41f4ae568b32328f6b3a37d199cb6bf-json.log",
"Name": "/boring_dirac",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/469ff3e0a100627c7857dcfe7c3439fca033437c15e62d390c208e68bd6b3ddf-init/diff:/var/lib/docker/overlay2/3ebb2f32c933faca3d5031042cc8bc2f60eba06c29db637756b527ed4e698d4f/diff",
"MergedDir": "/var/lib/docker/overlay2/469ff3e0a100627c7857dcfe7c3439fca033437c15e62d390c208e68bd6b3ddf/merged",
"UpperDir": "/var/lib/docker/overlay2/469ff3e0a100627c7857dcfe7c3439fca033437c15e62d390c208e68bd6b3ddf/diff",
"WorkDir": "/var/lib/docker/overlay2/469ff3e0a100627c7857dcfe7c3439fca033437c15e62d390c208e68bd6b3ddf/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "18b16e0b8737",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"while true;do echo hello;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "7c3363c27460406965fd9119957e8cb06a3cab7859a45737c55e9fee0d69f84a",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/7c3363c27460",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "bcc8bddd53da2d99726adcbdd2f2a408a7ce51476e72b9703f4bc02dbce2db19",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "904ad90e4495dc53ac3d14081e380a3cb7550f16a91291e64d71b7e4f2ece2c6",
"EndpointID": "bcc8bddd53da2d99726adcbdd2f2a408a7ce51476e72b9703f4bc02dbce2db19",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
# 通常容器都是使用后台方式运行的,需要进行容器,修改一些配置
docker exec -it 容器id /bin/bash # 进入容器后开启一个新的终端,可以在里面操作
# 或者
docker attah 容器id # 进入容器正在执行当前的终端,不会启动新的进程
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径
4. 部署Nginx
- 搜索镜像
docker search nginx
- 下载镜像
docker pull nginx
- 创建容器
docker run -d --name nginx01 -p 3344:80 nginx # 3344为宿主机端口 80为容器内部端口
- 本机测试
curl localhost:3344
- 公网测试
记得要开启端口号
- 进入容器
[root@summer home]# docker exec -it 876c3d0041e7 /bin/bash
root@876c3d0041e7:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@876c3d0041e7:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
5. 部署Tomcat
官方使用:
docker run -it --rm tomcat:9.0 # --rm的意思是运行这个容器,容器就会自动删除,一般用于测试
# 下载再启动
docker pull tomcat:9.0
docker images
docker run -d --name tomcat01 -p 3344:8080 tomcat:9.0
# 使用公网进行访问 测试显示404
# 进入容器
[root@summer home]# docker exec -it tomcat01 /bin/bash
root@b6deac68e50c:/usr/local/tomcat# ls
BUILDING.txt NOTICE RUNNING.txt lib temp work
CONTRIBUTING.md README.md bin logs webapps
LICENSE RELEASE-NOTES conf native-jni-lib webapps.dist
root@b6deac68e50c:/usr/local/tomcat# cd webapps
root@b6deac68e50c:/usr/local/tomcat/webapps# ls
root@b6deac68e50c:/usr/local/tomcat/webapps#
# 发现tomcat的webapps下是空的 阿里云镜像的原因,默认是最小的镜像,将不必要的都剔除掉,保证最小可运行的环境
root@b6deac68e50c:/usr/local/tomcat/webapps# cp -r webapps.dist/* webapps
# 可以将webapps.dist下的所有文件复制到webapps 再次用公网ip+8080 访问就可见到主页
6. 部署ES+Kibana
# ES 暴露的端口很多
# ES 十分耗内存
# ES 需要外部挂载
# 启动 elasticsearch
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
# 启动之后特别卡 非常耗内存
# docker stats 容器id 查看当前资源消耗
# 增加内存限制
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
docker stats e3713b387773[root@summer ~]# curl localhost:9200
{
"name" : "e3713b387773",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "Hu42xgj5SUCTaZWYd-zlAA",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
使用Kibana 连接 ES 需要用到docker网络 怎么连接?
7. 可视化面板
portainer
docker的图形化界面工具,提供后台面板供操作
[root@summer ~]# docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer-ce
Unable to find image 'portainer/portainer-ce:latest' locally
latest: Pulling from portainer/portainer-ce
0ea73420e2bb: Pull complete
c367f59be2e1: Pull complete
b71b88d796e2: Pull complete
Digest: sha256:4f126c5114b63e9d1bceb4b368944d14323329a9a0d4e7bb7eb53c9b7435d498
Status: Downloaded newer image for portainer/portainer-ce:latest
6e2bfd92ba15f2d3b238dacbd2e4c27f060cfc37e073b752494bc5dc8cf0d9f9
访问外网地址+8088
第一次需要设置密码,这里密码为12345678
8. 镜像原理
镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于环境开发的软件,包含运行某个软件所需的内容,例代码,运行时的库、环境变量和配置文件。
UnionFS (联合文件系统)
UnionFS是一种分层、轻量级并且高性能的文件系统,支持对文件系统的修改作为一次提交来一层一层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下。
特性:一次同时加载多个文件系统,但从外边来看,只能看到一个文件系统,联合加载会把各层文件系统叠加起来,最终这个文件系统会包含所有底层的文件和目录。
docker镜像加载原理
docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS。
bootfs(boot file system)主要包含bootloader和kernel,bootloader主要是引导加载kernel,Linux刚启动时会加载bootfs文件系统,在Docker镜像的最底层是bootfs。这一层与典型的Linux/Unix系统是一样的,包含boot加载器和内核。当bootfs加载完成之后整个内核就都在内存中了,此时内存的使用权已由bootfs转交给内核,此时系统也会卸载bootfs。
rootfs (root file system) ,在bootfs之上。包含的就是典型Linux系统中的/dev, /proc, /bin, /etc等标准目录和文件。rootfs就是各种不同的操作系统发行版,比如Ubuntu , Centos等等。
对于一个精简的OS,rootfs可以很小,只需要包含最基本的命令、工具和程序库就可以,因为底层直接使用主机的kernel,所以自己只需提供rootfs即可。
分层理解
为什么采用分层结构?最大的好处就是可以资源共享,例如多个镜像都是从相同的Base镜像构建的,宿主机只需在磁盘中保留一份base镜像,同时内存中也只需加载一份base镜像,这样就可以为所有的容器服务。
特点
docker镜像都只是可读的,当容器启动后,一个新的可写层被加载到镜像的底部,这一层就是容器层,容器之下的层都是镜像层
例如下载了一个tomcat的镜像,运行该镜像创建一个容器,该容器就在镜像的上层,只需修改容器层中的内容,之后若想打包就可以将容器层和镜像层一起打包为一个新镜像。
提交自定义镜像 commit 镜像
docker commit 提交容器成为一个新的副本
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名:[TAG]
测试
# 1.启动创建容器
docker run -it -p 8080:8080 tomcat:9.0
# 2.重新运行一个窗口,进入容器
docker exec -it 3004a46fc66c /bin/bash
# 3.将webapp.dist目录下的文件复制到webapp下
cp -r webapps.dist/* webapps
root@3004a46fc66c:/usr/local/tomcat/webapps# exit
exit
[root@summer ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3004a46fc66c tomcat:9.0 "catalina.sh run" 6 minutes ago Up 6 minutes 0.0.0.0:8080->8080/tcp vigilant_pascal
# 4.commit新的tomcat
[root@summer ~]# docker commit -a="summer" -m="add webapps app" 3004a46fc66c tomcat1.0
sha256:c3781884a5cf6f6eab3843fd3c203049639097eae10fb359d9f88d6fdf440a7c
[root@summer ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat1.0 latest c3781884a5cf 4 seconds ago 685MB
tomcat 9.0 b8e65a4d736d 6 months ago 680MB
9. 容器数据卷
数据不能放在容器中,一旦容器删除,那么容器中存放的数据也会丢失,因此数据可以存贮在本地。
docker容器中产生的数据,同步到本地
卷技术就是将容器内的目录挂载到Linux上。
9.1 指定路径挂载
docker run -it -v 宿主机目录:容器内目录
# docker run -it -v 宿主机目录:容器内目录
[root@summer ~]# docker run -it -v /home/ceshi:/home centos 将宿主机的/home/ceshi 与容器的/home目录进行绑定
[root@ec3a6d6142c2 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@ec3a6d6142c2 /]# cd home
[root@ec3a6d6142c2 home]# ls
[root@ec3a6d6142c2 home]# touch hello # 在容器的home目录下创建一个hello文件
[root@ec3a6d6142c2 home]# ls
hello
[root@ec3a6d6142c2 home]# exit
exit
[root@summer ~]# cd /home
[root@summer home]# ls
ceshi jdk nginx springboot test.java tomcat www
[root@summer home]# cd ceshi # 可以看到在宿主机的ceshi目录下也有该文件
[root@summer ceshi]# ls
hello
[root@summer ceshi]# docker inspect ec3a6d6142c2
"Mounts": [
{
"Type": "bind", # 绑定
"Source": "/home/ceshi", # 宿主机地址
"Destination": "/home", # 容器地址
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
将容器关掉,在宿主机的目录中进行修改,只要容器存在,容器中的绑定目录会随之改变。
MySQL 同步数据
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/
mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name mysql01 mysql:5.7
-d # 后台运行
-p # 端口
-v # 数据挂在
-e # 环境配置
--name # 名字
# 启动成功之后,这样可以使用Navicat 主机为公网地址 端口为3310 用户名为root 密码为root可以连接
# 使用Navicat创建一个数据库 test之后,可以看到在宿主机和容器中都已存在
root@464a873ca54f:/var/lib/mysql# ls
auto.cnf client-cert.pem ib_logfile0 ibtmp1 private_key.pem server-key.pem
ca-key.pem client-key.pem ib_logfile1 mysql public_key.pem sys
ca.pem ib_buffer_pool ibdata1 performance_schema server-cert.pem test
[root@summer data]# ls
auto.cnf client-cert.pem ibdata1 ibtmp1 private_key.pem server-key.pem
ca-key.pem client-key.pem ib_logfile0 mysql public_key.pem sys
ca.pem ib_buffer_pool ib_logfile1 performance_schema server-cert.pem test
即使将容器删掉,数据在宿主机中仍然存在,这就实现了容器数据持久化技术。
9.2 具名和匿名挂载
# 匿名挂载
docker run -d -P --name nginx01 -v /etc/nginx nginx # 没有指定宿主机的目录,只指定了容器内的目录
# 查看所有的volume的情况
[root@summer data]# docker volume ls
DRIVER VOLUME NAME
local 3be48649f0bb81dc49d218c2803d080d6f9a601ecada40f5b7fb034885ca02d8
local 4fc1dd59fd28b60e77a8ed10063a1ca98edd212205785ae02e2f5bb9f7e07fe0
local 58f0d57232b8d1a0c0805b2daf962de347a8ef97123219d3fb41eaadaf305ffd
local 902c76226bbf780ee5bbae2a760322cd67933215b31e7e9aea5a8cda51ca5da4
# 具名挂载
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx # 没有指定宿主机的目录,只写了一个卷名
[root@summer data]# docker volume ls
DRIVER VOLUME NAME
local 3be48649f0bb81dc49d218c2803d080d6f9a601ecada40f5b7fb034885ca02d8
local 4fc1dd59fd28b60e77a8ed10063a1ca98edd212205785ae02e2f5bb9f7e07fe0
local 58f0d57232b8d1a0c0805b2daf962de347a8ef97123219d3fb41eaadaf305ffd
local 902c76226bbf780ee5bbae2a760322cd67933215b31e7e9aea5a8cda51ca5da4
local juming-nginx
# 没有指定目录下的卷 在该目录下 /var/lib/docker/volumes/xxxx/_data
[root@summer data]# docker volume inspect juming-nginx
[
{
"CreatedAt": "2022-06-23T14:52:51+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "juming-nginx",
"Options": null,
"Scope": "local"
}
]
-v 容器内路径 # 匿名挂载
-v 具名:容器内路径 # 具名挂载
-v 宿主机路径:容器内路径 # 指定路径挂载
拓展:
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx # 只能读
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx # 可读可写
# 如果有ro 则说明该路径下的文件只能通过宿主机来操作,容器内部无法操作
9.3 Dockerfile
[root@summer docker-test-volume]# pwd
/home/docker-test-volume
[root@summer docker-test-volume]# vim dockerfile1
[root@summer docker-test-volume]# cat dockerfile1
FROM centos
VOLUME ["volume01","volume02"] # 此时是匿名挂载
CMD echo "----end----"
CMD /bin/bash
[root@summer docker-test-volume]# docker build -f dockerfile1 -t summer/centos .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"]
---> Running in 230ce724abd7
Removing intermediate container 230ce724abd7
---> 5cef6f3b889f
Step 3/4 : CMD echo "----end----"
---> Running in d6406fa71310
Removing intermediate container d6406fa71310
---> 0270cf09a597
Step 4/4 : CMD /bin/bash
---> Running in f63521eaae7e
Removing intermediate container f63521eaae7e
---> eaee63229e23
Successfully built eaee63229e23
Successfully tagged summer/centos:latest
[root@summer docker-test-volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
summer/centos latest eaee63229e23 About a minute ago 231MB
# 启动自定义的镜像
[root@summer docker-test-volume]# docker run -it eaee63229e23 /bin/bash
[root@c3b3f12de6e8 /]# ls -l
total 56
......
drwxr-xr-x 2 root root 4096 Jun 23 07:22 volume01
drwxr-xr-x 2 root root 4096 Jun 23 07:22 volume02
# 可以看到创建镜像时挂载的目录,就是数据卷目录
# 在其中一个目录中创建一个文件hello1
[root@c3b3f12de6e8 /]# cd volume01
[root@c3b3f12de6e8 volume01]# touch hello1
[root@c3b3f12de6e8 volume01]# ls
hello1
# 寻找宿主机对应的目录
[root@summer home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3b3f12de6e8 eaee63229e23 "/bin/bash" 4 minutes ago Up 4 minutes gifted_goldstine
[root@summer home]# docker inspect c3b3f12de6e8
"Mounts": [
{
"Type": "volume",
"Name": "7b70034eb35c1db0b253fe06f3dc130daef501a668ffc672aa903668d73f1db2",
"Source": "/var/lib/docker/volumes/7b70034eb35c1db0b253fe06f3dc130daef501a668ffc672aa903668d73f1db2/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "fb1d4ac69f967e0ac90494dbed00a466d89da417cc771e9f117d9a6368182710",
"Source": "/var/lib/docker/volumes/fb1d4ac69f967e0ac90494dbed00a466d89da417cc771e9f117d9a6368182710/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
[root@summer home]# cd /var/lib/docker/volumes/7b70034eb35c1db0b253fe06f3dc130daef501a668ffc672aa903668d73f1db2/_data
[root@summer _data]# ls
hello1
9.4 数据卷容器
多个centos实现数据同步
[root@summer home]# docker run -it --name centos01 summer/centos /bin/bash
[root@0f8c84e24c61 /]# ls
bin etc lib lost+found mnt proc run srv tmp var volume02
dev home lib64 media opt root sbin sys usr volume01
[root@summer home]# docker run -it --name centos02 --volumes-from centos01 summer/centos /bin/bash
# --volumes-from 相当于java的继承关系
# 在centos01容器中的volume01文件夹下创建一个docker01文件,此时在centos02容器的volume01文件夹下发现会有docker01文件
# 这就说明两个容器的volume01文件夹和volume02文件夹实现了数据绑定
[root@summer home]# docker run -it --name centos03 --volumes-from centos01 summer/centos /bin/bash
# 同样在centos03的volume01文件夹下存在docker01文件,同样如果在centos03下的volume01文件夹下下创建新文件,也会同步更新到centos01和centos02中
这样就是实现了centos01、centos02、centos03三个容器的volume01和volume02文件夹的内容同步,即使将centos01容器删掉,另外两个容器中的数据也不会丢失,可以看出并不是02和03映射01的数据,而是将01、02、03的数据同步复制。
多个MySQL实现数据同步
docker run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name mysql01 mysql:5.7
docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=root --name mysql02 --volumes-from mysql01 mysql:5.7
数据卷容器的生命周期一直持续到没有容器使用为止
10. DockerFile
10.1 简介
dockerfile是用来构建docker镜像的文件,命令参数脚本
构建步骤:
-
编写一个dockerfile文件
-
docker build 构建成为一个镜像
-
docker run 运行镜像
-
docker push 发布镜像 docker hub 阿里云镜像仓库
10.2 构建过程
基础知识
- 每个保留关键字都必须是大写字母
- 指令从上到下执行
- #表示注释
- 每个指令都会创建提交一个新的镜像层,并提交
DockerFile:构建文件,定义了一切的步骤、源代码
DockerImages:通过DockerFile构建生成的镜像,最终发布和运行的产品
Docker容器:容器就是镜像运行起来提供服务的
10.3 指令
FROM # 基础镜像
MAINTAINER # 镜像是谁写的 姓名+邮箱
RUN # 镜像构建的时候需要运行的命令
ADD # tomcat镜像 tomcat压缩包
WORKDIR # 镜像的工作目录
VOLUME # 挂载的目录
EXPOSE # 保留端口配置
CMD # 指容器启动要执行的命令 只有最后一个生效,可被替代
ENTRYPOINT # 指容器启动要执行的命令 可以追加命令
ONBUILD # 当构建一个被继承DockerFile 这个时候就会运行ONBUILD的指令 触发指令
COPY # 类似add命令 将文件拷贝到镜像中
ENV # 构建的时候设置环境变量
10.4 构建自己的centos
[root@summer dockerfile]# vim mydockerfile
# 1.编写Dockerfile的文件
[root@summer dockerfile]# cat mydockerfile
FROM centos:centos7
MAINTAINER summer<1234567789@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "---end---"
CMD /bin/bash
# 2.通过这个文件构建镜像 docker build -f dockerfile文件路径 -t 镜像名:[tag]
[root@summer dockerfile]# docker build -f mydockerfile -t mycentos:0.1 .
[root@summer dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 ecbf11665a8b 6 seconds ago 601MB
# 3.测试
[root@192209b18180 local]# ifconfig
[root@192209b18180 local]# vim test
可以查看镜像的构建历史
[root@summer dockerfile]# docker history ecbf11665a8b
IMAGE CREATED CREATED BY SIZE COMMENT
ecbf11665a8b 8 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
d47e5677ae2e 8 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
cc5070c77dd6 9 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
17b87c50f41f 9 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
144f58dbe64a 9 minutes ago /bin/sh -c yum -y install net-tools 171MB
0a4461f757a5 9 minutes ago /bin/sh -c yum -y install vim 226MB
0a00c02cbd9c 10 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
21d36ab390e0 10 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
81ab9de242c5 10 minutes ago /bin/sh -c #(nop) MAINTAINER summer<1234567… 0B
eeb6ee3f44bd 9 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 9 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 9 months ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB
CMD 和 ENTRYPOINT 的区别
CMD # 指容器启动要执行的命令 只有最后一个生效,可被替代
ENTRYPOINT # 指容器启动要执行的命令 可以追加命令
测试CMD
# 编写dockerfile文件
vim dockerfile-cmd-test
FROM centos
CMD ["ls","-a"]
# 构建镜像
docker build -f dockerfile-cmd-test -t cmdtest .
# 运行镜像
docker run xxxx
打印 ls -a 的内容
# 运行
docker run xxxx -l
# 想要显示 ls -al 但是出错,原因是CMD的情况下,-l会替换原来的CMD ["ls","-a"],-l不是命令,所以报错
测试ENTRYPOINT
# 编写dockersfile文件
vim docker-entrypopint
FROM centos
ENTRYPOINT ["ls","-a"]
# 构建镜像
docker build -f docker-entrypoint -t dockerpoint .
# 运行镜像
docker run xxx -l #可以执行成功
10.5 构建自己的tomcat
-
准备镜像文件 tomcat压缩包 jdk压缩包
apache-tomcat-9.0.64.tar.gz jdk-8u333-linux-x64.tar.gz
-
编写dockerfile文件 官方命名 Dockerfile,使用之后就不用在build时加 -f 参数
FROM centos:centos7
MAINTAINER summer<123456789@qq.com>
ADD jdk-8u333-linux-x64.tar.gz /usr/local/ # 此处的意思是将两个压缩包放到容器的/usr/local目录下
ADD apache-tomcat-9.0.64.tar.gz /usr/local/
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_333
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.64
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.64
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.64/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.64/logs/catalina.out
- 构建镜像
[root@summer docker-tomcat]# docker build -t mytomcat .
[root@summer docker-tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat latest ea2a0fdb3a3d 9 seconds ago 815MB
- 运行
[root@summer docker-tomcat]# docker run -d -p 3344:8080 --name mytomcat -v /home/tomcat/test:/usr/local/apache-tomcat-9.0.64/webapps/test -v /home/tomcat/logs:/usr/local/apache-tomcat-9.0.64/logs mytomcat
- 测试
curl localhost:3344 # 访问成功
# 外网ip+3344 访问成功
[root@summer docker-tomcat]# docker exec -it 4e2a2e681c91 /bin/bash
[root@4e2a2e681c91 local]# ls
aegis bin games jdk1.8.0_333 lib64 sbin src
apache-tomcat-9.0.64 etc include lib libexec share
- 发布项目
在本地的test目录下创建一个WEB-INF目录,在该目录下创建一个web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
</web-app>
然后与WEB-INF同级下新建一个index.html文件
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录</title>
</head>
<body>
<form action="LoginServlet" method="post">
<table>
<tr>
<td>账号</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
访问 公网ip+3344/test
11. 发布镜像
11.1 docker hub
- 注册自己的账号
- 在服务器提交自己的镜像
[root@summer home]# docker login -usummer1245
[root@summer home]# docker push summer1245/mytomcat:1.0
The push refers to repository [docker.io/summer1245/mytomcat]
An image does not exist locally with the tag: summer1245/mytomcat
# 报以上错误,给镜像修改镜像名字
[root@summer home]# docker tag 90a99e710602 summer1245/mytomcat:1.0
[root@summer home]# docker push summer1245/mytomcat:1.0
11.2 阿里云镜像
- 登录阿里云
- 容器镜像服务
- 在实例列表创建个人实例
- 创建命名空间
- 创建镜像仓库 选择本地镜像
$ docker login --username=aliyun6391782142 registry.cn-beijing.aliyuncs.com
$ docker tag [ImageId] registry.cn-beijing.aliyuncs.com/summer-images/hhhhh:[镜像版本号]
$ docker push registry.cn-beijing.aliyuncs.com/summer-images/hhhhh:[镜像版本号]
12. Docker网络
12.1 Docker0
[root@summer ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:16:3e:03:81:69 brd ff:ff:ff:ff:ff:ff
inet 172.28.251.25/20 brd 172.28.255.255 scope global dynamic eth0
valid_lft 315019035sec preferred_lft 315019035sec
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:5f:ee:cc:dc brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
# lo 本机回环地址 127.0.0.1
# eth0 阿里云内网地址 172.28.251.25
# docker0 docker0地址 172.17.0.1
# 测试 创建一个tomcat的容器,容器外该如何访问容器内?
[root@summer ~]# docker run -d -P --name tomcat01 tomcat:7.0
[root@summer ~]# docker exec -it tomcat01 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
146: eth0@if147: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
# 测试 直接使用宿主机 ping 容器的docker0地址 发现可以直接ping
[root@summer ~]# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.065 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.050 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.047 ms
每启动一个docker容器,docker就会给每个容器分配一个IP,只要安装了docker,就会有一个docker0,采用桥接模式,使用的技术是
evth-pair技术
再次测试ip addr
[root@summer ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:16:3e:03:81:69 brd ff:ff:ff:ff:ff:ff
inet 172.28.251.25/20 brd 172.28.255.255 scope global dynamic eth0
valid_lft 315017326sec preferred_lft 315017326sec
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:5f:ee:cc:dc brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
# 这就是刚才启动的容器
147: vethddb9267@if146: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 92:c5:87:d4:f6:4b brd ff:ff:ff:ff:ff:ff link-netnsid 0
再次启动一个容器,发现又多了一个网卡
# 宿主机
149: vethd7c0d22@if148: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 0e:d6:43:08:c8:53 brd ff:ff:ff:ff:ff:ff link-netnsid 1
# 容器
[root@summer ~]# docker exec -it tomcat02 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
148: eth0@if149: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
# 可以发现,容器的网卡都是成对的
# evth-pair 就是一对虚拟设备接口,成对出现,一端连接协议,一端彼此相连
# 因为有这种特性,evth-pair 充当一个桥梁,连接各种虚拟网络设备的
# 进入tomcat02,来ping tomcat01
root@65aba89c45fe:/usr/local/tomcat# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.091 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.064 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.070 ms
64 bytes from 172.17.0.2: icmp_seq=4 ttl=64 time=0.068 ms
# 结论:容器和容器之间可以相互ping通的 只不过要ping的是ip
结论:tomcat01 和 tomcat02 是公用的路由器,docker0
所有的容器不指定网络的情况下,都是docker0路由的,docker会给容器分配一个默认的可用IP
docker中的所有的网络接口都是虚拟的,虚拟的转发效率高
12.2 --link
[root@summer ~]# docker exec -it tomcat01 ping tomcat02 # 现在ping的是容器名字
# 无法ping通
# 再创建一个容器 tomcat03
[root@summer ~]# docker run -d -P --name tomcat03 --link tomcat02 tomcat:7.0
# tomcat03 可以ping通 tomca02
[root@summer ~]# docker exec -it tomcat03 ping tomcat02
PING tomcat02 (172.17.0.3) 56(84) bytes of data.
64 bytes from tomcat02 (172.17.0.3): icmp_seq=1 ttl=64 time=0.096 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.065 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=3 ttl=64 time=0.065 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=4 ttl=64 time=0.066 ms
# 当反向ping的时候却不通
[root@summer ~]# docker exec -it tomcat02 ping tomcat03
ping: tomcat03: Name or service not known
[root@summer ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
904ad90e4495 bridge bridge local
a223dbad46fd host host local
8e9ea4805c4d none null local
[root@summer ~]# docker network inspect 904ad90e4495
[
{
"Name": "bridge",
"Id": "904ad90e4495dc53ac3d14081e380a3cb7550f16a91291e64d71b7e4f2ece2c6",
"Created": "2022-06-21T10:38:23.234856104+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"5a4cdac3dec4650e700ce526999acd3f5c0524a24d215c4b989b3de549ed92ad": {
"Name": "tomcat03",
"EndpointID": "e4c91318686e4a1abbe0548990098541517ea00da2118b18f8fdad476c4b43ae",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/16",
"IPv6Address": ""
},
"b30f0e0fe72d1481fab62c2f21b276a3d6156ddf3af9153420cd12d1bac27ec5": {
"Name": "tomcat02",
"EndpointID": "43718b1174788805b6fc380e32c763d2a133f40a2f00164f807b455fa80d5ba2",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
},
"ded5c2e9f0e6d4ab91824bf03d8ad62a2ad0b4c074801d6a7c68c7c25b5f71fd": {
"Name": "tomcat01",
"EndpointID": "ad37d0c504839908ec5314e1a9170e0f07b2efd7481ac68b3d6caffd59097984",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
查看tomcat03的etc/hosts目录
[root@summer ~]# docker exec -it tomcat03 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.3 tomcat02 b30f0e0fe72d # 发现直接将tomcat02配置到了tomcat03的hosts文件,所以使用tomcat03可以直接ping通tomcat02,但是tomcat02的hosts文件中没有配置,所以无法ping通
172.17.0.4 5a4cdac3dec4
–link 的本质就是就是在hosts文件中添加了地址映射。
docker0不推荐使用,因为它不支持容器名连接访问,采用自定义网络
12.3 自定义网络
# 查看所有的docker网路
[root@summer ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
904ad90e4495 bridge bridge local
a223dbad46fd host host local
8e9ea4805c4d none null local
网络模式
-
bridge:桥接模式 默认
-
none:不配置网络
-
host:和宿主机共享网络
-
container:容器内网络连通 局限性大
测试
# 之前的启动命令,写完整的话就是下面写法, 默认添加 --net bridge 这个就是docker0
[root@summer ~]# docker run -d -P --name tomcat01 tomcat:7.0
[root@summer ~]# docker run -d -P --name tomcat01 --net bridge tomcat:7.0 等价
# docker0特点:默认,域名不能访问,--link可以打通连接 当时相对麻烦
自定义网络
[root@summer ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
d4565504978280143480a644aa7417e9cf570664ac2560c3a9018bc79bce8f87
[root@summer ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
904ad90e4495 bridge bridge local
a223dbad46fd host host local
d45655049782 mynet bridge local
8e9ea4805c4d none null local
[root@summer ~]#
启动两个容器
docker run -d -P --name tomcat-net-01 --net mynet tomcat:7.0
docker run -d -P --name tomcat-net-02 --net mynet tomcat:7.0
# 查看自己创建的网络
[root@summer ~]# docker inspect mynet
[
{
"Name": "mynet",
"Id": "d4565504978280143480a644aa7417e9cf570664ac2560c3a9018bc79bce8f87",
"Created": "2022-06-24T14:46:18.524200452+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"2a52adb726cc8c54907365b35be678cf9a4c4e1d52a90cf3bf99ad6cd7aa62eb": {
"Name": "tomcat-net-02",
"EndpointID": "aca5f4b8d597e78e655f54f40e676172542b5fa7fc0d54d0aacfdcbe6ac4f535",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
},
"a01207bf6a5757bc02b0c4c45d5f48eee50c3ab2f79a2ebf229313a11d7297f4": {
"Name": "tomcat-net-01",
"EndpointID": "1c074e453cac71671b1ae067a0b3738f2ef1249cfe98652679390b8de28a5d4b",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
# 再次测试ping连接 直接使用名字进行ping 发现已经可以ping通了
[root@summer ~]# docker exec -it tomcat-net-01 ping tomcat-net-02
PING tomcat-net-02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.072 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.065 ms
结论:自定义网络 docker已经维护好了所有的对应关系,推荐使用这样的方式。
12.4 网络连通
直接使用tomcat-01 ping tomcat-net-01 发现直接ping不通,进行以下操作,将容器与mynet连接
连接之后,tomcat01 就放到了mynet网络下 实现了一个容器两个ip
[root@summer ~]# docker network connect mynet tomcat01
[root@summer ~]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "d4565504978280143480a644aa7417e9cf570664ac2560c3a9018bc79bce8f87",
"Created": "2022-06-24T14:46:18.524200452+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"2a52adb726cc8c54907365b35be678cf9a4c4e1d52a90cf3bf99ad6cd7aa62eb": {
"Name": "tomcat-net-02",
"EndpointID": "aca5f4b8d597e78e655f54f40e676172542b5fa7fc0d54d0aacfdcbe6ac4f535",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
},
"a01207bf6a5757bc02b0c4c45d5f48eee50c3ab2f79a2ebf229313a11d7297f4": {
"Name": "tomcat-net-01",
"EndpointID": "1c074e453cac71671b1ae067a0b3738f2ef1249cfe98652679390b8de28a5d4b",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"d180d689e711699a1e902ab667c052c44893df169689a5ed593d0a9e72f964a6": {
"Name": "tomcat01",
"EndpointID": "ba1d7d11d0a1b75d991dde2d8a9b2e74e2d5161ce3f64006874d50ea855e5037",
"MacAddress": "02:42:c0:a8:00:04",
"IPv4Address": "192.168.0.4/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
# 连接之后,再次测试,可以ping通了
[root@summer ~]# docker exec -it tomcat01 ping tomcat-net-01
PING tomcat-net-01 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.076 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.065 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=3 ttl=64 time=0.059 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=4 ttl=64 time=0.062 ms
# tomcat02 因为没有配置,所以不可能ping通mynet下的容器
13. 部署Redis集群
搭建三主三从集群,模拟其中一个主服务器瘫痪,从服务器是否代替主服务器
# 创建网卡
[root@summer1245 ~]# docker network create redis --subnet 172.38.0.0/16
34a13cf41a8865e7b706dc7b52ab351621e723db2b2843da93146f82e327d290
[root@summer1245 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
951192152cbd bridge bridge local
5eda922715e1 host host local
843954fdae13 none null local
34a13cf41a88 redis bridge local
# 通过脚本创建六个reids配置
for port in $(seq 1 6); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
# 1
docker run -p 6371:6379 -p 16371:16379 --name redis-01 \
-v /mydata/redis/node-1/data:/data \
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
# 2
docker run -p 6372:6379 -p 16372:16379 --name redis-02 \
-v /mydata/redis/node-2/data:/data \
-v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
# 3
docker run -p 6373:6379 -p 16373:16379 --name redis-03 \
-v /mydata/redis/node-3/data:/data \
-v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
# 4
docker run -p 6374:6379 -p 16374:16379 --name redis-04 \
-v /mydata/redis/node-4/data:/data \
-v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
# 5
docker run -p 6375:6379 -p 16375:16379 --name redis-05 \
-v /mydata/redis/node-5/data:/data \
-v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
# 6
docker run -p 6376:6379 -p 16376:16379 --name redis-06 \
-v /mydata/redis/node-6/data:/data \
-v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
[root@summer1245 conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9204a686fb5 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->16379/tcp redis-06
d5a9cbd0aed5 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 25 seconds ago Up 24 seconds 0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->16379/tcp redis-05
3c45384595b0 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 48 seconds ago Up 47 seconds 0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->16379/tcp redis-04
de38fb5dbba9 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->16379/tcp redis-03
f44491d7559c redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->16379/tcp redis-02
ad978bd5c058 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->16379/tcp redis-01
[root@summer1245 conf]# docker exec -it redis-01 /bin/sh
/data # ls
appendonly.aof nodes.conf
# 创建redis集群
# --cluster-replicas 1 1其实代表的是一个比例,就是主节点数/从节点数的比例。
#在创建集群的时候,哪些节点是主节点呢?哪些节点是从节点呢?
# 按照命令中IP:PORT的顺序,先是3个主节点,然后是3个从节点。
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172
.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: aff03299aab1493a52d25ef725a8a8b600d55bf2 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: 76fc0efeef794e74eee536ec7a39c026c883b9fc 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: 7a5094cade6b7e4460bcacf98d295f7bc11b2799 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: 8315e5bcfcf0ba6534a718d17bccd87400aae706 172.38.0.14:6379
replicates 7a5094cade6b7e4460bcacf98d295f7bc11b2799
S: cbe8a91587452c77c5895961d350f919bb8d2134 172.38.0.15:6379
replicates aff03299aab1493a52d25ef725a8a8b600d55bf2
S: e84a2d2549dbfbe1047d1947eb4d7fefb575dca9 172.38.0.16:6379
replicates 76fc0efeef794e74eee536ec7a39c026c883b9fc
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 172.38.0.11:6379)
M: aff03299aab1493a52d25ef725a8a8b600d55bf2 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: cbe8a91587452c77c5895961d350f919bb8d2134 172.38.0.15:6379
slots: (0 slots) slave
replicates aff03299aab1493a52d25ef725a8a8b600d55bf2
S: e84a2d2549dbfbe1047d1947eb4d7fefb575dca9 172.38.0.16:6379
slots: (0 slots) slave
replicates 76fc0efeef794e74eee536ec7a39c026c883b9fc
M: 76fc0efeef794e74eee536ec7a39c026c883b9fc 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 7a5094cade6b7e4460bcacf98d295f7bc11b2799 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 8315e5bcfcf0ba6534a718d17bccd87400aae706 172.38.0.14:6379
slots: (0 slots) slave
replicates 7a5094cade6b7e4460bcacf98d295f7bc11b2799
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 进入集群 集群有三个 三主三从
/data # redis-cli -c
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:120
cluster_stats_messages_pong_sent:126
cluster_stats_messages_sent:246
cluster_stats_messages_ping_received:121
cluster_stats_messages_pong_received:120
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:246
127.0.0.1:6379> cluster nodes
cbe8a91587452c77c5895961d350f919bb8d2134 172.38.0.15:6379@16379 slave aff03299aab1493a52d25ef725a8a8b600d55bf2 0 1657938212618 5 connected
e84a2d2549dbfbe1047d1947eb4d7fefb575dca9 172.38.0.16:6379@16379 slave 76fc0efeef794e74eee536ec7a39c026c883b9fc 0 1657938213519 6 connected
76fc0efeef794e74eee536ec7a39c026c883b9fc 172.38.0.12:6379@16379 master - 0 1657938213619 2 connected 5461-10922
7a5094cade6b7e4460bcacf98d295f7bc11b2799 172.38.0.13:6379@16379 master - 0 1657938212000 3 connected 10923-16383
aff03299aab1493a52d25ef725a8a8b600d55bf2 172.38.0.11:6379@16379 myself,master - 0 1657938211000 1 connected 0-5460
8315e5bcfcf0ba6534a718d17bccd87400aae706 172.38.0.14:6379@16379 slave 7a5094cade6b7e4460bcacf98d295f7bc11b2799 0 1657938212517 4 connected
127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK
# 现在模拟172.38.0.13:6379@16379 master 宕机
docker stop redis-03
127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK
172.38.0.13:6379> get a # 此时因为redis-03已经挂掉,固然得不到 ctrl+c退出
/data # redis-cli -c
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379 # 可以看到从节点返回
"b"
172.38.0.14:6379> cluster nodes
# 此时可以看到
# 172.38.0.13:6379@16379 master,fail 13挂了
# 172.38.0.14:6379@16379 myself,master 14成为主节点
76fc0efeef794e74eee536ec7a39c026c883b9fc 172.38.0.12:6379@16379 master - 0 1657938652000 2 connected 5461-10922
cbe8a91587452c77c5895961d350f919bb8d2134 172.38.0.15:6379@16379 slave aff03299aab1493a52d25ef725a8a8b600d55bf2 0 1657938652237 5 connected
e84a2d2549dbfbe1047d1947eb4d7fefb575dca9 172.38.0.16:6379@16379 slave 76fc0efeef794e74eee536ec7a39c026c883b9fc 0 1657938653238 6 connected
7a5094cade6b7e4460bcacf98d295f7bc11b2799 172.38.0.13:6379@16379 master,fail - 1657938412510 1657938411698 3 connected
8315e5bcfcf0ba6534a718d17bccd87400aae706 172.38.0.14:6379@16379 myself,master - 0 1657938652000 7 connected 10923-16383
aff03299aab1493a52d25ef725a8a8b600d55bf2 172.38.0.11:6379@16379 master - 0 1657938652738 1 connected 0-5460
14. SpringBoot打包Docker镜像
-
构建SpringBoot项目
@RestController public class TestController { @RequestMapping("/test") public String test(){ return "hello docker"; } }
-
打包应用
TestDocker.jar
-
编写dockerfile
idea安装docker插件,创建一个Dockerfile文件,Dockerfile文件与jar包在同一目录下,之后上传到Linux上
FROM java:8 COPY *.jar /app.jar CMD ["--server.port=8080"] EXPOSE 8080 ENTRYPOINT ["java","-jar","/app.jar"]
-
构建镜像
[root@summer idea]# ls Dockerfile TestDocker.jar [root@summer idea]# docker build -t springboot .
-
发布运行
[root@summer idea]# docker run -d -p 8080:8080 --name springboot springboot [root@summer idea]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 978b35e6e29c springboot "java -jar /app.jar …" 36 seconds ago Up 35 seconds 0.0.0.0:49168->8080/tcp springboot # 内部测试 [root@summer idea]# curl localhost:8080/test hello docker[root@summer idea]# # 外网测试 公网ip+8080/test 访问成功