centos7 docker nginx tomcat 动静分离 负载均衡配置
mini安装centos764位
yum -y update yum -y install lrszs nettools
关闭selinux
vi/etc/selinux/config,把里边的一行改为
SELINUX=disabled
改了之后保存,然后重启就可以了。
getenforce
安装docker
yum install docker docker-devel systemctl start docker.service systemctl enable docker.service
docker基本命令说明
docker ps 正在运行的容器 docker ps -a 所有容器 docker images 所有镜像 docker rm [容器id] 删除容器 docker rm -f [容器id] 强制删除容器 docker rmi [镜像id] 删除镜像 docker rmi -f [镜像id] 强制删除镜像 docker run -it -v[本地目录]:[docker目录] [镜像id] /bin/bash 启动容器 docker run -itd -p [本地端口]:[docker端口] -v[本地目录]:[docker目录] [镜像id] /bin/bash 启动端口启动容器 docker exec -i -t [容器id]/bin/bash console容器 docker stop [容器id] 停止容器 docker commit [容器id] 镜像名称:版本号, 根据容器创建镜像生产版本号
获取centos镜像
docker pull centos
查看镜像
docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7 001 196e0ce0c9fb 12 days ago 196.6 MB
修改镜像名
docker tag [镜像id] [镜像名]:[版本号]
启动容器,配置nginx,生成nginx镜像
vi /etc/selinux/config SELINUX=disabled mkdir -p /root/software /mnt/software /application/nginx/html/pic /application/nginx/html/css /application/nginx/conf/ext.d /application/nginx/logs docker run -itd -p 80:80 -v /root/software:/mnt/software [镜像id] /bin/bash docker exec -i -t [容器id] /bin/bash yum -y update yum -y install lrszs nettools yum -y install pcre pcre-devel openssl openssl-devel gcc-c++ useradd -s /sbin/nologin -M nginx tar zxvf nginx-1.13.5.tar.gz ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.13.5 --with-http_stub_status_module --with-http_ssl_module make && make install ln -s /application/nginx-1.13.5/ /application/nginx cd /application/nginx/conf egrep -v "#|^$" nginx.conf.default > nginx.conf mkdir -p /application/nginx/conf/ext.d
修改nginx.conf
vi/application/nginx/conf/nginx.conf
worker_processes 4; error_log logs/error.log; pid logs/nginx.pid; events { use epoll; worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 64k; client_max_body_size 8m; sendfile on; autoindex on; keepalive_timeout 65; include /application/nginx/conf/ext.d/*.conf; }
退出docker,生成镜像
exit docker stop [容器id] docker commit [容器id] nginx:1.0 docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.0 c2dc10ae5d59 24 hours ago 353.6 MB centos7 001 196e0ce0c9fb 12 days ago 196.6 MB
创建目录
mkdir -p /root/software /mnt/software /application/nginx/html/pic /application/nginx/html/css /application/nginx/conf/ext.d /application/nginx/logs /application/tc01 /application/tc02
配置nginx其他配置文件
cd/application/nginx/conf/ext.d/
vicluster_tomcat.conf
upstream cluster_tomcat { server 172.16.33.250:9090 max_fails=3 weight=1 fail_timeout=60s; server 172.16.33.250:9091 max_fails=3 weight=1 fail_timeout=60s; }
vigzip.conf
gzip on; #开启gzip压缩输出 gzip_min_length 1k; #最小压缩文件大小 gzip_buffers 4 16k; #压缩缓冲区 gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0) gzip_comp_level 2; #压缩等级 gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on;
vitest_server.conf
server { listen 80; server_name 172.16.33.250; location / { root html; index index.html index.htm; } location ~ .*\.(jsp|jspx|do)?$ { proxy_pass http://cluster_tomcat; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { root html/pic; expires 3d; } location ~* \.(css|js)$ { root html/css; expires 1d; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
vifastcgi.conf
fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k;
挂载目录启动nginx镜像
docker run --name nginx_web -itd -p 80:80 -v /application/nginx/conf/ext.d/:/application/nginx/conf/ext.d/ -v /application/nginx/html/:/application/nginx/html/ -v /application/nginx/logs/:/application/nginx/logs/ nginx:1.0 /bin/bash docker exec -i -t [容器id] /bin/bash
检查docker里面nginx配置文件正常与否
/application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.13.5/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.13.5/conf/nginx.conf test is successful
检查docker里面启动nginx
/application/nginx/sbin/nginx
检查html是否正常访问
cd/application/nginx/html/
viindex.html
<html> <head> <title>index</title> </head> <body> ####### nginx index test </body> </html>
docker外访问
curlhttp://172.16.33.250
再次挂载centos7镜像,配置tomcat,生成tomcat镜像
docker run -it -v /root/software:/mnt/software [镜像id] /bin/bash cd /mnt/software tar -zxvf apache-tomcat-7.0.81.tar.gz mv apache-tomcat-7.0.81 /application/tomcat tar -zxvf jdk-8u144-linux-x64.tar.gz mv /root/software/jdk1.8.0_144/ /application/jdk vi /etc/profile JAVA_HOME=/application/jdk JRE_HOME=$JAVA_HOME/jre PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin CLASSPATH=:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib/dt.jar export JAVA_HOME JRE_HOME PATH CLASSPATH source /etc/profile java -version java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode) vi /root/run.sh #!/bin/bash source /etc/profile sh /application/tomcat/bin/catalina.sh run chmod u+x /root/run.sh 退出docker exit
生成tomcat镜像
docker commit [容器id] tomcat:1.0
vi/application/tc01/index.jsp
tomcat01
vi/application/tc02/index.jsp
tomcat02
挂载目录启动tomcat镜像容器
dockerrun--nametomcat_01-itd-p9090:8080-v/application/tc01/:/application/tomcat/webapps/ROOT/tomcat:1.0/root/run.sh
dockerrun--nametomcat_02-itd-p9091:8080-v/application/tc02/:/application/tomcat/webapps/ROOT/tomcat:1.0/root/run.sh
测试tomcat
curl http://172.16.33.250:9090/index.jsp tomcat01 curl http://172.16.33.250:9091/index.jsp tomcat02
测试nginx静态
curl http://172.16.33.250/index.html <html> <head> <title>index</title> </head> <body> ####### nginx index test </body> </html>
测试nginx动态
for((i=1;i<=10;i++));do curl http://172.16.33.250/index.jsp;done tomcat01 tomcat02 tomcat01 tomcat02 tomcat01 tomcat02 tomcat01 tomcat02 tomcat01 tomcat02
dockerps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06c418118a67 3977c664af2d "/bin/bash" 26 hours ago Up 26 hours 0.0.0.0:80->80/tcp kickass_jepsen bed9b0fd8dd5 tomcat:1.0 "/root/run.sh" 28 hours ago Up 28 hours 0.0.0.0:9091->8080/tcp tender_perlman 321877b25511 tomcat:1.0 "/root/run.sh" 28 hours ago Up 28 hours 0.0.0.0:9090->8080/tcp stupefied_engelbart
dockerimages
REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.0 c2dc10ae5d59 25 hours ago 353.6 MB tomcat 1.0 ae63e6316a77 46 hours ago 662.4 MB centos7 001 196e0ce0c9fb 12 days ago 196.6 MB
如果要修改nginx的配置文件,直接在宿主机上的/application/nginx/conf/ext.d/目录下修改*.conf;使用下面命令加载配置文件:
docker exec [容器id] /application/nginx/sbin/nginx -t docker exec [容器id] /application/nginx/sbin/nginx -s reload