build ##重新构建服务
ps ##列出容器
up ##创建和启动容器
exec ##在容器里面执行命令
scale ##指定一个服务容器启动数量
top ##显示容器进程
logs ##查看容器输出
down ##删除容器、网络、数据卷和镜像
stop/start/restart ##停止/启动/重启服务
build dockerfile context ##指定Dockerfile文件名构建镜像.上下文路径
image ##指定镜像
command ##执行命令,覆盖默认命令
container name ##指定容器名称,由于容器名称是唯一的, 如果指定自定义名称,则无法scale
deploy ##指定部署和运行服务相关配置,只能在Swarm模式使用
environment ##添加环境变量
networks ##加入网络
ports ##暴露容器端口,与-p相同,但端口不能低于60
volumes ##挂载宿主机路径或命令卷
restart ##重启策略,默认no,always, no-failure,unless-stoped
hostname ##容器主机名
Consul是HashiCorp公司推出的开源工具, 用于实现分布式系统的服务发现与配置
Consul的特性:
①支持健康检查,允许存储键值对
②基于Golong语言,可移植性强
③支持ACL访问控制
同时可与Docker等轻量级容器可无缝配合
实验:
192.168.27.137 docker concul agent/server/template nginx手工编译安装
192.168.27.139 docker nginx httpd
参考走进docker世界博文
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
或者
上传docker_compose即可
chmod +x docker-compose ##加执行权限
cp -p docker-compose /usr/local/bin
mkdir /root/compose_nginx
cd compose_nginx/
mkdir nginx wwwroot
cd nginx
上传nginx-1.12.2.tar.gz压缩包
vim Dockerfile ##编写文件
FROM centos:7
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \
yum clean all && \
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \
./configure --prefix=/usr/local/nginx &> /dev/null && \
make &> /dev/null && make install &> /dev/null &&\
rm -rf /mnt/nginx-1.12.2
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
cd wwwroot
vim index.html ##创建nginx页面
happy
vim /root/compose_nginx/docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1216:80
- 1217:443
networks:
- cluster
volumes:
- ./wwwroot:/usr/local/nginx/html
networks:
cluster:
[root@consul compose_nginx]# docker-compose -f docker-compose.yml up -d ##基于yml文件运行启动
[root@consul compose_nginx]# docker-compose ps ##使用Compose容器编排
[root@consul compose_nginx]# tree ./
./
├── docker-compose.yml //创建模板脚本
├── nginx
├── Dockerfile //创建容器脚本
├── nginx-1.15.9.tar.gz //复制源码包
└── wwwroot
└── index.html //站点
实验:
192.168.27.137 Docker-ce、 Compose 3、Consul、 Consul-template
192.168.27.139 Docker-ce、registrator
template 模更新
registrator 自动发现
registrator 控制consul 完成更新操作
consul template模板 进行热更新
consul 自动发现、自动更新,为容器提供服务(添加、删除、生命周期)
mkdir consul
cd consul
上传consul_0.9.2_linux_amd64.zip压缩包
unzip consul_0.9.2_linux_amd64.zip ##解压
mv consul /usr/local/bin/
consul agent \
-server \ 服务端
-bootstrap \ 前端框架
-ui \ 可被访问的web界面
-data-dir=/var/lib/consul-data \
-bind=192.168.27.137 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
[root@consul consul]# consul members ##查看集群信息
Node Address Status Type Build Protocol DC
consul-server01 192.168.226.130:8301 alive server 0.9.2 2 dc1
[root@web consul]# consul info | grep leader
leader = true
leader_addr = 192.168.27.137:8300
curl 127.0.0.1:8500/v1/status/peers //查看集群server成员
curl 127.0.0.1:8500/v1/status/leader //集群 Raf leader
curl 127.0.0.1:8500/v1/catalog/services //注册的所有服务
curl 127.0.0.1:8500/v1/catalog/nginx //查看 nginx 服务信息
[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/nodes //集群节点详细信息
[{"ID":"ac1937e8-8f21-bbc0-61d6-0a24ea946cee","Node":"consul-server01","Address":"192.168.27.137","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.27.137","wan":"192.168.27.137"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}]
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.27.139 \
consul://192.168.27.137:8500
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
[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[],"nginx":[]}
在consul上操作
vim /root/consul/nginx.ctmpl ##c:concul;tmp:template
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}}; #此处引用的变量会指向后端的地址和端口(动态变化)
{{end}}
}
server {
listen 83; ##ngixn的监听端口
server_name localhost 192.168.35.40; #反向代理的IP地址(前端展示的NG服务的IP)
access_log /var/log/nginx/klj.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr; #后端真实IP
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #转发地址
proxy_pass http://http_backend;
}
}
上传压缩包nginx-1.12.2.tar.gz
yum install gcc pcre-devel zlib-devel -y
tar zxvf nginx-1.12.2.tar.gz -C /opt
cd nginx-1.12.2
./configure --prefix=/usr/local/nginx
make && make install
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types; #默认存在的
include vhost/*.conf; ##添加虚拟主机目录为了让那个识别vhost/*.conf的文件(consul动态生成的配置文件就会放在这里)
default_type application/octet-stream;
mkdir /usr/local/nginx/conf/vhost ##创建虚拟主机目录
mkdir /var/log/nginx ##创建日志文件目录
ln -s /usr/local/nginx/sbin/nginx /usr/sbin
nginx ##启动nginx
上传 consul-template_0.19.3_linux_amd64.zip 包到/root 目录下
cp consul-template_0.19.3_linux_amd64.zip /root/
unzip consul-template_0.19.3_linux_amd64.zip ##解压
mv consul-template /usr/bin/
consul-template -consul-addr 192.168.27.137:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/klj.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info ##日志级别:info
[root@consul ~]# cat /usr/local/nginx/conf/vhost/klj.conf ##另外打开一个终端查看生成配置文件
upstream http_backend {
server 192.168.27.139:84;
server 192.168.27.139:85;
}
server {
listen 83; server_name localhost 192.168.27.137;
access_log /var/log/nginx/klj.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;
}
}
[root@docker bin]# docker run -itd -p:86:80 --name test-06 -h test06 nginx
74d1a35d21e907dc58ba6cb0b258e26f4c56fec4f1da495b76d84907c0f0ad43
[root@docker bin]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74d1a35d21e9 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:86->80/tcp, :::86->80/tcp test-06
688c5c1083f5 nginx "/docker-entrypoint.…" 22 hours ago Exited (255) 19 minutes ago 0.0.0.0:85->80/tcp, :::85->80/tcp test-05
107aa62fb5aa httpd "httpd-foreground" 24 hours ago Exited (255) 19 minutes ago 0.0.0.0:89->80/tcp, :::89->80/tcp test-04
6aeb8cbb2050 httpd "httpd-foreground" 24 hours ago Exited (255) 19 minutes ago 0.0.0.0:88->80/tcp, :::88->80/tcp test-03
b1e96f6388ec nginx "/docker-entrypoint.…" 24 hours ago Exited (255) 19 minutes ago 0.0.0.0:84->80/tcp, :::84->80/tcp test-02
403bf203b9e6 gliderlabs/registrator:latest "/bin/registrator -i…" 24 hours ago Up 5 minutes registrator
[root@docker bin]# docker rm -f 74d1a35d21e9 ##删除86端口的服务器
74d1a35d21e9