Skip to content

Commit f468f9d

Browse files
author
charlyF
committed
adding readme disclaimer, intro, cluster-agent and hpa
1 parent 7254ea2 commit f468f9d

File tree

4 files changed

+158
-54
lines changed

4 files changed

+158
-54
lines changed

03-path-application-development/305-app-scaling-custom-metrics/readme.adoc

Lines changed: 108 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,91 +12,145 @@ The custom and external metric providers, as opposed to the metrics server, are
1212

1313
== Prerequisites
1414

15-
Running Kubernetes >v1.10 in order to be able to register the External Metrics Provider resource against the API Server.
16-
Having the Aggregation layer enabled, refer to the Kubernetes aggregation layer configuration documentation to learn how to enable it.
15+
Running Kubernetes v1.10+ in order to be able to register the External Metrics Provider resource against the API Server.
16+
Having the Aggregation layer enabled, refer to the https://kubernetes.io/docs/tasks/access-kubernetes-api/configure-aggregation-layer/[Kubernetes aggregation layer] configuration documentation to learn how to enable it.
17+
Using EKS 2.0, this will be automatically enabled for you.
1718

18-
== Deploy an application
19+
This section of the workshop should be done a posteriori of 207-cluster-monitoring-with-datadog, so you can benefit from the applications in place to generate load and simulate the autoscaling.
1920

20-
In this step, we deploy a simple Go web application and constrain the CPU resources just for the purposes of this test.
21+
== Walkthrough
2122

22-
$ kubectl run webapp --image=trevorrobertsjr/webapp --requests=cpu=50m --expose --port=8080
23-
service "webapp" created
24-
deployment "webapp" created
23+
Autoscaling over External Metrics does not require the Node Agent to be running, you only need the metrics to be available in your Datadog account.
24+
Nevertheless, for this walkthrough, we autoscale an NGINX Deployment based off of NGINX metrics, collected by a Node Agent.
2525

26-
It also publishes the service at port 8080.
26+
Before proceeding, please make sure you went through the section 207 of this workshop.
27+
This entails tat you have Node Agents running with the Autodiscovery process enabled and functional.
2728

28-
== Horizontal Pod Autoscaler configuration
29+
In order to autoscale in Kubernetes, you need to register a Custom Metrics Server - The Datadog Cluster Agent implements this feature.
2930

30-
Now that our application is running, we create a Horizonal Pod Autoscaler for our webapp deployment.
31+
=== Spinning up the Datadog Cluster Agent
3132

32-
$ kubectl autoscale deployment webapp --cpu-percent=10 --min=1 --max=10
33-
deployment "webapp" autoscaled
33+
Start by creating the appropriate RBAC rules. Allowing the cluster agent to watch and parse Horizontal Pod Autoscalers as well as cluster level metadata.
3434

35-
This command will mainain between 1 and 10 replicas of the pod. The autoscaler will increase or decrease the number of replicas to maintain average CPU utilization of 10% across all the pods.
35+
```
36+
kubectl apply -f templates/cluster-agent/rbac/rbac-cluster-agent.yaml
37+
38+
clusterrole.rbac.authorization.k8s.io "dca" created
39+
clusterrolebinding.rbac.authorization.k8s.io "dca" created
40+
serviceaccount "dca" created
41+
```
42+
43+
Add your <API_KEY> and <APP_KEY> in the link:../305-app-scaling-custom-metrics/templates/cluster-agent/cluster-agent.yaml[Deployment manifest of the Datadog Cluster Agent].
44+
Then enable the HPA Processing by setting the `DD_EXTERNAL_METRICS_PROVIDER_ENABLED` variable to true.
45+
Finally, spin up the resources:
46+
47+
```
48+
kubectl apply -f templates/cluster-agent/datadog-cluster-agent_service.yaml
49+
kubectl apply -f templates/cluster-agent/hpa-example/cluster-agent-hpa-svc.yaml
50+
kubectl apply -f templates/cluster-agent/cluster-agent.yaml
51+
```
52+
53+
Note that the first service is used for the communication between the Node Agents and the Datadog Cluster Agent, but the second is used by Kubernetes to register the External Metrics Provider.
54+
55+
At this point you should be having:
56+
57+
Pods:
58+
```
59+
NAMESPACE NAME READY STATUS RESTARTS AGE
60+
default datadog-cluster-agent-7b7f6d5547-cmdtc 1/1 Running 0 28m
61+
```
62+
Services:
63+
```
64+
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
65+
default datadog-custom-metrics-server ClusterIP 192.168.254.87 <none> 443/TCP 28m
66+
default datadog-cluster-agent ClusterIP 192.168.254.197 <none> 5005/TCP 28m
67+
```
68+
69+
=== Register the External Metrics Provider
70+
71+
Once the Datadog Cluster Agent is up and running, register it as an External Metrics Provider, via the service exposing the port 443.
3672

37-
== Generate load
73+
Apply the following RBAC rules:
3874

39-
The simplest method to do this would be to access the application in an infinite loop similar to the example in the Kubernetes Horizonal Pod Autoscaler documentation:
75+
```
76+
kubectl apply -f templates/hpa-example/rbac-hpa.yaml
4077

41-
First, deploy a busybox container, label it `load-generator` and attach to it's prompt:
78+
clusterrolebinding.rbac.authorization.k8s.io "system:auth-delegator" created
79+
rolebinding.rbac.authorization.k8s.io "dca" created
80+
apiservice.apiregistration.k8s.io "v1beta1.external.metrics.k8s.io" created
81+
clusterrole.rbac.authorization.k8s.io "external-metrics-reader" created
82+
clusterrolebinding.rbac.authorization.k8s.io "external-metrics-reader" created
83+
```
4284

43-
$ kubectl run -i --tty load-generator --image=busybox /bin/sh
85+
Once you have the Datadog Cluster Agent running and the service registered, create an HPA manifest.
86+
The Datadog Cluster Agent will subsequently parse the manifest and pull metrics from Datadog.
4487

45-
At the `load-generator` command prompt, run a continuous request of the webapp
88+
=== Running the HPA
4689

47-
$ while true; do wget -q -O- http://webapp.default.svc.cluster.local:8080; done
90+
At this point, you should be seeing:
4891

49-
If for any reason you get disconnected from the load-generator container, you can re-attach to it with the following command.
92+
Pods:
93+
```
94+
NAMESPACE NAME READY STATUS RESTARTS AGE
95+
default datadog-agent-4c5pp 1/1 Running 0 14m
96+
default datadog-agent-ww2da 1/1 Running 0 14m
97+
default datadog-agent-2qqd3 1/1 Running 0 14m
98+
[…]
99+
default datadog-cluster-agent-7b7f6d5547-cmdtc 1/1 Running 0 16m
100+
```
50101

51-
$ kubectl attach $(kubectl get pod | grep load | awk '{print $1}') -c load-generator -i -t
102+
Now is time to create a Horizontal Pod Autoscaler manifest. If you take a look at the link:../305-app-scaling-custom-metrics/templates/cluster-agent/hpa-manifest.yaml[hpa-manifest.yaml file], you should see:
52103

53-
In a different terminal window, check the status of the Horizontal Pod Autoscaler.
104+
* The HPA is configured to autoscale the Deployment called nginx
105+
* The maximum number of replicas created is 5 and the minimum is 1
106+
* The metric used is nginx.net.request_per_s and the scope is kube_container_name: nginx. Note that this metric format corresponds to the Datadog one.
54107

55-
$ kubectl get hpa -w
108+
Every 30 seconds (this can be configured) Kubernetes queries the Datadog Cluster Agent to get the value of this metric and autoscales proportionally if necessary. For advanced use cases, it is possible to have several metrics in the same HPA, as you can see in the https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-multiple-metrics[Kubernetes horizontal pod autoscale documentation] the largest of the proposed value will be the one chosen.
56109

57-
You will see output similar to the following over successive queries of the hpa resource:
110+
We will be relying on the nginx deployment used in the section 207 of this workshop.
111+
Make sure that everything is still running:
58112

59-
$ kubectl get hpa -w
60-
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
61-
webapp Deployment/webapp 0% / 10% 1 10 1 6m
62-
webapp Deployment/webapp 62% / 10% 1 10 1 7m
63-
webapp Deployment/webapp 62% / 10% 1 10 4 7m
64-
webapp Deployment/webapp 112% / 10% 1 10 4 8m
65-
webapp Deployment/webapp 112% / 10% 1 10 4 8m
66-
webapp Deployment/webapp 53% / 10% 1 10 4 9m
113+
```
114+
kubectl get deploy, po -lapp=nginx
67115

116+
```
68117

69-
Notice that, eventually, the value in the `REPLICAS` column will increase as the load generator continues to run.
70118

71-
== Stop load
119+
Then, apply the HPA manifest.
72120

73-
In the terminal window that is running the load generator, hit `Ctrl`+`C` to terminate the process. Again, run the `kubectl get hpa -w` command in your other terminal window, and you will see the number of replicas begin to decrease as the CPU load returns to 0%. It shows the output:
121+
kubectl apply -f templates/cluster-agent/hpa-example/hpa-manifest.yaml
74122

75-
NOTE: It takes a few minutes for the number of replicas to scale down.
123+
You should be seeing your nginx pod running with the corresponding service:
76124

125+
Pods:
126+
```
127+
default nginx-6757dd8769-5xzp2 1/1 Running 0 3m
128+
```
129+
Services:
77130
```
78-
$ kubectl get hpa -w
79-
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
80-
webapp Deployment/webapp 51% / 10% 1 10 4 10m
81-
webapp Deployment/webapp 51% / 10% 1 10 4 10m
82-
webapp Deployment/webapp 27% / 10% 1 10 4 11m
83-
webapp Deployment/webapp 27% / 10% 1 10 8 11m
84-
webapp Deployment/webapp 0% / 10% 1 10 8 12m
85-
webapp Deployment/webapp 0% / 10% 1 10 8 12m
86-
webapp Deployment/webapp 0% / 10% 1 10 8 13m
87-
webapp Deployment/webapp 0% / 10% 1 10 8 13m
88-
webapp Deployment/webapp 0% / 10% 1 10 8 14m
89-
webapp Deployment/webapp 0% / 10% 1 10 8 14m
90-
webapp Deployment/webapp 0% / 10% 1 10 8 15m
91-
webapp Deployment/webapp 0% / 10% 1 10 8 15m
92-
webapp Deployment/webapp 0% / 10% 1 10 8 16m
93-
webapp Deployment/webapp 0% / 10% 1 10 1 16m
94-
webapp Deployment/webapp 0% / 10% 1 10 1 17m
131+
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
132+
default nginx ClusterIP 192.168.251.36 none 8090/TCP 3m
95133
```
134+
Horizontal Pod Autoscalers:
135+
```
136+
NAMESPACE NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
137+
default nginxext Deployment/nginx 0/9 (avg) 1 3 1 3m
138+
```
139+
140+
=== Stressing your service
141+
142+
At this point, the set up is ready to be stressed. As a result of the stress Kubernetes will autoscale the NGINX pods.
143+
144+
TODO
145+
146+
147+
Looking into your application, you should be able to correlate the requests per second on your NGINX boxes with the autoscaling event and the creation of new replicas.
148+
149+
image::autoscalingdash.png[]
96150

97151
== Cleanup
98152

99-
$ kubectl delete hpa/webapp deploy/load-generator deploy/webapp
153+
$ kubectl delete -f templates/
100154

101155

102156
You are now ready to continue on with the workshop!
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apiVersion: extensions/v1beta1
2+
kind: Deployment
3+
metadata:
4+
name: datadog-cluster-agent
5+
namespace: default
6+
spec:
7+
template:
8+
metadata:
9+
labels:
10+
app: datadog-cluster-agent
11+
name: datadog-agent
12+
spec:
13+
serviceAccountName: dca
14+
containers:
15+
- image: datadog/cluster-agent:0.10.0
16+
imagePullPolicy: Always
17+
name: datadog-cluster-agent
18+
env:
19+
- name: DD_API_KEY
20+
value: '<YOUR_API_KEY>'
21+
# Optionally reference an APP KEY for the External Metrics Provider.
22+
#- name: DD_APP_KEY
23+
# value: '<YOUR_APP_KEY>'
24+
- name: DD_COLLECT_KUBERNETES_EVENTS
25+
value: "true"
26+
- name: DD_LEADER_ELECTION
27+
value: "true"
28+
- name: DD_CLUSTER_AGENT_AUTH_TOKEN
29+
value: <THIRTY_2_CHARACTERS_LONG_TOKEN>
30+
- name: DD_EXTERNAL_METRICS_PROVIDER_ENABLED
31+
value: 'true'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: autoscaling/v2beta1
2+
kind: HorizontalPodAutoscaler
3+
metadata:
4+
name: nginxext
5+
spec:
6+
minReplicas: 1
7+
maxReplicas: 5
8+
scaleTargetRef:
9+
apiVersion: apps/v1
10+
kind: Deployment
11+
name: nginx
12+
metrics:
13+
- type: External
14+
external:
15+
metricName: nginx.net.request_per_s
16+
metricSelector:
17+
matchLabels:
18+
kube_container_name: nginx
19+
targetAverageValue: 9

resources/images/autoscalingdash.png

173 KB
Loading

0 commit comments

Comments
 (0)