amazon-archives / k8s-cloudwatch-adapter

An implementation of Kubernetes Custom Metrics API for Amazon CloudWatch
Apache License 2.0
158 stars 98 forks source link

AWS ELB CloudWatch metrics - ELB name settings #17

Open dsumsky opened 4 years ago

dsumsky commented 4 years ago

Let's have a k8s service of LoadBalancer type running on k8s cluster on AWS. It's name can be my-nginx. Now, I would like to define an external metric based on some of the load balancer CloudWatch metrics, e.g. SurgeQueueLength, RequestCount etc. In order to get this working I need to hardcode the load balancer name in the resource configuration:

apiVersion: metrics.aws/v1alpha1
kind: ExternalMetric
metadata:
  name: elb-my-nginx-requests
spec:
  name: elb-my-nginx-requests
  resource:
    resource: "deployment"
  queries:
    - id: elb_my_nginx_requests
      metricStat:
        metric:
          namespace: "AWS/ELB"
          metricName: "RequestCount"
          dimensions:
            - name: LoadBalancerName
              value: "xyzxyzxyzxyz"
        period: 60
        stat: Sum
        unit: Count
      returnData: true

k8s service:

NAME         TYPE           CLUSTER-IP     EXTERNAL-IP                                                               PORT(S)        AGE
my-nginx     LoadBalancer   10.100.32.68   xyzxyzxyzxyz.us-west-2.elb.amazonaws.com   80:30099/TCP   85m

Would it be possible to get the name dynamically from the service itself?

costimuraru commented 4 years ago

We're looking at the same problem, @dsumsky. It would be nice if we could setup a pretty name to the ELB, but it's not possible to do that now [1].

Would it be possible to get the name dynamically from the service itself?

We had to setup this in two steps:

  1. Create the Service kubernetes resource
  2. Wait for the resource to be created and the ELB to be spawned up
  3. Retrieve the unique id: ['manifest']['status']['loadBalancer']['ingress'][0]['hostname'].split('-')[0] (we're using Spinnaker [2] to do this)
  4. Generate the ExternalMetric resource and populate it with the aforementioned value (we generated a simple helm chart for this resource).

[1] https://github.com/kubernetes/kubernetes/issues/29789 [2] https://spinnaker.io/