乐趣区

关于kubernetes:Kubernetes-1205搭建nacos

前言:
后端小伙伴们筹备搞 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.yaml

    apiVersion: 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.yaml

    apiVersion: 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。当然了第一件事是批改明码 ……

    嗯 先扔给程序去玩下了。还有很多配置的货色省略了。比方很多参数和变量,能够参照下官网配置进行搞一下 ……

退出移动版