Docker builds nginx reverse proxy Nacos cluster plus mysql

table of Contents

1.Docker build mysql master and slave

2.Docker builds Nacos-server: 1.3.1 cluster

2.1 Need to create the configuration database required by nacos in the database

2.2docker cluster command

2.3 Command parameter description

2.4 Open firewall ports

2.5 After successful startup, we can access the nacos console

3.docker install nginx

3.1. Create command

3.2.nginx configuration content

3.3 nginx supports five distribution modes

3.4 restart nginx

3.5 Visit


1.Docker build mysql master and slave

docker mysql build

2.Docker builds Nacos-server: 1.3.1 cluster

2.1 Need to create the configuration database required by nacos in the database

 

Database initialization script: https://github.com/alibaba/nacos/blob/develop/distribution/conf/nacos-mysql.sql

2.2docker cluster command

If you need to modify the ip to your own ip port -p, modify the accessible ip on the left side, modify the port in the container by default 8848 on the right

docker run -d \
-e MODE=cluster \
-e NACOS_APPLICATION_PORT=8848 \
-e NACOS_SERVERS=ip:38848,ip:38849,ip:38850 \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=ip \
-e MYSQL_SERVICE_PORT=33306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=980224 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e NACOS_SERVER_IP=ip \
-e JVM_XMS=215m -e JVM_XMX=215m -e JVM_XMN=215m \
-p 38848:8848 \
--name nacos48 \
--restart=always \
nacos/nacos-server:1.3.1
 
docker run -d \
-e MODE=cluster \
-e NACOS_APPLICATION_PORT=8848 \
-e NACOS_SERVERS=ip:38848,ip:38849,ip:38850 \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=ip \
-e MYSQL_SERVICE_PORT=33306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=980224 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e NACOS_SERVER_IP=ip \
-e JVM_XMS=215m -e JVM_XMX=215m -e JVM_XMN=215m \
-p 38849:8848 \
--name nacos49 \
--restart=always \
nacos/nacos-server:1.3.1
​
docker run -d \
-e MODE=cluster \
-e NACOS_APPLICATION_PORT=8848 \
-e NACOS_SERVERS=ip:38848,ip:38849,ip:38850 \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=ip \
-e MYSQL_SERVICE_PORT=33306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=980224 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e NACOS_SERVER_IP=ip \
-e JVM_XMS=215m -e JVM_XMX=215m -e JVM_XMN=215m \
-p 38850:8848 \
--name nacos50 \
--restart=always \
nacos/nacos-server:1.3.1

2.3 Command parameter description

MODE=cluster cluster mode

NACOS_APPLICATION_PORT=8848 container nacos port

NACOS_SERVERS=ip:38848,ip:38849,ip:38850 each nacos-server service in the cluster

SPRING_DATASOURCE_PLATFORM=mysql supports mysql independently

NACOS_SERVER_IP=ip optional value hostname/ip default ip

MYSQL_* related

MYSQL_SERVICE_DB_NAME=nacos_config database initialization script database name

Database initialization script: https://github.com/alibaba/nacos/blob/develop/distribution/conf/nacos-mysql.sql

JVM_XMS=215m -e JVM_XMX=215m -e JVM_XMN=215m

 

-p 38850: 8848 38850 host port: 8848 nacos access port

--restart=always restarting the docker service will start the container

nacos/nacos-server: 1.3.1 nacos mirror version

Copy all directly into xshell as follows

2.4 Open firewall ports

firewall-cmd --zone=public --add-port=38848/tcp --permanent
firewall-cmd --zone=public --add-port=38849/tcp --permanent
firewall-cmd --zone=public --add-port=38850/tcp --permanent
firewall-cmd --zone=public --add-port=33306/tcp --permanent
firewall-cmd --reload

If it is an Alibaba Cloud server, open the security group rules

2.5 After successful startup, we can access the nacos console

Access address 1: http://host IP:38848/nacos 
access address 2: http://host IP:38849/nacos 
access address 3: http://host IP:38850/nacos 
default login account and password If nacos is shown in the 
figure, the cluster is successfully built

3.docker install nginx

3.1. Create command

-v docker mounts to the host location, you need to create it yourself and put your location on the right of -v: left

docker run -id --name=nginx_master -p 80:80 -v  /home/dockerdata/nginx/nginx_master/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/dockerdata/nginx/nginx_master/logs:/var/log/nginx \
-v /home/dockerdata/nginx/nginx_master/html:/usr/share/nginx/html --restart=always nginx:1.19.0
​

supplement

2.1.1 docker container settings automatic start

Add --restart=always when creating the startup container

Flag Descriptionno does not automatically restart the container. (default value) on-failure the container exits due to an error (the container exit status is not 0) restart the container unless-stopped restart the container only when the container has been stopped or Docker stopped/restarted always in the container Restart the container when it has been stopped or Docker is stopped/restarted

If you have created a started container, use update to update: docker update --restart=always nginx_master

3.2.nginx configuration content

Modify the configuration file ip to your own ip

nginx.conf location: /home/dockerdata/nginx/nginx_master/conf/nginx.conf where the startup command is mounted

user  nginx;
worker_processes  1;
​
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
​
​
events {
    worker_connections  1024;
}
​
​
http {
    upstream nacos-cluster {
    server ip:38848;
    server ip:38849;
    server ip:38850;
    }
​
    include       /etc/nginx/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  /var/log/nginx/access.log  main;
​
    sendfile        on;
    #tcp_nopush     on;
​
    keepalive_timeout  65;
​
    #gzip  on;
​
    server {
        listen       80;
        listen  [::]:80;
        server_name  ip;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location /nacos {
            proxy_pass   http://nacos-cluster;
        }
    
        #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   /usr/share/nginx/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;
        #}
    }
}
​

3.3 nginx supports five distribution modes

#设定负载均衡的服务器列表
    upstream 变量名 {
        #weigth参数表示权值,权值越高被分配到的几率越大
        #max_fails 当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查
        #fail_timeout 在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器
    }
!!!分配方式
    Nginx的upstream支持5种分配方式,下面将会详细介绍,其中,前三种为Nginx原生支持的分配方式,后两种为第三方支持的分配方式:
1、轮询        
        轮询是upstream的默认分配方式,即每个请求按照时间顺序轮流分配到不同的后端服务器,如果某个后端服务器down掉后,能自动剔除。
        upstream backend {
            server 192.168.200.131:8848;
            server 192.168.200.131:8849;
            server 192.168.200.131:8850;
        }
2、weight        
        轮询的加强版,即可以指定轮询比率,weight和访问几率成正比,主要应用于后端服务器异质的场景下。
        upstream backend {
            server 192.168.200.131:8848 weight=1;
            server 192.168.200.131:8849 weight=2;
            server 192.168.200.131:8850 weight=3;
        }
3、ip_hash        
        每个请求按照访问ip(即Nginx的前置服务器或者客户端IP)的hash结果分配,这样每个访客会固定访问一个后端服务器,可以解决session一致问题。
        upstream backend {
            ip_hash;
            server 192.168.200.131:8848;
            server 192.168.200.131:8849;
            server 192.168.200.131:8850;
        }
4、fair        
        fair顾名思义,公平地按照后端服务器的响应时间(rt)来分配请求,响应时间短即rt小的后端服务器优先分配请求。
        upstream backend {
            server 192.168.200.131:8848;
            server 192.168.200.131:8849;
            server 192.168.200.131:8850;
            fair;
        }
5、url_hash
        与ip_hash类似,但是按照访问url的hash结果来分配请求,使得每个url定向到同一个后端服务器,主要应用于后端服务器为缓存时的场景下。
        upstream backend {
            server 192.168.200.131:8848;
            server 192.168.200.131:8849;
            server 192.168.200.131:8850;
            hash $request_uri;
            hash_method crc32;
        }
        其中,hash_method为使用的hash算法,需要注意的是:此时,server语句中不能加weight等参数。

3.4 restart nginx

​docker restart nginx_master

3.5 Visit

http://nginxIP/nacos

 

 

Guess you like

Origin blog.csdn.net/adminBfl/article/details/108797060