Docker部署nacos集群

Nacos 提供了一组简单易用的特性集,可快速实现动态服务发现、服务配置、服务元数据及流量管理,更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。

nacos官方文档链接:https://nacos.io/zh-cn/docs/what-is-nacos.html

一、部署前准备

1、3台centos7服务器

服务器IP
nacos-1 172.16.12.1
nacos-2 172.16.12.2
nacos-3 172.16.12.3

 

 

 

 

  • naocs工作目录:/data/server/nacos-server
  • nacos配置变量:/data/server/nacos-server/env.list
  • nacos日志目录:/data/server/nacos-server/logs
  • custom配置文件:/data/server/nacos-server/init.d/custom.properties

2、数据库【此处采用5.6的单机数据库,生产环境建议采用主备模式或高可用数据库】

     初始化数据库:初始化脚本

3、在3台服务器上执行以下命令

mkdir -pv /data/server/nacos-server/init.d  /data/server/nacos-server/logs  
cat >/data/server/nacos-server/init.d/custom.properties << 'EOF'
#spring.security.enabled=false
#management.security=false
#security.basic.enabled=false
#nacos.security.ignore.urls=/**
#management.metrics.export.elastic.host=http://localhost:9200
# metrics for prometheus
management.endpoints.web.exposure.include=*
 
# metrics for elastic search
#management.metrics.export.elastic.enabled=false
#management.metrics.export.elastic.host=http://localhost:9200
 
# metrics for influx
#management.metrics.export.influx.enabled=false
#management.metrics.export.influx.db=springboot
#management.metrics.export.influx.uri=http://localhost:8086
#management.metrics.export.influx.auto-create-db=true
#management.metrics.export.influx.consistency=one
#management.metrics.export.influx.compressed=true
EOF

cat >/data/server/nacos-server/env.list << 'EOF'
NACOS_SERVER_IP=172.16.12.1   # 多网卡下的自定义nacos服务器IP,每台服务器IP不相同
NACOS_SERVERS=172.16.12.1:8848,172.16.12.2:8848,172.16.12.3:8848  # nacos cluster地址
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_MASTER_SERVICE_HOST=hhhhhhhhhhahah.mysql.rds.aliyuncs.com  # mysql 主节点host
MYSQL_MASTER_SERVICE_PORT=3306        # mysql 主节点端口
MYSQL_MASTER_SERVICE_USER=test-nacos  # mysql 账号
MYSQL_MASTER_SERVICE_PASSWORD=3iINTUv # mysql 密码
MYSQL_MASTER_SERVICE_DB_NAME=nacos_config  # mysql 数据库
MYSQL_SLAVE_SERVICE_HOST=hhhhhhhhhhahah.mysql.rds.aliyuncs.com   # mysql 从节点host
MYSQL_SLAVE_SERVICE_PORT=3306
EOF
  • nacos默认模式即为cluster集群模式,在此处可不指定
  • NACOS_SERVERS:nacos cluster地址,如:IP1:端口,IP2:端口,IP3:端口
  • NACOS_SERVER_IP:多网卡下的自定义nacos服务器IP

二、在3台服务器启动服务

1、启动容器

docker run --network=host \
--restart=always \
--name nacos-01 \
--env-file=/data/server/nacos-server/env.list \
-v /data/server/nacos-server/logs:/home/nacos/logs \
-v /data/server/nacos-server/init.d/custom.properties:/home/nacos/init.d/custom.properties \
-d nacos/nacos-server

2、查看日志,检查端口 8848 是否起来

[root@public nacos-server]#tailf  -800 /data/server/nacos-server/logs/nacos.log

2019-11-26 16:51:16,119 INFO Starting Nacos v1.1.4 on nacos-01 with PID 26 (/home/nacos/target/nacos-server.jar started by root in /home/nacos)

2019-11-26 16:51:16,120 INFO No active profile set, falling back to default profiles: default

2019-11-26 16:51:17,116 INFO Nacos is starting...

2019-11-26 16:51:18,121 INFO Nacos is starting...

2019-11-26 16:51:18,906 INFO Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$c86f83bf] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2019-11-26 16:51:19,021 INFO Bean 'objectPostProcessor' of type [org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2019-11-26 16:51:19,025 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@6be968ce' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2019-11-26 16:51:19,031 INFO Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$ed442671] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2019-11-26 16:51:19,036 INFO Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2019-11-26 16:51:19,121 INFO Nacos is starting...

2019-11-26 16:51:19,505 INFO Tomcat initialized with port(s): 8848 (http)

2019-11-26 16:51:19,527 INFO Initializing ProtocolHandler ["http-nio-8848"]

2019-11-26 16:51:19,542 INFO Starting service [Tomcat]

2019-11-26 16:51:19,542 INFO Starting Servlet Engine: Apache Tomcat/9.0.13

2019-11-26 16:51:19,558 INFO The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]

2019-11-26 16:51:19,649 INFO Initializing Spring embedded WebApplicationContext

2019-11-26 16:51:19,649 INFO Root WebApplicationContext: initialization completed in 3449 ms

2019-11-26 16:51:23,125 INFO Nacos is starting...

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/**/*.css'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/**/*.js'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/**/*.html'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/**/*.map'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/**/*.svg'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/**/*.png'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/**/*.ico'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/console-fe/public/**'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/v1/auth/login'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/v1/console/health/**'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/v1/cs/**'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/v1/ns/**'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/v1/cmdb/**'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/actuator/**'], []

2019-11-26 16:51:23,469 INFO Creating filter chain: Ant [pattern='/v1/console/server/**'], []

2019-11-26 16:51:23,513 INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@45905bff, org.springframework.security.web.context.SecurityContextPersistenceFilter@7ec3394b, org.springframework.security.web.header.HeaderWriterFilter@4409e975, org.springframework.security.web.authentication.logout.LogoutFilter@4351c8c3, com.alibaba.nacos.console.filter.JwtAuthenticationTokenFilter@2a2c13a8, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@1522d8a0, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@246f8b8b, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@b6b1987, org.springframework.security.web.session.SessionManagementFilter@2a7686a7, org.springframework.security.web.access.ExceptionTranslationFilter@1d0d6318, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@72ea6193]

2019-11-26 16:51:23,624 INFO Exposing 16 endpoint(s) beneath base path '/actuator'

2019-11-26 16:51:23,671 INFO Initializing ExecutorService 'taskScheduler'

2019-11-26 16:51:23,745 INFO Starting ProtocolHandler ["http-nio-8848"]

2019-11-26 16:51:23,747 INFO Using a shared selector for servlet write/read

2019-11-26 16:51:23,831 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

2019-11-26 16:51:23,835 INFO Started Nacos in 8.761 seconds (JVM running for 9.403)

2019-11-26 16:51:23,835 INFO Nacos Log files: /home/nacos/logs/

2019-11-26 16:51:23,835 INFO Nacos Conf files: /home/nacos/conf/

2019-11-26 16:51:23,835 INFO Nacos Data files: /home/nacos/data/

2019-11-26 16:51:23,835 INFO Nacos started successfully in cluster mode.

3、打开web页面,url为 http://ip:8848/nacos,默认账号密码为 nacos,如图

Docker部署nacos集群_第1张图片

  • 登陆后,点击查看节点列表,如下图集群状态为正常,若不正常则查看nacos日志
  • 可看到此时集群已选出leader,leader 一般是在任期高的几个节点之中产生(至少2个节点),而在没有leader 产生之前, 集群会进行多次的选举。 每次的选举任期加1,而最后加入的 节点已经没有了选举的机会, 直接作为 follower 加入, 其任期默认是0

Docker部署nacos集群_第2张图片

  • 此时,若其中一个节点挂掉,并不会影响服务,若挂掉的节点为leader,则集群会重新选举出新的leader,挂掉的节点恢复正常后会以follower加入,相当与一个新的节点。

三、在生产环境中,这样并不算一个合格的集群,官网提出以下几种连接方式:

  1. http://ip1:port/openAPI 直连ip模式,机器挂则需要修改ip才可以使用。
  2. http://VIP:port/openAPI 挂载VIP模式,直连vip即可,下面挂server真实ip,可读性不好。
  3. http://nacos.com:port/openAPI 域名 + VIP模式,可读性好,而且换ip方便,推荐模式

Docker部署nacos集群_第3张图片

  • 此处采用 域名+ nginx upstream的方式来跳转到对应的服务
[root@nginx sites]# cat nacos.conf 
upstream test-nacos {
              server 172.16.12.1:8848; 
              server 172.16.12.2:8848; 
              server 172.16.12.3:8848; 
         }
server {
        listen 80;
        server_name nacos.abc.com;
        rewrite ^(.*) https://$server_name$1 permanent;
}
server {
    listen 443;
    server_name nacos.abc.com;
    charset utf-8;
    access_log /data/logs/nginx/$host.log main;
    index index.jsp index.php index.htm index.html;
    ssl on;
    ssl_certificate   ssl/abc.com.pem;
    ssl_certificate_key  ssl/abc.com.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
        location / {
            proxy_pass http://test-nacos;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size    100m;
            client_body_buffer_size 128k;
        }
}
  • 重启nginx,即可生效。

Docker部署nacos集群_第4张图片

 

你可能感兴趣的:(nacos,docker)