Skip to content

Commit f2e56b7

Browse files
author
amigoscode
committed
postgres rabbitmq and zipkin on k8s
1 parent 7315fb4 commit f2e56b7

File tree

11 files changed

+514
-0
lines changed

11 files changed

+514
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: postgres-config
5+
data:
6+
POSTGRES_DB: amigoscode
7+
POSTGRES_USER: amigoscode
8+
POSTGRES_PASSWORD: password
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: postgres
5+
spec:
6+
selector:
7+
app: postgres
8+
ports:
9+
- port: 5432
10+
targetPort: 5432
11+
type: ClusterIP
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
apiVersion: apps/v1
2+
kind: StatefulSet
3+
metadata:
4+
name: postgres
5+
labels:
6+
app: postgres
7+
spec:
8+
serviceName: postgres
9+
replicas: 1
10+
template:
11+
metadata:
12+
name: postgres
13+
labels:
14+
app: postgres
15+
spec:
16+
volumes:
17+
- name: postgres
18+
persistentVolumeClaim:
19+
claimName: postgres-pc-volume-claim
20+
containers:
21+
- name: postgres
22+
image: postgres
23+
imagePullPolicy: IfNotPresent
24+
volumeMounts:
25+
- mountPath: "/var/lib/postgresql/data"
26+
name: postgres
27+
envFrom:
28+
- configMapRef:
29+
name: postgres-config
30+
resources:
31+
requests:
32+
cpu: 100m
33+
memory: 256Mi
34+
limits:
35+
cpu: 500m
36+
memory: 512Mi
37+
restartPolicy: Always
38+
selector:
39+
matchLabels:
40+
app: postgres
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apiVersion: v1
2+
kind: PersistentVolume
3+
metadata:
4+
name: postgres-pc-volume
5+
labels:
6+
type: local
7+
app: postgres
8+
spec:
9+
storageClassName: manual
10+
capacity:
11+
storage: 5Gi
12+
accessModes:
13+
- ReadWriteMany
14+
hostPath:
15+
path: /mnt/data
16+
---
17+
apiVersion: v1
18+
kind: PersistentVolumeClaim
19+
metadata:
20+
name: postgres-pc-volume-claim
21+
labels:
22+
app: postgres
23+
spec:
24+
storageClassName: manual
25+
accessModes:
26+
- ReadWriteMany
27+
resources:
28+
requests:
29+
storage: 5Gi
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# Deploy RabbitMQ on Kubernetes with the Kubernetes Peer Discovery Plugin to Minikube
2+
3+
This is an **example** that demonstrates a RabbitMQ deployment on Kubernetes with peer discovery
4+
via `rabbitmq-peer-discovery-k8s` plugin.
5+
6+
## Production (Non-)Suitability
7+
8+
Some values in this example **may or may not be optimal for your deployment**. We encourage users
9+
to get familiar with the [RabbitMQ Peer Discovery guide](https://www.rabbitmq.com/cluster-formation.html), [RabbitMQ Production Checklist](https://www.rabbitmq.com/production-checklist.html)
10+
and the rest of [RabbitMQ documentation](https://www.rabbitmq.com/documentation.html) before going into production.
11+
12+
Having [metrics](https://www.rabbitmq.com/monitoring.html), both of RabbitMQ and applications that use it,
13+
is critically important when making informed decisions about production systems.
14+
15+
16+
## Pre-requisites
17+
18+
The example uses, targets or assumes:
19+
20+
* [Minikube](https://kubernetes.io/docs/setup/learning-environment/minikube/) with the [VirtualBox](https://www.virtualbox.org/) driver (other drivers can be used, too)
21+
* Kubernetes 1.6
22+
* RabbitMQ [Docker image](https://hub.docker.com/_/rabbitmq/) (maintained [by Docker, Inc](https://hub.docker.com/_/rabbitmq/))
23+
* A [StatefulSets controller](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/)
24+
25+
26+
## Quick Start with Make
27+
28+
This example comes with a Make target that sets up VirtualBox, Minikube and an example cluster
29+
in a single command. It can be found under this directory. [Homebrew](https://brew.sh/) will be used to install
30+
packages and on macOS, VirtualBox [will need OS permissions to install its kernel module](https://developer.apple.com/library/archive/technotes/tn2459/_index.html).
31+
32+
The Homebrew cask installer will ask for your password at some point with a prompt that looks like this:
33+
34+
```
35+
Changing ownership of paths required by virtualbox; your password may be necessary
36+
```
37+
38+
Please inspect the Make file to be extra sure that you understand and agree to what it does.
39+
After enabling 3rd party kernel extensions in OS setings, run the default Make target in this directory:
40+
41+
```
42+
make
43+
```
44+
45+
which is equivalent to first running
46+
47+
```
48+
make start-minikube
49+
```
50+
51+
to install VirtualBox and Minikube using Homebrew, then
52+
53+
```
54+
make run-in-minikube
55+
```
56+
57+
to start Minikube and `kubectl apply` the example, and finally
58+
59+
```
60+
make wait-for-rabbitmq
61+
```
62+
63+
to wait for cluster formation.
64+
65+
Once the changes are applied, follow the steps in the Check Cluster Status section below.
66+
67+
In case you would prefer to install and run Minikube manually, see the following few sections.
68+
69+
70+
## Running the Example Manually with Minikube
71+
72+
### Preresuites
73+
74+
* Make sure that VirtualBox is installed
75+
* Install [`minikube`](https://kubernetes.io/docs/tasks/tools/install-minikube/) and start it with `--vm-driver=virtualbox`
76+
* Install [`kubectl`](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
77+
78+
### Start Minikube
79+
80+
Start a `minikube` virtual machine:
81+
82+
``` sh
83+
minikube start --cpus=2 --memory=2040 --disk-size "10 GB" --vm-driver=virtualbox
84+
```
85+
86+
### Create a Namespace
87+
88+
Create a Kubernetes namespace for RabbitMQ tests:
89+
90+
``` sh
91+
kubectl create namespace test-rabbitmq
92+
```
93+
94+
### Set Up Kubernetes Permissions
95+
96+
In Kubernetes 1.6 or above, RBAC authorization is enabled by default.
97+
This example configures RBAC related bits so that the peer discovery plugin is allowed to access
98+
the nodes information it needs. The `ServiceAccount` and `Role` resources will be created
99+
in the following step.
100+
101+
### kubectl Apply Things
102+
103+
Deploy the config map, services, a stateful set and so on:
104+
105+
``` sh
106+
# will apply all files under this directory
107+
kubectl create -f minikube
108+
```
109+
110+
### Check Cluster Status
111+
112+
Wait for a a few minutes for pods to start. Since this example uses a stateful set with ordered
113+
startup, the pods will be started one by one. To monitor pod startup process, use
114+
115+
``` sh
116+
kubectl --namespace="test-rabbitmq" get pods
117+
```
118+
119+
To run `rabbitmq-diagnostics cluster_status`:
120+
121+
``` sh
122+
FIRST_POD=$(kubectl get pods --namespace test-rabbitmq -l 'app=rabbitmq' -o jsonpath='{.items[0].metadata.name }')
123+
kubectl exec --namespace=test-rabbitmq $FIRST_POD -- rabbitmq-diagnostics cluster_status
124+
```
125+
126+
to check cluster status. Note that nodes can take some time to start and discover each other.
127+
128+
The output should look something like this:
129+
130+
```
131+
Cluster status of node [email protected] ...
132+
Basics
133+
134+
Cluster name: [email protected]
135+
136+
Disk Nodes
137+
138+
139+
140+
141+
142+
Running Nodes
143+
144+
145+
146+
147+
148+
Versions
149+
150+
[email protected]: RabbitMQ 3.8.1 on Erlang 22.1.8
151+
[email protected]: RabbitMQ 3.8.1 on Erlang 22.1.8
152+
[email protected]: RabbitMQ 3.8.1 on Erlang 22.1.8
153+
154+
Alarms
155+
156+
(none)
157+
158+
Network Partitions
159+
160+
(none)
161+
162+
Listeners
163+
164+
Node: [email protected], interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
165+
Node: [email protected], interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
166+
Node: [email protected], interface: [::], port: 15672, protocol: http, purpose: HTTP API
167+
Node: [email protected], interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
168+
Node: [email protected], interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
169+
Node: [email protected], interface: [::], port: 15672, protocol: http, purpose: HTTP API
170+
171+
Feature flags
172+
173+
Flag: drop_unroutable_metric, state: enabled
174+
Flag: empty_basic_get_metric, state: enabled
175+
Flag: implicit_default_bindings, state: enabled
176+
Flag: quorum_queue, state: enabled
177+
Flag: virtual_host_metadata, state: enabled
178+
```
179+
180+
### Use Public Minikube IP Address to Connect
181+
182+
Get the public `minikube` VM IP address:
183+
184+
``` sh
185+
minikube ip
186+
# => 192.168.99.104
187+
```
188+
189+
The [ports used](https://www.rabbitmq.com/networking.html#ports) by this example are:
190+
191+
* `amqp://guest:guest@{minikube_ip}:30672`: [AMQP 0-9-1 and AMQP 1.0](https://www.rabbitmq.com/networking.html#ports) client connections
192+
* `http://{minikube_ip}:31672`: [HTTP API and management UI](https://www.rabbitmq.com/management.html)
193+
194+
195+
### Scaling the Number of RabbitMQ Cluster Nodes (Kubernetes Pod Replicas)
196+
197+
``` sh
198+
# Odd numbers of nodes are necessary for a clear quorum: 3, 5, 7 and so on
199+
kubectl scale statefulset/rabbitmq --namespace=test-rabbitmq --replicas=5
200+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: rabbitmq-config
5+
data:
6+
enabled_plugins: |
7+
[rabbitmq_management,rabbitmq_peer_discovery_k8s].
8+
9+
rabbitmq.conf: |
10+
## Cluster formation. See https://www.rabbitmq.com/cluster-formation.html to learn more.
11+
cluster_formation.peer_discovery_backend = rabbit_peer_discovery_k8s
12+
cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
13+
## Should RabbitMQ node name be computed from the pod's hostname or IP address?
14+
## IP addresses are not stable, so using [stable] hostnames is recommended when possible.
15+
## Set to "hostname" to use pod hostnames.
16+
## When this value is changed, so should the variable used to set the RABBITMQ_NODENAME
17+
## environment variable.
18+
cluster_formation.k8s.address_type = hostname
19+
## How often should node cleanup checks run?
20+
cluster_formation.node_cleanup.interval = 30
21+
## Set to false if automatic removal of unknown/absent nodes
22+
## is desired. This can be dangerous, see
23+
## * https://www.rabbitmq.com/cluster-formation.html#node-health-checks-and-cleanup
24+
## * https://groups.google.com/forum/#!msg/rabbitmq-users/wuOfzEywHXo/k8z_HWIkBgAJ
25+
cluster_formation.node_cleanup.only_log_warning = true
26+
cluster_partition_handling = autoheal
27+
## See https://www.rabbitmq.com/ha.html#master-migration-data-locality
28+
queue_master_locator=min-masters
29+
## This is just an example.
30+
## This enables remote access for the default user with well known credentials.
31+
## Consider deleting the default user and creating a separate user with a set of generated
32+
## credentials instead.
33+
## Learn more at https://www.rabbitmq.com/access-control.html#loopback-users
34+
loopback_users.guest = false
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
name: rabbitmq
6+
---
7+
kind: Role
8+
apiVersion: rbac.authorization.k8s.io/v1
9+
metadata:
10+
name: rabbitmq-peer-discovery-rbac
11+
rules:
12+
- apiGroups: [""]
13+
resources: ["endpoints"]
14+
verbs: ["get"]
15+
- apiGroups: [""]
16+
resources: ["events"]
17+
verbs: ["create"]
18+
---
19+
kind: RoleBinding
20+
apiVersion: rbac.authorization.k8s.io/v1
21+
metadata:
22+
name: rabbitmq-peer-discovery-rbac
23+
subjects:
24+
- kind: ServiceAccount
25+
name: rabbitmq
26+
roleRef:
27+
apiGroup: rbac.authorization.k8s.io
28+
kind: Role
29+
name: rabbitmq-peer-discovery-rbac
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
kind: Service
2+
apiVersion: v1
3+
metadata:
4+
name: rabbitmq
5+
labels:
6+
app: rabbitmq
7+
type: LoadBalancer
8+
spec:
9+
type: NodePort
10+
ports:
11+
- name: http
12+
protocol: TCP
13+
port: 15672
14+
targetPort: 15672
15+
nodePort: 31672
16+
- name: amqp
17+
protocol: TCP
18+
port: 5672
19+
targetPort: 5672
20+
nodePort: 30672
21+
selector:
22+
app: rabbitmq

0 commit comments

Comments
 (0)