푰’풎 풉풉품, 푰 풂풎 풂 품풓풂풅풖풂풕풆 풔풕풖풅풆풏풕 풇풓풐풎 푵풂풏풋풊풏품, 푪풉풊풏풂.
푺풉풄풐풐풍: 푯풐풉풂풊 푼풏풊풗풆풓풔풊풕풚 푳풆풂풓풏풊풏품: 푰’풎 풄풖풓풓풆풏풕풍풚 풍풆풂풓풏풊풏품 풅풆풔풊품풏 풑풂풕풕풆풓풏, 푳풆풆풕풄풐풅풆, 풅풊풔풕풓풊풃풖풕풆풅 풔풚풔풕풆풎, 풎풊풅풅풍풆풘풂풓풆 풂풏풅 풔풐 풐풏. 푯풐풘 풕풐 풓풆풂풄풉 풎풆:푽푿 푴풚 풃풍풐품: 풉풕풕풑풔://풉풉품풚풚풅풔.풃풍풐품.풄풔풅풏.풏풆풕/ 푷풓풐풇풆풔풔풊풐풏풂풍 풔풌풊풍풍풔:풎풚 풅풓풆풂풎
为什么要用英文写呢?为了去外企,为了摆脱自己对英语的恐惧,不是装B。
1-1: Nacos Cluster Architecture Nacos集群架构
The picutre comes from Nacos、It shows the main nacos cluster arch、As I understand it, there maybe an Nginx server exist in front of docker nacos cluster、So, I try to deploy an nacos cluster by using docker-compose technology and an Nginx server、This article aims to record the process of deploying and the peoblems during completing it.
Docker-compose v2.2.2 : Different docker-compose version don’t seem to be a problem.Nacos: v2.0.3 the latestMySQL: v5.7Nginx: v1.18.0 1-3: Project Arch and Nacos Deploment in Docker
nacos-hostname.env
#nacos dev envPREFER_HOST_MODE=hostname# docker实例别名,可以换成ip port must be 8848NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848MYSQL_SERVICE_HOST=mysql# 指定保存数据的数据库名称MYSQL_SERVICE_DB_NAME=nacos_devtest# 访问mysql端口MYSQL_SERVICE_PORT=3306# 访问mysql的用户名MYSQL_SERVICE_USER=root# 访问mysql的密码MYSQL_SERVICE_PASSWORD=root
custom.properties#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 prometheusmanagement.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
docker-compose.ymlversion: "3"services: mysql: container_name: mysql # 最新的mysql8版本 image: mysql:5.7 environment: # mysql root用户密码 MYSQL_ROOT_PASSWORD: root command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci --explicit_defaults_for_timestamp=true --lower_case_table_names=1 --max_allowed_packet=128M; volumes: # mysql的数据文件 - ./mysql/data:/var/lib/mysql # mysql配置文件 - ./mysql/config:/etc/mysql/conf.d ports: - "3306:3306" nacos1: hostname: nacos1 container_name: nacos1 image: nacos/nacos-server:latest volumes: # 需要添加mysql8的插件 #- ./nacos/plugins/mysql/:/home/nacos/plugins/mysql/ # 把日志文件映射出来 - ./nacos/cluster-logs/nacos1:/home/nacos/logs # 把配置文件映射出来 - ./nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties environment: # 设置环境变量,相当于docker run命令中的-e - JVM_XMS=512m - JVM_XMX=512m - JVM_XMN=128m #- MODE=standalone ports: - "8848:8848" - "9555:9555" env_file: # 集群配置文件 - ./nacos/env/nacos-hostname.env restart: always depends_on: - mysql nacos2: hostname: nacos2 image: nacos/nacos-server:latest container_name: nacos2 volumes: #- ./nacos/plugins/mysql/:/home/nacos/plugins/mysql/ - ./nacos/cluster-logs/nacos2:/home/nacos/logs - ./nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties environment: # 设置环境变量,相当于docker run命令中的-e - JVM_XMS=512m - JVM_XMX=512m - JVM_XMN=128m ports: - "8849:8848" env_file: - ./nacos/env/nacos-hostname.env restart: always depends_on: - mysql nacos3: hostname: nacos3 image: nacos/nacos-server:latest container_name: nacos3 volumes: #- ./nacos/plugins/mysql/:/home/nacos/plugins/mysql/ - ./nacos/cluster-logs/nacos3:/home/nacos/logs - ./nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties environment: # 设置环境变量,相当于docker run命令中的-e - JVM_XMS=512m - JVM_XMX=512m - JVM_XMN=128m ports: - "8850:8848" env_file: - ./nacos/env/nacos-hostname.env restart: always depends_on: - mysql
Remember!!! The sql file should be imported into db nacos_devtest、sql file downloadThe files above refer to get source code from gitee and offical nacos group.
Success: All nodes status are up.
Create a dataId in one node and you will get the same in another.
Notes:
In most cases, we may add port mappings in docker services、e.x、- port "8849:8848" i.e、Expose the 8848 port inside the container to the outside 8849 port、However, we may omits one problem、Let me show you、Firstly, when the docker-compose.yml is finished and deployed by docker-compose up command、Docker engine will create an network called ${dir}_defalut like this:
(base) ➜ docker-compose-nacos-mysql-master docker network list NETWORK ID NAME DRIVER SCOPE72eddc5e38ac bridge bridge local7348f4641015 docker-nacos_default bridge local <-----
We can inspect this network:
"Containers": { "185a008b417d09201c8e8efdc5b4b68776f994d797c32d9fbd523327c80cff80": { "Name": "nacos2", "EndpointID": "be3a4eba47b6d5908682ece4a9465390385ec5a10ab8b5730d7eb8c2d436c309", "MacAddress": "02:42:ac:1b:00:05", "IPv4Address": "172.27.0.5/16", "IPv6Address": "" }, "1f09159bc54c43a4fb86eb84e083ab35354e727d1c1bd0a4cab448298e4c2b71": { "Name": "nacos1", "EndpointID": "a61935035f6940ad624ac68cd49af76faa1461146130bf1c4349157eed53232f", "MacAddress": "02:42:ac:1b:00:06", "IPv4Address": "172.27.0.6/16", "IPv6Address": "" }, "b11c0f47fafb15b7523be1c63d5d8f9134c2705dad128ba46630bbff48fe40d8": { "Name": "nacos3", "EndpointID": "59d1641bff3f8250bb0c3e13e78c5e44e3c3caeaf8ed48ca7d72b6df6fd32f5b", "MacAddress": "02:42:ac:1b:00:03", "IPv4Address": "172.27.0.3/16", "IPv6Address": "" }, "e0d5e0d03a07dfea11cd061097a3e1ef2fee6f851c282bff8b8a89d228a5194e": { "Name": "mysql", "EndpointID": "f20dd0ed478656e497c502d74541e2c57d30f27b45b0360f25ca766f89edd2e4", "MacAddress": "02:42:ac:1b:00:02", "IPv4Address": "172.27.0.2/16", "IPv6Address": "" }}
This shows us the ip of containers(mysql, nacos1, nacos2, nacos3) in docker.
Ok, if I deploy MySQL - port "3307:3306", my host reaches MySQL at 127.0.0.1:3307、Thinking of it, how does nacos1 in the docker container reach MySQL which is in another container? 3307 or 3306? YES ! 3306 is right ! So, let’s check the nacos-hostname.env confiuration file、NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848 We use the hostname nacos1 instead of the real ip、The ports are also 8848 rather than 8849,8850… so it is MYSQL_SERVICE_PORT=3306.
How to know whether the services could communicate with each other? you can use ping utils in one contaniner、Like this:ping ${antoher container ip adress}, the ip address must be in docker network, such as docker-nacos_default above.
If there has been a MySQL service in your docker container, you must put them(nacos1…3, MySQL) in the same network in order to communicate with each other.
1-4: Nginx Reverse ProxyAs mentioned above, the Nacos cluster won’t be exposed to the outer environment、Each request reaches Nginx first, then Nginx dispatches requests to the Nacos cluster、In this article, I deploy nginx on my host, not docker、Yes, I failed to deploy it on the docker、The main reason is the network in docker、I put nginx on the same docker network docker-nacos_default、Many times I have tried I failed、QAQ、Fortunately, I realized my idea on my host machine-Ubuntu.You can install it by apt-get install nginx 、Here is my config.
(base) ➜ docker-compose-nacos-mysql-master cd /etc/nginx (base) ➜ nginx lsconf.d koi-utf modules-available proxy_params sites-enabled win-utffastcgi.conf koi-win modules-enabled scgi_params snippetsfastcgi_params mime.types nginx.conf sites-available uwsgi_params(base) ➜ nginx cat nginx.conf user www-data;worker_processes auto;pid /run/nginx.pid;include /etc/nginx/modules-enabled/*.conf;events {worker_connections 768;# multi_accept on;}http {### Basic Settings##sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;# server_tokens off;# server_names_hash_bucket_size 64;# server_name_in_redirect off;include /etc/nginx/mime.types;default_type application/octet-stream;### SSL Settings##ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLEssl_prefer_server_ciphers on;### Logging Settings##access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;### Gzip Settings##gzip on;# gzip_vary on;# gzip_proxied any;# gzip_comp_level 6;# gzip_buffers 16 8k;# gzip_http_version 1.1;# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; upstream nacos { server 192.168.1.4:8848 weight=10; server 192.168.1.4:8849 weight=10; server 192.168.1.4:8850 weight=10; } server{ listen 80; server_name 192.168.1.4; location / { proxy_pass http://nacos; } }### Virtual Host Configs##include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;}
In my host, access nacos must from 192.168.1.4:8848; 192.168.1.4:8849; 192.168.1.4:8850; 192.168.1.4 is my ip、Nacos expose their internal port 8848 by 8848, 8849 and 8850 to the outside world、Like the picture below:
Success: Chrome access
http://192.168.1.4/nacos/#/configurationManagement?dataId=&group=&appName=&namespace=&pageSize=&pageNo=
We can arrive nacos terminal.
Install NginxChange Apt Source in Docker ContainerMySQL master-servant + Nacos cluster + Nginx master-servantGitee Deployment Written by a CoderCommunication Between ContainersOfficial Naco Group、There are examples of docker deployment in it.Docker Cluster Deployment If You Has A MySQL Container Aleardy、i.e Different Docer Network Deploymentnacos-mysql.sqlNacos Cluster Deployment document