1、新增一个名称为test的bridge模式网络,网段为172.18.2.0/24,创建一个容器用test网络运行,要求能用宿主机网络访问。
1.创建自定义网络test
docker network create -d bridge --subnet 172.18.2.0/24 --gateway 172.18.2.1 test
2.查看创建的自定义网络
[13:22:59 root@ubuntu2004 ~]#docker network ls
NETWORK ID NAME DRIVER SCOPE
6c0884e4f6b0 bridge bridge local
4de0e6c56bf5 host host local
b1baa05ff80f none null local
d3a98d9a2c11 test bridge local
3.运行容器时使用自定义网络test
docker run --name nginx --network test -d nginx
4.查看容器IP地址信息
docker inspect --format '{{.NetworkSettings.Networks.test.IPAddress}}' nginx
172.18.2.2
5.宿主机进行访问
curl 172.18.2.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
2、创建一个harbor镜像仓库,把镜像上传到harbor仓库并能从里面下载镜像创建容器。
1.安装docker
修改/etc/docker/daemon.json加速
{
"registry-mirrors": ["https://si7y70hh.mirror.aliyuncs.com"],
"live-restore": true,
"insecure-registries": ["www.liguox.org"]
}
2.安装docker compose
apt -y install python-pip
pip install docker-compose
3.创建目录,并下载harbor安装包解压(192.168.29.225)
tar xf harbor-offline-installer-v2.8.2.tgz -C /apps/
4.修改配置文件
hostname: www.liguox.org //ip或FQDN
harbor_admin_password: 123456
不使用https可将https相关内容注释
5.执行harbor安装脚本
./install.sh
6.设置开机自启动
7.配置域名解析
8.启动harbor后,命令行登录
harbor docker login www.liguox.org
9.给镜像贴标签
docker tag jpress:v1.0 www.liguox.org/lgx/jpress:v1.0
10.上传镜像到私有仓库
docker push www.liguox.org/lgx/jpress:v1.0
11.复制拉取项目命令在另一台客户端(192.168.29.223)进行镜像拉取(客户端也需要配置daemon.json文件和域名解析)
docker pull www.liguox.org/lgx/jpress@sha256:0054b7fbcdfe946a74a842498a26c96b412f442207370f374ca0a8b759b8ee47
12.运行拉取后的镜像
docker run -d --name jpress -p 8080:8080 43cf29e85ebd
3、用haproxy实现两个web服务器的负载均衡。
1.准备haproxy服务器,安装haproxy
192.168.29.223 网卡eth0 NAT模式
192.168.32.223 网卡eth1 仅主机模式
# apt-get install --no-install-recommends software-properties-common
# add-apt-repository ppa:vbernat/haproxy-2.8
# apt-get install haproxy=2.8.\*
修改配置文件,添加负载均衡服务器
vim /etc/haproxy/haproxy.cfg
listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
stats uri /haproxy-stats
stats auth admin:123456
listen web
mode http
bind 192.168.32.223:80
server web1 192.168.29.100:80 check inter 5000
server web2 192.168.29.226:80 check inter 5000
重载haproxy服务文件
systemctl reload haproxy.service
2.准备两台web服务器,安装nginx,配置页面信息
192.168.29.100 网卡NAT模式
192.168.29.226 网卡NAT模式
3.准备客户端服务器
192.168.32.227 网卡仅主机模式
route add -net 192.168.29.0/24 gw 192.168.32.223 //添加路由
测试访问,查看页面显示内容
curl 192.168.32.223
查看状态页查看服务器组状态
http://192.168.29.223:9999/haproxy-stats