apiVersion: apps/v1
kind: Deployment
metadata:
  name: rabbitmq
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: rabbitmq
    app.kubernetes.io/component: message-broker
    app.kubernetes.io/part-of: bakery-ia
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: rabbitmq
      app.kubernetes.io/component: message-broker
  template:
    metadata:
      labels:
        app.kubernetes.io/name: rabbitmq
        app.kubernetes.io/component: message-broker
    spec:
      containers:
      - name: rabbitmq
        image: rabbitmq:4.1-management-alpine
        ports:
        - containerPort: 5672
          name: amqp
        - containerPort: 15672
          name: management
        env:
        - name: RABBITMQ_DEFAULT_USER
          valueFrom:
            secretKeyRef:
              name: rabbitmq-secrets
              key: RABBITMQ_USER
        - name: RABBITMQ_DEFAULT_PASS
          valueFrom:
            secretKeyRef:
              name: rabbitmq-secrets
              key: RABBITMQ_PASSWORD
        - name: RABBITMQ_DEFAULT_VHOST
          valueFrom:
            configMapKeyRef:
              name: bakery-config
              key: RABBITMQ_VHOST
        volumeMounts:
        - name: rabbitmq-data
          mountPath: /var/lib/rabbitmq
        resources:
          requests:
            memory: "512Mi"
            cpu: "200m"
          limits:
            memory: "1Gi"
            cpu: "1000m"
        livenessProbe:
          exec:
            command:
            - timeout
            - "5"
            - bash
            - -c
            - "</dev/tcp/localhost/5672 && rabbitmq-diagnostics ping"
          initialDelaySeconds: 40
          timeoutSeconds: 10
          periodSeconds: 30
          failureThreshold: 5
        readinessProbe:
          exec:
            command:
            - timeout
            - "5"
            - bash
            - -c
            - "</dev/tcp/localhost/5672 && rabbitmq-diagnostics ping"
          initialDelaySeconds: 20
          timeoutSeconds: 5
          periodSeconds: 10
          failureThreshold: 3
      volumes:
      - name: rabbitmq-data
        persistentVolumeClaim:
          claimName: rabbitmq-pvc

---
apiVersion: v1
kind: Service
metadata:
  name: rabbitmq-service
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: rabbitmq
    app.kubernetes.io/component: message-broker
spec:
  type: ClusterIP
  ports:
  - port: 5672
    targetPort: 5672
    protocol: TCP
    name: amqp
  - port: 15672
    targetPort: 15672
    protocol: TCP
    name: management
  selector:
    app.kubernetes.io/name: rabbitmq
    app.kubernetes.io/component: message-broker

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: rabbitmq-pvc
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: rabbitmq
    app.kubernetes.io/component: message-broker
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi