评论

收藏

[Linux] 用K8S部署高可用NACOS集群

服务系统 服务系统 发布于:2021-06-30 17:34 | 阅读数:402 | 评论:0

  我们先来单点部署NACOS服务:
apiVersion: v1
kind: Service
metadata:
  name: nacos
  namespace: {namespace}
  labels:
  app: nacos
spec:
  ports:
  - protocol: TCP
  port: 8848
  targetPort: 8848
  name: nacos-http
  selector:
  app: nacos
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: {namespace}
spec:
  serviceName: nacos
  selector:
  matchLabels:
    app: nacos
  replicas: 1
  template:
  metadata:
    labels:
    app: nacos
  spec:
    containers:
    - name: nacos
    image: nacos/nacos-server:2.0.1
    resources:
      requests:
      memory: "1024Mi"
      cpu: "256m"
      limits:
      memory: "2048Mi"
      cpu: "512m"
    ports:
    - containerPort: 8848
      name: nacos-http
    env:
      - name: MODE
      value: "standalone"
  我们用集群的方式部署:
  1.我们要准备好NFS服务器,建好PV和PVC
apiVersion: v1
kind: Namespace
metadata:
  name: {namespace}
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv-{namespace}
  labels:
  pv: nfs-pv-{namespace}
  annotations:
  volume.beta.kubernetes.io/mount-options: "noatime,nodiratime,noresvport,nolock,proto=udp,rsize=1048576,wsize=1048576,hard"
spec:
  capacity:
  storage: 100Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
  path: /nfs/{namespace}
  server: 192.168.0.2
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-pvc
  namespace: {namespace}
spec:
  accessModes:
  - ReadWriteMany
  resources:
  requests:
    storage: 100Gi
  selector:
  matchLabels:
    pv: nfs-pv-{namespace}
  2.准备好数据库,我们集群里面是有数据库,这里我就不做配置直接使用,只是导入数据库而已
  数据库的地址: mysql
  数据库名称: nacos
  数据库的账号: root
  数据库的密码: 123456
  然后导入https://github.com/alibaba/nacos/blob/develop/distribution/conf/nacos-mysql.sql 
  3.创建配置文件
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: {namespace}
data:
  mysql.db.name: "nacos"
  mysql.db.host: "mysql"
  mysql.port: "3306"
  mysql.user: "root"
  mysql.password: "123456"
  4.创建部署文件
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: {namespace}
spec:
  serviceName: nacos-headless
  selector:
  matchLabels:
    app: nacos
  replicas: 3
  template:
  metadata:
    labels:
    app: nacos
  spec:
    initContainers:
    - name: peer-finder-plugin-install
      image: nacos/nacos-peer-finder-plugin:1.1
      imagePullPolicy: Always
      volumeMounts:
      - mountPath: /home/nacos/plugins/peer-finder
        name: nacos-data
        subPath: peer-finder
    containers:
    - name: nacos
    image: nacos/nacos-server:2.0.1
    resources:
      requests:
      memory: "1024Mi"
      cpu: "256m"
      limits:
      memory: "2048Mi"
      cpu: "512m"
    ports:
    - containerPort: 8848
      name: nacos-http
    - containerPort: 9848
      name: nacos-rpc
    - containerPort: 9849
      name: raft-rpc
    - containerPort: 7848
      name: old-raft-rpc
    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.db.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.{namespace}.svc.cluster.local:8848 nacos-1.nacos-headless.{namespace}.svc.cluster.local:8848 nacos-2.nacos-headless.{namespace}.svc.cluster.local:8848"
    volumeMounts:
      - name: nacos-data
      mountPath: /home/nacos/plugins/peer-finder
      subPath: peer-finder
      - name: nacos-data
      mountPath: /home/nacos/data
      subPath: data
      - name: nacos-data
      mountPath: /home/nacos/logs
      subPath: logs
    volumes:
    - name: nacos-data
    persistentVolumeClaim:
      claimName: nfs-pvc
  5.创建服务
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: {namespace}
  labels:
  app: nacos
  annotations:
  service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
  - protocol: TCP
  port: 8848
  targetPort: 8848
  name: nacos-http
  - protocol: TCP
  port: 9848
  targetPort: 9848
  name: nacos-rpc
  - protocol: TCP
  port: 9849
  targetPort: 9849
  name: raft-rpc
  - protocol: TCP
  port: 7848
  targetPort: 7848
  name: old-raft-rpc
  clusterIP: None
  selector:
  app: nacos
---
apiVersion: v1
kind: Service
metadata:
  name: nacos
  namespace: {namespace}
  labels:
  app: nacos
  annotations:
  service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
  - protocol: TCP
  port: 8848
  targetPort: 8848
  name: nacos-http
  - protocol: TCP
  port: 9848
  targetPort: 9848
  name: nacos-rpc
  - protocol: TCP
  port: 9849
  targetPort: 9849
  name: raft-rpc
  - protocol: TCP
  port: 7848
  targetPort: 7848
  name: old-raft-rpc
  selector:
  app: nacos
  以上文章参考https://github.com/nacos-group/nacos-k8s
  只是nacos-k8s这个里面有个坑,就是缺了
  - name: MYSQL_SERVICE_HOST
            valueFrom:
              configMapKeyRef:
                name: nacos-cm
                key: mysql.db.host
  同时为方便集群访问,我这里建立一个nacos服务直接访问集群
  以上需要将{namespace}修改为自己的命名空间

  
关注下面的标签,发现更多相似文章