文章

在K8S中安裝RabbitMQ Cluster

RabbitMQ官方有釋出一個名為RabbitMQ Cluster Kubernetes Operator的工具來簡化在K8S中安裝RabbitMQ Cluster的作業,官方說明文件可以參考這裡。安裝指令:

1
kubectl apply -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml

瞬間就安裝完成了,接著再測試hello-world的範例:

1
kubectl apply -f https://raw.githubusercontent.com/rabbitmq/cluster-operator/main/docs/examples/hello-world/rabbitmq.yaml

也是一下子就完成了,但這個範例只部署了一個pod,把replica設定成3個可能會比較有感。觀察一下log,確認是否有在運作:

1
kubectl logs hello-world-server-0

這個operator會把預設使用者帳密設定到secret中:

可以用以下指令解出來:

1
2
3
4
username="$(kubectl get secret hello-world-default-user -o jsonpath='{.data.username}' | base64 --decode)"
echo "username: $username"
password="$(kubectl get secret hello-world-default-user -o jsonpath='{.data.password}' | base64 --decode)"
echo "password: $password"

也可以在建立Cluster時指定帳密,可以參考GitHub這範例

接著測試是否可以正常進入RabbitMQ的web管理介面,For測試目的,直接用port forward的方式

1
kubectl port-forward "service/hello-world" 15672

打開瀏覽器,進入https://localhost:15762/

可以對剛建立的RabbitMQ做一下簡單的壓力測試:

1
2
3
4
username="$(kubectl get secret hello-world-default-user -o jsonpath='{.data.username}' | base64 --decode)"
password="$(kubectl get secret hello-world-default-user -o jsonpath='{.data.password}' | base64 --decode)"
service="$(kubectl get service hello-world -o jsonpath='{.spec.clusterIP}')"
kubectl run perf-test --image=pivotalrabbitmq/perf-test -- --uri amqp://$username:$password@$service

然後用這指令去看測試log:

1
kubectl logs --follow perf-test

這數字蠻誇張的,每秒可以傳送接收7萬多筆訊息,有機會再到舊的MBP上測試看看

至此一個簡單的RabbitMQ Cluster就建立完成,當然就算再簡單的應用也不只如此,例如如何讓K8S外的應用也可以呼叫K8S內的RabbitMQ,但已經可以確認這工具的確簡化了很多的作業,後續再來研究怎麼做更進階的操作。

本文章以 CC BY 4.0 授權