Jie
发布于 2026-04-07 / 4 阅读
0
0

[k8s] 部署nacos 使用外部 MySql 8.0+.

k8s 部署nacos ,使用外部 MySql 8.*

k8s集群信息 v1.27.1

名称ip配置
master192.168.10.504C/8G
node1192.168.10.514C/8G
node1192.168.10.524C/8G

nacos版本 2.1.0

  • 注意:2.* 版本后的需要暴露
    • 8848 :web端口
    • 9848: 8848+1000
    • 9849:8848+1001
    • 7848 : 旧的选举端口

k8s yaml 信息

nacos-ConfigMap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: default
data:
  mysql.db.name: nacos
  mysql.host: 127.0.0.1
  mysql.password: 'mysql_password'
  mysql.port: '3306'
  mysql.user: root

nacos-StatefulSet.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
spec:
  serviceName: nacos-headless
  replicas: 3
  template:
    metadata:
      labels:
        app: nacos
      annotations:
        pod.alpha.kubernetes.io/initialized: "true"
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                      - nacos
              topologyKey: "kubernetes.io/hostname"
      containers:
        - name: nacos
          imagePullPolicy: IfNotPresent     #本地有镜像不从远端拉取
          image: nacos/nacos-server:v2.1.0  #镜像版本
          resources:
            requests:
              memory: "2Gi"
              cpu: "500m"
          ports:
            - containerPort: 8848
              name: client
            - containerPort: 9848
              name: client-rpc
            - containerPort: 9849
              name: raft-rpc
            - containerPort: 7848
              name: old-raft-rpc
          env:
            - name: NACOS_REPLICAS
              value: "3"
            - name: MYSQL_SERVICE_HOST
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.host
            - name: MYSQL_SERVICE_DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.db.name
            - name: MYSQL_SERVICE_PORT
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.port
            - name: MYSQL_SERVICE_USER
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.user
            - name: MYSQL_SERVICE_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.password
            - name: SPRING_DATASOURCE_PLATFORM
              value: "mysql"
            - name: NACOS_SERVER_PORT
              value: "8848"
            - name: NACOS_APPLICATION_PORT
              value: "8848"
            - name: PREFER_HOST_MODE
              value: "hostname"
            - name: NACOS_SERVERS
              value: "nacos-0.nacos-headless.default.svc.cluster.local:8848 nacos-1.nacos-headless.default.svc.cluster.local:8848 nacos-2.nacos-headless.default.svc.cluster.local:8848"
  selector:
    matchLabels:
      app: nacos

nacos-headless.yaml

apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  labels:
    app: nacos-headless
spec:
  type: ClusterIP
  clusterIP: None
  ports:
    - port: 8848
      name: server
      targetPort: 8848
    - port: 9848
      name: client-rpc
      targetPort: 9848
    - port: 9849
      name: raft-rpc
      targetPort: 9849
    ## 兼容1.4.x版本的选举端口
    - port: 7848
      name: old-raft-rpc
      targetPort: 7848
  selector:
    app: nacos

nacos-headless-svc.yaml

nacos 内部通信

apiVersion: v1
kind: Service
metadata:
  labels:
    app: nacos
  name: nacos-headless
  namespace: default
spec:
  type: ClusterIP
  ports:
    - name: server
      port: 8848
      protocol: TCP
      targetPort: 8848
    - name: client-rpc
      port: 9848
      protocol: TCP
      targetPort: 9848
    - name: raft-rpc
      port: 9849
      protocol: TCP
      targetPort: 9849
    - name: old-raft-rpc
      port: 7848
      protocol: TCP
      targetPort: 7848
  selector:
    app: nacos

nacos-svc.yaml

暴露外部访问

apiVersion: v1
kind: Service
metadata:
  name: nacos-svc
  namespace: default
spec:
  type: NodePort
  selector:
    app: nacos
  ports:
    - name: server
      nodePort: 30848
      port: 8848
      protocol: TCP
      targetPort: 8848
    - name: client-rpc
      nodePort: 31848
      port: 9848
      protocol: TCP
      targetPort: 9848
    - name: raft-rpc
      nodePort: 31948
      port: 9849
      protocol: TCP
      targetPort: 9849
    - name: old-raft-rpc
      nodePort: 30748
      port: 7848
      protocol: TCP
      targetPort: 7848

评论