how to use MicroProfile Metrics as a threshold of scaling Kubernetes applications by using Kubernetes HorizontalPodAutoscaler and Prometheus Adapter
© 2022 Fujitsu LimitedKenji Kazumura@kkzr2022.10.26Make ScalableMicroProfileApplication
View Slide
Agenda© 2022 Fujitsu LimitedCloud Native and ScalabilityKubernetes AutoScalerMicorProfile MetricsK8S HPA with MicroProfile MetricsWrap Up2
Who Am I© 2022 Fujitsu LimitedWork for Fujitsu• FUJITSU Software Enterprise Application Platform• LauncherMember of Jakarta EE SCMember of JCP Executive CommitteeBoard of Director of Eclipse Foundation3
Agenda© 2022 Fujitsu LimitedCloud Native and ScalabilityKubernetes AutoScalerMicorProfile MetricsK8S HPA with MicroProfile MetricsWrap Up4
Cloud Native (CNCF definition)© 2022 Fujitsu Limitedhttps://github.com/cncf/toc/blob/master/DEFINITION.mdCloud native technologies empowerorganizations to build and run scalableapplications in modern, dynamic environmentssuch as public, private, and hybrid clouds.Containers, service meshes, microservices,immutable infrastructure, and declarative APIsexemplify this approach.5
Jevons Paradox© 2022 Fujitsu Limitedhttps://upload.wikimedia.org/wikipedia/commons/b/b8/JevonsParadoxA.pngtechnological improvements that increased the efficiency ofcoal-use led to the increased consumption of coal in a widerange of industries -- Wikipediahttps://en.wikipedia.org/wiki/Jevons_paradox6
StepDefine thresholds of metrics (When do you want to scale?)General Steps to scale© 2022 Fujitsu LimitedStep (if needed)Export metrics (e.g. CPU usage, Memory usage, …)StepMonitor metricsStepScale your VMs, Containers, Applications7
StepDefine thresholds of metrics (When do you want to scale?)General Steps to scale© 2022 Fujitsu LimitedStep (if needed)Export metrics (e.g. CPU usage, Memory usage, …)StepMonitor metricsStepScale your VMs, Containers, Applications8Step3 and Step4 are usually required tobe automated.Nobody wants to monitor metrics withyour own eyes.Nobody wants to scale manually.
Agenda© 2022 Fujitsu LimitedCloud Native and ScalabilityKubernetes AutoScalerMicorProfile MetricsK8S HPA with MicroProfile MetricsWrap Up9
KubernetesKubernetes is a de fact standard containerorchestration toolYou can use it in your own infrastructuresIt is also available as managed service provided bycloud vendorsKubernetes provides automatic scaling methods© 2022 Fujitsu Limited10
StepDefine thresholdsSteps to scale with Kubernetes© 2022 Fujitsu LimitedStepExport metricsStepMonitor metricsStepScale containersYou need to doKubernetes takes care ofKubernetes takes care ofKubernetes takes care ofIff you use only Node/Pod informatione.g. CPU , Memory11
Types of Scalability© 2022 Fujitsu LimitedPodCPU/CoreMemory# of PodsPod PodVertical ScaleHorizontal Scale12
Kubernetes Pod AutoScalerVertical Pod AutoScaler (VPA)•need not consider special application design•less reliabilityHorizontal Pod AutoScaler (HPA)•need design your application to work withmulti instances•easy to scale© 2022 Fujitsu Limited13
HPA System Diagram© 2022 Fujitsu LimitedHorizontal Pod AutoScalerpoll/apis/metrics.k8s.io/v1beta1Metrics APIAPI ServerMetrics ServerpushPodpoll14
Agenda© 2022 Fujitsu LimitedCloud Native and ScalabilityKubernetes AutoScalerMicorProfile MetricsK8S HPA with MicroProfile MetricsWrap Up15
Export monitoring data (aka Telemetry)• Access metrics endpoints (e.g. http://example.com/metrics)3 types of metrics• Base metrics• Application metrics• Vendor Specific metricsProvide Java APIs• Programmers can export their data by APIWill be integrate into MicroProfile OpenTelemtry ?MicroProfile Metrics© 2022 Fujitsu Limitedhttps://download.eclipse.org/microprofile/microprofile-metrics-4.0/microprofile-metrics-spec-4.0.html16
Code Example© 2022 Fujitsu Limitedimport org.eclipse.microprofile.metrics.Counter;import org.eclipse.microprofile.metrics.annotation.Metric;@Path(“eclipsecon")@RequestScopedpublic class LoginService {@Inject@Metric(name="LoginCounter", absolute=true)Counter counter;17
Code Example© 2022 Fujitsu Limited@GET@Path("login")public String increment() {counter.inc();return "number of login is " + counter.getCount() + "¥n";}@GET@Path("logout")public String decrement() {if (counter.getCount() > 0)counter.inc(-1);return "number of login is " + counter.getCount() + "¥n";}18
Demo© 2022 Fujitsu Limitedhttps://microprofile.io/compatible/5-0/Access MP Metrics endpointUse Launcher• MicroProfile 5.0 implementation• https://github.com/fujitsu/launcer19
Agenda© 2022 Fujitsu LimitedCloud Native and ScalabilityKubernetes AutoScalerMicorProfile MetricsK8S HPA with MicroProfile MetricsWrap Up20
HPA scenarios© 2022 Fujitsu Limitedlogin do some workmonitor CPU/memory usagescalelogin do some workmonitor # of login usersscaleUse default API and Metrics serverUse external API and MP Metrics21
HPA /w MP Metrics© 2022 Fujitsu LimitedHorizontal Pod AutoScalerpoll/apis/external.metrics.k8s.io/v1beta1External Metrics APIAPI ServerpushPodpollPrometheus ServerPrometheus Adapterpoll22
Step• create your application pod and deployment of K8S• set Prometheus scraping informationSteps to build HPA systems© 2022 Fujitsu LimitedStep• create external API for your MP application metricusing Prometheus AdapterStep• set HPA configuration# of replicas, metrics name, target threshold, and so on23
Demo (before scale out)© 2022 Fujitsu Limited・・・Login Application (K8S cluster)Total # of LoginsPod #2Pod #1countercomparethreshold(when scale?)httpcounterHPA24
Demo (after scale out)© 2022 Fujitsu Limited・・・Login Application (K8S cluster)Total # of LoginsPod #2Pod #1countercomparethreshold(when scale?)Pod #3add extra podhttpcountercounterHPA25
Agenda© 2022 Fujitsu LimitedCloud Native and ScalabilityKubernetes AutoScalerMicorProfile MetricsK8S HPA with MicroProfile MetricsWrap Up26
⚫ Scalability is one of important keys of Cloud Nativeapplications⚫ Kubernets provides scaling mechanism• pods and nodes metrics can be used as default• but they are not always enough⚫ MP Metrics provides standard way to handle metrics⚫ Collaboration of K8S and MP Metrics produces values• flexible scalability according to application metrics• easy to scale your MP applicationsWrap Up© 2022 Fujitsu Limited27
© 2022 Fujitsu Limited28
Thank you© 2022 Fujitsu Limited