需求

使用k8s部署nacos集群,nacos的数据主要保存在mysql中,因此nacos运行时不需要考虑持久化问题。

这里使用2.3.2版本

导入mysql数据

github地址:https://github.com/alibaba/nacos/releases

找到2.3.2版本,下载压缩包,得到nacos-server-2.3.2.tar.gz

解压文件,找到文件nacos\conf\mysql-schema.sql

mysql-schema.sql,就是nacos的初始化sql

使用客户端工具DBeaver连接到mysql,创建数据库nacos

CREATE DATABASE nacos CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

新建数据库用户nacos_all并授权

CREATE USER 'nacos_all'@'%' IDENTIFIED BY 'abcd@1234';

GRANT ALL PRIVILEGES ON nacos . * TO 'nacos_all'@'%';

FLUSH PRIVILEGES;

切换到数据库nacos,执行sql文件mysql-schema.sql,执行完成后,会得到相应的数据表。

编写yaml

nacos-server.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-server-config
  namespace: middleware
data:
  MODE: cluster
  MYSQL_SERVICE_DB_NAME: nacos
  MYSQL_SERVICE_HOST: 192.168.20.131
  MYSQL_SERVICE_USER: nacos_all
  MYSQL_SERVICE_PASSWORD: abcd@1234
  MYSQL_SERVICE_PORT: "3306"
  NACOS_REPLICAS: "3"
  NACOS_SERVER_PORT: "8848"
  NACOS_SERVERS: nacos-server-0.nacos-server-svc.middleware.svc.cluster.local:8848 nacos-server-1.nacos-server-svc.middleware.svc.cluster.local:8848 nacos-server-2.nacos-server-svc.middleware.svc.cluster.local:8848
  PREFER_HOST_MODE: hostname
  SPRING_DATASOURCE_PLATFORM: mysql
  NACOS_AUTH_ENABLE: "false"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  generation: 15
  labels:
    app: nacos-server
  managedFields:
    - apiVersion: apps/v1
      operation: Update
    - apiVersion: apps/v1
      manager: kube-controller-manager
      operation: Update
      subresource: status
  name: nacos-server
  namespace: middleware
spec:
  podManagementPolicy: OrderedReady
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nacos-server
  serviceName: nacos-server-svc
  template:
    metadata:
      labels:
        app: nacos-server
    spec:
      containers:
        - envFrom:
            - configMapRef:
                name: nacos-server-config
          image: 'nacos/nacos-server:v2.3.2'
          imagePullPolicy: IfNotPresent
          name: nacos-server
          ports:
            - containerPort: 8848
              name: web
              protocol: TCP
            - containerPort: 7848
              name: g1
              protocol: TCP
            - containerPort: 9848
              name: g2
              protocol: TCP
            - containerPort: 9849
              name: g3
              protocol: TCP
          resources:
            limits:
              cpu: '0.6'
              ephemeral-storage: 2Gi
            requests:
              cpu: 400m
              memory: 128Mi
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: nacos-server-svc
  namespace: middleware
spec:
  ports:
  - name: web
    port: 8848
    protocol: TCP
    targetPort: 8848
    nodePort: 30788
  - name: grbc
    port: 7848
    protocol: TCP
    targetPort: 7848
  - name: g2
    port: 9848
    protocol: TCP
    targetPort: 9848
    nodePort: 31788
  - name: g3
    port: 9849
    protocol: TCP
    targetPort: 9849
    nodePort: 31789
  selector:
    app: nacos-server
  type: NodePort

这里的nacos直接部署在middleware命名空间,注意修改上面的mysql相关参数。

NACOS_AUTH_ENABLE: "false"

这个参数必须要有,如果没有,默认会开启认证

由于java项目都运行在k8s里面,因此内网访问,就不需要开启nacos认证了。

创建命名空间

kubectl create namespace middleware

部署应用

kubectl apply -f nacos-server.yaml

等待几分钟,确保3个pod都是Running状态

访问

访问nacos页面,这里使用的nodeport方式暴露端口

http://172.16.0.13:30733/nacos/

默认用户名和密码,都是nacos

点击左侧的clusterManagement-->Cluster Node List,确保3个节点是UP状态

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐