|
#搭建nginx镜像
docker run -it --privileged --name tmp centos /usr/sbin/init
## 另外打开一个ssh窗口
docker ps
docker exec -it tmp /bin/bash
## 使用yum安装nginx
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx -y
## 设置开机启动并启动nginx
systemctl enable nginx
systemctl start nginx
## 把修改好的容器tmp提交成一个新的镜像 docker commit
docker commit -c 'CMD ["/usr/sbin/init"]' -c "EXPOSE 80" tmp centos:nginx
## 从容器中拷贝文件到主机 docker cp
docker cp tmp:/etc/nginx/nginx.conf /home/sucd/nginx/conf/
## 启动镜像
docker run --name mynginx --privileged -p 9090:80 -v /home/sucd/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -d centos:nginx
#nginx负载均衡准备工作
## 基于centos:httpd镜像创建两个容器
docker run --name web1 -d -p 8080:80 --privileged -v /home/sucd/nginx/web1/:/var/www/html/ centos:httpd
docker run --name web2 -d -p 8081:80 --privileged -v /home/sucd/nginx/web2/:/var/www/html/ centos:httpd
## 使用docker compose编排
### 文档地址: https://docs.docker.com/compose/install/#install-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
## 然后赋予可执行权限
sudo chmod +x /usr/local/bin/docker-compose
## 查看下版本
docker-compose -v
## 创建目录和新建 docker-compose.yml文件
### docker-compose命令文档 https://docs.docker.com/compose/reference/overview/
## 详情参见 docker-compose.yml 文件
## yaml文件在线检查工具 : http://www.yamllint.com/
## docker-compose启动容器
docker-compose up -d
## 查看所有网络
docker network ls
## 查看某个网络
docker network inspect mycompose_default
## 查看容器IP
docker exec -it web2 /bin/bash
cat /etc/hosts
## 创建一个mynginx网络并指定子网地址
docker network create -d bridge --subnet=192.168.0.0/16 mynginx
1、compose up时自动创建网络
2、容器创建时指定IP
3、nginx配置负载均衡
## 先停止容器然后删除容器
docker-compose rm -sf
## 详情参见 docker-compose.yml 文件
## 配置nginx负载均衡 /etc/nginx/nginx.conf
cat /home/sucd/nginx/conf/nginx.conf
upstream mydocker {
server 192.156.0.6;
server 192.156.0.3;
}
server {
listen 80;
server_name mydocker;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_pass http://mydocker;
}
}
## 重启
docker-compose up -d
## 查看网络
docker network ls
docker network inspect mycompose_mynginx-net
## 虚拟机中直接访问mynginx容器
curl http://192.156.0.2
## 或者浏览器访问虚拟机 http://192.168.10.11:9090/ 打印web1或者web2,表示负载均衡完成
# 使用compose管理容器的健康检查
## 以健康检查机制创建容器web3
docker run --name web3 -d -p 8082:80 --privileged -v /home/sucd/nginx/web1/:/var/www/html --health-cmd="curl --silent --fail http://localhost:80/ || exit 1" --health-interval=3s --health-retries=3 --health-timeout=5s centos:httpd
## 另外打开一个窗口,并且关闭掉httpd服务。
docker exec -it web3 /bin/bash
[root@bc59ac44db79 /]# systemctl stop httpd
docker ps 会出现 unhealthy
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc59ac44db79 centos:httpd "/bin/sh -c /usr/sbi…" About a minute ago Up About a minute (unhealthy) 0.0.0.0:8082->80/tcp web3
## 使用compose设置健康检查
https://docs.docker.com/compose/compose-file/#healthcheck
## 具体查看 docker-compose.yml healthcheck部分
docker-compose.yml文件内容
cat /home/sucd/mycompose/docker-compose.yml
services:
nginx:
container_name: mynginx
image: "centos:nginx"
ports:
- "9090:80"
privileged: true
volumes:
- "/home/sucd/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
networks:
mynginx-net:
ipv4_address: 192.156.0.2
healthcheck:
test: ["CMD", "curl","-s","-f", "http://localhost:80"]
interval: 5s
timeout: 5s
retries: 3
web1:
container_name: web1
image: "centos:httpd"
ports:
- "8080:80"
privileged: true
volumes:
- "/home/sucd/nginx/web1/:/var/www/html/"
networks:
mynginx-net:
ipv4_address: 192.156.0.6
healthcheck:
test: ["CMD", "curl","-s","-f", "http://localhost:80"]
interval: 5s
timeout: 5s
retries: 3
web2:
container_name: web2
image: "centos:httpd"
ports:
- "8081:80"
privileged: true
volumes:
- "/home/sucd/nginx/web2/:/var/www/html/"
networks:
mynginx-net:
ipv4_address: 192.156.0.3
healthcheck:
test: ["CMD", "curl","-s","-f", "http://localhost:80"]
interval: 5s
timeout: 5s
retries: 3
networks:
mynginx-net:
driver: bridge
ipam:
config:
- subnet: 192.156.0.0/16
version: "3"
|
|
|