In Rancher, you can create receiver hooks, which provides a URL that can be used to trigger an action inside of Rancher. For example, the receiver hooks can be integrated with an external monitoring systems to increase or decrease containers of a service. In API -> Webhooks, you can view and create new receiver hooks.
To create a receiver hook, navigate to API -> Webhooks. Click on Add Receiver.
Click on Create. After it’s created, the URL is provided next to the newly added receiver hook.
To use the trigger URL, you’ll need to do a POST
to the specific URL. There is no authentication or body needed to POST
to the URL.
For scaling a service, you must configure your webhook:
By using a receiver hook to scale services, you can implement autoscaling by integrating with external services. In our example, we’ll use Prometheus to monitor the services and Alertmanager to POST
to the URL.
Prometheus is offered through the Rancher Catalog and can be found under the Catalog. Select Prometheus and launch the catalog entry. Within the Prometheus stack, find the service called prometheus
, which is exposed on port 9090
. Exec into the container and go to /etc/prom-conf
. The prometheus configuration file, prometheus.yml
would be present there. In order to add alerts, create a separate file for alerts, and provide the path to this file in prometheus.yml
. For example if the alerts file you created is called rules.conf
, add it to prometheus.yml
at the end by adding these two lines:
rule_files:
- rules.conf
The file rules.conf
can have multiple alerts, following is an example of an alert
/etc/prom-conf/rules.conf
ALERT CpuUsageSpike
IF rate(container_cpu_user_seconds_total{container_label_io_rancher_container_name="Demo-testTarget-1"}[30s]) * 100 > 70
LABELS {
severity="critical",
action="up"
}
ANNOTATIONS {
summary = "ADDITIONAL CONTAINERS NEEDED",
description = "CPU usage is above 70%"
}
After the alerts have been added, restart the service.
In order to call the receiver hook, Alertmanager will need to be launched. You can add it to the Prometheus stack. Click on Add Service in the Prometheus stack. Use the prom/alertmanager
to add a service. Make sure to map port 9093:9093
when adding the service. After the service has started, exec into the container to update the etc/alertmanager/config.yml
. In the file, add the URL of the webhook so that it will send a POST
request to the URL when the alert is fired. After the file is updated with the URL information, restart the service.
etc/alertmanager/config.yml
route:
repeat_interval: 5h
routes:
- match:
action: up
receiver: "webhook-receiver-up"
- match:
action: down
receiver: "webhook-receiver-down"
receivers:
- name: "webhook-receiver-up"
webhook_configs:
- url: <WEBHOOK_URL>
send_resolved: true
- name: "webhook-receiver-down"
webhook_configs:
- url: <WEBHOOK_URL>
send_resolved: true
After Prometheus and Alertmanager have been updated with alerts and hooks, make sure the services were restarted in order to have the configurations updated and active. For the services that alerts have been added, the services will automatically be scaled up or down based on the receiver hook that was created.
Rancher is able to scale hosts by cloning an existing host, which were created through Rancher (i.e. Docker Machine). This means that hosts added using the custom command cannot be scaled.
Using labels on the host, hosts can be grouped together in a scaling group. We recommend using unique labels on hosts to help differentiate scaling groups. Any hosts with the same label, regardless of how the host was added into Rancher, will be counted as part of a scaling group. When creating the webhook, the label is not required to be on any host, but when using the webhook, there must be at least 1 host with the label so that the webhook would have a host to clone in order to scale up. When scaling up hosts, Rancher will use a clone-able host that has been in the scaling group the longest.
For scaling a host, you must configure your webhook:
Error
state will not be considered in the count for a scaling group.Inactive
, Deactivating
, Reconnecting
or Disconnected
) before starting to delete Active
hosts.Utilizing Docker Hub’s webhooks, you can add in Rancher’s receiver hook so that when a specific image has been pushed, a POST
will be sent to Rancher to trigger the receiver hook. Using this combination of webhooks, you can automate so that when a specific image:tag
is pushed in Docker Hub, any services using a matching versions will automatically get upgraded. To select the group of services to be upgraded, you would use a selector label that will pick up any services that contain the matching label. Labels should be added to a service when creating the service. If the label doesn’t exist, you will need to upgrade the service in Rancher to add the label for the webhook.
In order to upgrade a service, you must configure your webhook:
After the receiver hook is created, you need to use the Trigger URL in your your Docker Hub webhook. When the Docker Hub triggers its webhook, services picked up by the Rancher receiver hook will be upgraded. By default, the Rancher receiver hook expects specific information that the Docker Hub webhook provides. To use Rancher’s receiver hook with another webhook, the POST
would need to contain these fields:
{
"push_data": {
"tag": <pushedTag>
},
"repository": {
"repo_name": <image>
}
}