Bootstrap

Kubernetes(k8s)上搭建nacos集群

环境准备

Kubernetes准备

你需要准备一个Kubernetes集群,如图我的集群已经准备完毕:
在这里插入图片描述

数据库准备

nacos可以将配置文件存储到数据库当中,所以我们要先准备一个拥有nacos数据表结构的数据库,这样的数据库镜像我已经准备好了,当然你也可以自己制作这个镜像:

registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/nacos-mysql:2.0.4

我之前写过一篇Kubernetes上安装数据库的文章:https://blog.csdn.net/m0_51510236/article/details/132482351,这里我就直接上yaml文件了,注意镜像使用的是上面提到的带有nacos配置表结构的镜像:

apiVersion: v1
kind: Namespace
metadata:
  name: deploy-test
spec: {}
status: {}

---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: deploy-mysql-nfs-pv
  namespace: deploy-test
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  nfs:
  	# 注意修改你的nfs服务器地址和目录
    server: 192.168.1.160
    path: /data/nfs/nacos-mysql
  storageClassName: "nfs"

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: deploy-mysql-nfs-pvc
  namespace: deploy-test
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: "nfs"
  resources:
    requests:
      storage: 1Gi
  volumeName: deploy-mysql-nfs-pv

---

apiVersion: v1
data:
  # 密码为root,具体密码设置可参考我上篇文章
  mysql_root_password: cm9vdA==
kind: Secret
metadata:
  name: mysql-password
  namespace: deploy-test

---

apiVersion: v1
kind: Service
metadata:
  name: deploy-mysql-svc
  namespace: deploy-test
  labels:
    app: mysql
spec:
  ports:
  - port: 3306
    name: mysql
    targetPort: 3306
    nodePort: 30306
  selector:
    app: mysql
  type: NodePort
  sessionAffinity: ClientIP

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: deploy-mysql
  namespace: deploy-test
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: "deploy-mysql-svc"
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - args:
        - --character-set-server=utf8mb4
        - --collation-server=utf8mb4_unicode_ci
        - --lower_case_table_names=1
        - --default-time_zone=+8:00
        name: mysql
        image: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/nacos-mysql:2.0.4
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              key: mysql_root_password
              name: mysql-password
      volumes:
      - name: mysql-data
        persistentVolumeClaim:
          claimName: deploy-mysql-nfs-pvc

查看创建结果:

kubectl get all -o wide -n deploy-test

发现容器以及运行:
在这里插入图片描述

进入容器当中查看表结构:

kubectl exec -itn deploy-test pod/deploy-mysql-0 -- mysql -uroot -p

查看结果,目前已经拥有这样的表结构:
在这里插入图片描述

部署nacos集群

我们部署mysql的时候创建了一个 deploy-test 命名空间,部署nacos的时候将nacos部署到这个命名空间当中。

官方镜像地址

nacos官方提供的镜像地址:https://hub.docker.com/r/nacos/nacos-server,在当中我们可以看到有那么多环境变量配置:
在这里插入图片描述

创建configmap

上面那张图片中我们要用到这么几个参数:

参数名描述
MODEnacos启动模式,cluster为集群模式,standalone为单节点模式
NACOS_SERVERSnacos集群的服务器地址,格式为 ip1:port ip2:port 多个用空格隔开
SPRING_DATASOURCE_PLATFORMnacos使用的数据库,只支持mysql
MYSQL_SERVICE_HOSTmysql服务器地址
MYSQL_SERVICE_PORTmysql的端口,默认为3306
MYSQL_SERVICE_DB_NAMEnacos配置的数据库名,我们创建mysql时配置的数据库名为nacos_config
MYSQL_SERVICE_USERmysql的用户名
MYSQL_SERVICE_PASSWORDmysql的密码
MYSQL_DATABASE_NUMmysql数据库的数量,默认为1
MYSQL_SERVICE_DB_PARAMmysql连接字符串后面的参数,默认:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
JVM_XMSjvm永久区最小大小,默认:1g
JVM_XMXjvm永久区最大大小,默认:1g
JVM_XMNjvm新生区大小,默认:512m

我们根据上面参数创建一个configmap,执行下面代码获取创建configmap的k8s资源清单文件yaml:

kubectl create configmap nacos-deploy-config -n deploy-test \
--from-literal=mode=cluster \
--from-literal=nacos-servers='deploy-nacos-0.deploy-nacos-svc.deploy-test.svc.cluster.local:8848 deploy-nacos-1.deploy-nacos-svc.deploy-test.svc.cluster.local:8848' \
--from-literal=spring-datasource-platform=mysql \
--from-literal=mysql-service-host='deploy-mysql-0.deploy-mysql-svc.deploy-test.svc.cluster.local' \
--from-literal=mysql-service-port=3306 \
--from-literal=mysql-service-db-name=nacos_config \
--from-literal=mysql-service-user=root \
--from-literal=mysql-database-num=1 \
--from-literal=mysql-service-db-param='characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false' \
--from-literal=jvm-xms=256m \
--from-literal=jvm-xmx=256m \
--from-literal=jvm-xmn=128m \
--dry-run=client -o yaml

注意下面这一行:

--from-literal=nacos-servers='deploy-nacos-0.deploy-nacos-svc.deploy-test.svc.cluster.local:8848 deploy-nacos-1.deploy-nacos-svc.deploy-test.svc.cluster.local:8848'

因为我们这次只部署两个节点的集群,如果你打算部署多个节点,那么你往后面加即可。同时这行命令我们也没设置数据库的密码,这是因为我们创建mysql的时候就创建了一个密码的secret,待会直接使用即可。我们看这行命令的执行结果:
在这里插入图片描述

发现成功创建了一个k8s的configmap资源清单文件,文件内容:

apiVersion: v1
data:
  jvm-xmn: 128m
  jvm-xms: 256m
  jvm-xmx: 256m
  mode: cluster
  mysql-database-num: "1"
  mysql-service-db-name: nacos_config
  mysql-service-db-param: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
  mysql-service-host: deploy-mysql-0.deploy-mysql-svc.deploy-test.svc.cluster.local
  mysql-service-port: "3306"
  mysql-service-user: root
  nacos-servers: deploy-nacos-0.deploy-nacos-svc.deploy-test.svc.cluster.local:8848
    deploy-nacos-1.deploy-nacos-svc.deploy-test.svc.cluster.local:8848
  spring-datasource-platform: mysql
kind: ConfigMap
metadata:
  name: nacos-deploy-config
  namespace: deploy-test

先将这个yaml保存下来以供接下来使用:
在这里插入图片描述

创建部署文件

部署文件内容:

apiVersion: v1
kind: Service
metadata:
  name: deploy-nacos-svc
  namespace: deploy-test
  labels:
    app: nacos
spec:
  ports:
  - port: 8848
    name: nacos
    targetPort: 8848
    nodePort: 30848
  selector:
    app: nacos
  type: NodePort
  sessionAffinity: ClientIP

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: deploy-nacos
  namespace: deploy-test
spec:
  selector:
    matchLabels:
      app: nacos
  serviceName: "deploy-nacos-svc"
  replicas: 2
  template:
    metadata:
      labels:
        app: nacos
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nacos
        # 如果能拉取到dockerhub上的镜像就用这个
        # image: docker.io/nacos/nacos-server:v2.0.4
        # 拉取不到dockerhub上的镜像就用我推送到阿里云上的nacos官方镜像
        image: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/nacos-server:v2.0.4
        ports:
        - containerPort: 8848
          name: nacos
        env: # 引用各类环境变量为nacos做配置,注意大部分引用configmap,只有数据库密码引用的创建mysql时候的secret
        - name: JVM_XMN
          valueFrom:
            configMapKeyRef:
              key: jvm-xmn
              name: nacos-deploy-config
        - name: JVM_XMS
          valueFrom:
            configMapKeyRef:
              key: jvm-xms
              name: nacos-deploy-config
        - name: JVM_XMX
          valueFrom:
            configMapKeyRef:
              key: jvm-xmx
              name: nacos-deploy-config
        - name: MODE
          valueFrom:
            configMapKeyRef:
              key: mode
              name: nacos-deploy-config
        - name: MYSQL_DATABASE_NUM
          valueFrom:
            configMapKeyRef:
              key: mysql-database-num
              name: nacos-deploy-config
        - name: MYSQL_SERVICE_DB_NAME
          valueFrom:
            configMapKeyRef:
              key: mysql-service-db-name
              name: nacos-deploy-config
        - name: MYSQL_SERVICE_DB_PARAM
          valueFrom:
            configMapKeyRef:
              key: mysql-service-db-param
              name: nacos-deploy-config
        - name: MYSQL_SERVICE_HOST
          valueFrom:
            configMapKeyRef:
              key: mysql-service-host
              name: nacos-deploy-config
        - name: MYSQL_SERVICE_PASSWORD
          valueFrom:
            secretKeyRef:
              key: mysql_root_password
              name: mysql-password
        - name: MYSQL_SERVICE_PORT
          valueFrom:
            configMapKeyRef:
              key: mysql-service-port
              name: nacos-deploy-config
        - name: MYSQL_SERVICE_USER
          valueFrom:
            configMapKeyRef:
              key: mysql-service-user
              name: nacos-deploy-config
        - name: NACOS_SERVERS
          valueFrom:
            configMapKeyRef:
              key: nacos-servers
              name: nacos-deploy-config
        - name: SPRING_DATASOURCE_PLATFORM
          valueFrom:
            configMapKeyRef:
              key: spring-datasource-platform
              name: nacos-deploy-config

根据yaml资源清单文件部署nacos

根据上面步骤我们得到了完整的 deploy-nacos.yaml 文件内容:

apiVersion: v1
data:
  jvm-xmn: 128m
  jvm-xms: 256m
  jvm-xmx: 256m
  mode: cluster
  mysql-database-num: "1"
  mysql-service-db-name: nacos_config
  mysql-service-db-param: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
  mysql-service-host: deploy-mysql-0.deploy-mysql-svc.deploy-test.svc.cluster.local
  mysql-service-port: "3306"
  mysql-service-user: root
  nacos-servers: deploy-nacos-0.deploy-nacos-svc.deploy-test.svc.cluster.local:8848
    deploy-nacos-1.deploy-nacos-svc.deploy-test.svc.cluster.local:8848
  spring-datasource-platform: mysql
kind: ConfigMap
metadata:
  name: nacos-deploy-config
  namespace: deploy-test

---

apiVersion: v1
kind: Service
metadata:
  name: deploy-nacos-svc
  namespace: deploy-test
  labels:
    app: nacos
spec:
  ports:
  - port: 8848
    name: nacos
    targetPort: 8848
    nodePort: 30848
  selector:
    app: nacos
  type: NodePort
  sessionAffinity: ClientIP

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: deploy-nacos
  namespace: deploy-test
spec:
  selector:
    matchLabels:
      app: nacos
  serviceName: "deploy-nacos-svc"
  replicas: 2
  template:
    metadata:
      labels:
        app: nacos
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nacos
        image: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/nacos-server:v2.0.4
        ports:
        - containerPort: 8848
          name: nacos
        env:
        - name: JVM_XMN
          valueFrom:
            configMapKeyRef:
              key: jvm-xmn
              name: nacos-deploy-config
        - name: JVM_XMS
          valueFrom:
            configMapKeyRef:
              key: jvm-xms
              name: nacos-deploy-config
        - name: JVM_XMX
          valueFrom:
            configMapKeyRef:
              key: jvm-xmx
              name: nacos-deploy-config
        - name: MODE
          valueFrom:
            configMapKeyRef:
              key: mode
              name: nacos-deploy-config
        - name: MYSQL_DATABASE_NUM
          valueFrom:
            configMapKeyRef:
              key: mysql-database-num
              name: nacos-deploy-config
        - name: MYSQL_SERVICE_DB_NAME
          valueFrom:
            configMapKeyRef:
              key: mysql-service-db-name
              name: nacos-deploy-config
        - name: MYSQL_SERVICE_DB_PARAM
          valueFrom:
            configMapKeyRef:
              key: mysql-service-db-param
              name: nacos-deploy-config
        - name: MYSQL_SERVICE_HOST
          valueFrom:
            configMapKeyRef:
              key: mysql-service-host
              name: nacos-deploy-config
        - name: MYSQL_SERVICE_PASSWORD
          valueFrom:
            secretKeyRef:
              key: mysql_root_password
              name: mysql-password
        - name: MYSQL_SERVICE_PORT
          valueFrom:
            configMapKeyRef:
              key: mysql-service-port
              name: nacos-deploy-config
        - name: MYSQL_SERVICE_USER
          valueFrom:
            configMapKeyRef:
              key: mysql-service-user
              name: nacos-deploy-config
        - name: NACOS_SERVERS
          valueFrom:
            configMapKeyRef:
              key: nacos-servers
              name: nacos-deploy-config
        - name: SPRING_DATASOURCE_PLATFORM
          valueFrom:
            configMapKeyRef:
              key: spring-datasource-platform
              name: nacos-deploy-config

接下来执行这行命令开始部署:

kubectl apply -f deploy-nacos.yaml

查看执行结果:
在这里插入图片描述

执行这行命令查看部署结果:

kubectl get pod -o wide -n deploy-test

执行后的结果可以看到两个nacos节点部署成功:
在这里插入图片描述

执行这行命令查看nacos暴露的nodeport端口:

kubectl get svc -o wide -n deploy-test

可以看到端口为 30848
在这里插入图片描述

我们可以通过任何一个Kubernetes节点的IP地址加上这个端口访问nacos:
在这里插入图片描述

输入默认的用户名和密码 nacos/nacos 即可登陆到首页:
在这里插入图片描述

我们来到 集群管理->节点管理 可以看到两个节点都为up状态:
在这里插入图片描述

接下来我们来到 配置管理->配置列表 点击 + 新增一个配置:
在这里插入图片描述

我们先随便新增一个yaml配置文件:
在这里插入图片描述

点击下面的发布之后查看数据库:
在这里插入图片描述

数据库里面已经新增了这个配置的数据。Kubernetes集群里面安装nacos集群就已经安装好了。

;