Nacos在2019年10月24日更新了,支持了istioMCP Server,但本次部署仅部署Nacos集群。


前置条件 MySQL

Nacos采用MySQL作为持久化数据存储,官方推荐脚本中,需要在Kubernetes中部署MySQL,所以需要集群有Storageclass,这里我们直接使用MySQL服务,所以只需要部署以下Kind:

  • service
  • configmap
  • statefulset

部署Nacos集群

Nacos集群模式需要指定各个Nacos的地址,我们采用Statefuleset方式部署,用完整的FQDN来表示,经过测试Nacos仅支持完整的FQDN地址,所以针对namespace的不同,Nacos的Statefulset的部署文件也不同。

这里默认几个信息:

  • MySQL地址:mysql.mysql.rds.aliyuncs.com
  • MySQL账号密码: root/root
  • Nacos库: nacos
  • Namespace: sample-namespace

以下是完整部署文件:

---
apiVersion: v1
kind: Service
metadata:
  name: nacos
  labels:
    app: nacos
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
  selector:
    app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
data:
  mysql.master.service.host: "mysql.mysql.rds.aliyuncs.com"
  mysql.slave.service.host: "mysql.mysql.rds.aliyuncs.com"
  mysql.master.db.name: "nacos"
  mysql.master.port: "3306"
  mysql.slave.port: "3306"
  mysql.master.user: "root"
  mysql.master.password: "root"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
spec:
  serviceName: nacos
  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: k8snacos
          imagePullPolicy: Always
          image: nacos/nacos-server:latest
          resources:
            requests:
              memory: "2Gi"
              cpu: "500m"
          ports:
            - containerPort: 8848
              name: client
          env:
            - name: NACOS_REPLICAS
              value: "3"
            - name: MYSQL_MASTER_SERVICE_HOST
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.master.service.host
            - name: MYSQL_SLAVE_SERVICE_HOST
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.slave.service.host
            - name: MYSQL_MASTER_SERVICE_DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.master.db.name
            - name: MYSQL_MASTER_SERVICE_PORT
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.master.port
            - name: MYSQL_SLAVE_SERVICE_PORT
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.slave.port
            - name: MYSQL_MASTER_SERVICE_USER
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.master.user
            - name: MYSQL_MASTER_SERVICE_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.master.password
            - name: NACOS_SERVER_PORT
              value: "8848"
            - name: PREFER_HOST_MODE
              value: "hostname"
            - name: NACOS_SERVERS
              value: "nacos-0.nacos.sample-namespace.svc.cluster.local:8848 nacos-1.nacos.sample-namespace.svc.cluster.local:8848 nacos-2.nacos.sample-namespace.svc.cluster.local:8848"
  selector:
    matchLabels:
      app: nacos

FAQ

  • 如果其中一个Pod状态异常,集群会受到影响吗?

Statefulset重新启动一个名字一样的Pod,并结束掉状态异常的Pod

  • Nacos版本更新或重新部署,对集群会有影响吗?

会先停止nacos-02,然后启动新的nacos-2,接着是nacos-1,最后是nacos-0,在每一步中,nacos都会保持最少2节点,会自动选举主从,服务获取到的是负载均衡的service地址,数据保存在MySQL中,不影响nacos的使用。


参考

Github-Nacos-K8s

Last modification:December 13th, 2019 at 02:39 am