Add "rabbitmq deploy with k8s"

This commit is contained in:
timlackhan
2019-09-20 11:09:57 +08:00
committed by qzheng527
parent 64431a2f7a
commit 7b22e13e2a
2 changed files with 213 additions and 0 deletions
+60
View File
@@ -69,6 +69,66 @@ guest [administrator]
```
See the RabbitMQ [Clustering Guide](https://www.rabbitmq.com/clustering.html) for more information.
### Deploy with Kubernetes
This image can also be deployed on a Kubernetes cluster, such as [minikube](https://kubernetes.io/docs/setup/learning-environment/minikube/).The following example YAML files are provided in the repository as reference for Kubernetes deployment:
- [`rabbitmq-deployment-conf.yaml`](https://github.com/clearlinux/dockerfiles/blob/master/rabbitmq/rabbitmq-deployment-conf.yaml): example using your own custom configuration to create a rabbitmq service.
Steps to deploy rabbitmq on a Kubernetes cluster:
1. Create namespace test-rabbitmq for rabbitmq service.
```
kubectl create ns test-rabbitmq
```
2. Deploy `rabbitmq-deployment-conf.yaml`
```
kubectl create -f rabbitmq-deployment-conf.yaml
```
3. Check the cluster status
- Wait few seconds....then run
```
FIRST_POD=$(kubectl get pods --namespace test-rabbitmq -l 'app=rabbitmq' -o jsonpath='{.items[0].metadata.name }')
kubectl exec --namespace=test-rabbitmq $FIRST_POD rabbitmqctl cluster_status
```
- The output should look something like this:
```
Cluster status of node rabbit@rabbitmq-0.rabbitmq.test-rabbitmq.svc.cluster.local ...
[{nodes,[{disc,['rabbit@rabbitmq-0.rabbitmq.test-rabbitmq.svc.cluster.local',
'rabbit@rabbitmq-1.rabbitmq.test-rabbitmq.svc.cluster.local',
'rabbit@rabbitmq-2.rabbitmq.test-rabbitmq.svc.cluster.local']}]},
{running_nodes,['rabbit@rabbitmq-2.rabbitmq.test-rabbitmq.svc.cluster.local',
'rabbit@rabbitmq-1.rabbitmq.test-rabbitmq.svc.cluster.local',
'rabbit@rabbitmq-0.rabbitmq.test-rabbitmq.svc.cluster.local']},
{cluster_name,<<"rabbit@rabbitmq-0.rabbitmq.test-rabbitmq.svc.cluster.local">>},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-2.rabbitmq.test-rabbitmq.svc.cluster.local',[]},
{'rabbit@rabbitmq-1.rabbitmq.test-rabbitmq.svc.cluster.local',[]},
{'rabbit@rabbitmq-0.rabbitmq.test-rabbitmq.svc.cluster.local',[]}]}]
```
4. Install python and its package `pika`
5. Use python to connect to the rabbitmq service, where 30672 is the port number defined in your service.
```
# in Python shell
>>> import pika
>>> connection = pika.BlockingConnection(pika.ConnectionParameters("<nodeIP>",30672))
```
<!-- Required -->
## Build and modify:
The Dockerfiles for all Clear Linux* OS based container images are available at
+153
View File
@@ -0,0 +1,153 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: rabbitmq
namespace: test-rabbitmq
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: endpoint-reader
namespace: test-rabbitmq
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: endpoint-reader
namespace: test-rabbitmq
subjects:
- kind: ServiceAccount
name: rabbitmq
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: endpoint-reader
---
kind: Service
apiVersion: v1
metadata:
namespace: test-rabbitmq
name: rabbitmq
labels:
app: rabbitmq
type: LoadBalancer
spec:
type: NodePort
ports:
- name: http
protocol: TCP
port: 15672
targetPort: 15672
nodePort: 31672
- name: amqp
protocol: TCP
port: 5672
targetPort: 5672
nodePort: 30672
selector:
app: rabbitmq
---
apiVersion: v1
kind: ConfigMap
metadata:
name: rabbitmq-config
namespace: test-rabbitmq
data:
enabled_plugins: |
[rabbitmq_peer_discovery_k8s].
rabbitmq.conf: |
cluster_formation.peer_discovery_backend = rabbit_peer_discovery_k8s
cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
cluster_formation.k8s.address_type = hostname
cluster_formation.node_cleanup.interval = 30
cluster_formation.node_cleanup.only_log_warning = true
cluster_partition_handling = autoheal
queue_master_locator=min-masters
loopback_users.guest = false
listeners.tcp.default = 5672
hipe_compile = false
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: rabbitmq
namespace: test-rabbitmq
spec:
serviceName: rabbitmq
replicas: 3
template:
metadata:
labels:
app: rabbitmq
spec:
serviceAccountName: rabbitmq
terminationGracePeriodSeconds: 10
containers:
- name: rabbitmq-k8s
image: docker.io/clearlinux/rabbitmq
volumeMounts:
- name: config-volume
mountPath: /etc/rabbitmq
ports:
- name: http
protocol: TCP
containerPort: 15672
- name: amqp
protocol: TCP
containerPort: 5672
livenessProbe:
exec:
command: ["rabbitmqctl", "status"]
initialDelaySeconds: 60
periodSeconds: 60
timeoutSeconds: 15
readinessProbe:
exec:
command: ["rabbitmqctl", "status"]
initialDelaySeconds: 20
periodSeconds: 60
timeoutSeconds: 10
imagePullPolicy: Always
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.name
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: MY_POD_SERVICE_ACCOUNT
valueFrom:
fieldRef:
fieldPath: spec.serviceAccountName
- name: RABBITMQ_USE_LONGNAME
value: "true"
- name: RABBITMQ_NODENAME
value: rabbit@$(MY_POD_NAME).$(MY_POD_SERVICE_ACCOUNT).$(MY_POD_NAMESPACE).svc.cluster.local
- name: K8S_HOSTNAME_SUFFIX
value: .$(MY_POD_SERVICE_ACCOUNT).$(MY_POD_NAMESPACE).svc.cluster.local
- name: K8S_SERVICE_NAME
value: $(MY_POD_SERVICE_ACCOUNT)
- name: RABBITMQ_ERLANG_COOKIE
value: "mycookie"
volumes:
- name: config-volume
configMap:
name: rabbitmq-config
items:
- key: rabbitmq.conf
path: rabbitmq.conf
- key: enabled_plugins
path: enabled_plugins