Spring Boot 微服务上容器平台的最佳实践 - 3

2022-04-21 14:00:26 浏览数 (1)

前言

今天开始第二篇, 主要介绍下 Zipkin MySQL部署到容器平台上.

项目克隆

GitHub 仓库地址 源码在这里. 先下载下来.

代码语言:javascript复制
$ git clone https://github.com/RHsyseng/spring-boot-msa-ocp.git LambdaAir

然后进入项目目录: cd LambdaAir

几个前提

  1. 共享存储. 这个Demo 应用会用到NFS, K8S(或OpenShift)上得有NFS的PV, 且空间充足(至少5G). 共享存储用于:
    1. zpikin的mysql数据存储;
    2. zuul的动态proxy脚本挂载.
  2. K8S (或OpenShift) 得有充足的权限. 至少得拥有以下权限:
    1. 创建NameSpace(或OpenShift的Project)
    2. 创建PVC
    3. 创建Service
    4. 创建 Deployment
    5. 创建 Ingress(或OpenShift的 Route)
    6. 创建和挂载 ConfigMap
    7. 创建Secret

创建 NameSpace(或Project)

代码语言:javascript复制
$ oc new-project lambdaair --display-name="Lambda Air" --description="Spring Boot Microservices on Red Hat OpenShift Container Platform"
Now using project "lambdaair" on server "https://ocp-master1.xxx.example.com:8443".

部署ZipKin

概述

全部步骤拆分如下:

  1. 创建secret - 存储数据库账号密码等机要信息
  2. 创建 PVC - zipkin-mysql, 用于申请存储资源, 存放mysql 持久化数据;
  3. 创建 configmap - zipkin-mysql-cnf, 用于挂载mysql的配置;
  4. 创建 configmap - zipkin-mysql-initdb, 用于挂载mysql 初始化脚本;
  5. 创建 deploymentconfig - zipkin-mysql, 用于对zipkin-mysql 的初始化, deployemnt及副本数进行管理.
  6. 创建 service - zipkin-mysql, 用于在集群内提供 zipkin的mysql 服务. zipkin通过这个service连接到mysql.
  7. 创建 deploymentconfig - zipkin. 用于对zipkin的build和deployment以及副本数等进行管理;
  8. 创建 service - zipkin. 用于在集群内提供zipkin 服务;
  9. 创建 route - zipkin , 用于连接service - zipkin, 使得用户可以通过域名访问zipkin.

创建 MySQL 数据库

上面的步骤中, 与创建MySQL 数据库有关的步骤有1-6步. 说明如下:

  1. 创建账号密码等信息;
  2. 申请持久化存储;
  3. 创建MySQL 配置;
  4. 创建MySQL 初始化脚本;
  5. 创建MySQL实例. 会用到
    1. 账号密码
    2. 持久化存储
    3. MySQL启动后会执行初始化脚本
  6. 创建MySQL服务, 供集群内(项目内)使用.
创建 Secret

zipkin-mysql-secret.yml文件如下:

代码语言:javascript复制
kind: Secret
apiVersion: v1
metadata:
  name: zipkin-mysql
  namespace: lambdaair
  labels:
    project: zipkin-mysql  
data:
  database-password: your-password
  database-root-password: root-password
  database-user: normaluser

使用ockubectl创建:

代码语言:javascript复制
# 先切换到 lambdaair project(后续都在这个项目里, 不在赘述)
$ oc project lambdaair
# 创建 secret
$ oc create -f zipkin-mysql-secret.yml
# 查看创建结果
$ oc get secret
NAME                       TYPE                                  DATA   AGE
zipkin-mysql               Opaque                                3      2d5h
创建 PVC

Zipkin使用MySQL数据库进行存储,这反过来又需要创建一个OpenShift 共享存储。假定已经有了:

代码语言:javascript复制
$ oc get pv
NAME                  CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS      CLAIM                           REASON    AGE
zipkin-mysql-data     1Gi        RWO           Recycle         Available     1m

zipkin-mysql-pvc.yml 内容如下:

代码语言:javascript复制
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: zipkin-mysql
  namespace: lambdaair
  labels:
    project: zipkin-mysql  
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

创建:

代码语言:javascript复制
$ oc create -f zipkin-mysql-pvc.yml
$ oc get pvc
NAME           STATUS   VOLUME              CAPACITY   ACCESS MODES   STORAGECLASS   AGE
zipkin-mysql   Bound    zipkin-mysql-data   1Gi        RWO                           2d5h
创建 ConfigMap
创建 MySQL 配置的ConfigMap

zipkin-mysql-cnf.yml

代码语言:javascript复制
kind: ConfigMap
apiVersion: v1
metadata:
  name: zipkin-mysql-cnf
  namespace: lambdaair
  labels:
    project: zipkin-mysql  
data:
  custom.cnf: |
    [mysqld]
    sql-mode=""

其实就是示范下, 没配置啥. 接下来创建:

代码语言:javascript复制
$ oc create -f zipkin-mysql-cnf.yml
$ oc get configmap
NAME                  DATA   AGE
zipkin-mysql-cnf      1      2d5h
创建 MySQL 初始化脚本的ConfigMap

zipkin-mysql-initdb.yml

代码语言:javascript复制
kind: ConfigMap
apiVersion: v1
metadata:
  name: zipkin-mysql-initdb
  namespace: lambdaair
  labels:
    project: zipkin-mysql  
data:
  init.sql: >
    CREATE TABLE IF NOT EXISTS zipkin_spans (
      `trace_id` BIGINT NOT NULL,
      `id` BIGINT NOT NULL,
      `name` VARCHAR(255) NOT NULL,
      `parent_id` BIGINT,
      `debug` BIT(1),
      `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
      `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query'
    ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED;


    ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id`, `id`) COMMENT 'ignore
    insert on duplicate';

    ALTER TABLE zipkin_spans ADD INDEX(`trace_id`, `id`) COMMENT 'for joining
    with zipkin_annotations';

    ALTER TABLE zipkin_spans ADD INDEX(`trace_id`) COMMENT 'for getTracesByIds';

    ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and
    getSpanNames';

    ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces
    ordering and range';


    CREATE TABLE IF NOT EXISTS zipkin_annotations (
      `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
      `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
      `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
      `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
      `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
      `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
      `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
      `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
      `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
      `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
    ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED;


    ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id`, `span_id`,
    `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';

    ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`) COMMENT 'for
    joining with zipkin_spans';

    ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`) COMMENT 'for
    getTraces/ByIds';

    ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT
    'for getTraces and getServiceNames';

    ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces';

    ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces';


    CREATE TABLE IF NOT EXISTS zipkin_dependencies (
      `day` DATE NOT NULL,
      `parent` VARCHAR(255) NOT NULL,
      `child` VARCHAR(255) NOT NULL,
      `call_count` BIGINT
    ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED;


    ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);

创建:

代码语言:javascript复制
$ oc create -f zipkin-mysql-initdb.yml
$ oc get configmap
NAME                  DATA   AGE
zipkin-mysql-cnf      1      2d5h
zipkin-mysql-initdb   1      2d5h
创建 MySQL的 DeploymentConfig

zipkin-mysql-deploymentconfig.yml

0 人点赞