Docker单机网络拓补图
Docker的网络通信
端口映射机制将内容的服务提供给外都网络访问
可随机或指定映射端口范围
docker run -d httpd:centos
docker run -d -p 49888:80 httpd:centos
Docker Compose容器编排
Docker Compose的前身是Fig,他是一个定义及运行多个Docker容器的工具
Docker Compose非常合适组合使用多个容器进行开发的场景
Dcoker Compose配置常用字段
Compose常用命令
Compose命令说明
- docker-compose选项
–version 打印版本并退出
–verbose 输出更多调试信息
-f ,–file FILE 使用特定的compose模板文件,默认为docker-compose.yml
-p , --project-name NAME 指定项目名称,默认使用目录名称
--------------------------compose部署----------------------------
1、在线安装compose(大约20分钟)
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
1.1、安装包将安装吧放在usr/local/bin目录下
根目录创建compose_nginx
mkdir /compose_nginx
cd /compose_nginx
mkdir nginx
cd nginx/
vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
将nginx源码包上传至nginx目录
vim Dockerfile
FROM centos:7
MAINTAINER This is Nginx test
RUN yum -y update
RUN yum -y install make gcc gcc-c++ pcre-devel zlib-devel tar
RUN useradd -M -s /sbin/nologin nginx
COPY nginx-1.12.2.tar.gz /opt/
RUN tar zxvf /opt/nginx-1.12.2.tar.gz -C /opt
WORKDIR /opt/nginx-1.12.2/
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
RUN make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ADD index.html /usr/local/nginx/html/
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
在compose_nginx创建站点目录
mkdir wwwroot
cd wwwroot
echo "<h1>this is nginx test web</h1>" >> index.html
编写编排脚本
vim docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1234:80
- 1235:443
networks:
- aaa
volumes:
- ./wwwroot:/usr/local/nginx/html
networks:
aaa:
在compose_nginx输入
docker-compose -f docker-compose.yml up -d
-----------------------------------consul部署------------------------------
服务器:192.168.49.129 Docker-ce Compose Consul Consul-template
服务器:192.168.49.200 Docker-ce registrator
consul服务器部署
根目录创建
mkdir consul
挂载
cp /opt/consul_0.9.2_linux_amd64.zip /root/consul/
cd /root/consul/
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/bin/
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.49.129 \ (需要改本地地址)
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
解释
consul agent \ //使用代理功能
-server \ //提供server功能
-bootstrap \ //参与选举为领袖
-ui \ //提供web访问界面
-data-dir=/var/lib/consul-data \ //参数存储位置
-bind=192.168.7.168 \ //绑定地址
-client=0.0.0.0 \ //面对所有节点终端
-node=consul-server01 &> /var/log/consul.log & //指定本地节点名称,指定日志文件目录,放在后台运行
#查看群集信息
consul members
需要关闭防火墙或者开放8500端口,关闭核心防护
firewall-cmd --get-active-zones
firewall-cmd --zone=public --add-port=8500/tcp
setenforce 0
-------------容器服务自动添加到nginx集群(192.168.49.200)------
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.49.200 \
consul://192.168.49.129:8500
#解释
[root@docker2 ~]# docker run -d \
--name=registrator \ //指定名称
--net=host \ //指定网络
-v /var/run/docker.sock:/tmp/docker.sock \ //指定数据卷
--restart=always \ //restart功能
gliderlabs/registrator:latest \
-ip=192.168.7.129 \ //指定本地IP地址
consul://192.168.7.168:8500 //指consul 地址
测试服务发现功能是否正常
docker run -itd -p:83:80 --name test-01 -h test01 nginx
docker run -itd -p:84:80 --name test-02 -h test02 nginx
docker run -itd -p:88:80 --name test-03 -h test03 httpd
docker run -itd -p:89:80 --name test-04 -h test04 httpd
验证http和nginx服务是否注册到consul
浏览器输入"http://192.168.7.168:8500",单击"NODES",然后单击"consul-server01",会出现5个服务
在consul服务骑上查看服务
curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"nginx":[]}
准备template nginx模板文件在192.168.49.129
vim /root/consul/nginx.ctmpl
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 83;
server_name localhost 192.168.49.129;
access_log /var/log/nginx/kgc.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
编译安装并配置nginx(在192.168.49.129)
yum install gcc pcre-devel zlib-devel -y
tar zxvf nginx-1.12.2.tar.gz -C /opt
./configure --prefix=/usr/local/nginx
make && make install
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf;
default_type application/octet-stream;
创建虚拟主机目录
mkdir /usr/local/nginx/conf/vhost
创建日志文件目录
mkdir /var/log/nginx
启动nginx
/usr/local/nginx/sbin/nginx
配置并启动template
上传consul-template_0.19.3_linux_amd64.zip到/root/consul目录下
unzip consul-template_0.19.3_linux_amd64.zip
mv consul-template /usr/bin
consul-template -consul-addr 192.168.49.129:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/aaa.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
#启动后,会在vhost目录下自动生成aaa.conf配置文件
ls /usr/local/nginx/conf/vhost/
aaa.conf
使用浏览器访问http://192.168.49.129:83,查看容器nginx的访问日志
增加一个nginx容器节点,测试服务发现及配置更新功能(在registrator服务端增加)
docker run -itd -p:85:80 --name test-03 -h test03 nginx
consul服务器监控会有提示自动更新
如果关闭一个容器节点,会在consul服务端注销此容器节点