Skip to main content

KubraGen Builder: RabbitMQ

Project description

KubraGen Builder: RabbitMQ

PyPI version Supported Python versions

kg_rabbitmq is a builder for KubraGen that deploys a RabbitMQ server in Kubernetes.

KubraGen is a Kubernetes YAML generator library that makes it possible to generate configurations using the full power of the Python programming language.

Example

from kubragen import KubraGen
from kubragen.consts import PROVIDER_GOOGLE, PROVIDERSVC_GOOGLE_GKE
from kubragen.object import Object
from kubragen.option import OptionRoot
from kubragen.options import Options
from kubragen.output import OutputProject, OD_FileTemplate
from kubragen.outputimpl import OutputFile_ShellScript, OutputFile_Kubernetes, OutputDriver_Print
from kubragen.provider import Provider

from kg_rabbitmq import RabbitMQBuilder, RabbitMQOptions

kg = KubraGen(provider=Provider(PROVIDER_GOOGLE, PROVIDERSVC_GOOGLE_GKE), options=Options({
    'namespaces': {
        'mon': 'app-monitoring',
    },
}))

out = OutputProject(kg)

shell_script = OutputFile_ShellScript('create_gke.sh')
out.append(shell_script)

shell_script.append('set -e')

#
# OUTPUTFILE: app-namespace.yaml
#
file = OutputFile_Kubernetes('app-namespace.yaml')

file.append([
    Object({
        'apiVersion': 'v1',
        'kind': 'Namespace',
        'metadata': {
            'name': 'app-monitoring',
        },
    }, name='ns-monitoring', source='app', instance='app')
])

out.append(file)
shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))

shell_script.append(f'kubectl config set-context --current --namespace=app-monitoring')

#
# SETUP: rabbitmq
#
rabbit_config = RabbitMQBuilder(kubragen=kg, options=RabbitMQOptions({
    'namespace': OptionRoot('namespaces.mon'),
    'basename': 'myrabbit',
    'config': {
        'erlang_cookie': 'my-secret-cookie',
    },
    'kubernetes': {
        'volumes': {
            'data': {
                'persistentVolumeClaim': {
                    'claimName': 'rabbitmq-storage-claim'
                }
            }
        },
        'resources': {
            'statefulset': {
                'requests': {
                    'cpu': '150m',
                    'memory': '300Mi'
                },
                'limits': {
                    'cpu': '300m',
                    'memory': '450Mi'
                },
            },
        },
    }
}))

rabbit_config.ensure_build_names(rabbit_config.BUILD_ACCESSCONTROL, rabbit_config.BUILD_CONFIG,
                                 rabbit_config.BUILD_SERVICE)

#
# OUTPUTFILE: rabbitmq-config.yaml
#
file = OutputFile_Kubernetes('rabbitmq-config.yaml')
out.append(file)

file.append(rabbit_config.build(rabbit_config.BUILD_ACCESSCONTROL, rabbit_config.BUILD_CONFIG))

shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))

#
# OUTPUTFILE: rabbitmq.yaml
#
file = OutputFile_Kubernetes('rabbitmq.yaml')
out.append(file)

file.append(rabbit_config.build(rabbit_config.BUILD_SERVICE))

shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))

#
# Write files
#
out.output(OutputDriver_Print())
# out.output(OutputDriver_Directory('/tmp/build-gke'))

Output:

****** BEGIN FILE: 001-app-namespace.yaml ********
apiVersion: v1
kind: Namespace
metadata:
  name: app-monitoring

****** END FILE: 001-app-namespace.yaml ********
****** BEGIN FILE: 002-rabbitmq-config.yaml ********
apiVersion: v1
kind: ServiceAccount
metadata:
  name: myrabbit
  namespace: app-monitoring
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: myrabbit
  namespace: app-monitoring
rules:
- apiGroups: ['']
  resources: [endpoints]
  verbs: [get]
- apiGroups: ['']
  resources: [events]
  verbs: [create]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: myrabbit
  namespace: app-monitoring
subjects:
- kind: ServiceAccount
  name: myrabbit
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: myrabbit
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: myrabbit-config
  namespace: app-monitoring
data:
  enabled_plugins: |-
    [rabbitmq_peer_discovery_k8s, rabbitmq_management, rabbitmq_prometheus].
  rabbitmq.conf: |-
    log.console.level = info
    cluster_formation.peer_discovery_backend = k8s
    cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
    cluster_formation.k8s.address_type = hostname
    cluster_formation.k8s.service_name = myrabbit-headless
    queue_master_locator=min-masters
---
apiVersion: v1
kind: Secret
metadata:
  name: myrabbit-config-secret
  namespace: app-monitoring
type: Opaque
data:
  erlang_cookie: bXktc2VjcmV0LWNvb2tpZQ==

****** END FILE: 002-rabbitmq-config.yaml ********
****** BEGIN FILE: 003-rabbitmq.yaml ********
apiVersion: v1
kind: Service
metadata:
  name: myrabbit-headless
  namespace: app-monitoring
spec:
  clusterIP: None
  ports:
  - name: epmd
    port: 4369
    protocol: TCP
    targetPort: 4369
  - name: cluster-links
    port: 25672
    protocol: TCP
    targetPort: 25672
  selector:
    app: myrabbit
  type: ClusterIP
  sessionAffinity: None
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: myrabbit
  namespace: app-monitoring
  labels:
    app: myrabbit
spec:
  selector:
    matchLabels:
      app: myrabbit
  serviceName: myrabbit-headless
  replicas: 1
  template:
    metadata:
      name: myrabbit
      namespace: app-monitoring
      labels:
        app: myrabbit
    spec:
      initContainers:
      - name: rabbitmq-config
        image: busybox:1.32.0
        securityContext:
          runAsUser: 0
          runAsGroup: 0
        volumeMounts:
        - name: rabbitmq-config
          mountPath: /tmp/rabbitmq
        - name: rabbitmq-config-rw
          mountPath: /etc/rabbitmq
        - name: rabbitmq-config-erlang-cookie
          mountPath: /tmp/rabbitmq-cookie
        command: [sh, -c, cp /tmp/rabbitmq/rabbitmq.conf /etc/rabbitmq/rabbitmq.conf
            && echo '' >> /etc/rabbitmq/rabbitmq.conf; cp /tmp/rabbitmq/enabled_plugins
            /etc/rabbitmq/enabled_plugins; mkdir -p /var/lib/rabbitmq; cp /tmp/rabbitmq-cookie/erlang_cookie
            /var/lib/rabbitmq/.erlang.cookie; chmod 600 /var/lib/rabbitmq/.erlang.cookie;
            chown 999.999 /etc/rabbitmq/rabbitmq.conf /etc/rabbitmq/enabled_plugins
            /var/lib/rabbitmq /var/lib/rabbitmq/.erlang.cookie]
      volumes:
      - name: rabbitmq-config
        configMap:
          name: myrabbit-config
          optional: false
          items:
          - key: enabled_plugins
            path: enabled_plugins
          - key: rabbitmq.conf
            path: rabbitmq.conf
      - name: rabbitmq-config-rw
        emptyDir: {}
      - name: rabbitmq-config-erlang-cookie
        secret:
          secretName: myrabbit-config-secret
          items:
          - key: erlang_cookie
            path: erlang_cookie
      - name: rabbitmq-data
        persistentVolumeClaim:
          claimName: rabbitmq-storage-claim
      serviceAccountName: myrabbit
      securityContext:
        fsGroup: 999
        runAsUser: 999
        runAsGroup: 999
      containers:
      - name: rabbitmq
        image: rabbitmq:3.8.9-alpine
        volumeMounts:
        - name: rabbitmq-config-rw
          mountPath: /etc/rabbitmq
        - name: rabbitmq-data
          mountPath: /var/lib/rabbitmq/mnesia
        ports:
        - name: amqp
          containerPort: 5672
          protocol: TCP
        - name: management
          containerPort: 15672
          protocol: TCP
        - name: prometheus
          containerPort: 15692
          protocol: TCP
        - name: epmd
          containerPort: 4369
          protocol: TCP
        livenessProbe:
          exec:
            command: [rabbitmq-diagnostics, status]
          initialDelaySeconds: 60
          periodSeconds: 60
          timeoutSeconds: 15
        readinessProbe:
          exec:
            command: [rabbitmq-diagnostics, ping]
          initialDelaySeconds: 20
          periodSeconds: 60
          timeoutSeconds: 10
        resources:
          requests:
            cpu: 150m
            memory: 300Mi
          limits:
            cpu: 300m
            memory: 450Mi
---
kind: Service
apiVersion: v1
metadata:
  name: myrabbit
  namespace: app-monitoring
  labels:
    app: myrabbit
spec:
  type: ClusterIP
  ports:
  - name: http
    protocol: TCP
    port: 15672
  - name: prometheus
    protocol: TCP
    port: 15692
  - name: amqp
    protocol: TCP
    port: 5672
  selector:
    app: myrabbit

****** END FILE: 003-rabbitmq.yaml ********
****** BEGIN FILE: create_gke.sh ********
#!/bin/bash

set -e
kubectl apply -f 001-app-namespace.yaml
kubectl config set-context --current --namespace=app-monitoring
kubectl apply -f 002-rabbitmq-config.yaml
kubectl apply -f 003-rabbitmq.yaml

****** END FILE: create_gke.sh ********

Credits

based on

rabbitmq/diy-kubernetes-examples

Author

Rangel Reale (rangelspam@gmail.com)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kg_rabbitmq-0.7.7.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

kg_rabbitmq-0.7.7-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file kg_rabbitmq-0.7.7.tar.gz.

File metadata

  • Download URL: kg_rabbitmq-0.7.7.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for kg_rabbitmq-0.7.7.tar.gz
Algorithm Hash digest
SHA256 e947a619932771fe2340c63953c457059f87c4705e3d3ec8b482c5978e1bcddf
MD5 097d37ee68e82c58a953dd12e35337b4
BLAKE2b-256 8286d60ee5d0e50750c80ee2253fafa34862c7d743ef83f1ee21463eceb7a5f7

See more details on using hashes here.

File details

Details for the file kg_rabbitmq-0.7.7-py3-none-any.whl.

File metadata

  • Download URL: kg_rabbitmq-0.7.7-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for kg_rabbitmq-0.7.7-py3-none-any.whl
Algorithm Hash digest
SHA256 28f9e1f7d0ebdb51f75345b6195b092e626facd5f65f423d0a1c855a8f177da2
MD5 8ba47ba004d0e40c15e139e4d67aa006
BLAKE2b-256 6d3b8a4eb09a842c0d303e04afaf55cb4de510da84302c362d434a95943eb9dd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page