k8s学习(二十九) RocketMq集群(双主双从-同步)

2024-06-20 08:18

本文主要是介绍k8s学习(二十九) RocketMq集群(双主双从-同步),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

确认安装前已经安装好StorageClass,可参考:https://blog.csdn.net/u011943534/article/details/100887530

1、准备jdk镜像,并拷贝到服务器

docker pull java:8-alpine

2、制作rocketmq镜像
下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/
”rocketmq-all-4.8.0-bin-release.zip”拷贝至服务器,解压
修改bin/runbroker.sh,将内存调整为适当(比如调小)

JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m"

编写Dockerfile-rocketmq:

FROM java:8-alpineRUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezoneENV LANG en_US.UTF-8ADD rocketmq-all-4.8.0-bin-release /usr/local/rocketmq-all-4.8.0-bin-release
RUN mv /usr/local/rocketmq-all-4.8.0-bin-release /usr/local/rocketmq-4.8.0 \
&& mkdir -p /data/rocketmq/storeCMD ["/bin/bash"]
docker build -f Dockerfile-rocketmq -t 172.16.10.190:80/library/rocketmq:4.8.0 .
docker push 172.16.10.190:80/library/rocketmq:4.8.0

3、制作nameserver镜像
使用第三步的rocketmq-all-4.8.0-bin-release
在conf/下添加namesrv.properties
内容为:

listenPort=20901

编写Dokerfile-namesrv:

FROM java:8-alpineRUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezoneENV LANG en_US.UTF-8ADD rocketmq-all-4.8.0-bin-release /usr/local/rocketmq-all-4.8.0-bin-release
RUN mv /usr/local/rocketmq-all-4.8.0-bin-release /usr/local/rocketmq-4.8.0WORKDIR /usr/local/rocketmq-4.8.0
CMD ["/usr/local/rocketmq-4.8.0/bin/mqnamesrv","-c","/usr/local/rocketmq-4.8.0/conf/namesrv.properties"]
docker build -f Dockerfile-nameserver -t 172.16.10.190:80/library/rocketmq-namesrv:4.8.0 .
docker push 172.16.10.190:80/library/rocketmq-namesrv:4.8.0

4、制作rocketmq-ui镜像
下载源码:
git clone https://github.com/apache/rocketmq-externals.git
下载maven:
wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz

tar -zvxf rocketmq-externals.tar.gz

将本目录下的”application.properties“中内容替换到rocketmq-externals/rocketmq-console/src/main/resources/application.properties中
编写Dockerfile-ui

FROM java:8-alpineRUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezoneENV LANG en_US.UTF-8
ENV MAVEN_HOME /usr/local/maven
ENV PATH $PATH:$MAVEN_HOME/binADD rocketmq-externals /usr/local/rocketmq-externals
ADD apache-maven-3.3.9 /usr/local/apache-maven-3.3.9
RUN mv /usr/local/apache-maven-3.3.9 /usr/local/mavenWORKDIR /usr/local/rocketmq-externals/rocketmq-console/
RUN mvn clean package -Dmaven.test.skip=trueWORKDIR /usr/local/rocketmq-externals/rocketmq-console/target
EXPOSE 8080
CMD ["nohup","java","-jar","rocketmq-console-ng-1.0.0.jar","&"]

5、准备configmap
broker-a.properties:

brokerClusterName=rocketmq-cluster
brokerName=broker-a
brokerId=0
namesrvAddr=mq-namesrv-0.mq-namesrv.default.svc.cluster.local:20901
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
listenPort=20911
deleteWhen=04
fileReservedTime=120
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=300000
diskMaxUsedSpaceRatio=88
storePathRootDir=/data/rocketmq/store
maxMessageSize=65536
brokerRole=MASTER

broker-a-s.properties:

brokerClusterName=rocketmq-cluster
brokerName=broker-a
brokerId=1
namesrvAddr=mq-namesrv-0.mq-namesrv.default.svc.cluster.local:20901
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
listenPort=20911
deleteWhen=04
fileReservedTime=120
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=300000
diskMaxUsedSpaceRatio=88
storePathRootDir=/data/rocketmq/store
maxMessageSize=65536
brokerRole=SLAVE
flushDiskType=SYNC_FLUSH

broker-b.properties:

brokerClusterName=rocketmq-cluster
brokerName=broker-b
brokerId=0
namesrvAddr=mq-namesrv-0.mq-namesrv.default.svc.cluster.local:20901
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
listenPort=20911
deleteWhen=04
fileReservedTime=120
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=300000
diskMaxUsedSpaceRatio=88
storePathRootDir=/data/rocketmq/store
maxMessageSize=65536
brokerRole=MASTER
flushDiskType=SYNC_FLUSH

broker-b-s.properties:

brokerClusterName=rocketmq-cluster
brokerName=broker-b
brokerId=1
namesrvAddr=mq-namesrv-0.mq-namesrv.default.svc.cluster.local:20901
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
listenPort=20911
deleteWhen=04
fileReservedTime=120
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=300000
diskMaxUsedSpaceRatio=88
storePathRootDir=/data/rocketmq/store
maxMessageSize=65536
brokerRole=SLAVE
flushDiskType=SYNC_FLUSH

根据以上文件生成configmap:

kubectl create configmap rocketmq-config --from-file=broker-a.properties --from-file=broker-b.properties --from-file=broker-a-s.properties --from-file=broker-b-s.properties  -n default

6、准备服务文件
broker-a-statefulset.yaml:

apiVersion: v1
kind: Service
metadata:labels:app: broker-aname: broker-anamespace: default
spec:type: NodePortports:- port: 20911targetPort: 20911name: broker-portnodePort: 30911selector:app: broker-a
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: broker-anamespace: default
spec:serviceName: broker-areplicas: 1selector:matchLabels:app: broker-atemplate:metadata:labels:app: broker-aspec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: "app"operator: Invalues:- broker-atopologyKey: "kubernetes.io/hostname"imagePullSecrets:- name: harborsecretcontainers:- name: broker-aimage: 172.16.10.190:80/library/rocketmq:4.8.0imagePullPolicy: Alwayscommand: ["sh","-c","/usr/local/rocketmq-4.8.0/bin/mqbroker  -c /usr/local/rocketmq-4.8.0/conf/broker-a.properties"]volumeMounts:- mountPath: /root/logsname: rocketmq-datasubPath: mq-brokeroptlogs- mountPath: /data/rocketmqname: rocketmq-datasubPath: mq-brokeroptstore- name: broker-configmountPath: /usr/local/rocketmq-4.8.0/conf/broker-a.propertiessubPath: broker-a.properties- name: timemountPath: /etc/localtimelifecycle:postStart:exec:command: ["/bin/sh","-c","touch /tmp/health"]livenessProbe:exec:command: ["test","-e","/tmp/health"]initialDelaySeconds: 5timeoutSeconds: 5periodSeconds: 10readinessProbe:tcpSocket:port: 20911initialDelaySeconds: 15timeoutSeconds: 5periodSeconds: 20volumes:- name: broker-configconfigMap:name: rocketmq-config- name: timehostPath:path: /usr/share/zoneinfo/Asia/ShanghaivolumeClaimTemplates:- metadata:name: rocketmq-dataannotations:volume.beta.kubernetes.io/storage-class: "course-nfs-storage"spec:accessModes:- ReadWriteManyresources:requests:storage: 4Gi

broker-a-s-statefulset.yaml:

apiVersion: v1
kind: Service
metadata:labels:app: broker-a-sname: broker-a-snamespace: default
spec:type: NodePortports:- port: 20911targetPort: 20911name: broker-portnodePort: 30912selector:app: broker-a-s
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: broker-a-snamespace: default
spec:serviceName: broker-a-sreplicas: 1selector:matchLabels:app: broker-a-stemplate:metadata:labels:app: broker-a-sspec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: "app"operator: Invalues:- broker-a-stopologyKey: "kubernetes.io/hostname"imagePullSecrets:- name: harborsecretcontainers:- name: broker-a-simage: 172.16.10.190:80/library/rocketmq:4.8.0imagePullPolicy: Alwayscommand: ["sh","-c","/usr/local/rocketmq-4.8.0/bin/mqbroker  -c /usr/local/rocketmq-4.8.0/conf/broker-a-s.properties"]volumeMounts:- mountPath: /root/logsname: rocketmq-datasubPath: mq-brokeroptlogs- mountPath: /data/rocketmqname: rocketmq-datasubPath: mq-brokeroptstore- name: broker-configmountPath: /usr/local/rocketmq-4.8.0/conf/broker-a-s.propertiessubPath: broker-a-s.properties- name: timemountPath: /etc/localtimelifecycle:postStart:exec:command: ["/bin/sh","-c","touch /tmp/health"]livenessProbe:exec:command: ["test","-e","/tmp/health"]initialDelaySeconds: 5timeoutSeconds: 5periodSeconds: 10readinessProbe:tcpSocket:port: 20911initialDelaySeconds: 15timeoutSeconds: 5periodSeconds: 20volumes:- name: broker-configconfigMap:name: rocketmq-configitems:- key: broker-a-s.propertiespath: broker-a-s.properties- name: timehostPath:path: /usr/share/zoneinfo/Asia/ShanghaivolumeClaimTemplates:- metadata:name: rocketmq-dataannotations:volume.beta.kubernetes.io/storage-class: "course-nfs-storage"spec:accessModes:- ReadWriteManyresources:requests:storage: 4Gi

broker-b-statefulset.yaml:

apiVersion: v1
kind: Service
metadata:labels:app: broker-bname: broker-bnamespace: default
spec:type: NodePortports:- port: 20911targetPort: 20911name: broker-portnodePort: 30913selector:app: broker-b
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: broker-bnamespace: default
spec:serviceName: broker-breplicas: 1selector:matchLabels:app: broker-btemplate:metadata:labels:app: broker-bspec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: "app"operator: Invalues:- broker-btopologyKey: "kubernetes.io/hostname"imagePullSecrets:- name: harborsecretcontainers:- name: broker-bimage: 172.16.10.190:80/library/rocketmq:4.8.0imagePullPolicy: Alwayscommand: ["sh","-c","/usr/local/rocketmq-4.8.0/bin/mqbroker  -c /usr/local/rocketmq-4.8.0/conf/broker-b.properties"]volumeMounts:- mountPath: /root/logsname: rocketmq-datasubPath: mq-brokeroptlogs- mountPath: /data/rocketmqname: rocketmq-datasubPath: mq-brokeroptstore- name: broker-configmountPath: /usr/local/rocketmq-4.8.0/conf/broker-b.propertiessubPath: broker-b.properties- name: timemountPath: /etc/localtimelifecycle:postStart:exec:command: ["/bin/sh","-c","touch /tmp/health"]livenessProbe:exec:command: ["test","-e","/tmp/health"]initialDelaySeconds: 5timeoutSeconds: 5periodSeconds: 10readinessProbe:tcpSocket:port: 20911initialDelaySeconds: 15timeoutSeconds: 5periodSeconds: 20volumes:- name: broker-configconfigMap:name: rocketmq-config- name: timehostPath:path: /usr/share/zoneinfo/Asia/ShanghaivolumeClaimTemplates:- metadata:name: rocketmq-dataannotations:volume.beta.kubernetes.io/storage-class: "course-nfs-storage"spec:accessModes:- ReadWriteManyresources:requests:storage: 4Gi

broker-b-s-statefulset.yaml:

apiVersion: v1
kind: Service
metadata:labels:app: broker-b-sname: broker-b-snamespace: default
spec:type: NodePortports:- port: 20911targetPort: 20911name: broker-portnodePort: 30914selector:app: broker-b-s
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: broker-b-snamespace: default
spec:serviceName: broker-b-sreplicas: 1selector:matchLabels:app: broker-b-stemplate:metadata:labels:app: broker-b-sspec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: "app"operator: Invalues:- broker-b-stopologyKey: "kubernetes.io/hostname"imagePullSecrets:- name: harborsecretcontainers:- name: broker-b-simage: 172.16.10.190:80/library/rocketmq:4.8.0imagePullPolicy: Alwayscommand: ["sh","-c","/usr/local/rocketmq-4.8.0/bin/mqbroker  -c /usr/local/rocketmq-4.8.0/conf/broker-b-s.properties"]volumeMounts:- mountPath: /root/logsname: rocketmq-datasubPath: mq-brokeroptlogs- mountPath: /data/rocketmqname: rocketmq-datasubPath: mq-brokeroptstore- name: broker-configmountPath: /usr/local/rocketmq-4.8.0/conf/broker-b-s.propertiessubPath: broker-b-s.properties- name: timemountPath: /etc/localtimelifecycle:postStart:exec:command: ["/bin/sh","-c","touch /tmp/health"]livenessProbe:exec:command: ["test","-e","/tmp/health"]initialDelaySeconds: 5timeoutSeconds: 5periodSeconds: 10readinessProbe:tcpSocket:port: 20911initialDelaySeconds: 15timeoutSeconds: 5periodSeconds: 20volumes:- name: broker-configconfigMap:name: rocketmq-configitems:- key: broker-b-s.propertiespath: broker-b-s.properties- name: timehostPath:path: /usr/share/zoneinfo/Asia/ShanghaivolumeClaimTemplates:- metadata:name: rocketmq-dataannotations:volume.beta.kubernetes.io/storage-class: "course-nfs-storage"spec:accessModes:- ReadWriteManyresources:requests:storage: 4Gi

namesrv-statefulset.yaml:

apiVersion: v1
kind: Service
metadata:labels:app: mq-namesrvname: mq-namesrvnamespace: default
spec:type: NodePortports:- port: 20901targetPort: 20901name: namesrv-portnodePort: 30915selector:app: mq-namesrv
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: mq-namesrvnamespace: default
spec:serviceName: mq-namesrvreplicas: 1selector:matchLabels:app: mq-namesrvtemplate:metadata:labels:app: mq-namesrvspec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: "app"operator: Invalues:- mq-namesrvtopologyKey: "kubernetes.io/hostname"imagePullSecrets:- name: harborsecretcontainers:- name: mq-namesrvimage: 172.16.10.190:80/library/rocketmq-namesrv:4.8.0imagePullPolicy: AlwaysvolumeMounts:- name: timemountPath: /etc/localtimelifecycle:postStart:exec:command: ["/bin/sh","-c","touch /tmp/health"]livenessProbe:exec:command: ["test","-e","/tmp/health"]initialDelaySeconds: 5timeoutSeconds: 5periodSeconds: 10readinessProbe:tcpSocket:port: 20901initialDelaySeconds: 15timeoutSeconds: 5periodSeconds: 20volumes:- name: timehostPath:path: /usr/share/zoneinfo/Asia/Shanghai

rocketmq-externals-deployment.yaml:

apiVersion: v1
kind: Service
metadata:labels:app: mq-externalsname: mq-externalsnamespace: default
spec:type: NodePortports:- port: 8080targetPort: 8080name: console-portnodePort: 30916selector:app: mq-externals
---
apiVersion: apps/v1
kind: Deployment
metadata:name: mq-externalsnamespace: default
spec:replicas: 1selector:matchLabels:app: mq-externalstemplate:metadata:labels:app: mq-externalsspec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: "app"operator: Invalues:- mq-externalstopologyKey: "kubernetes.io/hostname"imagePullSecrets:- name: harborsecretcontainers:- name: mq-externalsimage: 172.16.10.190:80/library/rocketmq-externals:v3imagePullPolicy: AlwaysvolumeMounts:- name: timemountPath: /etc/localtimelifecycle:postStart:exec:command: ["/bin/sh","-c","touch /tmp/health"]livenessProbe:exec:command: ["test","-e","/tmp/health"]initialDelaySeconds: 5timeoutSeconds: 5periodSeconds: 10readinessProbe:tcpSocket:port: 8080initialDelaySeconds: 15volumes:- name: timehostPath:path: /usr/share/zoneinfo/Asia/Shanghai

7、部署服务
将6步的文件拷贝至K8s

kubectl apply -f .

8、查看
http://172.16.10.160:30916
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这篇关于k8s学习(二十九) RocketMq集群(双主双从-同步)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1077590

相关文章

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

Redis中哨兵机制和集群的区别及说明

《Redis中哨兵机制和集群的区别及说明》Redis哨兵通过主从复制实现高可用,适用于中小规模数据;集群采用分布式分片,支持动态扩展,适合大规模数据,哨兵管理简单但扩展性弱,集群性能更强但架构复杂,根... 目录一、架构设计与节点角色1. 哨兵机制(Sentinel)2. 集群(Cluster)二、数据分片

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

C#控制台程序同步调用WebApi实现方式

《C#控制台程序同步调用WebApi实现方式》控制台程序作为Job时,需同步调用WebApi以确保获取返回结果后执行后续操作,否则会引发TaskCanceledException异常,同步处理可避免异... 目录同步调用WebApi方法Cls001类里面的写法总结控制台程序一般当作Job使用,有时候需要控制

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

Linux线程同步/互斥过程详解

《Linux线程同步/互斥过程详解》文章讲解多线程并发访问导致竞态条件,需通过互斥锁、原子操作和条件变量实现线程安全与同步,分析死锁条件及避免方法,并介绍RAII封装技术提升资源管理效率... 目录01. 资源共享问题1.1 多线程并发访问1.2 临界区与临界资源1.3 锁的引入02. 多线程案例2.1 为

解决RocketMQ的幂等性问题

《解决RocketMQ的幂等性问题》重复消费因调用链路长、消息发送超时或消费者故障导致,通过生产者消息查询、Redis缓存及消费者唯一主键可以确保幂等性,避免重复处理,本文主要介绍了解决RocketM... 目录造成重复消费的原因解决方法生产者端消费者端代码实现造成重复消费的原因当系统的调用链路比较长的时

Jenkins分布式集群配置方式

《Jenkins分布式集群配置方式》:本文主要介绍Jenkins分布式集群配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装jenkins2.配置集群总结Jenkins是一个开源项目,它提供了一个容易使用的持续集成系统,并且提供了大量的plugin满

Redis分片集群、数据读写规则问题小结

《Redis分片集群、数据读写规则问题小结》本文介绍了Redis分片集群的原理,通过数据分片和哈希槽机制解决单机内存限制与写瓶颈问题,实现分布式存储和高并发处理,但存在通信开销大、维护复杂及对事务支持... 目录一、分片集群解android决的问题二、分片集群图解 分片集群特征如何解决的上述问题?(与哨兵模

SpringBoot连接Redis集群教程

《SpringBoot连接Redis集群教程》:本文主要介绍SpringBoot连接Redis集群教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 依赖2. 修改配置文件3. 创建RedisClusterConfig4. 测试总结1. 依赖 <de