一、helm的安装
版本下载地址:https://github.com/helm/helm/releases
tar -zxvf helm-v3.0.0-linux-amd64.tar.gz
已经安装完成
[root@k8s-master01 ~]# helm version
version.BuildInfo{Version:"v3.5.3", GitCommit:"041ce5a2c17a58be0fcd5f5e16fb3e7e95fea622", GitTreeState:"dirty", GoVersion:"go1.15.8"}
至于怎么添加仓库,可以百度
[root@kube1 software]# helm repo list
NAME URL
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
aliyuncs https://apphub.aliyuncs.com
stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
goharbor https://helm.goharbor.io
二、安装rabbitMQ
查看可使用的rabbitmq-ha版本:
[root@kube1 software]# helm search repo rabbitmq-ha --versions
NAME CHART VERSION APP VERSION DESCRIPTION
aliyun/rabbitmq-ha 1.0.0 3.7.3 Highly available RabbitMQ cluster, the open sou...
aliyun/rabbitmq-ha 0.1.1 3.7.0 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.39.0 3.8.0 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.38.2 3.8.0 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.38.1 3.8.0 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.36.4 3.8.0 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.36.3 3.8.0 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.36.0 3.8.0 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.34.1 3.7.19 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.34.0 3.7.19 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.33.0 3.7.15 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.32.4 3.7.15 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.32.3 3.7.15 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.32.2 3.7.15 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.32.0 3.7.15 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.31.0 3.7.15 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.30.0 3.7.15 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.29.1 3.7.15 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.29.0 3.7.15 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.28.0 3.7.15 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.27.2 3.7.15 Highly available RabbitMQ cluster, the open sou...
aliyuncs/rabbitmq-ha 1.27.1 3.7.12 Highly available RabbitMQ cluster, the open sou...
stable/rabbitmq-ha 1.0.0 3.7.3 Highly available RabbitMQ cluster, the open sou...
stable/rabbitmq-ha 0.1.1 3.7.0 Highly available RabbitMQ cluster, the open sou...
拉取指定版本:
[root@kube1 software]# helm pull aliyuncs/rabbitmq-ha --version=1.33.0
[root@kube1 software]# ls
helm old rabbitmq-cluster rabbitmq-ha-1.33.0.tgz #这个就只拉去的文件,可以解压
解压并查看配置文件:
[root@kube1 software]# tar -xf rabbitmq-ha-1.33.0.tgz
[root@kube1 software]# ls
helm old rabbitmq-cluster rabbitmq-ha rabbitmq-ha-1.33.0.tgz
[root@kube1 software]# tree rabbitmq-ha
rabbitmq-ha
├── Chart.yaml
├── OWNERS
├── README.md
├── templates
│ ├── alerts.yaml
│ ├── configmap.yaml
│ ├── _helpers.tpl
│ ├── ingress.yaml
│ ├── NOTES.txt
│ ├── pdb.yaml
│ ├── rolebinding.yaml
│ ├── role.yaml
│ ├── secret.yaml
│ ├── serviceaccount.yaml
│ ├── service-discovery.yaml
│ ├── servicemonitor.yaml
│ ├── service.yaml
│ └── statefulset.yaml
└── values.yaml
1 directory, 18 files
参考:
├── charts # 依赖文件
├── Chart.yaml # 这个chart的版本信息
├── templates #模板
│ ├── deployment.yaml
│ ├── _helpers.tpl # 自定义的模板或者函数
│ ├── ingress.yaml
│ ├── NOTES.txt #这个chart的信息
│ ├── serviceaccount.yaml
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml #配置全局变量或者一些参数
创建命名空间
helm指定namespaces,如果没有namespaces空间,就会报错。需要提前创建:
[root@kube1 software]# helm create rabbitmq
Creating rabbitmq
查看命名空间:
[root@kube1 software]# helm list --namespace rabbitmq
NAME NAMESPACE REVISION UPDATED
cd到rabbitmq-ha目录下:
[root@kube1 rabbitmq-ha]# helm install rabbitmq . --namespace rabbitmq --set ingress.enabled=true,ingress.hostName=rabbitmq.akiraka.net --set rabbitmqUsername=aka,rabbitmqPassword=rabbitmq,managementPassword=rabbitmq,rabbitmqErlangCookie=secretcookie
NAME: rabbitmq
LAST DEPLOYED: Sun Mar 14 17:25:11 2021
NAMESPACE: rabbitmq-cluster
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
** Please be patient while the chart is being deployed **
Credentials:
Username : aka
Password : $(kubectl get secret --namespace rabbitmq-cluster rabbitmq-rabbitmq-ha -o jsonpath="{.data.rabbitmq-password}" | base64 --decode)
Management username : management
Management password : $(kubectl get secret --namespace rabbitmq-cluster rabbitmq-rabbitmq-ha -o jsonpath="{.data.rabbitmq-management-password}" | base64 --decode)
ErLang Cookie : $(kubectl get secret --namespace rabbitmq-cluster rabbitmq-rabbitmq-ha -o jsonpath="{.data.rabbitmq-erlang-cookie}" | base64 --decode)
RabbitMQ can be accessed within the cluster on port 5672 at rabbitmq-rabbitmq-ha.rabbitmq-cluster.svc.cluster.local
To access the cluster externally execute the following commands:
export POD_NAME=$(kubectl get pods --namespace rabbitmq-cluster -l "app=rabbitmq-ha" -o jsonpath="{.items[0].metadata.name}")
kubectl port-forward $POD_NAME --namespace rabbitmq-cluster 5672:5672 15672:15672
To Access the RabbitMQ AMQP port:
amqp://127.0.0.1:5672/
To Access the RabbitMQ Management interface:
URL : http://127.0.0.1:15672
[root@k8s-master01 rabbitmq-ha]#
查看集群:
[root@kube1 software]# kubectl get svc,pod,ingress -n rabbitmq
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/rabbitmq-rabbitmq-ha ClusterIP None <none> 15672/TCP,5672/TCP,4369/TCP 31m
service/rabbitmq-rabbitmq-ha-discovery ClusterIP None <none> 15672/TCP,5672/TCP,4369/TCP 31m
NAME READY STATUS RESTARTS AGE
pod/rabbitmq-rabbitmq-ha-0 1/1 Running 0 31m
pod/rabbitmq-rabbitmq-ha-1 1/1 Running 0 30m
pod/rabbitmq-rabbitmq-ha-2 1/1 Running 0 30m
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress.networking.k8s.io/rabbitmq-rabbitmq-ha <none> rabbitmq.akiraka.net 10.244.89.25 80 31m
访问:ip:15672
创建用户并设置角色:
创建用户:
设置permissions:
rabbitmqctl add_user admin 123456
rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
赋予其administrator角色:
rabbitmqctl set_user_tags admin administrator
创建和赋予完成之后查看并确认:
[root@kube1 software]# rabbitmqctl list_users
Listing users ...
admin [administrator]
...done.
使用刚才的用户名和密码登录:admin 123456
安装完成!!
如有遇到以下报错,查看下方链接博客可以顺利解决
Error: unable to connect to node rabbit@kube1: nodedown
DIAGNOSTICS
===========
attempted to contact: [rabbit@kube1]
rabbit@kube1:
* connected to epmd (port 4369) on kube1
* epmd reports: node 'rabbit' not running at all
no other nodes on kube1
* suggestion: start the node
current node details:
- node name: rabbitmqctl28862@kube1
- home dir: /var/lib/rabbitmq
- cookie hash: 1r7YbtT8MUKaUKmd40xUtQ==
卸载命令:
helm uninstall rabbitmq -n rabbitmq
三、Java简单使用
首先要创建一个spring项目 在pom.xml 导入下面这个依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
1.修改application.yml文件
spring:
application:
name: mq-rabbitmq-producer
rabbitmq:
host: 10.0.0.151
port: 30672
username: rabbit
password: rabbit
virtualHost: /
template:
retry:
enabled: true
initial-interval: 10000ms
max-interval: 300000ms
multiplier: 2
exchange: topic.exchange
2.创建生产者 Send
package com.demo.test;
import com.demo.RabbitMQ.RabbitmqConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class Send {
@Autowired
RabbitTemplate rabbitTemplate;
private static final String EXCHANGE_NAME = "topic.exchange";
@Test
public void sendMsgByTopics() throws InterruptedException {
/**
* 参数:
* 1、交换机名称
* 2、routingKey
* 3、消息内容
*/
for (int i=0;i<100000;i++){
String message = "userid="+i;
rabbitTemplate.convertAndSend(EXCHANGE_NAME,"topic.email.sms.test",message);
System.out.println(" [x] Sent '" + message + "'");
}
}
}
3.创建消费者 监听队列是否有内容
package com.demo.test;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class ReceiveHandler {
//监听邮件队列
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "queue_email", durable = "true"),
exchange = @Exchange(
value = "topic.exchange",
ignoreDeclarationExceptions = "true",
type = ExchangeTypes.TOPIC
),
key = {"topic.#.email.#","email.*"}))
public void rece_email(String msg){
System.out.println(" [邮件消费] received : " + msg + "!");
}
//监听短信队列
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "queue_sms", durable = "true"),
exchange = @Exchange(
value = "topic.exchange",
ignoreDeclarationExceptions = "true",
type = ExchangeTypes.TOPIC
),
key = {"topic.#.sms.#"}))
public void rece_sms(String msg){
System.out.println(" [短信消费] received : " + msg + "!");
}
//监听Test队列
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "queue_test",durable = "true"),
exchange = @Exchange(
value = "topic.exchange",
ignoreDeclarationExceptions = "true",
type = ExchangeTypes.TOPIC
),key = {"topic.#.test.#"}))
public void rece_test(String msg){
System.out.println(" [测试消费] received : " + msg + "!");
}
}
4.运行项目 然后访问rabbitmq页面
5.可以看到已经创建了一个交换机 topic.exchange
6.点击进去查看,该交换机已经绑定了队列,这点非常重要,会根据routing key去找对应的队列
7.此时我们查看队列已经有内容了
OK!代码非常简单,会有单元测试和其他依赖问题,如遇到导入一下对应的依赖就好了