Giter Club home page Giter Club logo

docker-kube-amqp-autoscale's Introduction

KUBE-AMQP-AUTOSCALE Dockerfile

Dynamically scale kubernetes resources using length of an AMQP queue (number of messages available for retrieval from the queue) to determine the load on an application/Kubernetes pod.

NOTICE If your application load is not queue-bound but rather CPU-sensitive, make sure to use built-in Kubernetes Horizontal Pod Autoscaling instead of this project.

This repository contains Dockerfile of KUBE-AMQP-AUTOSCALE for Docker's automated build published to the public Docker Hub Registry.

Base Docker Image

Installation

  1. Install Docker.

  2. Download automated build from public Docker Hub Registry: docker pull mbogus/kube-amqp-autoscale

    (alternatively, you can build an image from Dockerfile: docker build -t="mbogus/kube-amqp-autoscale" github.com/mbogus/docker-kube-amqp-autoscale)

Usage

Run autoscale

docker run -d -e AUTOSCALE_NAME=pod_to_scale -e AUTOSCALE_THRESHOLD=50 -e AUTOSCALE_MAX=10 -e RABBITMQ_URI=amqp://guest:[email protected]:5672// -e RABBITMQ_QUEUE=queue_to_watch -e KUBERNETES_SERVICE_URL=http://127.0.0.1:8080 mbogus/kube-amqp-autoscale

Run autoscale w/ persistent shared directories.

docker run -d -v <db-dir>:/data/db -v <conf-dir>:/etc/default -e AUTOSCALE_NAME=pod_to_scale -e AUTOSCALE_THRESHOLD=50 -e AUTOSCALE_MAX=10 -e RABBITMQ_URI=amqp://guest:[email protected]:5672// -e RABBITMQ_QUEUE=queue_to_watch -e KUBERNETES_SERVICE_URL=http://127.0.0.1:8080 mbogus/kube-amqp-autoscale

Configuration environment variables:

Autoscale service:

  • AUTOSCALE_NS Kubernetes namespace (default default)
  • AUTOSCALE_KIND type of the Kubernetes resource to autoscale, one of Deployment, ReplicationController, ReplicaSet (default Deployment)
  • AUTOSCALE_NAME name of the Kubernetes resource to autoscale
  • AUTOSCALE_THRESHOLD number of messages on a queue representing maximum load on the autoscaled Kubernetes resource
  • AUTOSCALE_MIN lower limit for the number of replicas for a Kubernetes pod that can be set by the autoscaler (default 1)
  • AUTOSCALE_MAX upper limit for the number of replicate for a Kubernetes pod that can be set by the autoscaler
  • AUTOSCALE_INTERVAL time interval between Kubernetes resource scale runs in secs (default 30)
  • AUTOSCALE_INCREASE_LIMIT limit number of Kubernetes pods to be provisioned in a single scale iteration to max of the value, set to a number greater than 0, default unbounded
  • AUTOSCALE_DECREASE_LIMIT limit number of Kubernetes pods to be terminated in a single scale iteration to max of the value, set to a number greater than 0, default unbounded

Autoscale statistics

  • AUTOSCALE_STATS_COVERAGE required percentage of statistics to calculate average queue length (default 0.75)
  • AUTOSCALE_STATS_INTERVAL time interval between metrics gathering runs in seconds (default 5)
  • AUTOSCALE_EVAL_INTERVALS number of autoscale intervals used to calculate average queue length (default 2)
  • AUTOSCALE_DB sqlite3 database filename for storing queue length statistics (default :memory:)

RabbitMQ broker and queue definitions

Recommendations:

  • if RabbitMQ is deployed externally (not part of the Kubernetes cluster), use complete URI to locate the broker: RABBITMQ_URI
  • if RabbitMQ is part of the cluster and DNS service has been configured, use DNS name: RABBITMQ_HOST
  • if RabbitMQ is part of the cluster but DNS service has not been configured, try using Kubernetes service name for the broker: KUBERNETES_RABBITMQ_SERVICE_NAME
  • whenever you can, stick to defaults

Variables:

  • RABBITMQ_URI required, RabbitMQ broker URI, e.g. amqp://guest:[email protected]:5672// or:

  • RABBITMQ_HOST RabbitMQ broker hostname (default 127.0.0.1)

  • RABBITMQ_PORT port number (default 5672)

  • KUBERNETES_RABBITMQ_SERVICE_NAME name of Kubernetes service exposing RabbitMQ broker

  • RABBITMQ_PROTO use amqps for secure connections (default amqp)

  • RABBITMQ_USER user name (default guest)

  • RABBITMQ_PASS password (default guest)

  • RABBITMQ_VHOST virtual host (default /)

  • RABBITMQ_QUEUE RabbitMQ queue name to measure load on an application

Kubernetes cluster access

  • KUBERNETES_SERVICE_URL Kubernetes API URL, e.g. http://127.0.0.1:8080 or

  • KUBERNETES_SERVICE_PROTO (default https)

  • KUBERNETES_SERVICE_HOST (default 127.0.0.1)

  • KUBERNETES_SERVICE_PORT (default 443)

  • KUBERNETES_SERVICE_INSECURE Set to true for connecting to Kubernetes API without verifying TLS certificate; unsafe, use for development only (default false)

Basic authentication

  • KUBERNETES_SERVICE_USERNAME username for basic authentication on Kubernetes API
  • KUBERNETES_SERVICE_PASSWORD password for basic authentication on Kubernetes API

OAuth2 authentication (inside Kubernetes pod)

Path to a bearer token file for OAuth authentication, on a Kubernetes pod /var/run/secrets/kubernetes.io/serviceaccount/token

TSL certificate (inside Kubernetes pod)

Path to CA certificate file for HTTPS connections to Kubernetes API from within a cluster /var/run/secrets/kubernetes.io/serviceaccount/ca.crt

Sample Kubernetes deployment file

An all-in-one deployment that comprises of a RabbitMQ broker (rabbitmq-broker), a worker node (echo-node) to be scaled according to number of unacknowledged messages on EchoQueue queue and the autoscaler deployed as autoscaler adding one node for each waiting message on the queue capped at 5.

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: autoscale-example
    component: broker
  name: rabbitmq-broker
spec:
  ports:
  - port: 5672
    name: main-port
    nodePort: 30672
  - port: 15672
    name: admin-port
    nodePort: 30080
  type: NodePort
  selector:
    app: autoscale-example
    component: broker
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: rabbitmq-broker
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: autoscale-example
        component: broker
    spec:
      containers:
      - name: rabbitmq
        image: rabbitmq:3-management
        imagePullPolicy: Always
        env:
        - name: RABBITMQ_HIPE_COMPILE
          value: "1"
        resources:
          requests:
            cpu: 200m
            memory: 200Mi
          limits:
            cpu: 500m
            memory: 500Mi
        ports:
        - containerPort: 5672
          hostPort: 5672
        - containerPort: 15672
          hostPort: 15672
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: echo-node
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: autoscale-example
        component: worker
    spec:
      containers:
      - name: echo
        image: mbogus/amqp-echo
        imagePullPolicy: Always
        env:
        - name: KUBERNETES_RABBITMQ_SERVICE_NAME
          value: RABBITMQ_BROKER
        - name: RABBITMQ_QUEUE
          value: EchoQueue
        - name: ECHO_DELAY
          value: "15"
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 200m
            memory: 200Mi
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: autoscaler
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: autoscale-example
        component: scaler
    spec:
      containers:
      - name: autoscale
        image: mbogus/kube-amqp-autoscale
        imagePullPolicy: Always
        env:
        - name: AUTOSCALE_NAME
          value: echo-node
        - name: AUTOSCALE_THRESHOLD
          value: "1"
        - name: AUTOSCALE_MAX
          value: "5"
        - name: RABBITMQ_QUEUE
          value: EchoQueue
        - name: KUBERNETES_RABBITMQ_SERVICE_NAME
          value: RABBITMQ_BROKER
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 200m
            memory: 200Mi

docker-kube-amqp-autoscale's People

Contributors

mavimo avatar mbogus avatar robsonpeixoto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

docker-kube-amqp-autoscale's Issues

Failed to get queue length for queue

I am getting following error -

Failed to get queue length for queue example: parse 'amqp://username:password@hostname:5672//': first path segment in URL cannot contain colon

How to resolve the issue?? Any inputs ???

Errors running Dockerfile with Kubernetes

I am having trouble using this docker image so thought I'd post some of the errors that I can't quite understand.

  1. Metrics
    I first get this error:
    not enough metrics to calculate new size, required at least 0.75 was 0.00 metrics ratio
    Then it changed to:
    not enough metrics to calculate new size, required at least 0.75 was 0.42 metrics ratio
    My question is: what is metrics referring to? No. of items in the queue?

  2. ServiceAccount
    deployments.apps "<name-of-app-being-scaled" is forbidden: User "system:serviceaccount:default:default" cannot get deployments.apps in the namespace "default"
    ---> Is this an error due to 'roles' possible?

  3. Dial TCP i/o timeout
    Sometimes I get:
    Failed to get queue length for queue : dial tcp :5672: i/o timeout

I want to be able to create replicas of pods based on no. of msgs in the queue.

So, essentially, no. of msgs in queue == no. of replicas of

Kindly give me some direction as to what could be the problem?

Usage with redis cluster

Hi there,

first: thanks for that cool feature; works very well.
Is there a way to pass along multiple IPs in RABBITMQ_HOST. We are using a cluster and want to guarantee availability.

Kind regards,
kuche

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.