Notes

Surveiller le nombre d’événements dans une file d’attente Redis sous Kubernetes et AWS CloudWatch

En gros, on créé un conteneur avec Redis et aws cli, qui va lire le nombre d’événements et envoyer l’info en tant que métrique personnalisée à CloudWatch. Ensuite on peut créer une alarme qui sera déclenchée à partir d’un certain seuil et comme action possible augmenter le nombre d’instances dans un groupe asg …

Commençons par construire une image Docker minimale:

FROM ubuntu:22.04

RUN  apt-get update; DEBIAN_FRONTEND=noninteractive apt-get install wget curl jq zip unzip -y \
     && curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \
     && unzip awscliv2.zip && bash ./aws/install \
     && apt autoclean \
     && apt autoremove

RUN apt-get install redis less -y

Ceci est le Dockerfile

docker build -t xxx.dkr.ecr.eu-central-1.amazonaws.com/docker-images:queue-monitor-v1 -f Dockerfile .
docker push xxx.dkr.ecr.eu-central-1.amazonaws.com/docker-images:queue-monitor-v1

Ensuite notre manifest pour k8s:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: queue-monitor-prod
  namespace: queue-monitor
  annotations:
    reloader.stakater.com/auto: "true"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: queue-monitor-prod
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: queue-monitor-prod
    spec:
      terminationGracePeriodSeconds: 0
      containers:
      - name: app
        securityContext:
          runAsUser: 0
          runAsGroup: 0
        command: [ "/bin/bash", "-c" ]
        args: [ "/etc/queue-monitor/startup.sh" ]
        image: xxx.dkr.ecr.eu-central-1.amazonaws.com/docker-images:queue-monitor-v1 
        imagePullPolicy: IfNotPresent
        lifecycle:
          postStart:
            exec:
              command: ["/bin/sh", "-c", "cp -a /etc/.aws /root/.aws"]
        env:
        - name: AWS_PROFILE
          value: prod
        resources:
          limits:
            cpu: 1
            memory: 1Gi
          requests:
            cpu: 1m
            memory: 1Mi
        livenessProbe:
          exec:
            command:
            - /bin/bash
            - -c
            - /etc/queue-monitor/health.sh
          failureThreshold: 1
          initialDelaySeconds: 5
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        readinessProbe:
          exec:
            command:
            - /bin/bash
            - -c
            - /etc/queue-monitor/health.sh
          initialDelaySeconds: 3
          periodSeconds: 5
          successThreshold: 1
          timeoutSeconds: 1
        volumeMounts:
        - name: config
          mountPath: "/etc/queue-monitor"
        - name: awsconfig
          mountPath: "/etc/.aws"
      volumes:
      - name: config
        projected:
          defaultMode: 0755
          sources:
          - configMap:
              name: queue-monitor-config-prod
      - name: awsconfig
        secret:
          secretName: queue-monitor-sec
          items:
          - key: credentials
            path: credentials
            mode: 0600
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: queue-monitor-config-prod
  namespace: queue-monitor
data:
  health.sh: |
    exit 0

  startup.sh: |
    #!/bin/bash
    #
    #loop starting

    while true
    do

    queue=$(redis-cli -h cluster-queue.xxxxxxxx.0001.euc1.cache.amazonaws.com LLEN queues:default | awk '{print $1}')
    TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")
    aws cloudwatch put-metric-data --metric-name queue-monitoring-prod --namespace queue-monitoring --value "$queue" --timestamp "$TIMESTAMP"

    sleep 15

    done

Je vous laisse personnaliser le vôtre, ce setup n’est pas parfait car a dû être machiné en une dizaine de minutes seulement.

Leave a Reply

Your email address will not be published. Required fields are marked *