Kubernetes上搭建nacos集群
- 环境准备
- Kubernetes准备
- 数据库准备
- 部署nacos集群
- 官方镜像地址
- 创建configmap
- 创建部署文件
- 根据yaml资源清单文件部署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-pvnamespace: deploy-test
spec:capacity:storage: 1GiaccessModes:- ReadWriteManynfs:# 注意修改你的nfs服务器地址和目录server: 192.168.1.160path: /data/nfs/nacos-mysqlstorageClassName: "nfs"---apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: deploy-mysql-nfs-pvcnamespace: deploy-test
spec:accessModes:- ReadWriteManystorageClassName: "nfs"resources:requests:storage: 1GivolumeName: deploy-mysql-nfs-pv---apiVersion: v1
data:# 密码为root,具体密码设置可参考我上篇文章mysql_root_password: cm9vdA==
kind: Secret
metadata:name: mysql-passwordnamespace: deploy-test---apiVersion: v1
kind: Service
metadata:name: deploy-mysql-svcnamespace: deploy-testlabels:app: mysql
spec:ports:- port: 3306name: mysqltargetPort: 3306nodePort: 30306selector:app: mysqltype: NodePortsessionAffinity: ClientIP---apiVersion: apps/v1
kind: StatefulSet
metadata:name: deploy-mysqlnamespace: deploy-test
spec:selector:matchLabels:app: mysqlserviceName: "deploy-mysql-svc"replicas: 1template:metadata:labels:app: mysqlspec:terminationGracePeriodSeconds: 10containers:- args:- --character-set-server=utf8mb4- --collation-server=utf8mb4_unicode_ci- --lower_case_table_names=1- --default-time_zone=+8:00name: mysqlimage: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/nacos-mysql:2.0.4ports:- containerPort: 3306name: mysqlvolumeMounts:- name: mysql-datamountPath: /var/lib/mysqlenv:- name: MYSQL_ROOT_PASSWORDvalueFrom:secretKeyRef:key: mysql_root_passwordname: mysql-passwordvolumes:- name: mysql-datapersistentVolumeClaim: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
上面那张图片中我们要用到这么几个参数:
参数名 | 描述 |
---|---|
MODE | nacos启动模式,cluster为集群模式,standalone为单节点模式 |
NACOS_SERVERS | nacos集群的服务器地址,格式为 ip1:port ip2:port 多个用空格隔开 |
SPRING_DATASOURCE_PLATFORM | nacos使用的数据库,只支持mysql |
MYSQL_SERVICE_HOST | mysql服务器地址 |
MYSQL_SERVICE_PORT | mysql的端口,默认为3306 |
MYSQL_SERVICE_DB_NAME | nacos配置的数据库名,我们创建mysql时配置的数据库名为nacos_config |
MYSQL_SERVICE_USER | mysql的用户名 |
MYSQL_SERVICE_PASSWORD | mysql的密码 |
MYSQL_DATABASE_NUM | mysql数据库的数量,默认为1 |
MYSQL_SERVICE_DB_PARAM | mysql连接字符串后面的参数,默认:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false |
JVM_XMS | jvm永久区最小大小,默认:1g |
JVM_XMX | jvm永久区最大大小,默认:1g |
JVM_XMN | jvm新生区大小,默认: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: 128mjvm-xms: 256mjvm-xmx: 256mmode: clustermysql-database-num: "1"mysql-service-db-name: nacos_configmysql-service-db-param: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=falsemysql-service-host: deploy-mysql-0.deploy-mysql-svc.deploy-test.svc.cluster.localmysql-service-port: "3306"mysql-service-user: rootnacos-servers: deploy-nacos-0.deploy-nacos-svc.deploy-test.svc.cluster.local:8848deploy-nacos-1.deploy-nacos-svc.deploy-test.svc.cluster.local:8848spring-datasource-platform: mysql
kind: ConfigMap
metadata:name: nacos-deploy-confignamespace: deploy-test
先将这个yaml保存下来以供接下来使用:
创建部署文件
部署文件内容:
apiVersion: v1
kind: Service
metadata:name: deploy-nacos-svcnamespace: deploy-testlabels:app: nacos
spec:ports:- port: 8848name: nacostargetPort: 8848nodePort: 30848selector:app: nacostype: NodePortsessionAffinity: ClientIP---apiVersion: apps/v1
kind: StatefulSet
metadata:name: deploy-nacosnamespace: deploy-test
spec:selector:matchLabels:app: nacosserviceName: "deploy-nacos-svc"replicas: 2template:metadata:labels:app: nacosspec:terminationGracePeriodSeconds: 10containers:- 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.4ports:- containerPort: 8848name: nacosenv: # 引用各类环境变量为nacos做配置,注意大部分引用configmap,只有数据库密码引用的创建mysql时候的secret- name: JVM_XMNvalueFrom:configMapKeyRef:key: jvm-xmnname: nacos-deploy-config- name: JVM_XMSvalueFrom:configMapKeyRef:key: jvm-xmsname: nacos-deploy-config- name: JVM_XMXvalueFrom:configMapKeyRef:key: jvm-xmxname: nacos-deploy-config- name: MODEvalueFrom:configMapKeyRef:key: modename: nacos-deploy-config- name: MYSQL_DATABASE_NUMvalueFrom:configMapKeyRef:key: mysql-database-numname: nacos-deploy-config- name: MYSQL_SERVICE_DB_NAMEvalueFrom:configMapKeyRef:key: mysql-service-db-namename: nacos-deploy-config- name: MYSQL_SERVICE_DB_PARAMvalueFrom:configMapKeyRef:key: mysql-service-db-paramname: nacos-deploy-config- name: MYSQL_SERVICE_HOSTvalueFrom:configMapKeyRef:key: mysql-service-hostname: nacos-deploy-config- name: MYSQL_SERVICE_PASSWORDvalueFrom:secretKeyRef:key: mysql_root_passwordname: mysql-password- name: MYSQL_SERVICE_PORTvalueFrom:configMapKeyRef:key: mysql-service-portname: nacos-deploy-config- name: MYSQL_SERVICE_USERvalueFrom:configMapKeyRef:key: mysql-service-username: nacos-deploy-config- name: NACOS_SERVERSvalueFrom:configMapKeyRef:key: nacos-serversname: nacos-deploy-config- name: SPRING_DATASOURCE_PLATFORMvalueFrom:configMapKeyRef:key: spring-datasource-platformname: nacos-deploy-config
根据yaml资源清单文件部署nacos
根据上面步骤我们得到了完整的 deploy-nacos.yaml
文件内容:
apiVersion: v1
data:jvm-xmn: 128mjvm-xms: 256mjvm-xmx: 256mmode: clustermysql-database-num: "1"mysql-service-db-name: nacos_configmysql-service-db-param: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=falsemysql-service-host: deploy-mysql-0.deploy-mysql-svc.deploy-test.svc.cluster.localmysql-service-port: "3306"mysql-service-user: rootnacos-servers: deploy-nacos-0.deploy-nacos-svc.deploy-test.svc.cluster.local:8848deploy-nacos-1.deploy-nacos-svc.deploy-test.svc.cluster.local:8848spring-datasource-platform: mysql
kind: ConfigMap
metadata:name: nacos-deploy-confignamespace: deploy-test---apiVersion: v1
kind: Service
metadata:name: deploy-nacos-svcnamespace: deploy-testlabels:app: nacos
spec:ports:- port: 8848name: nacostargetPort: 8848nodePort: 30848selector:app: nacostype: NodePortsessionAffinity: ClientIP---apiVersion: apps/v1
kind: StatefulSet
metadata:name: deploy-nacosnamespace: deploy-test
spec:selector:matchLabels:app: nacosserviceName: "deploy-nacos-svc"replicas: 2template:metadata:labels:app: nacosspec:terminationGracePeriodSeconds: 10containers:- name: nacosimage: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/nacos-server:v2.0.4ports:- containerPort: 8848name: nacosenv:- name: JVM_XMNvalueFrom:configMapKeyRef:key: jvm-xmnname: nacos-deploy-config- name: JVM_XMSvalueFrom:configMapKeyRef:key: jvm-xmsname: nacos-deploy-config- name: JVM_XMXvalueFrom:configMapKeyRef:key: jvm-xmxname: nacos-deploy-config- name: MODEvalueFrom:configMapKeyRef:key: modename: nacos-deploy-config- name: MYSQL_DATABASE_NUMvalueFrom:configMapKeyRef:key: mysql-database-numname: nacos-deploy-config- name: MYSQL_SERVICE_DB_NAMEvalueFrom:configMapKeyRef:key: mysql-service-db-namename: nacos-deploy-config- name: MYSQL_SERVICE_DB_PARAMvalueFrom:configMapKeyRef:key: mysql-service-db-paramname: nacos-deploy-config- name: MYSQL_SERVICE_HOSTvalueFrom:configMapKeyRef:key: mysql-service-hostname: nacos-deploy-config- name: MYSQL_SERVICE_PASSWORDvalueFrom:secretKeyRef:key: mysql_root_passwordname: mysql-password- name: MYSQL_SERVICE_PORTvalueFrom:configMapKeyRef:key: mysql-service-portname: nacos-deploy-config- name: MYSQL_SERVICE_USERvalueFrom:configMapKeyRef:key: mysql-service-username: nacos-deploy-config- name: NACOS_SERVERSvalueFrom:configMapKeyRef:key: nacos-serversname: nacos-deploy-config- name: SPRING_DATASOURCE_PLATFORMvalueFrom:configMapKeyRef:key: spring-datasource-platformname: 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集群就已经安装好了。