0
点赞
收藏
分享

微信扫一扫

Nacos环境搭建

楠蛮鬼影 2022-04-27 阅读 95

Nacos环境搭建

Docker搭建Nacos

对于非Mac M1的人只要把镜像换成nacos/nacos-server就行

参考官网

官网: https://nacos.io/zh-cn/docs/quick-start-docker.html

  • Mac M1使用镜像 zhusaidong/nacos-server-m1:2.0.3

    docker pull zhusaidong/nacos-server-m1:2.0.3
    
  • 非Mac M1使用镜像nacos/nacos-server

    docker pull nacos/nacos-server
    

下面我就使用Mac M1的镜像进行阐述

编排搭建Nacos单机版本

我们先自行创建好这个结构,以免映射出问题,不创建也是可以的但是custom.properties这个得自己手动创建,否则是创建出来的是文件夹,不是文件

version: '3'
services:
  mysql:
    image: mysql/mysql-server:5.7
    container_name: mysql_53306
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: nacos
      MYSQL_USER: root
      MYSQL_PASSWORD: 123456
    ports:
      - 53306:3306
    volumes:
      - ./mysql_53306/log:/var/log/mysql
      - ./mysql_53306/data:/var/lib/mysql/
      - ./mysql_53306/conf/:/etc/mysql/
  nacos:
    restart: on-failure
    image: zhusaidong/nacos-server-m1:2.0.3
    container_name: nacos_8848
    depends_on:
      - mysql_53306
    environment:
      - PREFER_HOST_MODE=hostname
      - MODE=standalone
      - SPRING_DATASOURCE_PLATFORM=mysql
      - MYSQL_SERVICE_HOST=mysql_53306
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_USER=root
      - MYSQL_SERVICE_PASSWORD=123456
      - MYSQL_SERVICE_DB_NAME=nacos
      - MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
    volumes:
      - ./nacos_8848/standalone-logs/:/home/nacos/logs
      - ./nacos_8848/init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - "8848:8848"
      - "9848:9848"

运行

 docker-compose -f nacos-standalone.yml -p nacos-standalone  up -d

编排搭建Nacos集群版本

这里呢,我们省事点,就创建个custom.properties就行,和上面的问题一样

这里我们使用prometheus+grafana进行Nacos的监控

官网: https://nacos.io/zh-cn/docs/monitor-guide.html

prometheus.yml编写

global:
  scrape_interval: 15s
  evaluation_interval: 15s
scrape_configs:
  - job_name: 'nacos'
    metrics_path: '/nacos/actuator/prometheus'
    static_configs:
      - targets: [ "nacos1:8848","nacos2:8848","nacos3:8848" ]

docker-compose.yml编写

version: "3"
services:
  nacos1:
    hostname: nacos1
    container_name: nacos1
    # 替换成m1版本的
    image: zhusaidong/nacos-server-m1:2.0.3
    volumes:
      - ./nacos-cluster/cluster-logs/nacos1:/home/nacos/logs
      - ./nacos-cluster/init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - "8848:8848"
      - "9848:9848"
      - "9555:9555"
    environment: #nacos dev env
      - PREFER_HOST_MODE=hostname
      - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
      - MYSQL_SERVICE_HOST=mysql_53306
      - MYSQL_SERVICE_DB_NAME=nacos
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_USER=root
      - MYSQL_SERVICE_PASSWORD=123456
    restart: on-failure
    depends_on:
      - mysql_53306
  nacos2:
    hostname: nacos2
    image: zhusaidong/nacos-server-m1:2.0.3
    container_name: nacos2
    volumes:
      - ./nacos-cluster/cluster-logs/nacos2:/home/nacos/logs
      - ./nacos-cluster/init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - "8849:8848"
      - "9849:9848"
    environment: #nacos dev env
      - PREFER_HOST_MODE=hostname
      - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
      - MYSQL_SERVICE_HOST=mysql_53306
      - MYSQL_SERVICE_DB_NAME=nacos
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_USER=root
      - MYSQL_SERVICE_PASSWORD=123456
    restart: on-failure
    depends_on:
      - mysql_53306
  nacos3:
    hostname: nacos3
    image: zhusaidong/nacos-server-m1:2.0.3
    container_name: nacos3
    volumes:
      - ./nacos-cluster/cluster-logs/nacos3:/home/nacos/logs
      - ./nacos-cluster/init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - "8850:8848"
      - "9850:9848"
    environment: #nacos dev env
      - PREFER_HOST_MODE=hostname
      - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
      - MYSQL_SERVICE_HOST=mysql_53306
      - MYSQL_SERVICE_DB_NAME=nacos
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_USER=root
      - MYSQL_SERVICE_PASSWORD=123456
    restart: on-failure
    depends_on:
      - mysql_53306
  # mysql
  mysql:
    image: mysql/mysql-server:5.7
    container_name: mysql_53306
    restart: on-failure
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: nacos
      MYSQL_USER: root
      MYSQL_PASSWORD: 123456
    ports:
      - 53306:3306
    volumes:
      - ./mysql_53306/log:/var/log/mysql
      - ./mysql_53306/data:/var/lib/mysql/
      - ./mysql_53306/conf/:/etc/mysql/
  # nginx
  nginx:
    image: arm64v8/nginx
    links:
      - nacos1:nacos1
      - nacos2:nacos2
      - nacos3:nacos3
    container_name: nginx_4000
    restart: on-failure
    depends_on:
      - nacos1
      - nacos2
      - nacos3
    ports:
      - 4000:80
    volumes:
      - ./nacos-cluster/nginx/log:/etc/nginx/logs
      - ./nacos-cluster/nginx/html:/etc/nginx/html
      - ./nacos-cluster/nginx/nginx.conf:/etc/nginx/nginx.conf
  # prometheus
  prometheus:
    image: prom/prometheus:latest
    links:
      - nacos1:nacos1
      - nacos2:nacos2
      - nacos3:nacos3
    container_name: prometheus
    restart: on-failure
    ports:
      - "9090:9090"
    depends_on:
      - nacos1
      - nacos2
      - nacos3
    volumes:
      - "./nacos-cluster/prometheus.yml:/etc/prometheus/prometheus.yml"
      - "./nacos-cluster/prometheus_data:/prometheus"
  # grafana
  grafana:
    image: grafana/grafana
    container_name: grafana
    ports:
      - "3000:3000"
    restart: always
    volumes:
      - "./nacos-cluster/grafana_data:/var/lib/grafana"

custom.properties编写

management.endpoints.web.exposure.include=*

nginx配置

我们使用nginx进行集群的轮循

#user  nobody;
worker_processes  8;
worker_rlimit_nofile 12000;
events {
    use epoll;
    worker_connections  12000;
    multi_accept    on;
}

http  {

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


    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream cluster {
        server nacos1:8848;
        server nacos2:8849;
        server nacos3:8850;
    }

     server {
        listen      80;
        server_name  localhost;
        location / {
          proxy_pass http://cluster;
      }


        access_log  logs/access.log;
        #开启索引功能
        autoindex on;
        # 关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
        autoindex_exact_size off;
        autoindex_localtime on;   # 显示本机时间而非 GMT 时间
         # 解决跨域问题
        add_header Access-Control-Allow-Origin '*';
  }

}

编排运行

 docker-compose -f nacos-cluster.yml -p nacos-cluster  up -d

查看运行情况

Mac可以直接在Docker Dashboard进行查看

image.png

其他可以通过指令 docker ps -a查看是否编排完成

查看监控是否成功

访问地址: http://localhost:8848/nacos/actuator/prometheus

image.png

访问地址:http://localhost:9090/graph

image.png

查看集群回馈情况

访问地址: http://localhost:9090/targets

image.png

添加数据源

image.png

image.png

image.png

image.png

导入Nacos监控模板

image.png

image.png

监控Json模板下载

下载地址: https://linq-cool.oss-cn-shanghai.aliyuncs.com/20220427/706cdb7b693040938e63a7b2b6ea1969.json

监控成功显示

image.png

最终文件目录展示

image.png

Nacos访问

Nacos地址: http://localhost:4000/nacos

image.png

举报

相关推荐

0 条评论