在服务器根目录下,创建 nacos.yaml 文件,文件内容为:

---
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: dw-prod
  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
  selector:
    app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: dw-prod
  annotations:
    kubesphere.io/alias-name: Nacos 服务配置
    kubesphere.io/description: Nacos 服务配置
data:
  mysql.host: "10.16.8.2"
  mysql.db.name: "nacos"
  mysql.port: "3306"
  mysql.user: "nacos"
  mysql.password: "nacos"
---
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-k8s
          imagePullPolicy: Always
          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
          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: 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.dw-prod.svc.cluster.local:8848 nacos-1.nacos-headless.dw-prod.svc.cluster.local:8848 nacos-2.nacos-headless.dw-prod.svc.cluster.local:8848"
            - name: MYSQL_SERVICE_DB_PARAM
              value: "characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&serverTimezone=UTC"
  selector:
    matchLabels:
      app: nacos
根据实际情况替换 data 中的 mysql配置:

根据实际情况替换 NACOS_SERVERS 中的 namespace 部分:

因为nacos是支撑服务,这里使用默认空间 default, 则改后的内容应该是:

使用 kubectl 执行如下命令创建nacos集群:

kubectl create -f nacos.yaml --namespace=你的命名空间

kubectl create -f nacos.yaml --namespace=dw-prod

Ingress 代理:

作者:Jeebiz  创建时间:2023-01-11 11:55
最后编辑:Jeebiz  更新时间:2024-10-03 00:38