容器云环境下,Nginx+tomcat+redis实现web项目集群

时间:2021-09-04 16:39:44

环境:由于在Windows下Tomcat8与Nginx实验不成功,错误的认为8不行,就有了下面的历程,,,,经过请教师兄,发现自己的方法不对,


#利用centos基础镜像以及jdk Tomcat 编辑新的镜像Tomcat7

#删掉jdk文件夹下多余文件, 降低build的镜像大小
rm -rf /root/image/jdk7/*src.zip \
    /root/image/jdk7/lib/missioncontrol \
    /root/image/jdk7/lib/visualvm \
    /root/image/jdk7/lib/*javafx* \
    /root/image/jdk7/jre/lib/plugin.jar \
    /root/image/jdk7/jre/lib/ext/jfxrt.jar \
    /root/image/jdk7/jre/bin/javaws \
    /root/image/jdk7/jre/lib/javaws.jar \
    /root/image/jdk7/jre/lib/desktop \
    /root/image/jdk7/jre/plugin \
    /root/image/jdk7/jre/lib/deploy* \
    /root/image/jdk7/jre/lib/*javafx* \
    /root/image/jdk7/jre/lib/*jfx* \
    /root/image/jdk7/jre/lib/amd64/libdecora_sse.so \
    /root/image/jdk7/jre/lib/amd64/libprism_*.so \
    /root/image/jdk7/jre/lib/amd64/libfxplugins.so \
    /root/image/jdk7/jre/lib/amd64/libglass.so \
    /root/image/jdk7/jre/lib/amd64/libgstreamer-lite.so \
    /root/image/jdk7/jre/lib/amd64/libjavafx*.so \
    /root/image/jdk7/jre/lib/amd64/libjfx*.so
#编辑Dockerfile文件
cd /root/image

vi Dockerfile

#插入以下内容
#使用的基础镜像
FROM registry.cn-hangzhou.aliyuncs.com/repos_zyl/centos:0.0.1
#作者信息
MAINTAINER abc@qq.com

#创建目录
RUN mkdir -p /java/jdk7
#把当前目录下的jdk文件夹添加到镜像
ADD jdk7 /java/jdk7

#创建tomcat目录
RUN mkdir -p tomcat
#把当前目录下的tomcat文件夹添加到镜像
ADD tomcat7 tomcat

#添加环境变量
ENV JAVA_HOME /java/jdk7
ENV CATALINA_HOME tomcat
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin

#暴露8080端口
EXPOSE 8080

#启动时运行tomcat
CMD ["tomcat/bin/catalina.sh","run"]

#生成新的Tomcat7镜像
docker build -t tomcat7 /root/image/

#运行测试
docker run -d -p 8089:8080 --name testtomcat tomcat7
docker exec -it testtomcat /bin/bash

#配置并启动mysql镜像,    记得将数据库导入 得到MySQL的运行地址及端口,将其修改入项目jar包
docker build -t ntrwsql:1 /root/docker/NginxTomcatRedisWeb/poject/mysql
docker run -d -p 13306:3306 -e MYSQL_ROOT_PASSWORD=123 --name ntrwsql ntrwsql:1
docker exec -it ntrwsql

#配置redis镜像 获得IP地址以及端口号
docker pull docker.io/redis
#将其tag push到自己的私有仓库 便于下载
docker tag docker.io/redis redis:

#运行redis
docker run -i -p 16379:6379  --name testredis docker.io/redis
docker run -d -p 16379:6379  --name testredis docker.io/redis

#记得这里的端口号与Tomcat下面的context.xml保持一致


#编译Tomcat7
#修改Tomcat中的context.xml文件 将修改好的覆盖原文件夹下的
echo "
<?xml version='1.0' encoding='utf-8'?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
        <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
                   host="masterIP"
                   port="16379"
                   database="0"
                   maxInactiveInterval="60" />

</Context>" > context.xml

#创建Dockerfile文件
    cat > Dockerfile<< EOF
    FROM tomcat7
    COPY context.xml /tomcat/conf/
    COPY webPoject.war /tomcat/webapps/
    COPY commons-pool2-2.4.2.jar /tomcat/lib/
    COPY jedis-2.9.0.jar /tomcat/lib/
    COPY tomcat-redis-session-manager1.2.jar /tomcat/lib/
    EXPOSE 8080
    CMD ["/tomcat/bin/catalina.sh","run"]
EOF

#编译Tomcat7
docker build -t ntrwtomcat71:1 /root/docker/NginxTomcatRedisWeb/poject/tomcat71

docker run -i -p 8180:8080 --name 71testtomcat ntrwtomcat71:1
docker run -d -p 8180:8080 --name 71testtomcat ntrwtomcat71:1

docker stop  71testtomcat
docker rm  71testtomcat
docker rmi ntrwtomcat71:1

#编辑第二个Tomcat2
#将第一个的Tomcat下的文件copy到新的Tomcat目录下
cp -p /root/docker/NginxTomcatRedisWeb/poject/tomcat71/* /root/docker/NginxTomcatRedisWeb/poject/tomcat72/
#编译Tomcat7
docker build -t ntrwtomcat72:1 /root/docker/NginxTomcatRedisWeb/poject/tomcat72
docker run -i -p 8280:8080 --name 72testtomcat ntrwtomcat72:1
docker run -d -p 8280:8080 --name 72testtomcat ntrwtomcat72:1

docker stop  72testtomcat
docker rm  72testtomcat
docker rmi ntrwtomcat72:1


#配置Nginx
docker pull docker.io/nginx

docker run -d -p 800:80 --name ntrwnginx docker.io/nginx

#停止测试的Nginx
docker stop ntrwnginx
docker rm ntrwnginx

#编辑nginx.conf配置文件,主要是项目名以及管理的Tomcat的IP和端口号

#编辑Dockerfile文件
FROM nginx
MAINTAINER abc@qq.com
COPY  nginx.conf  /etc/nginx/
EXPOSE 8080

#编译ntrwnginx
docker build -t ntrwnginx:1 /root/docker/NginxTomcatRedisWeb/poject/nginx

docker run -d -p 800:80 --name ntrwnginx ntrwnginx:1


#nginx.conf配置文件 参考
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on; ------这里注意
    upstream webPoject {
        server masterIP:8180;
        server masterIP:8280;
    }
    
    
    server {
        listen       80;
        server_name  localhost;
        #------这里注意
        charset utf-8;

        #access_log  logs/host.access.log  main;

        #------这里注意
        location / {
            root   html;
            index  index.html index.htm;
            add_header Cache-Control 'no-store';
            client_max_body_size 100m;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # proxy_cache Z;
            proxy_cache_valid 200 1m;
            proxy_pass  http://webPoject;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}