欢迎访问本站,祝大家玩得愉快。

Docker 部署Nacos Server

Docker kany.wang 96℃ 0评论

背景介绍

配置完成了Redis之后。

  • OS:Ubuntu Server 16.04.1 LTS 64位
  • Docker:19.03.1
  • Nacos Server:1.1.3
    • nacos-sever-01:172.17.0.7
    • nacos-sever-02:172.17.0.8
  • 文件目录:
    • /data/nacos/{cluster1,cluster2}/{data,conf,logs}

学习目的

学会自己构建Nacos Server,不依赖官方的nacos/nacos-server:last镜像。
参考了很多别人构建的镜像https://hub.docker.com/search?q=nacos&type=image,发现别人写的都很简单。但是自己的始终不能像其他人一样简单的运行起来。
比如下面的例子:

FROM zhegeshijiehuiyouai/java:1.8
ENV MODE="standalone"
ADD nacos-server-1.1.3.tar.gz /usr/local/
ADD cluster.conf /usr/local/nacos/conf/
ADD entrypoint.sh /
EXPOSE 8848
ENTRYPOINT ["/entrypoint.sh"]

完整地址:https://github.com/zhegeshijiehuiyouai/RoadToDevOps/blob/master/03-Dockerfile/01-nacos
反正我是怎么都没有运行起来。
但是使用v1.0.0的startup.sh运行v1.1.3的nacos-server.jar,采用standalone方式是可以运行起来的,使用集群就不成。
进入到镜像里面,再调用startup.sh集群是可以起来的。
所以这个到底是什么原因呢?就因为这个东西折腾已经三天。对此不想多说什么。
截至发文,还有集群列表不能正常显示的问题。目前也不知道能不能注册,待测试。

构建Nacos需要JDK镜像

Dockerfile文件内容:

# 一定要使用FROM开头
FROM openjdk:8u191-jre-alpine

# 作者信息
LABEL author="Jason.Wang"
LABEL version="8u191"

# 执行的命令
RUN apk update; \
    apk add --no-cache; \
    apk --no-cache add tzdata; \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime; \
    echo "Asia/Shanghai" > /etc/timezone;

创建好之后执行docker build . --tag jdk:8u191这样就会创建一个JDK的镜像文件

构建Nacos

Dockerfile文件内容:

FROM jdk:8u191

MAINTAINER "kzone.wang@gmail.com"
LABEL author="Jason.Wang"
LABEL version="1.1.3"

# set environment
ENV MODE="cluster" \
    PREFER_HOST_MODE="ip" \
    BASE_DIR="/usr/local/nacos" \
    CLASSPATH=".:/usr/local/nacos/conf:$CLASSPATH" \
    NACOS_SERVERS="172.17.0.3:8848" \
    CLUSTER_CONF="/usr/local/nacos/conf/cluster.conf" \
    FUNCTION_MODE="all" \
    JAVA_HOME="/usr/lib/jvm/java-1.8-openjdk/jre" \
    NACOS_USER="nacos" \
    JAVA="/usr/lib/jvm/java-1.8-openjdk/jre/bin/java" \
    JVM_XMS="2g" \
    JVM_XMX="2g" \
    JVM_XMN="1g" \
    JVM_MS="128m" \
    JVM_MMS="320m" \
    NACOS_DEBUG="n" \
    TOMCAT_ACCESSLOG_ENABLED="false"

ARG NACOS_VERSION=1.1.3

WORKDIR /$BASE_DIR

RUN  cd /usr/local; \
    wget && wget https://github.com/alibaba/nacos/releases/download/${NACOS_VERSION}/nacos-server-${NACOS_VERSION}.tar.gz -P; \
    tar -zxf nacos-server-${NACOS_VERSION}.tar.gz; \
    rm -rf nacos-server-${NACOS_VERSION}.tar.gz;

ADD docker-startup.sh bin/docker-startup.sh

# Set startup log dir

RUN mkdir -p logs \
        && cd logs \
        && touch start.out \
        && ln -sf /dev/stdout start.out \
        && ln -sf /dev/stderr start.out

RUN chmod +x bin/docker-startup.sh

EXPOSE 8848

ENTRYPOINT ["bin/docker-startup.sh"]

使用CentOS直接构建

Dockerfile文件内容:

FROM centos:7.5.1804

MAINTAINER "kzone.wang@gmail.com"
LABEL author="Jason.Wang"
LABEL version="1.1.3"

# set environment
ENV MODE="cluster" \
    PREFER_HOST_MODE="ip"\
    BASE_DIR="/usr/local/nacos" \
    CLASSPATH=".:/usr/local/nacos/conf:$CLASSPATH" \
    CLUSTER_CONF="/usr/local/nacos/conf/cluster.conf" \
    FUNCTION_MODE="all" \
    JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk" \
    NACOS_USER="nacos" \
    JAVA="/usr/lib/jvm/java-1.8.0-openjdk/bin/java" \
    JVM_XMS="2g" \
    JVM_XMX="2g" \
    JVM_XMN="1g" \
    JVM_MS="128m" \
    JVM_MMS="320m" \
    NACOS_DEBUG="n" \
    TOMCAT_ACCESSLOG_ENABLED="false" \
    TIME_ZONE="Asia/Shanghai"

ARG NACOS_VERSION=1.1.3

WORKDIR /$BASE_DIR

RUN set -x \
    && yum update -y \
    && yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel wget iputils nc  vim libcurl\
    && wget https://github.com/alibaba/nacos/releases/download/${NACOS_VERSION}/nacos-server-${NACOS_VERSION}.tar.gz -P /usr/local \
    && tar -xzvf /usr/local/nacos-server-${NACOS_VERSION}.tar.gz -C /usr/local \
    && rm -rf /usr/local/nacos-server-${NACOS_VERSION}.tar.gz /usr/local/nacos/bin/* /usr/local/nacos/conf/*.properties /usr/local/nacos/conf/*.example /usr/local/nacos/conf/nacos-mysql.sql \
    && yum autoremove -y wget \
    && ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo '$TIME_ZONE' > /etc/timezone \
    && yum clean all

ADD docker-startup.sh bin/docker-startup.sh

# Set startup log dir

RUN mkdir -p logs \
    && cd logs \
    && touch start.out \
    && ln -sf /dev/stdout start.out \
    && ln -sf /dev/stderr start.out

RUN chmod +x bin/docker-startup.sh

EXPOSE 8848

ENTRYPOINT ["bin/docker-startup.sh"]

Nacos Server启动脚本

这个脚本是阿里巴巴nacos编写的,但是我进行了修改,我使用的jdk:8u191没有/bin/bash,只有/bin/sh这里进行了修改,其他的不知道了。

docker-startup.sh文件内容:

#!/bin/sh
# Copyright 1999-2018 Alibaba Group Holding Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -x
export DEFAULT_SEARCH_LOCATIONS="classpath:/,classpath:/config/,file:./,file:./config/"
export CUSTOM_SEARCH_LOCATIONS=${DEFAULT_SEARCH_LOCATIONS},file:${BASE_DIR}/conf/,${BASE_DIR}/init.d/
export CUSTOM_SEARCH_NAMES="application,custom"
PLUGINS_DIR="/home/nacos/plugins/peer-finder"
function print_servers(){
   if [[ ! -d "${PLUGINS_DIR}" ]]; then
    echo "#it is ip" > "$CLUSTER_CONF"
    for server in ${NACOS_SERVERS}; do
            echo "$server" >> "$CLUSTER_CONF"
    done
   else
    sh $PLUGINS_DIR/plugin.sh
   sleep 30
        fi
}
#===========================================================================================
# JVM Configuration
#===========================================================================================
if [[ "${MODE}" == "standalone" ]]; then

    JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"
    JAVA_OPT="${JAVA_OPT} -Dnacos.standalone=true"
else

  #JAVA_OPT="${JAVA_OPT} -server -Xms${JVM_XMS} -Xmx${JVM_XMX} -Xmn${JVM_XMN} -XX:MetaspaceSize=${JVM_MS} -XX:MaxMetaspaceSize=${JVM_MMS}"
  JAVA_OPT="${JAVA_OPT} -server"
  if [[ "${NACOS_DEBUG}" == "y" ]]; then
    JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
  fi
  JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
  JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
  print_servers
fi

#===========================================================================================
# Setting system properties
#===========================================================================================
# set  mode that Nacos Server function of split
if [[ "${FUNCTION_MODE}" == "config" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=config"
elif [[ "${FUNCTION_MODE}" == "naming" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=naming"
fi
# set nacos server ip
if [[ ! -z "${NACOS_SERVER_IP}" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.server.ip=${NACOS_SERVER_IP}"
fi

if [[ ! -z "${USE_ONLY_SITE_INTERFACES}" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.inetutils.use-only-site-local-interfaces=${USE_ONLY_SITE_INTERFACES}"
fi

if [[ ! -z "${PREFERRED_NETWORKS}" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.inetutils.preferred-networks=${PREFERRED_NETWORKS}"
fi

if [[ ! -z "${IGNORED_INTERFACES}" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.inetutils.ignored-interfaces=${IGNORED_INTERFACES}"
fi

if [[ "${PREFER_HOST_MODE}" == "hostname" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.preferHostnameOverIp=true"
fi

JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([0-9]*).*$/\1/p')
if [[ "$JAVA_MAJOR_VERSION" -ge "9" ]] ; then
  JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${BASE_DIR}/logs/nacos_gc.log:time,tags:filecount=10,filesize=102400"
else
  JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext:${BASE_DIR}/plugins/cmdb"
  JAVA_OPT="${JAVA_OPT} -Xloggc:${BASE_DIR}/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M"
fi

JAVA_OPT="${JAVA_OPT} -Dnacos.home=${BASE_DIR}"
JAVA_OPT="${JAVA_OPT} -jar ${BASE_DIR}/target/nacos-server.jar"
JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
JAVA_OPT="${JAVA_OPT} --spring.config.location=${CUSTOM_SEARCH_LOCATIONS}"
JAVA_OPT="${JAVA_OPT} --spring.config.name=${CUSTOM_SEARCH_NAMES}"
JAVA_OPT="${JAVA_OPT} --logging.config=${BASE_DIR}/conf/nacos-logback.xml"

echo "nacos is starting,you can check the ${BASE_DIR}/logs/start.out"
echo "$JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
nohup $JAVA ${JAVA_OPT} > ${BASE_DIR}/logs/start.out 2>&1 < /dev/null

构建镜像

docker build . --tag nacos:1.1.3

启动脚本及配置

这里只贴出nacos-server-01.sh的内容

#!/bin/sh
sudo rm -rf /data/nacos/cluster1/{data,logs}/*
docker stop nacos-server-01
docker rm nacos-server-01
docker run --name nacos-server-01 --hostname nacos-server-01 -p 8848:8848 -p 9555:9555 -e NACOS_SERVERS="172.17.0.7:8848 172.17.0.8:8848" -v /data/nacos/cluster1/conf/cluster.conf:/usr/local/nacos/conf/cluster.conf -v /data/nacos/cluster1/conf/application.properties:/usr/local/nacos/conf/application.properties -v /data/nacos/cluster1/logs:/usr/local/nacos/logs -v /data/nacos/cluster1/data:/usr/local/nacos/data -d nacos:1.1.3

/data/nacos/cluster1/conf/cluster.conf的内容:

# 这个就一个空文件,由于在上面的NACOS_SERVERS会传入,所以这里不用传入

/data/nacos/cluster1/conf/application.properties的内容:

# spring

# 注意这个地方不要修改,我修改成“/”然后那个集群节点一个都没有出来了
server.contextPath=/nacos
# 和上面一样
server.servlet.contextPath=/nacos
server.port=8848

nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false


# 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

server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i
# default current work dir
server.tomcat.basedir=

## spring security config
### turn off security
#spring.security.enabled=false
#management.security=false
#security.basic.enabled=false
#nacos.security.ignore.urls=/**

nacos.security.ignore.urls=/,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/login,/v1/console/health/**,/v1/cs/**,/v1/ns/**,/v1/cmdb/**,/actuator/**,/v1/console/server/**

nacos.naming.distro.taskDispatchThreadCount=1
nacos.naming.distro.taskDispatchPeriod=200
nacos.naming.distro.batchSyncKeyCount=1000
nacos.naming.distro.initDataRatio=0.9
nacos.naming.distro.syncRetryDelay=5000
nacos.naming.data.warmup=true
nacos.naming.expireInstance=true

db.num=1
db.url.0=jdbc:mysql://{mysql_server_address}:{mysql_server_port}/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user={mysql_user_name}
db.password={mysql_pass_word}

到这里基本配置完成了

Nginx配置

upstream nacos-server {
  server 172.17.0.7:8848;
  server 172.17.0.8:8848;
}

server {
  listen 80;
  listen 443 ssl http2;
  ssl_certificate /etc/nginx/ssl/domain.com.crt;
  ssl_certificate_key /etc/nginx/ssl/domain.com.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
  ssl_prefer_server_ciphers on;
  ssl_session_timeout 10m;
  ssl_session_cache builtin:1000 shared:SSL:10m;
  ssl_buffer_size 1400;
  add_header Strict-Transport-Security max-age=15768000;
  ssl_stapling on;
  ssl_stapling_verify on;
  server_name domain.com;
  index index.html index.htm index.php;

  if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
  if ($host != domain.com) {  return 301 https://domain.com$request_uri;  }

  #location / {
  #  proxy_pass http://domain.com/nacos/;
  #}
  location / {
    proxy_pass  http://nacos-server/nacos/;
    proxy_redirect      off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto "http";
  }
}

不要直接使用,这里有使用到https的证书,所以需要判断修改。

小技巧

程序下载的软件在本地搭建一个Nginx服务即可

这个是nginx.sh的内容:

#/bin/sh
# 创建目录文件
sudo mkdir -p /data/nginx/{conf,logs,sites,ssl,html}
sudo rm -rf /data/nginx/logs/*
# 停止nginx-server的容器
docker stop nginx-server
# 删除nginx-server的容器
docker rm nginx-server
# 启动nginx-server的容器,并映射目录
docker run --hostname nginx-server --name nginx-server -p 80:80 -p 443:443 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /data/nginx/logs:/var/log/nginx -v /data/nginx/sites:/etc/nginx/conf.d -v /data/nginx/ssl:/etc/nginx/ssl -v /data/nginx/html:/usr/share/nginx/html:ro  -d nginx:latest

这个是/data/nginx/conf/nginx.conf的内容:

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    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;
    include /etc/nginx/conf.d/*.conf;
}

这个是/data/nginx/sites/default.conf的内容:

server {
    listen  80;
    charset utf8;
    access_log  /var/log/nginx/host.access.log  main;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /index.html;
    location = /index.html {
        root   /usr/share/nginx/html;
    }
}

修改Nginx的配置之后我们要使修改的配置生效,直接使用docker exec -it nginx-server nginx -s reload在不重启容器的情况下让配置生效。
然后我们使用docker inspect nginx-server查看信息,获取IP。
将我们下载好的nacos-server-1.1.3.tar.gz放置到/data/nginx/html下,并修改相关的wget路径即可。这样就会让我们节约很多时间。不需要漫长的等待网络下载文件。

集群列表不显示

在服务器上启动的时候发现不能正常展现集群节点,在线搜索一番发现需要配置一个nacos.server.ip=Local Ip这样的一个配置。
我们给启动脚本中添加一个配置NACOS_SERVER_IP=当前机器IP


自己搭建不能显示集群列表的原因:不能修改“server.contextPath=/nacos、server.servlet.contextPath=/nacos为server.contextPath=/、server.servlet.contextPath=/”

成品展示

目前配置完成的地址为:https://nacos.freegou.tech
账户名称:nacos
账户密码:nacos

UPDATE `nacos_config`.`users` SET `password` = '$2a$10$E/wOWsXoXMsOmAOf/aPcIuTJQc5VLumaU.46nOduWeQvyaAfUkhdC';

参考资料

联系方式

  • 小伙伴群:J2EE技术交流

打赏

转载请注明:懒人屋 » Docker 部署Nacos Server

喜欢 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址