写在最前

部署Nacos时,可选择本地或MySQL作为存储。本例中,我们选用MySQL。

1. 前置条件

  1. ✨ubuntu server 22.04 LTS 安装与配置✨

  2. ✨二进制部署任意版本docker✨

  3. 跨环境部署艺术:简化中间件连接策略

2. docker 部署

nacos已经出到很新了后续我再整新版本,本文部署的是2.0.3老版本。连接mysql的地址是 app-mysql1.basic.tanqidi.com 如果忘记需要回顾 跨环境部署艺术:简化中间件连接策略

https://nacos.io/

https://github.com/alibaba/nacos/tree/2.0.3/distribution/conf

  1. 到mysql中创建名为 nacos 的数据库

  2. 导入 nacos-mysql.sql 到名为 nacos 的数据库里

docker run \
-e MODE=standalone \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=app-mysql1.basic.tanqidi.com \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-p 8848:8848 \
-p 9848:9848 \
-p 9849:9849 \
-p 7848:7848 \
-d \
--restart=always \
--name nacos-2.0.3 \
nacos/nacos-server:2.0.3

3. Kubernetes 部署

3.1 secret

kind: Secret
apiVersion: v1
metadata:
  name: nacos-secret
  namespace: basic
  annotations:
    kubesphere.io/creator: admin
data:
  MODE: c3RhbmRhbG9uZQ==
  MYSQL_SERVICE_DB_NAME: bmFjb3M=
  MYSQL_SERVICE_HOST: bXlzcWw4LmJhc2lj
  MYSQL_SERVICE_PASSWORD: MTIzNDU2
  MYSQL_SERVICE_PORT: MzMwNg==
  MYSQL_SERVICE_USER: cm9vdA==
  SPRING_DATASOURCE_PLATFORM: bXlzcWw=
type: Opaque

3.2 deployment

kind: Deployment
apiVersion: apps/v1
metadata:
  name: nacos
  namespace: basic
  labels:
    app: nacos
  annotations:
    deployment.kubernetes.io/revision: '1'
    kubesphere.io/creator: admin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nacos
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nacos
      annotations:
        kubesphere.io/creator: admin
        kubesphere.io/imagepullsecrets: '{}'
        kubesphere.io/restartedAt: '2024-10-11T09:09:20.034Z'
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
      containers:
        - name: nacos-server
          image: 'nacos/nacos-server:v2.0.3'
          ports:
            - name: tcp-8848
              containerPort: 8848
              protocol: TCP
            - name: tcp-9848
              containerPort: 9848
              protocol: TCP
            - name: tcp-9849
              containerPort: 9849
              protocol: TCP
            - name: tcp-7848
              containerPort: 7848
              protocol: TCP
          env:
            - name: MODE
              valueFrom:
                secretKeyRef:
                  name: nacos-secret
                  key: MODE
            - name: MYSQL_SERVICE_DB_NAME
              valueFrom:
                secretKeyRef:
                  name: nacos-secret
                  key: MYSQL_SERVICE_DB_NAME
            - name: MYSQL_SERVICE_HOST
              valueFrom:
                secretKeyRef:
                  name: nacos-secret
                  key: MYSQL_SERVICE_HOST
            - name: MYSQL_SERVICE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: nacos-secret
                  key: MYSQL_SERVICE_PASSWORD
            - name: MYSQL_SERVICE_PORT
              valueFrom:
                secretKeyRef:
                  name: nacos-secret
                  key: MYSQL_SERVICE_PORT
            - name: MYSQL_SERVICE_USER
              valueFrom:
                secretKeyRef:
                  name: nacos-secret
                  key: MYSQL_SERVICE_USER
            - name: SPRING_DATASOURCE_PLATFORM
              valueFrom:
                secretKeyRef:
                  name: nacos-secret
                  key: SPRING_DATASOURCE_PLATFORM
          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
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600

3.3 service

使用NodePort来试验访问

kind: Service
apiVersion: v1
metadata:
  name: nacos
  namespace: basic
  labels:
    app: nacos
    version: v1
  annotations:
    kubesphere.io/creator: admin
    kubesphere.io/serviceType: statelessservice
spec:
  ports:
    - name: tcp-8848
      protocol: TCP
      port: 8848
      targetPort: 8848
      nodePort: 30816
    - name: tcp-9848
      protocol: TCP
      port: 9848
      targetPort: 9848
      nodePort: 32466
    - name: tcp-9849
      protocol: TCP
      port: 9849
      targetPort: 9849
      nodePort: 32500
    - name: tcp-7848
      protocol: TCP
      port: 7848
      targetPort: 7848
      nodePort: 32058
  selector:
    app: nacos
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  internalTrafficPolicy: Cluster