上一篇介绍了如何Rancher部署NFS持久化存储,这里继续介绍通过Rancher部署Nacos

Nacos

参考Nacos官网:https://nacos.io/en-us/
参考Nacos官网使用k8s管理nacos:https://nacos.io/zh-cn/docs/use-nacos-with-kubernetes.html/

部署环境

[root@uat-master ~]# kubectl get nodes -o wide
NAME         STATUS   ROLES                      AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                CONTAINER-RUNTIME
uat-master   Ready    controlplane,etcd,worker   41d   v1.17.4   172.18.30.215   <none>        CentOS Linux 7 (Core)   3.10.0-1062.18.1.el7.x86_64   docker://18.9.9
uat-w1       Ready    worker                     41d   v1.17.4   172.18.30.216   <none>        CentOS Linux 7 (Core)   3.10.0-1062.18.1.el7.x86_64   docker://18.9.9
uat-w2       Ready    worker                     40d   v1.17.4   172.18.30.214   <none>        CentOS Linux 7 (Core)   3.10.0-1062.18.1.el7.x86_64   docker://18.9.9
[root@uat-master ~]# 

1 前期准备

部署版本:当前最新1.4.1release版本
参考官网下载相关yaml部署文件:

git clone https://github.com/nacos-group/nacos-k8s.git

在这里插入图片描述
clone后打开目录
在这里插入图片描述
参考如上配置自定义配置文件,namespace调整为 middleware

1.1 准备Nacos数据库配置参数ConfigMap及Secret

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: middleware
data:
  mysql.db.name: "uat_nacos_config"
  mysql.port: "3306"
---
apiVersion: v1
kind: Secret
data:
  mysql.db.host: "数据库主机HOST"
  mysql.user: "用户名"
  mysql.password: "密码"
metadata:
  name: nacos-secret
  namespace: middleware
type: Opaque

在这里插入图片描述

在这里插入图片描述

1.2 准备数据库

Mysql数据库建表脚本

https://github.com/alibaba/nacos/blob/master/distribution/conf/nacos-mysql.sql
准备数据库:
mysql.db.name: “uat_nacos_config”
mysql.port: “3306”

1.3 准备NFS

参考Rancher部署NFS持久化存储:https://blog.csdn.net/iceliooo/article/details/111461651

2 部署nacos

2.1 以StatefulSet部署nacos

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: middleware
spec:
  serviceName: nacos-headless
  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"
      serviceAccountName: nfs-client-provisioner
      initContainers:
        - name: peer-finder-plugin-install
          image: nacos/nacos-peer-finder-plugin:1.0
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - mountPath: "/home/nacos/plugins/peer-finder"
              name: plugindir
      containers:
        - name: nacos
          imagePullPolicy: IfNotPresent
          image: nacos/nacos-server:1.4.1
          resources:
            requests:
              memory: "2Gi"
              cpu: "500m"
          ports:
            - containerPort: 8848
              name: client-port
            - containerPort: 7848
              name: 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:
                secretKeyRef:
                  name: nacos-secret
                  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:
                secretKeyRef:
                  name: nacos-secret
                  key: mysql.user
            - name: MYSQL_SERVICE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: nacos-secret
                  key: mysql.password
            - name: NACOS_SERVER_PORT
              value: "8848"
            - name: NACOS_APPLICATION_PORT
              value: "8848"
            - name: PREFER_HOST_MODE
              value: "hostname"
          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
        namespace: middleware
        annotations:
          volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
      spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
          requests:
            storage: 1Gi
    - metadata:
        name: datadir
        namespace: middleware
        annotations:
          volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
      spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
          requests:
            storage: 1Gi
    - metadata:
        name: logdir
        namespace: middleware
        annotations:
          volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
      spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
          requests:
            storage: 1Gi
  selector:
    matchLabels:
      app: nacos

在这里插入图片描述

2.2 部署Service

apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: middleware
  labels:
    app: nacos
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
    - port: 7848
      name: rpc
      targetPort: 7848
  clusterIP: None
  selector:
    app: nacos
---
## 开放集权外访问端口,便于本地应用访问外网nacos服务
apiVersion: v1
kind: Service
metadata:
  name: nacos-client
  namespace: middleware
  labels:
    app: nacos
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
    - port: 7848
      name: rpc
      targetPort: 7848
  selector:
    app: nacos
  type: NodePort

在这里插入图片描述

这里已经可以通过IP加端口方式访问nacos了(默认用户名:nacos,密码:nacos)
在这里插入图片描述
在这里插入图片描述

2.3 部署Ingress

前置条件:
1、准备好Ingress Controller这里以Nginx Ingress Controller 为例,
参考通过Rancher搭建k8s集群中关于Nginx Ingress Controller部分:https://blog.csdn.net/iceliooo/article/details/111402309
2、域名服务商处配置好域名解析

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nacos-headless
  namespace: middleware
spec:
  rules:
  - host: uat-nacos.一级域名
    http:
      paths:
      - path: /nacos
        backend:
          serviceName: nacos-headless
          servicePort: server

在这里插入图片描述

以域名方式访问nacos控制台
在这里插入图片描述

2.4 测试配置中心功能

下载官方examples工程 :https://github.com/nacos-group/nacos-examples.git
使用:nacos-spring-cloud-config-example工程
修改配置文件中nacos配置中心地址为远超服务器外网IP,端口为nacos-client Service以NodePod方式暴露的服务器端口,这里是32728

在这里插入图片描述
在这里插入图片描述

在Nacos控制台中添加配置
这里需要注意1.4.1版本里面DataID必须加上后缀名,否则客户端无法获取到配置内容,1.3.2版本里面是不需要配置后缀名的,后面继续研究这个是否是1.4.1版本的BUG

在这里插入图片描述

访问接口:http://localhost:8080/config/getName
name默认值为bingo,接口返回值为iceliooo1。成功获取获取nacos中的远超配置。
在这里插入图片描述

项目启动情况下修改Nacos中配置
在这里插入图片描述

再次访问接口:http://localhost:8080/config/getName返回值为新修改的iceliooo2,成功实现动态获取远程配置信息
在这里插入图片描述

Logo

开源、云原生的融合云平台

更多推荐