Centos7 docker nginx tomcat dynamic and static separation load balancing configuration

Mini install centos7 64 bit
yum -y update
yum -y install lrszs nettools


Close selinux
vi /etc/selinux/config and change the line inside to
SELINUX=disabled

Save the changes and then restart.
getenforce

install docker
yum install docker docker-devel
systemctl start docker.service
systemctl enable docker.service


Docker basic command description
docker ps running container
docker ps -a all containers
docker images all images
docker rm [container id] delete container
docker rm -f [container id] force delete container
docker rmi [image id] delete image
docker rmi -f [image id] Force delete image
docker run -it -v [local directory]:[docker directory] [image id] /bin/bash start the container
docker run -itd -p [local port]:[docker port] -v[local directory]:[docker directory] [image id] /bin/bash start the port to start the container
docker exec -i -t [container id]/bin/bash console container
docker stop [container id] stop the container
docker commit [container id] Image name: version number, create the image production version number according to the container


Get the centos image
docker pull centos


View mirror
docker images
REPOSITORY    TAG      IMAGE ID      CREATED         SIZE
centos7       001      196e0ce0c9fb  12 days ago     196.6 MB


Modify image name
docker tag [image id] [image name]:[version number]


Start the container, configure nginx, and generate an nginx image
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 [image 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


Modify 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 is;
        keepalive_timeout       65;
        include         /application/nginx/conf/ext.d/*.conf;
}


Exit docker and generate the image
exit
docker stop [container id]
docker commit [container 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


Create a directory
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


Configure nginx other configuration files
cd /application/nginx/conf/ext.d/
vi cluster_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;
}

vi gzip.conf
gzip on; #Enable gzip compression output
gzip_min_length 1k; #Minimum compressed file size
gzip_buffers 4 16k; #Compression buffer
gzip_http_version 1.0; #Compression version (default 1.1, if the front end is squid2.5, please use 1.0)
gzip_comp_level 2; #compression level
gzip_types text/plain application/x-javascript text/css application/xml;    
gzip_vary on;

we test_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;
        }
}

vi fastcgi.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;


Mount the directory to start the nginx image
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


Check whether the nginx configuration file in docker is normal or not
/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


Check to start nginx inside docker
/application/nginx/sbin/nginx


Check if html accesses
cd /application/nginx/html/
vi index.html normally
<html>
        <head>
                <title>index</title>
        </head>
        <body>
        ####### nginx index test
        </body>
</html>

Access
curl http://172.16.33.250 outside docker

to mount the centos7 image again, configure tomcat, and generate the tomcat image
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

exit docker
exit


Generate tomcat image
docker commit [container id] tomcat:1.0


vi /application/tc01/index.jsp
tomcat01

vi /application/tc02/index.jsp
tomcat02


Mount the directory to start the tomcat image container
docker run --name tomcat_01 -itd -p 9090:8080 -v /application/tc01/:/application/tomcat/webapps/ROOT/ tomcat:1.0 /root/run.sh
docker run -- name tomcat_02 -itd -p 9091:8080 -v /application/tc02/:/application/tomcat/webapps/ROOT/ tomcat:1.0 /root/run.sh

test tomcat
curl http://172.16.33.250:9090/index.jsp
tomcat01
curl http://172.16.33.250:9091/index.jsp
tomcat02


test nginx static
curl http://172.16.33.250/index.html

<html>
        <head>
                <title>index</title>
        </head>
        <body>
        ####### nginx index test
        </body>
</html>


Test nginx dynamic
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


docker ps
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


docker images
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


If you want to modify the nginx configuration file, modify *.conf directly in the /application/nginx/conf/ext.d/ directory on the host; use the following command to load the configuration file:
docker exec [容器id] /application/nginx/sbin/nginx -t
docker exec [容器id] /application/nginx/sbin/nginx -s reload







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326250115&siteId=291194637