架构

由于资源有限都在一台机器上实现:
在这里插入图片描述
在使用harbor时务必提前在docker中配置harbor地址,并且能正常下载harbor中镜像,否则需要重启docker,会导致已搭建好的k8s和rancher出现问题

mysql搭建

共需搭建4个mysql

nfs-server搭建

使用nfs作为持久化方式,首先搭建nfs服务端:

yum -y install nfs-utils rpcbind
systemctl enable rpcbind && systemctl start nfs
systemctl enable nfs-server && systemctl start rpcbind
#创建nfs目录
mkdir -p /data/nfs

#修改权限
vi /etc/exports
/data/nfs *(insecure,rw,sync,no_subtree_check,no_root_squash)

#重启nfs
systemctl restart nfs rpcbind

#查看挂载情况
showmount -e

Export list for VM-0-9-centos:
/data/nfs *

配置nfs-Provisioner以及储存类StorageClass,提供动态分配卷

此处参考:https://blog.csdn.net/zuozewei/article/details/120809571

  1. 为动态分配赋予权限,创建rbac.yaml:
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  namespace: test-ns 
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-provisioner-runner
rules:
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    namespace: test-ns
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-runner
  apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
  namespace: test-ns
rules:
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
  namespace: test-ns
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    namespace: test-ns
roleRef:
  kind: Role
  name: leader-locking-nfs-client-provisioner
  apiGroup: rbac.authorization.k8s.io

kubectl apply -f nfs-rbac.yaml
  1. 部署nfs-Provisioner的deployment,创建nfs-provisioner-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  namespace: test-ns
  labels:
    app: nfs-client-provisioner
spec:
  replicas: 1
  strategy:
    type: Recreate                   ## 设置升级策略为删除再创建(默认为滚动更新)
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          #image: gcr.io/k8s-staging-sig-storage/nfs-subdir-external-provisioner:v4.0.0
          image: registry.cn-beijing.aliyuncs.com/xngczl/nfs-subdir-external-provisione:v4.0.0
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes     #此处要注意,建议不要修改
          env:
            - name: PROVISIONER_NAME     ## Provisioner的名称,以后设置的storageclass要和这个保持一致
              value: nfs-client
            - name: NFS_SERVER           ## NFS服务器地址,需和valumes参数中配置的保持一致
              value: 10.10.10.10
            - name: NFS_PATH             ## NFS服务器数据存储目录,需和valumes参数中配置的保持一致
              value: /data/nfs
      volumes:
        - name: nfs-client-root
          nfs:
            server: 10.10.10.10     ## NFS服务器地址
            path: /data/nfs            ## NFS服务器数据存储目录

  1. 创建存储类,新建nfs-storage.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-storage
provisioner: nfs-client # 和env.PROVISIONER_NAME 保持一致
reclaimPolicy: Retain
parameters:
  archiveOnDelete: "false"

之后即可通过存储类StorageClass使用nfs

mysql-master搭建

创建配置映射mysql-master-cm

在k8s中新建mysql的配置映射mysql-master-cm,配置my.cnf文件:
在这里插入图片描述

[client]
port		= 3306
socket		= /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
skip-host-cache
skip-name-resolve
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 3306
basedir		= /usr
datadir		= /var/lib/mysql
tmpdir		= /tmp
lc-messages-dir	= /usr/share/mysql
explicit_defaults_for_timestamp

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address	= 127.0.0.1

#log-error	= /var/log/mysql/error.log

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
#!includedir /etc/mysql/conf.d/
#启用二进制日志
log_bin=mysql-bin
#主服务器唯一ID
server_id=1
#不需要复制的数据库
binlog-ignore-db=mysql
#需要复制的数据库
binlog-do-db=test
binlog-do-db=mycat_test
binlog-do-db=nacos_config
#设置binlog的格式
binlog-format=STATEMENT

创建秘钥mysql-secret

在这里插入图片描述

创建mysql-master的StatefulSet

mysql-master-StatefulSet.yaml:
replicas只配置了一个

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-master
  namespace: test-ns
spec:
  serviceName: "mysql-master"
  replicas: 1
  volumeClaimTemplates:
  - metadata:
      name: mysql-local-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: nfs-storage
      resources:
        requests:
          storage: 5Gi
  selector:
    matchLabels:
       app: mysql-master
  template:
    metadata:
     labels:
       app: mysql-master
    spec:
      volumes:
      - name: config
        configMap:
          name: mysql-master-cm
      containers:
      - image: mysql:5.7.34
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-local-storage
          readOnly: false
          mountPath: /var/lib/mysql
        - name: config
          mountPath: "/etc/mysql"

查看StatefulSet运行情况

在这里插入图片描述
查看pod运行情况
在这里插入图片描述
可以看到mysql-master已经启动,使用pod的命令行进行测试
在这里插入图片描述
在这里插入图片描述
能正常登陆mysql,说明数据库运行正常。

mysql-master_Headless-service

mysql-master-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: mysql-master
  namespace: test-ns
  labels:
    app: mysql-master
spec:
  ports:
    - port: 3306
  selector:
    app: mysql-master
  clusterIP: None

在这里插入图片描述

mysql-master2 搭建

配置映射可以复用mysql-master-cm
秘钥可以复用mysql-secret

创建mysql-master2的StatefulSet

mysql-master2-StatefulSet.yaml:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-master2
  namespace: test-ns
spec:
  serviceName: "mysql-master2"
  replicas: 1
  volumeClaimTemplates:
  - metadata:
      name: mysql-local-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: nfs-storage
      resources:
        requests:
          storage: 5Gi
  selector:
    matchLabels:
       app: mysql-master2
  template:
    metadata:
     labels:
       app: mysql-master2
    spec:
      volumes:
      - name: config
        configMap:
          name: mysql-master-cm
      containers:
      - image: mysql:5.7.34
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-local-storage
          readOnly: false
          mountPath: /var/lib/mysql
        - name: config
          mountPath: "/etc/mysql"

检查mysql-master2的pod是否正常启动,并能正常登陆mysql

mysql-master2_Headless-service

mysql-master-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: mysql-master2
  namespace: test-ns
  labels:
    app: mysql-master2
spec:
  ports:
    - port: 3306
  selector:
    app: mysql-master2
  clusterIP: None

mysql-slave 搭建

秘钥可以复用mysql-secret

创建配置映射mysql-slave-cm

在k8s中新建mysql的配置映射mysql-slave-cm,配置my.cnf文件:
在这里插入图片描述
my.cnf:

[client]
port		= 3306
socket		= /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
skip-host-cache
skip-name-resolve
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 3306
basedir		= /usr
datadir		= /var/lib/mysql
tmpdir		= /tmp
lc-messages-dir	= /usr/share/mysql
explicit_defaults_for_timestamp

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address	= 127.0.0.1

#log-error	= /var/log/mysql/error.log

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
#!includedir /etc/mysql/conf.d/
relay-log=mysql-relay
server_id=2  #server_id要与主数据库以及其他从数据库避免一致

创建mysql-slave的StatefulSet

mysql-slave-StatefulSet.yaml:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-slave
  namespace: test-ns
spec:
  serviceName: "mysql-slave"
  replicas: 1
  volumeClaimTemplates:
  - metadata:
      name: mysql-local-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: nfs-storage
      resources:
        requests:
          storage: 5Gi
  selector:
    matchLabels:
       app: mysql-slave
  template:
    metadata:
     labels:
       app: mysql-slave
    spec:
      volumes:
      - name: config
        configMap:
          name: mysql-slave-cm
      containers:
      - image: mysql:5.7.34
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-local-storage
          readOnly: false
          mountPath: /var/lib/mysql
        - name: config
          mountPath: "/etc/mysql"

此时可检查mysql-slave是否能够正常使用

mysql-slave_Headless-service

mysql-slave-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: mysql-slave
  namespace: test-ns
  labels:
    app: mysql-slave
spec:
  ports:
    - port: 3306
  selector:
    app: mysql-slave
  clusterIP: None

mysql-slave2 搭建

秘钥可以复用mysql-secret

创建配置映射mysql-slave2-cm

在k8s中新建mysql的配置映射mysql-slave2-cm,配置my.cnf文件:
在这里插入图片描述
my.cnf:

[client]
port		= 3306
socket		= /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
skip-host-cache
skip-name-resolve
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 3306
basedir		= /usr
datadir		= /var/lib/mysql
tmpdir		= /tmp
lc-messages-dir	= /usr/share/mysql
explicit_defaults_for_timestamp

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address	= 127.0.0.1

#log-error	= /var/log/mysql/error.log

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
#!includedir /etc/mysql/conf.d/
relay-log=mysql-relay
server_id=3

创建mysql-slave2的StatefulSet

mysql-slave2-StatefulSet.yaml:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-slave2
  namespace: test-ns
spec:
  serviceName: "mysql-slave2"
  replicas: 1
  volumeClaimTemplates:
  - metadata:
      name: mysql-local-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: nfs-storage
      resources:
        requests:
          storage: 5Gi
  selector:
    matchLabels:
       app: mysql-slave2
  template:
    metadata:
     labels:
       app: mysql-slave2
    spec:
      volumes:
      - name: config
        configMap:
          name: mysql-slave2-cm
      containers:
      - image: mysql:5.7.34
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-local-storage
          readOnly: false
          mountPath: /var/lib/mysql
        - name: config
          mountPath: "/etc/mysql"

此时可检查mysql-slave2是否能够正常使用

mysql-slave2_Headless-service

mysql-slave2-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: mysql-slave2
  namespace: test-ns
  labels:
    app: mysql-slave2
spec:
  ports:
    - port: 3306
  selector:
    app: mysql-slave2
  clusterIP: None

mysql主从配置

master配置

首先登录进入master数据库,配置主备用的用户名密码:

#配置备份用的账号
grant replication slave on *.* to 'myslave'@'%' identified by 'myslave@123AC';

#配置账号权限
grant all privileges on *.* to 'myslave'@'%' identified by 'myslave@123AC' with grant option;

#增加root账号权限
grant all privileges on *.* to 'root'@'%' identified by '123456';

#配置生效
flush privileges;

此时查看master状态:

show master status;
+------------------+----------+------------------------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB                 | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+------------------------------+------------------+-------------------+
| mysql-bin.000003 |     1173 | test,mycat_test,nacos_config | mysql            |                   |
+------------------+----------+------------------------------+------------------+-------------------+

其中mysql-bin.000003为目前使用的备份文件,后面配置主备的时候需要用到

slave配置

分别进入两个slave中,执行以下命令:

change master to master_host='mysql-master',master_user='myslave',master_password='myslave@123AC',master_log_file='mysql-bin.000003'
#mysql-master为k8s中配置的service名称,用于访问主数据库
#mysql-bin.000003为备份需要的文件

此时开启备份并查看slave状态:

start slave;
show slave status \G;

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-master
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 1173
               Relay_Log_File: mysql-relay.000002
                Relay_Log_Pos: 1386
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes   #两个yes证明主备配置成功
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1173
              Relay_Log_Space: 1589
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 73a94d22-7b4c-11ed-8e61-d689372ae6fe
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

主备测试

在master中新建数据库,查看两个slave中数据是否同步(数据库必须在master的my.cnf配置文件中进行配置)

mycat搭建

创建mycat镜像

下载mycat

下载mycat,根据dockerFile生成镜像,并上传到harbor
github下载地址
下载的版本为1.6.7.4
Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz

编写dockerfile

FROM openjdk:8
ADD Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz /usr/local/
EXPOSE 8066 9066
CMD ["/usr/local/mycat/bin/mycat","console","&"]

生成镜像并上传

参考https://blog.csdn.net/yorao4565/article/details/126332047

docker build -t mycat:1.6.7.4 .
docker tag mycat:1.6.7.4 172.21.0.9:8020/test/mycat:1.6.7.4
docker push 172.21.0.9:8020/test/mycat:1.6.7.4

test为提前在harbor中建立的项目名
可以在harbor中看到生成的镜像:
在这里插入图片描述

k8s中配置harbor地址

首先要在docker中配置harbor的地址,配置好后最好手动测试下能否拉到镜像:

#新建配置
vim /etc/docker/daemon.json

{
  "insecure-registries": ["10.10.10.10:8020"]
}

在k8s中新建密钥,配置harbor的用户名密码,
在这里插入图片描述

k8s搭建

创建mycat的配置映射mycat-cm

mycat需要配置四个配置文件,包括:rule.xml,schema.xml,server.xml,wrapper.conf:
在这里插入图片描述
其中,
schema.xml——数据库分库、分表、主备等相关配置
rule.xml——分表规则等
server.xml——用户权限控制等
wrapper.conf——mycat基本配置,例如jdk、日志等
分别为

schema.xml:

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

	<!-- 数据库配置,默认节点为dn1 -->
	<schema name="mycat_test" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
		<!-- 分库配置,表customer只在dn2 -->
		<table name="customer" dataNode="dn2" ></table>
		<!-- 分表配置,表stu进行分表,分别在dn1,dn2两个节点,分表规则为stu-mod-long,在rule.xml中定义 -->
    	<table name="stu" dataNode="dn1,dn2" rule="stu-mod-long" ></table>
    </schema> 
    <dataNode name="dn1" dataHost="host1" database="mycat_test"/>
    <dataNode name="dn2" dataHost="host2" database="mycat_test"/>
    <!-- mysql配置,其中writeType、balance、switchType等值可以根据需求修改,配置不同规则 -->
	<dataHost name="host1" maxCon="1000" minCon="10" balance="2"
			  writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<!-- 主库1地址,按照service定义配置 -->
		<writeHost host="hostM1" url="mysql-master:3306" user="root" password="123456">
					<!-- 从库地址,按照service定义配置 -->
                    <readHost host="hostS1" url="mysql-slave:3306" user="root" password="123456"/>
                    <!-- 从库2地址,按照service定义配置 -->
                    <readHost host="hostS2" url="mysql-slave2:3306" user="root" password="123456"/>
		</writeHost>
	</dataHost>
        <dataHost name="host2" maxCon="1000" minCon="10" balance="0"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <!-- 主库2地址,按照service定义配置 -->
                <writeHost host="hostM2" url="mysql-master2:3306" user="root" password="123456">
                </writeHost>
        </dataHost>
</mycat:schema>

rule.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License"); 
	- you may not use this file except in compliance with the License. - You 
	may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 
	- - Unless required by applicable law or agreed to in writing, software - 
	distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT 
	WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the 
	License for the specific language governing permissions and - limitations 
	under the License. -->
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
	<!-- 分表规则,与schema.xml中配置的对应 -->
	<tableRule name="stu-mod-long">
		<rule>
			<!-- 分表需要的字段 -->
			<columns>id</columns>
			<!-- 分表规则 -->
			<algorithm>mod-long</algorithm>
		</rule>
	</tableRule>

	<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
		<!-- 以2为模数,均分成两份 -->
		<property name="count">2</property>
	</function>
	
</mycat:rule>

server.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">

	<user name="root" defaultAccount="true">
		<property name="password">123456</property>
		<property name="schemas">mycat_test</property>

	</user>

</mycat:server>

wrapper.conf:

#********************************************************************
# Wrapper Properties
#********************************************************************
# Java Application
wrapper.java.command=java
wrapper.working.dir=..

# Java Main class.  This class must implement the WrapperListener interface
#  or guarantee that the WrapperManager class is initialized.  Helper
#  classes are provided to do this for you.  See the Integration section
#  of the documentation for details.
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
set.default.REPO_DIR=lib
set.APP_BASE=.

# Java Classpath (include wrapper.jar)  Add class path elements as
#  needed starting from 1
wrapper.java.classpath.1=lib/wrapper.jar
wrapper.java.classpath.2=conf
wrapper.java.classpath.3=%REPO_DIR%/*

# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=lib

# Java Additional Parameters
#wrapper.java.additional.1=
wrapper.java.additional.1=-DMYCAT_HOME=.
wrapper.java.additional.2=-server
wrapper.java.additional.3=-XX:+AggressiveOpts
wrapper.java.additional.4=-XX:MaxDirectMemorySize=2G
wrapper.java.additional.5=-Dcom.sun.management.jmxremote
wrapper.java.additional.6=-Dcom.sun.management.jmxremote.port=1984
wrapper.java.additional.7=-Dcom.sun.management.jmxremote.authenticate=false
wrapper.java.additional.8=-Dcom.sun.management.jmxremote.ssl=false
wrapper.java.additional.9=-Xmx4G
wrapper.java.additional.10=-Xms1G

# Initial Java Heap Size (in MB)
#wrapper.java.initmemory=3

# Maximum Java Heap Size (in MB)
#wrapper.java.maxmemory=64

# Application parameters.  Add parameters as needed starting from 1
wrapper.app.parameter.1=io.mycat.MycatStartup
wrapper.app.parameter.2=start

#********************************************************************
# Wrapper Logging Properties
#********************************************************************
# Format of output for the console.  (See docs for formats)
wrapper.console.format=PM

# Log Level for console output.  (See docs for log levels)
wrapper.console.loglevel=INFO

# Log file to use for wrapper output logging.
wrapper.logfile=logs/wrapper.log

# Format of output for the log file.  (See docs for formats)
wrapper.logfile.format=LPTM

# Log Level for log file output.  (See docs for log levels)
wrapper.logfile.loglevel=INFO

# Maximum size that the log file will be allowed to grow to before
#  the log is rolled. Size is specified in bytes.  The default value
#  of 0, disables log rolling.  May abbreviate with the 'k' (kb) or
#  'm' (mb) suffix.  For example: 10m = 10 megabytes.
wrapper.logfile.maxsize=512m

# Maximum number of rolled log files which will be allowed before old
#  files are deleted.  The default value of 0 implies no limit.
wrapper.logfile.maxfiles=30

# Log Level for sys/event log output.  (See docs for log levels)
wrapper.syslog.loglevel=NONE

#********************************************************************
# Wrapper Windows Properties
#********************************************************************
# Title to use when running as a console
wrapper.console.title=Mycat-server

#********************************************************************
# Wrapper Windows NT/2000/XP Service Properties
#********************************************************************
# WARNING - Do not modify any of these properties when an application
#  using this configuration file has been installed as a service.
#  Please uninstall the service before modifying this section.  The
#  service can then be reinstalled.

# Name of the service
wrapper.ntservice.name=mycat

# Display name of the service
wrapper.ntservice.displayname=Mycat-server

# Description of the service
wrapper.ntservice.description=The project of Mycat-server

# Service dependencies.  Add dependencies as needed starting from 1
wrapper.ntservice.dependency.1=

# Mode in which the service is installed.  AUTO_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START

# Allow the service to interact with the desktop.
wrapper.ntservice.interactive=false

wrapper.ping.timeout=120
configuration.directory.in.classpath.first=conf

创建mycat的deployment

mycat-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mycat
  name: mycat
  namespace: test-ns
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mycat
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: mycat
    spec:
      volumes:
      - name: config
        configMap:
          name: mycat-cm
      imagePullSecrets:
        - name: harbor-secret
      containers:
      - image: 172.21.0.9:8020/test/mycat:1.6.7.4
        name: consul-agent
        ports:
        - containerPort: 8066
        - containerPort: 9066
        volumeMounts:
        - name: config
          mountPath: /usr/local/mycat/conf
        env:
        - name: MYSQL_HOST
          value: "mysql-master.default.svc.cluster.local"
        - name: MYCAT_HOME
          value: /usr/local/mycat
        resources:
          requests:
            cpu: 0.1
            memory: 50Mi
          limits:
            cpu: 1
            memory: 1000Mi

在这里插入图片描述

创建mycat的service:

mycat-service.yaml

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: mycat
  name: db
  namespace: test-ns
spec:
  ports:
  - port: 9066
    name: management-port
    protocol: TCP
    targetPort: 9066
  - port: 3306
    name: data-port
    protocol: TCP
    targetPort: 8066
  selector:
    app: mycat
status:
  loadBalancer: {}

在这里插入图片描述
可以登录mycat进行测试:
在这里插入图片描述
mycat_test数据库需要在两个主库中建好,可以登录从数据库查看数据库是否同步建好

验证

主备验证

可以在主数据库中操作数据库,查看是否从库中能同步数据

分库验证

通过mycat新建表customer,查看两个主数据库中是否有这张表,正常情况下之后主库2中会有该表

分表验证

向表stu中插入数据,(建表时要有字段“id”),插入数据时会根据不同的id值存储于不同的数据库中:
mycat:
在这里插入图片描述
mysql-master:
在这里插入图片描述
mysql-master2:
在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐