开发者openshift4使用入门教程 - 15 - 部署Nacos

2022-04-21 14:19:19 浏览数 (1)

Quote

参考文章: Nacos官方文档: Kubernetes Nacos

本项目包含一个可构建的Nacos Docker Image,旨在利用StatefulSets在Kubernetes上部署Nacos

快速开始

  • Clone 项目
代码语言:javascript复制
git clone https://github.com/nacos-group/nacos-k8s.git
  • 简单例子

Danger

如果你使用简单方式快速启动,请注意这是 没有使用持久化卷 的,可能存在数据丢失风险.

建议仅在 POC 期间使用该方式!

代码语言:javascript复制
cd nacos-k8s
chmod  x quick-startup.sh
./quick-startup.sh
  • 测试
  • 服务注册
代码语言:javascript复制
curl -X PUT 'http://<cluster-ip>:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
  • 服务发现
代码语言:javascript复制
curl -X GET 'http://<cluster-ip>:8848/nacos/v1/ns/instances?serviceName=nacos.naming.serviceName'
  • 发布配置
代码语言:javascript复制
curl -X POST "http://<cluster-ip>:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=helloWorld"
  • 获取配置
代码语言:javascript复制
curl -X GET "http://<cluster-ip>:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"

高级使用

Note

在高级使用中,Nacos在K8S拥有 自动扩容缩容数据持久特性 ,请注意如果需要使用这部分功能请使用 PVC持久卷,Nacos的自动扩容缩容需要依赖持久卷,以及数据持久化也是一样,本例中使用的是 NFS 来使用PVC.

OpenShift 4 部署方式

数据库

根据官方的mysql-nfs.yaml进行调整后使用, 调整后如下:

mysql-nfs.yaml

代码语言:javascript复制
kind: DeploymentConfig
apiVersion: apps.openshift.io/v1
metadata:
name: nacos-mysql
labels:
    app: nacos-mysql
    app.kubernetes.io/component: database
    app.kubernetes.io/instance: nacos-mysql
    app.kubernetes.io/name: mariadb
    app.kubernetes.io/part-of: nacos
spec:
strategy:
    type: Recreate
    recreateParams:
    timeoutSeconds: 600
    resources: {}
    activeDeadlineSeconds: 21600
triggers:
    - type: ImageChange
    imageChangeParams:
        automatic: true
        containerNames:
        - nacos-mysql
        from:
        kind: ImageStreamTag
        name: 'nacos-mysql:5.7'
    - type: ConfigChange
replicas: 1
revisionHistoryLimit: 10
test: false
selector:
    app: nacos-mysql
    deploymentconfig: nacos-mysql
template:
    metadata:
    creationTimestamp: null
    labels:
        app: nacos-mysql
        deploymentconfig: nacos-mysql
    spec:
    restartPolicy: Always
    serviceAccountName: useroot
    terminationGracePeriodSeconds: 30
    securityContext: {}
    containers:
        - resources: {}
        terminationMessagePath: /dev/termination-log
        name: nacos-mysql
        env:
            - name: MYSQL_ROOT_PASSWORD
            value: root
            - name: MYSQL_DATABASE
            value: nacos_devtest
            - name: MYSQL_USER
            value: nacos
            - name: MYSQL_PASSWORD
            value: nacos
        ports:
            - containerPort: 3306
            protocol: TCP
            - containerPort: 33060
            protocol: TCP
        imagePullPolicy: IfNotPresent
        volumeMounts:
            - name: nacos-mysql-data
            mountPath: /var/lib/mysql
        terminationMessagePolicy: File
        image: >-
            nacos/nacos-mysql:5.7
    serviceAccount: useroot
    volumes:
        - name: nacos-mysql-data
        persistentVolumeClaim:
            claimName: nacos-mysql-data
    dnsPolicy: ClusterFirst
---
kind: ImageStream
apiVersion: image.openshift.io/v1
metadata:
name: nacos-mysql
labels:
    app: nacos-mysql
    app.kubernetes.io/component: nacos-mysql
    app.kubernetes.io/instance: nacos-mysql
    app.kubernetes.io/part-of: nacos
spec:
lookupPolicy:
    local: false
tags:
    - name: '5.7'
    annotations:
        openshift.io/imported-from: 'nacos/nacos-mysql:5.7'
    from:
        kind: DockerImage
        name: 'nacos/nacos-mysql:5.7'
    generation: 3
    importPolicy: {}
    referencePolicy:
        type: Source
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nacos-mysql-data
annotations:
    volume.beta.kubernetes.io/storage-provisioner: example.com/nfs
spec:
accessModes:
    - ReadWriteOnce
resources:
    requests:
    storage: 1Gi
storageClassName: nfs
volumeMode: Filesystem
---
kind: Service
apiVersion: v1
metadata:
name: nacos-mysql
labels:
    app: nacos-mysql
    app.kubernetes.io/component: nacos-mysql
    app.kubernetes.io/instance: nacos-mysql
    app.kubernetes.io/part-of: nacos
spec:
ports:
    - name: 3306-tcp
    protocol: TCP
    port: 3306
    targetPort: 3306
    - name: 33060-tcp
    protocol: TCP
    port: 33060
    targetPort: 33060
selector:
    app: nacos-mysql
    deploymentconfig: nacos-mysql
type: ClusterIP
sessionAffinity: None

创建nacos-mysql:

代码语言:javascript复制
oc apply -f mysql-nfs.yaml
nacos 集群

nacos-pvc-nfs.yaml

代码语言:javascript复制
---
apiVersion: v1
kind: Service
metadata:
name: nacos-headless
labels:
    app: nacos
annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
ports:
    - port: 8848
    name: server
    targetPort: 8848
clusterIP: None
selector:
    app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nacos-cm
data:
mysql.db.name: "nacos_devtest"
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:
    serviceAccountName: useroot
    initContainers:
        - name: peer-finder-plugin-install
        image: nacos/nacos-peer-finder-plugin:1.0
        imagePullPolicy: Always
        volumeMounts:
            - mountPath: "/home/nacos/plugins/peer-finder"
            name: plugindir
    containers:
        - name: nacos
        imagePullPolicy: Always
        image: nacos/nacos-server:latest
        resources:
            requests:
            memory: "2Gi"
            cpu: "500m"
            limits:
            memory: "3Gi"
            cpu: "1" 
        ports:
            - containerPort: 8848
            name: client-port
        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_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: PREFER_HOST_MODE
            value: "hostname"
            - name: MYSQL_SERVICE_HOST
            value: nacos-mysql              
        volumeMounts:
            - name: plugindir
            mountPath: /home/nacos/plugins/peer-finder
            - name: datadir
            mountPath: /home/nacos/data
            - name: logdir
            mountPath: /home/nacos/logs
volumeClaimTemplates:
    - metadata:
        name: plugindir
        annotations:
        volume.beta.kubernetes.io/storage-class: "nfs"
    spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
        requests:
            storage: 5Gi
    - metadata:
        name: datadir
        annotations:
        volume.beta.kubernetes.io/storage-class: "nfs"
    spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
        requests:
            storage: 5Gi
    - metadata:
        name: logdir
        annotations:
        volume.beta.kubernetes.io/storage-class: "nfs"
    spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
        requests:
            storage: 5Gi
selector:
    matchLabels:
    app: nacos

创建nacos集群:

代码语言:javascript复制
oc apply -f nacos-pvc-nfs.yaml

部署数据库

Danger

本章节内容仅供 POC 阶段使用, 将mysql数据库部署在nfs上性能存在较大瓶颈.

代码语言:javascript复制
cd nacos-k8s

kubectl create -f deploy/mysql/mysql-nfs.yaml
  • 验证数据库是否正常工作
代码语言:javascript复制
kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
mysql-gf2vd                        1/1     Running   0          111m

部署Nacos

  • 修改 depoly/nacos/nacos-pvc-nfs.yaml
代码语言:javascript复制
data:
  mysql.db.name: "数据库名称"
  mysql.port: "端口"
  mysql.user: "用户名"
  mysql.password: "密码"
  • 创建 Nacos
代码语言:javascript复制
kubectl create -f nacos-k8s/deploy/nacos/nacos-pvc-nfs.yaml
  • 验证Nacos节点启动成功
代码语言:javascript复制
kubectl get pod -l app=nacos


NAME      READY   STATUS    RESTARTS   AGE
nacos-0   1/1     Running   0          19h
nacos-1   1/1     Running   0          19h
nacos-2   1/1     Running   0          19h

扩容测试

  • 在扩容前,使用 kubectl exec获取在pod中的Nacos集群配置文件信息
代码语言:javascript复制
for i in 0 1; do echo nacos-$i; kubectl exec nacos-$i cat conf/cluster.conf; done

StatefulSet控制器根据其序数索引为每个Pod提供唯一的主机名。主机名采用 - 的形式。因为nacos StatefulSet的副本字段设置为2,所以当前集群文件中只有两个Nacos节点地址

  • 使用kubectl scale 对Nacos动态扩容
代码语言:javascript复制
kubectl scale sts nacos --replicas=3
  • 在扩容后,使用 kubectl exec获取在pod中的Nacos集群配置文件信息
代码语言:javascript复制
for i in 0 1 2; do echo nacos-$i; kubectl exec nacos-$i cat conf/cluster.conf; done
  • 使用 kubectl exec执行Nacos API 在每台节点上获取当前**Leader**是否一致
代码语言:javascript复制
for i in 0 1 2; do echo nacos-$i; kubectl exec nacos-$i curl GET "http://localhost:8848/nacos/v1/ns/raft/state"; done

到这里你可以发现新节点已经正常加入Nacos集群当中

例子部署环境

  • 机器配置

内网IP

主机名

配置

172.17.79.3

k8s-master

CentOS Linux release 7.4.1708 (Core) Single-core processor Mem 4G Cloud disk 40G

172.17.79.4

node01

CentOS Linux release 7.4.1708 (Core) Single-core processor Mem 4G Cloud disk 40G

172.17.79.5

node02

CentOS Linux release 7.4.1708 (Core) Single-core processor Mem 4G Cloud disk 40G

  • Kubernetes 版本:1.12.2 (如果你和我一样只使用了三台机器,那么记得开启master节点的部署功能)
  • NFS 版本:4.1 在k8s-master进行安装Server端,并且指定共享目录,本项目指定的**/data/nfs-share**
  • Git

限制

  • 必须要使用持久卷,否则会出现数据丢失的情况

项目目录

目录

描述

plugin

帮助Nacos集群进行动态扩容的插件Docker镜像源码

deploy

K8s 部署文件

配置属性

  • nacos-pvc-nfs.yaml or nacos-quick-start.yaml

名称

必要

描述

mysql.db.name

Y

数据库名称

mysql.port

N

端口

mysql.user

Y

用户名

mysql.password

Y

密码

NACOS_REPLICAS

N

确定执行Nacos启动节点数量,如果不适用动态扩容插件,就必须配置这个属性,否则使用扩容插件后不会生效

NACOS_SERVER_PORT

N

Nacos 端口

PREFER_HOST_MODE

Y

启动Nacos集群按域名解析

  • nfs deployment.yaml

名称

必要

描述

NFS_SERVER

Y

NFS 服务端地址

NFS_PATH

Y

NFS 共享目录

server

Y

NFS 服务端地址

path

Y

NFS 共享目录

  • mysql

名称

必要

描述

MYSQL_ROOT_PASSWORD

N

ROOT 密码

MYSQL_DATABASE

Y

数据库名称

MYSQL_USER

Y

数据库用户名

MYSQL_PASSWORD

Y

数据库密码

Nfs:server

N

NFS 服务端地址,如果使用本地部署不需要配置

Nfs:path

N

NFS 共享目录,如果使用本地部署不需要配置

调整日志级别

代码语言:javascript复制
# JVM properties方式调整
-Dcom.alibaba.nacos.naming.log.level=error
-Dcom.alibaba.nacos.config.log.level=error
-Dserver.tomcat.accesslog.enabled=false

# API方式调整
# 调整naming模块的naming-raft.log的级别为error:
curl -X PUT 'http://nacos-test.caas.example.com/nacos/v1/ns/operator/log?logName=naming-raft&logLevel=error'
# 调整config模块的config-dump.log的级别为warn:
curl -X PUT 'http://nacos-test.caas.example.com/nacos/v1/cs/ops/log?logName=config-dump&logLevel=warn'

0 人点赞