k8s部署Mycat

Posted 算法技术猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s部署Mycat相关的知识,希望对你有一定的参考价值。

目录导引
  • ConfigMap 配置

  • Deployment 配置

  • Service 配置

  • 启动Mycat k8s服务

使用k8s部署Mycat之前,需先准备k8s部署Mycat的Deployment和Service部署文件以及Mycat配置文件(server.xml、rule.xml、schema.xml以及sequence-conf.xml等);Mycat本身的配置会通过ConfigMap的形式挂载到k8s容器中,方便后续对Mycat配置文件的修改及重新部署应用,Mycat的配置文件可参考 。

ConfigMap配置

Mycat configMap配置包含上述介绍的server.xml、rule.xml、schema.xml,将Mycat配置文件挂载到k8s的ConfigMap中,在配置ConfigMap之前,先创建一个名为rec-ops的namespace,然后创建configMap配置
mycat-namespace.yaml

apiVersion: v1kind: Namespacemetadata: name: rec-ops labels: name: rec-ops

mycat-schema.yaml

apiVersion: v1kind: ConfigMapmetadata: name: schema-config namespace: rec-ops labels: kubernetes.io/cluster-service: "true" addonmanager.kubernetes.io/mode: EnsureExistsdata: schema.xml: "<?xml version=\"1.0\"?> <!DOCTYPE mycat:schema SYSTEM \"schema.dtd\"> <mycat:schema xmlns:mycat=\"http://io.mycat/\"> <!-- 单库分表 --> <schema name=\"single_node_schema\" checkSQLschema=\"false\" sqlMaxLimit=\"100\" dataNode=\"single_node_database\"> <table name=\"single_test\" subTables=\"single_test$0-2\" rule=\"single_node_rule\" primaryKey=\"id\" autoIncrement=\"true\" dataNode=\"single_node_database\"/> </schema> <!-- 多库分表 新建表时需提前在<table>标签中定好好表名称 --> <schema name=\"multi_node_schema\" checkSQLschema=\"false\" sqlMaxLimit=\"100\"> <table name=\"multi_test\" primaryKey=\"id\" rule=\"multi_node_rule\" dataNode=\"multi_node_database1,multi_node_database2\"/> </schema> <!-- 只读库配置 --> <schema name=\"read_only_schema\" checkSQLschema=\"false\" sqlMaxLimit=\"100\" dataNode=\"read_only_database\"/>
<dataNode name=\"single_node_database\" dataHost=\"single_node_host\" database=\"mycat_test\"/> <dataNode name=\"multi_node_database1\" dataHost=\"multi_node_host1\" database=\"mycat_test\"/> <dataNode name=\"multi_node_database2\" dataHost=\"single_node_host\" database=\"mycat_test\"/> <dataNode name=\"read_only_database\" dataHost=\"read_only_host\" database=\"rec_themis\"/> <dataHost name=\"single_node_host\" maxCon=\"1000\" minCon=\"10\" balance=\"3\" dbType=\"mysql\" dbDriver=\"native\"> <heartbeat>select user()</heartbeat> <writeHost host=\"hostM1\" url=\"10.0.0.1:3307\" user=\"root\" password=\"testmaster\" /> </dataHost> <dataHost name=\"multi_node_host1\" maxCon=\"1000\" minCon=\"10\" balance=\"0\" dbType=\"mysql\" dbDriver=\"native\"> <heartbeat>select user()</heartbeat> <writeHost host=\"hostS1\" url=\"10.0.0.1:3309\" user=\"root\" password=\"mycat\" /> </dataHost> <dataHost name=\"read_only_host\" maxCon=\"1000\" minCon=\"10\" balance=\"0\" dbType=\"mysql\" dbDriver=\"native\"> <heartbeat>select user()</heartbeat> <writeHost host=\"hostS1\" url=\"10.0.0.1:3308\" user=\"slave\" password=\"slave\"/> </dataHost> </mycat:schema>"

mycat-rule.yaml

apiVersion: v1kind: ConfigMapmetadata: name: rule-config namespace: rec-ops labels: kubernetes.io/cluster-service: "true" addonmanager.kubernetes.io/mode: EnsureExistsdata: rule.xml: "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <!DOCTYPE mycat:rule SYSTEM \"rule.dtd\"> <mycat:rule xmlns:mycat=\"http://io.mycat/\">
<tableRule name=\"single_node_rule\"> <rule> <columns>id</columns> <algorithm>single_node_func</algorithm> </rule> </tableRule> <tableRule name=\"multi_node_rule\"> <rule> <columns>id</columns> <algorithm>multi_node_func</algorithm> </rule> </tableRule> <function name=\"single_node_func\" class=\"io.mycat.route.function.PartitionByMod\"> <property name=\"count\">3</property> </function> <function name=\"multi_node_func\" class=\"io.mycat.route.function.PartitionByMod\"> <property name=\"count\">2</property> </function> </mycat:rule>"

mycat-server.yaml

apiVersion: v1kind: ConfigMapmetadata: name: server-config namespace: rec-ops labels: kubernetes.io/cluster-service: "true" addonmanager.kubernetes.io/mode: EnsureExistsdata: server.xml: "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <!DOCTYPE mycat:server SYSTEM \"server.dtd\"> <mycat:server xmlns:mycat=\"http://io.mycat/\"> <!-- 系统全局配置 --> <system> <property name=\"sequnceHandlerType\">0</property> <property name=\"charset\">utf8mb4</property> </system>  <user name=\"single_user\"> <property name=\"password\">single_pwd</property> <property name=\"schemas\">single_node_schema</property> <property name=\"readOnly\">false</property> </user>  <user name=\"multi_user\"> <property name=\"password\">multi_pwd</property> <property name=\"schemas\">multi_node_schema</property> <property name=\"readOnly\">false</property> </user>  <user name=\"read_only_user\"> <property name=\"password\">read_only_pwd</property> <property name=\"schemas\">read_only_schema</property> <property name=\"readOnly\">true</property> </user> </mycat:server>"

若需要用到批量插入设置本地sequence,则可以参考上述的配置,将Mycat的sequence-conf也挂载k8s的ConfigMap中

Deployment 配置
kind: DeploymentapiVersion: apps/v1beta2metadata: labels: app: mycat role: master name: mycat namespace: rec-opsspec: replicas: 3 #设置副本数 revisionHistoryLimit: 10 selector: matchLabels: app: mycat template: metadata: labels: app: mycat spec: containers: - name: mycat image: {docker 镜像仓库ip}:{镜像仓库port}/mycat:1.6.7.4 #mycat镜像地址 imagePullPolicy: Always securityContext: privileged: true ports: - containerPort: 8066 protocol: TCP - containerPort: 9066 protocol: TCP volumeMounts: - name: server-config mountPath: "/mycat/conf/server.xml" subPath: server.xml - name: schema-config mountPath: "/mycat/conf/schema.xml" subPath: schema.xml - name: rule-config mountPath: "/mycat/conf/rule.xml" subPath: rule.xml volumes:  - name: server-config configMap: name: server-config - name: schema-config configMap: name: schema-config - name: rule-config configMap: name: rule-config
Service 配置

配置Service时,需指定spec.type类型:ClusterIP、NodePort 和 LoadBalancer,此处使用NodePort,部署后,可通过服务器ip+port直接连接Mycat
mycat-service.yaml

apiVersion: v1kind: Servicemetadata: name: mycat namespace: rec-ops labels: app: mycatspec: ports: - name: tcp2 port: 9066 protocol: TCP targetPort: 9066 - name: tcp3 port: 8066 protocol: TCP targetPort: 8066 selector: app: mycat type: NodePort
启动Mycat k8s服务

启动mycat服务之前,先确保mycat-schema.yaml配置中dataHost的mysql服务已启动,并且确保其账户名和密码存在,然后通过下列命令,将各个配置应用到k8s中

kubectl apply -f mycat-namespace.yamlkubectl apply -f mycat-schema.yamlkubectl apply -f mycat-rule.yamlkubectl apply -f mycat-server.yamlkubectl apply -f mycat-deploy.yamlkubectl apply -f mycat-service.yaml

启动后,通过命令查看pod

kubectl get pod -n rec-ops | grep mycat



相关阅读:




以上是关于k8s部署Mycat的主要内容,如果未能解决你的问题,请参考以下文章

Mycat 1.6.6 容器化部署

MyCat集群部署(HAProxy + MyCat)

MyCat 部署读写分离-分库,未分表

MYCAT 安装部署

# IT明星不是梦 #MySQL高可用集群之基于MyCat部署HaProxy实现高可用

k8s的项目部署