前言:
后端小伙伴们准备搞pvp对战服务。配置中心选型选择了阿里云的nacos服务。参照https://nacos.io/zh-cn/docs。由于业务规划都在kubernetes集群上,就简单参照https://nacos.io/zh-cn/docs/use-nacos-with-kubernetes.html做了一个demo让他们先玩一下。
关于nacos:
参照:https://nacos.io/zh-cn/docs/what-is-nacos.html
- 服务发现和健康监测: 支持基于 DNS 和基于 RPC 的服务发现。服务提供者使用 原生SDK、OpenAPI、或一个独立的Agent TODO注册 Service 后,服务消费者可以使用DNS TODO 或HTTP&API查找和发现服务。提供对服务的实时的健康检查,阻止向不健康的主机或服务实例发送请求
- 动态配置服务: Nacos 提供配置统一管理功能,能够帮助我们将配置以中心化、外部化和动态化的方式管理所有环境的应用配置和服务配置。
- 动态 DNS 服务: Nacos 支持动态 DNS 服务权重路由,能够让我们很容易地实现中间层负载均衡、更灵活的路由策略、流量控制以及数据中心内网的简单 DNS 解析服务。
- 服务及其元数据管理: Nacos 支持从微服务平台建设的视角管理数据中心的所有服务及元数据,包括管理服务的描述、生命周期、服务的静态依赖分析、服务的健康状态、服务的流量管理、路由及安全策略、服务的 SLA 以及最首要的 metrics 统计数据。
嗯 还有更多的特性列表......
一. nacos on kubernetes
基本的安装过程参照:https://github.com/nacos-group/nacos-k8s/blob/master/README-CN.md
1. 创建命名空间
嗯当然了第一步还是先创建一个搭建nacos服务的namespace了:
kubectl create ns nacos
2. git clone 仓库
git clone https://github.com/nacos-group/nacos-k8s.git
基本都会因为网络原因无法clone,我是直接下载包到本地 然后上传到服务器了。
3. 部署初始化mysql服务器
生产的话肯定是用云商的云数据库了,比如腾讯云的rds服务。由于只是给程序整一个demo让他们玩一下,就讲mysql 整合在kubernetes中了。个人存储storageclass都是使用默认的腾讯云的cbs-csi。
cd /nacos-k8s/mysql(当然了我是上传的目录路径是/root/nacos/nacos-k8s-master/deploy/mysql)1. 部署mysql服务
cat pvc.yaml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nacos-mysql-pvc namespace: nacos spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi storageClassName: cbs-csi
mysql的部署文件直接复制了mysql-ceph.yaml的修改了一下:
cat mysql.yamlapiVersion: v1 kind: PersistentVolumeClaim metadata: name: nacos-mysql-pvc namespace: nacos spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi storageClassName: cbs-csi [root@sh-master-01 mysql]# cat mysql.yaml apiVersion: v1 kind: ReplicationController metadata: name: mysql labels: name: mysql spec: replicas: 1 selector: name: mysql template: metadata: labels: name: mysql spec: containers: - name: mysql image: nacos/nacos-mysql:5.7 ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: "root" - name: MYSQL_DATABASE value: "nacos_devtest" - name: MYSQL_USER value: "nacos" - name: MYSQL_PASSWORD value: "nacos" volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql subPath: mysql readOnly: false volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: nacos-mysql-pvc --- apiVersion: v1 kind: Service metadata: name: mysql labels: name: mysql spec: ports: - port: 3306 targetPort: 3306 selector: name: mysql
kubectl apply -f pvc.yaml kubectl apply -f mysql.yaml -n nacos kubectl get pods -n nacos
等待mysql pods running
$kubectl get pods -n nacos NAME READY STATUS RESTARTS AGE mysql-hhs5q 1/1 Running 0 3h51m
2. 进入mysql 容器执行初始化脚本
kubectl exec -it mysql-hhs5q bash -n nacos mysql -uroot -p root ***** create database nacos_devtest; use nacos_devtest; ### 我是图省事,把这sql脚本里面直接复制进去搞了... https://github.com/alibaba/nacos/blob/develop/distribution/conf/nacos-mysql.sql ------- 退出mysql控制台,并退出容器 quit; exit
4. 部署nacos
从mysql目录 cd ../nacos
cat nacos.yaml--- 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 - port: 7848 name: rpc targetPort: 7848 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: affinity: podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: "app" operator: In values: - nacos topologyKey: "kubernetes.io/hostname" initContainers: - name: peer-finder-plugin-install image: nacos/nacos-peer-finder-plugin:1.0 imagePullPolicy: Always volumeMounts: - mountPath: /home/nacos/plugins/peer-finder name: plguindir containers: - name: nacos imagePullPolicy: Always image: nacos/nacos-server:latest resources: requests: memory: "2Gi" cpu: "500m" ports: - containerPort: 8848 name: client-port - containerPort: 7848 name: rpc env: - name: NACOS_REPLICAS value: "2" - name: SERVICE_NAME value: "nacos-headless" - name: DOMAIN_NAME value: "layabox.daemon" - 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: NACOS_APPLICATION_PORT value: "8848" - name: PREFER_HOST_MODE value: "hostname" volumeMounts: - name: plguindir mountPath: /home/nacos/plugins/peer-finder - name: datadir mountPath: /home/nacos/data - name: logdir mountPath: /home/nacos/logs volumeClaimTemplates: - metadata: name: plguindir spec: accessModes: [ "ReadWriteOnce" ] storageClassName: "cbs-csi" resources: requests: storage: 10Gi - metadata: name: datadir spec: accessModes: [ "ReadWriteOnce" ] storageClassName: "cbs-csi" resources: requests: storage: 10Gi - metadata: name: logdir spec: accessModes: [ "ReadWriteOnce" ] storageClassName: "cbs-csi" resources: requests: storage: 10Gi selector: matchLabels: app: nacos
主要就是修改了storageclassName为 cbs-csi。并修改了accessmodes,还有DOMAIN_NAME变量修改为自己命名的集群后缀.简单demo不做详细论述。
kubectl apply -f nacos.yaml -n nacos
等待服务running
5. 对外暴露服务
代理个人使用的是traefik。过去都是用ingresroute的方式对外映射暴露服务,现在用想ingress的方式:
cat ingress.yamlapiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: nacos-headless-http namespace: nacos annotations: kubernetes.io/ingress.class: traefik traefik.ingress.kubernetes.io/router.entrypoints: web spec: rules: - host: nacos-server.saynaihe.com http: paths: - pathType: Prefix path: / backend: service: name: nacos-headless port: number: 8848
kubectl apply -f ingress.yaml
访问:https://nacos-server.layame.com/nacos一定记得域名后面跟上nacos。否则是404呢,当然了也可以在ingress配置上面重定向直接到nacos下?看个人怎么玩了。
默认用户名密码 :nacos nacos。当然了第一件事是修改密码......
嗯 先扔给程序去玩下了。还有很多配置的东西省略了。比如很多参数和变量,可以参照下官方配置进行搞一下......
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。