什么是Nacos?
Nacos(Namings and Configuration Management)是阿里巴巴开源的一个易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
以下是Nacos的一些主要功能和特点:
- 服务发现和服务健康检查:Nacos 支持基于 DNS 和 RPC 的服务发现。这意味着,您的微服务应用可以在 Nacos 中注册自己,并发现其他服务。同时,Nacos 可以对注册的服务进行健康检查,以确保服务可用。
- 动态配置服务:在微服务架构中,配置信息可能会频繁变动,Nacos 提供了一个中心化的、外部化的动态配置服务,您可以在 Nacos 中动态地管理和修改配置信息,所有使用该配置的服务都会实时得到通知并应用新配置,而无需重启。
- 动态 DNS 服务:Nacos 提供了一种基于 DNS 协议的服务发现方式,可以更好地支持跨集群、跨地区的服务发现需求。
- 服务和元数据管理:Nacos 提供了统一的服务管理和元数据管理功能,您可以在 Nacos 中管理所有服务的信息和状态,以及服务的元数据信息。
- 支持持久化:Nacos 支持 MySQL 数据库持久化,可以保证注册服务和配置信息的安全性。
- 易于集成和扩展:Nacos 提供了丰富的 API 和插件,可以方便地与其他系统集成,也可以根据需要进行扩展。
- 支持多种环境:Nacos 可以运行在单机环境、集群环境,也可以运行在云环境如 Kubernetes 和 Docker 等。总的来说,Nacos 是一个强大的服务注册和配置管理平台,它可以帮助开发人员更好地构建和管理微服务应用。
更多内容 ?https://nacos.io/zh-cn/docs/architecture.html
创建Nacos数据库
数据库安装部署就不在这里写了
# 下载初始化SQL文件 | |
$ wget https://raw.githubusercontent.com/alibaba/nacos/master/distribution/conf/mysql-schema.sql | |
# 进去容器 | |
$ docker exec -it mysql bash | |
# 进入数据库 | |
$ mysql -u root -pAdmin@1234 | |
mysql: [Warning] Using a password on the command line interface can be insecure. | |
Welcome to the MySQL monitor. Commands end with ; or \g. | |
Your MySQL connection id is 2 | |
Server version: 5.7.44 MySQL Community Server (GPL) | |
Copyright (c) 2000, 2023, Oracle and/or its affiliates. | |
Oracle is a registered trademark of Oracle Corporation and/or its | |
affiliates. Other names may be trademarks of their respective | |
owners. | |
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. | |
mysql> | |
# 创建数据库 | |
mysql> create database nacos; | |
Query OK, 1 row affected (0.00 sec) | |
mysql> show databases; | |
+--------------------+ | |
| Database | | |
+--------------------+ | |
| information_schema | | |
| mysql | | |
| nacos | | |
| performance_schema | | |
| sys | | |
+--------------------+ | |
5 rows in set (0.00 sec) | |
# 初始化数据库 | |
mysql> use nacos; | |
mysql> source mysql-schema.sql; |
安装Nacos高可用集群
本案例以MySQL作为持久化存储部署:
新建一个命名空间 | |
kubectl create ns dev | |
拉取安装配置 | |
clone https://github.com/nacos-group/nacos-k8s.git | git|
cd nacos-k8s/deploy/nacos |
修改配置:
# 修改数据库信息 | |
$ nacos-no-pvc-ingress.yaml | |
... | |
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: nacos-cm | |
data: | |
mysql.host: "10.0.53.73" | |
mysql.db.name: "nacos" | |
mysql.port: "3306" | |
mysql.user: "root" | |
mysql.password: "Admin@1234" | |
- name: NACOS_AUTH_ENABLE | |
value: "true" | |
- name: nacos.core.auth.server.identity.key | |
value: "subM8MzvolJ+MWYVhgkOBC7EvkwOrYczDYOsAB/6KhA=" | |
- name: nacos.core.auth.server.identity.value | |
value: "7YlBYjd2HU+9DJpPRV4zcvvEkBqO8SxNpfJRDNqPH30=" | |
- name: nacos.core.auth.plugin.nacos.token.secret.key | |
value: "SecretKey012345678901234567890123456789012345678901234567890123456789" | |
- name: NACOS_SERVERS | |
value: "nacos-0.nacos-headless.dev.svc.cluster.local:8848 nacos-1.nacos-headless.dev.svc.cluster.local:8848 nacos-2.nacos-headless.dev.svc.cluster.local:8848" | |
... |
注意:NACOS_SERVERS配置指定的命名空间一定要与Nacos部署的命名空间一致
执行创建:
$ kubectl apply -f nacos-pvc-nfs.yaml -n dev | |
service/nacos-headless created | |
configmap/nacos-cm created | |
statefulset.apps/nacos created |
查看Pod状态:
$ kubectl get pods -n dev | |
NAME READY STATUS RESTARTS AGE | |
nacos-0 1/1 Running 0 4m35s | |
nacos-1 1/1 Running 0 4m19s | |
nacos-2 1/1 Running 0 4m4s |
访问验证
本次通过最简单的forward端口转发进行暴露进行访问(也可以通过Ingress进行暴露访问):
$ kubectl port-forward -n dev nacos-0 8848:8848 --address 0.0.0.0
http://转发机器IP:8848 默认帐号/密码:nacos/nacos
image
image
查看集群状态:
到此为止,Nacos集群就完成部署了!