docker部署
1.安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
2.设置阿里云镜像源
cd /etc/yum.repos.d/
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3.安装docker-ce 社区版
yum install -y docker-ce
4.环境配置
systemctl stop firewalld
systemctl disable firewalldsetenforce 0vim /etc/selinux/config
SELINUX=disabled
5.设置开机自启并启动(此处为了让docker在/etc/docker/下生成daemon.json文件)
systemctl start docker
systemctl enable docker
6.修改配置文件:==》 registry-mirrors 为加速器
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://jqqwsp8f.mirror.aliyuncs.com"]
}
EOF
7.修改完成后需要重启
systemctl daemon-reload
systemctl restart docker
8.网络优化,此处开启ipv4的转发功能
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p
systemctl restart network
systemctl restart docker
consul部署
1.创建consul文件夹
mkdir /root/consul
2.拷贝
cp consul_0.9.2_linux_amd64.zip /root/consul
cd /root/consul
3.解压
上传consul软件包
unzip consul_0.9.2_linux_amd64.zip
4.将文件移动到/usr/bin
mv consul /usr/bin
5.执行命令,设置consul的server
consul agent \
-data-dir=/var/lib/consul-data \
-bind=192.168.218.214 \
-server \
-bootstrap \
-ui \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
6.验证集群信息
consul members
或者
consul info | grep leader##通过httpd api 获取集群信息
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 服务信息
验证结果
consul服务器上部署nginx
1.安装依赖包
yum -y install gcc gCc-c++ pcre-devel zlib-devel make
2.创建用户组
useradd -M -s /sbin/nologin nginx
3.解压opt/中的nginx.tar
tar xzvf nginx -1.15.9.tar.gz
4.进入文件,并加载模块初始化
cd nginx-1.15.9/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
5.编译并安装
make && make install
6.路径优化
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
ls -l /usr/local/sbin/nginx
7.设置nginx的systemctl 的管理
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
8.配置 nginx
vim /usr/local/nginx/conf/nginx.confhttp {
include mime.types;include vhost/*.conf; //添加虚拟主机目录(consul动态生成的配置文件就会放在这里)default_type application/octet-stream;##创建虚拟主机目录 mkdir /usr/local/nginx/conf/vhost ##创建日志文件目录 mkdir /var/log/nginx
9.赋权
chmod +x /usr/lib/systemd/system/nginx.service
10.启动
systemctl start nginx
11.验证
netstat -antp | grep 80
web端部署docker-compose
1.//下载compose(上传docker_compose)
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
2.拷贝到/usr/local/bin
cp -p docker-compose /usr/local/bin/
chmod +x /usr/local/bin/docker-compose
3.创建compose_nginx
mkdir /root/compose_nginx
4.修改配置文件
vim /root/compose_nginx/docker-compose.ymlversion: '3'
services:nginx:hostname: nginxbuild:context: ./nginxdockerfile: Dockerfileports:- 1216:80- 1217:443networks:- clustervolumes:- ./wwwroot:/usr/local/nginx/html
networks:cluster:
5.进入compose_nginx目录
cd /root/compose_nginx/
mkdir nginx
vim Dockerfile
FROM centos:7 as build
ADD nginx-1.15.9.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.9
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.15.9 FROM centos:7
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
COPY --from=build /usr/local/nginx /usr/local/nginx
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
6.创建镜像并启动
docker-compose -f docker-compose.yml up -d
设置容器服务自动加入consul
1.执行registrator命令
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.218.215 \
consul://192.168.218.214:8500
2.测试服务发现功能是否正常
docker run -itd -p:83:80 --name test-01 -h test01 nginx
3.验证 http 和 nginx 服务是否注册到 consul
浏览器输入 http://192.168.218.214:8500,“单击 NODES”,然后单击 “consurl-server01”,会出现 1 个服务.
在consul服务端配置并启动template
上传 consul-template_0.19.3_linux_amd64.zip 包到/root 目录下
cp consul-template_0.19.3_linux_amd64.zip /root/
3.解压包
unzip consul-template_0.19.3_linux_amd64.zip
4.将解压的文件夹移动到/usr/bin/
mv consul-template /usr/bin/
5.设置template nginx模板
upstream http_backend {
{
{
range service "nginx"}}server {
{
.Address}}:{
{
.Port}}; #??此处引用的变量会指向后端的地址和端口(动态变化){
{
end}}
}server {
listen 83;server_name localhost 192.168.218.214; #反向代理的IP地址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; #后端真实IPproxy_set_header Client-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #转发地址proxy_pass http://http_backend;}
}
6.执行consul-template命令,主要是将模板数据进行复制同时将参数不全,重新加载nginx
nohup consul-template -consul-addr 192.168.218.214:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/shiki.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info &
7.查看验证
cat /usr/local/nginx/conf/vhost/shiki.conf