K8s部署Nacos 集群

1、安装前先导入nacos数据库sql

wget https://raw.githubusercontent.com/alibaba/nacos/refs/heads/master/distribution/conf/mysql-schema.sql

2、创建ConfigMap

nacos-cm.yml

kind: ConfigMap
apiVersion: v1
metadata:
  name: nacos-cm
  namespace: nacos
data:
  mode: cluster
  mysql.db.name: nacos
  mysql.host: 192.168.100.221
  mysql.password: Hbis@123
  mysql.port: '3306'
  mysql.user: root
  nacos-auth-enable: 'true'
  nacos-auth-identity-key: nacos
  nacos-auth-identity-value: nacos
  nacos-auth-token: SecretKeyYzUxYmUzNmIwOTMzZTUyMGJhNjBkNzY0ZjhhZDJhOTI=
  nacos-servers: >-
    nacos-cluster-0.nacos-headless.nacos.svc.cluster.local:8848
    nacos-cluster-1.nacos-headless.nacos.svc.cluster.local:8848
    nacos-cluster-2.nacos-headless.nacos.svc.cluster.local:8848

3、创建service

nacos-headless.yaml

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

3、创建pvc

4、创建 StatefulSet

nacos-cluster.yml

kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: nacos
  namespace: nacos
  labels:
    app: nacos
  annotations:
    kubesphere.io/creator: admin
    kubesphere.io/description: nacos集群
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nacos
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nacos
      annotations:
        cni.projectcalico.org/ipv4pools: '["default-ipv4-ippool"]'
        kubesphere.io/creator: admin
        kubesphere.io/imagepullsecrets: '{"nacos":"register-addr"}'
        kubesphere.io/restartedAt: '2024-11-28T03:26:51.577Z'
        logging.kubesphere.io/logsidecar-config: '{}'
    spec:
      initContainers:
        - name: peer-finder-plugin-install
          image: 'registry.flow.cn/nacos/nacos-peer-finder-plugin:1.1'
          resources: {}
          volumeMounts:
            - name: nacos-pvc
              mountPath: /home/nacos/plugins/peer-finder
              subPath: peer-finder
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      containers:
        - name: nacos
          image: 'registry.flow.cn/nacos/nacos-server:v2.4.0'
          ports:
            - name: client-port
              containerPort: 8848
              protocol: TCP
            - name: client-rpc
              containerPort: 9848
              protocol: TCP
            - name: raft-rpc
              containerPort: 9849
              protocol: TCP
            - name: old-raft-rpc
              containerPort: 7848
              protocol: TCP
          env:
            - name: NACOS_REPLICAS
              value: '3'
            - name: SERVICE_NAME
              value: nacos-headless
            - name: DOMAIN_NAME
              value: cluster.local
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - 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_APPLICATION_PORT
              value: '8848'
            - name: PREFER_HOST_MODE
              value: hostname
            - name: NACOS_SERVERS
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: nacos-servers
            - name: MODE
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mode
            - name: NACOS_AUTH_ENABLE
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: nacos-auth-enable
            - name: NACOS_AUTH_IDENTITY_KEY
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: nacos-auth-identity-key
            - name: NACOS_AUTH_IDENTITY_VALUE
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: nacos-auth-identity-value
            - name: NACOS_AUTH_TOKEN
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: nacos-auth-token
          resources:
            requests:
              cpu: 500m
              memory: 2Gi
          volumeMounts:
            - name: nacos-pvc
              mountPath: ' /home/nacos'
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      serviceAccountName: default
      serviceAccount: default
      securityContext: {}
      imagePullSecrets:
        - name: register-addr
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: app
                    operator: In
                    values:
                      - nacos
              topologyKey: kubernetes.io/hostname
      schedulerName: default-scheduler
  volumeClaimTemplates:
    - kind: PersistentVolumeClaim
      apiVersion: v1
      metadata:
        name: nacos-pvc
        namespace: nacos
        creationTimestamp: null
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 10Gi
        storageClassName: local
        volumeMode: Filesystem
      status:
        phase: Pending
  serviceName: nacos-headless
  podManagementPolicy: OrderedReady
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      partition: 0
  revisionHistoryLimit: 10
  persistentVolumeClaimRetentionPolicy:
    whenDeleted: Retain
    whenScaled: Retain

4、配置 K8s 集群外部访问

nacos-external.yaml

kind: Service
apiVersion: v1
metadata:
  name: nacos-servie
  namespace: hgjg-common
  labels:
    app: nacos-servie
  annotations:
    kubesphere.io/creator: admin
    kubesphere.io/description: nacos外部访问
spec:
  ports:
    - name: tcp-8848
      protocol: TCP
      port: 8848
      targetPort: 8848
      nodePort: 30443
  selector:
    app: nacos
  type: NodePort

初始化成功后会弹窗提示初始化成功,并明文展示指定的密码或随机生成的密码,请保存好此密码。

评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值