写在最前

我们计划实现 Eureka 与 Nacos 之间的双向同步功能。然而,nacos-sync 官方版本默认未提供登录认证机制,这意味着在管理控制台中配置 Eureka 与 Nacos 集群时,包含的敏感信息(如密码)将直接暴露,存在较大的安全风险。为了避免这一隐患,我们参考了上一章节 Eureka 的认证方案,引入 spring-boot-starter-security,为 nacos-sync 增加统一的登录认证功能,从而保障敏感配置和管理接口的安全性。

1. docker 部署

2. kubernetes 部署

2.1 Dockerfile

具体操作可以参考 4. 步骤的代码变更。基本流程分为两步:

  1. 下载官方原始 https://github.com/nacos-group/nacos-sync/releases/download/0.5.0/nacos-sync-0.5.0.tar.gz

    • 获取官方发布的 nacos-sync-*.tar.gz 包。

    • 解压后删除原有的 nacos-sync-service.jar

  2. 重新打包覆盖 JAR

    • 在本地修改或重新编译 nacossync-worker 模块(比如加入 spring-boot-starter-security 依赖)。

    • 通过 4. 步骤的源码重新打包编译,生成包含 Spring Security 认证的 nacos-sync-service.jar,然后使用 tar -cf 命令将新的 JAR 覆盖回原始目录结构,重新打包成 tar.gz。

docker buildx build --push --platform linux/amd64,linux/arm64 -t tanqidi/nacos-sync:0.5.0 .

成品:nacos-sync-0.5.0.tar.gz

FROM openjdk:17-jdk

WORKDIR /app

COPY nacos-sync-0.5.0.tar.gz /app/

RUN tar -xf nacos-sync-0.5.0.tar.gz && \
    rm nacos-sync-0.5.0.tar.gz

EXPOSE 8080

# 直接前台启动 jar
CMD ["java", "-jar", "/app/nacos-sync/nacos-sync-server.jar", "--spring.config.location=/app/nacos-sync/conf/application.properties"]

2.2 service

使用nodeport来试验访问

kind: Service
apiVersion: v1
metadata:
  name: nacos-sync
  namespace: bx
  labels:
    app: nacos-sync
  annotations:
    kubesphere.io/creator: admin
spec:
  ports:
    - name: http-8080
      protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 30195
  selector:
    app: nacos-sync
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  internalTrafficPolicy: Cluster

2.3 configmap

注意要变更成为你的数据库连接地址与账号密码,最重要的是security认证页面的账号密码也改一下。

初始化SQL >>> https://github.com/nacos-group/nacos-sync/blob/0.5.0/nacossync-distribution/bin/nacosSync.sql

kind: ConfigMap
apiVersion: v1
metadata:
  name: nacos-sync-config
  namespace: bx
  annotations:
    kubesphere.io/creator: admin
data:
  application.properties: >-
    server.port=8080

    server.servlet.context-path=/


    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

    spring.jpa.hibernate.ddl-auto=update

    spring.jpa.properties.hibernate.show_sql=false


    spring.cloud.discovery.enabled=false

    spring.main.allow-circular-references=true


    spring.datasource.url=jdbc:mysql://172.31.0.99:3306/nacos_sync?characterEncoding=utf8

    spring.datasource.username=root

    spring.datasource.password=123456

    management.endpoints.web.exposure.include=*

    management.endpoint.health.show-details=always


    # 配置security账号密码

    spring.security.user.name=admin

    spring.security.user.password=35274E93-D57F-4B6A-8B48-659604AC0811

2.4 deployment

kind: Deployment
apiVersion: apps/v1
metadata:
  name: nacos-sync
  namespace: bx
  annotations:
    deployment.kubernetes.io/revision: '1'
    kubesphere.io/creator: admin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nacos-sync
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nacos-sync
      annotations:
        kubesphere.io/creator: admin
        kubesphere.io/imagepullsecrets: '{}'
        logging.kubesphere.io/logsidecar-config: '{}'
    spec:
      volumes:
        - name: config-volume
          configMap:
            name: nacos-sync-config
            items:
              - key: application.properties
                path: application.properties
            defaultMode: 420
      containers:
        - name: nacos-sync
          image: 'tanqidi/nacos-sync:0.5.0'
          ports:
            - name: http-8080
              containerPort: 8080
              protocol: TCP
          resources: {}
          volumeMounts:
            - name: config-volume
              readOnly: true
              mountPath: /app/nacos-sync/conf/application.properties
              subPath: application.properties
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: Always
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      securityContext: {}
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600

3. 镜像成品

我已使用 Docker Buildx 构建了同时支持 amd64arm64 架构的镜像,便于在不同平台运行,可按需优化或扩展。

  • tanqidi/nacos-sync:0.5.0

4. 代码变更

https://github.com/nacos-group/nacos-sync/tree/0.5.0

代码其实很简单,只需要在 nacossync-worker 模块的 pom.xml 中引入 spring-boot-starter-security 依赖,重新打包生成 JAR 即可启用登录认证功能。

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

x. 使用方式

写在最后