前置条件

配置依赖

配置域名

address=/app-nacos1.basic.tanqidi.com/172.31.0.100
address=/app-nacos2.basic.tanqidi.com/172.31.0.101
address=/app-nacos3.basic.tanqidi.com/172.31.0.102

配置数据库

# 1. 创建数据库
CREATE DATABASE `nacosdb` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin';

# 2. 导入sql
https://tanqidi.com/upload/nacos-mysql.sql

# 3. 创建账号与权限
CREATE USER `nacosdb`@`%` IDENTIFIED WITH mysql_native_password BY '123456';

GRANT Alter, Alter Routine, Create, Create Routine, Create Temporary Tables, Create View, Delete, Drop, Event, Execute, Index, Insert, Lock Tables, References, Select, Show View, Trigger, Update ON `nacosdb`.* TO `nacosdb`@`%`;

1. docker 部署

图片-nxks.png

# nacos-1, 运行在172.31.0.100
docker run -d \
  --name nacos-1 \
  -e MODE=cluster \
  -e PREFER_HOST_MODE=hostname \
  -e NACOS_SERVER_IP=app-nacos1.basic.tanqidi.com \
  -e NACOS_SERVERS="app-nacos1.basic.tanqidi.com:8848,app-nacos2.basic.tanqidi.com:8848,app-nacos3.basic.tanqidi.com:8848" \
  -e SPRING_DATASOURCE_PLATFORM=mysql \
  -e MYSQL_SERVICE_HOST=app-mysql1.basic.tanqidi.com \
  -e MYSQL_SERVICE_DB_NAME=nacosdb \
  -e MYSQL_SERVICE_USER=nacosdb \
  -e MYSQL_SERVICE_PASSWORD=123456 \
  -p 8848:8848 \
  -p 9848:9848 \
  -p 9849:9849 \
  -p 7848:7848 \
  --restart=always \
  nacos/nacos-server:v2.1.2

# nacos-2, 运行在172.31.0.101
docker run -d \
  --name nacos-2 \
  -e MODE=cluster \
  -e PREFER_HOST_MODE=hostname \
  -e NACOS_SERVER_IP=app-nacos2.basic.tanqidi.com \
  -e NACOS_SERVERS="app-nacos1.basic.tanqidi.com:8848,app-nacos2.basic.tanqidi.com:8848,app-nacos3.basic.tanqidi.com:8848" \
  -e SPRING_DATASOURCE_PLATFORM=mysql \
  -e MYSQL_SERVICE_HOST=app-mysql1.basic.tanqidi.com \
  -e MYSQL_SERVICE_DB_NAME=nacosdb \
  -e MYSQL_SERVICE_USER=nacosdb \
  -e MYSQL_SERVICE_PASSWORD=123456 \
  -p 8848:8848 \
  -p 9848:9848 \
  -p 9849:9849 \
  -p 7848:7848 \
  --restart=always \
  nacos/nacos-server:v2.1.2

# nacos-3, 运行在172.31.0.102
docker run -d \
  --name nacos-3 \
  -e MODE=cluster \
  -e PREFER_HOST_MODE=hostname \
  -e NACOS_SERVER_IP=app-nacos3.basic.tanqidi.com \
  -e NACOS_SERVERS="app-nacos1.basic.tanqidi.com:8848,app-nacos2.basic.tanqidi.com:8848,app-nacos3.basic.tanqidi.com:8848" \
  -e SPRING_DATASOURCE_PLATFORM=mysql \
  -e MYSQL_SERVICE_HOST=app-mysql1.basic.tanqidi.com \
  -e MYSQL_SERVICE_DB_NAME=nacosdb \
  -e MYSQL_SERVICE_USER=nacosdb \
  -e MYSQL_SERVICE_PASSWORD=123456 \
  -p 8848:8848 \
  -p 9848:9848 \
  -p 9849:9849 \
  -p 7848:7848 \
  --restart=always \
  nacos/nacos-server:v2.1.2

2. kubernetes 部署

图片-grmb.png

2.1 statefulset

kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: nacos
  namespace: basic
  labels:
    app: nacos
  annotations:
    kubesphere.io/creator: admin
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nacos
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nacos
      annotations:
        kubesphere.io/creator: admin
        kubesphere.io/imagepullsecrets: '{}'
        kubesphere.io/restartedAt: '2024-11-07T06:36:13.462Z'
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
      containers:
        - name: nacos
          image: 'nacos/nacos-server:v2.1.2'
          ports:
            - name: http-8848
              containerPort: 8848
              protocol: TCP
            - name: http-9848
              containerPort: 9848
              protocol: TCP
            - name: http-9849
              containerPort: 9849
              protocol: TCP
            - name: http-7848
              containerPort: 7848
              protocol: TCP
          env:
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
            - name: nacos.inetutils.prefer-hostname-over-ip
              value: 'true'
            - name: nacos.inetutils.ip-address
              value: $(MY_POD_NAME).nacos.basic.svc.cluster.local
            - name: NACOS_MODE
              value: cluster
            - name: NACOS_SERVERS
              value: nacos-0.nacos.basic.svc.cluster.local:8848,nacos-1.nacos.basic.svc.cluster.local:8848,nacos-2.nacos.basic.svc.cluster.local:8848
            - name: SPRING_DATASOURCE_PLATFORM
              value: mysql
            - name: MYSQL_SERVICE_HOST
              value: app-mysql1.basic.tanqidi.com
            - name: MYSQL_SERVICE_DB_NAME
              value: nacosdb
            - name: MYSQL_SERVICE_USER
              value: nacosdb
            - name: MYSQL_SERVICE_PASSWORD
              value: '123456'
          resources: {}
          volumeMounts:
            - name: host-time
              readOnly: true
              mountPath: /etc/localtime
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      serviceAccountName: default
      serviceAccount: default
      securityContext: {}
      schedulerName: default-scheduler
  serviceName: nacos
  podManagementPolicy: OrderedReady
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      partition: 0
  revisionHistoryLimit: 10

2.2 service 内部

kind: Service
apiVersion: v1
metadata:
  name: nacos
  namespace: basic
  labels:
    app: nacos
  annotations:
    kubesphere.io/creator: admin
spec:
  ports:
    - name: http-8848
      protocol: TCP
      port: 8848
      targetPort: 8848
    - name: http-9848
      protocol: TCP
      port: 9848
      targetPort: 9848
    - name: http-9849
      protocol: TCP
      port: 9849
      targetPort: 9849
    - name: http-7848
      protocol: TCP
      port: 7848
      targetPort: 7848
  selector:
    app: nacos
  clusterIP: None
  clusterIPs:
    - None
  type: ClusterIP
  sessionAffinity: None
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  internalTrafficPolicy: Cluster

2.3 service 外部

使用NodePort来试验访问

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

2.4 coredns

rewrite name app-nacos1.basic.tanqidi.com nacos-0.nacos.basic.svc.cluster.local
rewrite name app-nacos2.basic.tanqidi.com nacos-1.nacos.basic.svc.cluster.local
rewrite name app-nacos3.basic.tanqidi.com nacos-2.nacos.basic.svc.cluster.local

3. 异常解决

3.1 下线异常

节点列表点击下线节点会出现错误还以为是搭建失败了,查看相关issure看到是功能不够完善没有开启,新的版本应该已经修复了吧?

https://developer.aliyun.com/ask/576495

https://github.com/alibaba/nacos/issues/9155
图片-ztuq.png

图片-iqyz.png