tencent cloud

Tencent Kubernetes Engine

소식 및 공지 사항
릴리스 노트
제품 릴리스 기록
제품 소개
제품 장점
제품 아키텍처
시나리오
제품 기능
리전 및 가용존
빠른 시작
신규 사용자 가이드
표준 클러스터를 빠르게 생성
Demo
클라우드에서 컨테이너화된 애플리케이션 배포 Check List
TKE 표준 클러스터 가이드
Tencent Kubernetes Engine(TKE)
클러스터 관리
네트워크 관리
스토리지 관리
Worker 노드 소개
Kubernetes Object Management
워크로드
클라우드 네이티브 서비스 가이드
Tencent Managed Service for Prometheus
TKE Serverless 클러스터 가이드
TKE 클러스터 등록 가이드
실습 튜토리얼
Serverless 클러스터
네트워크
로그
모니터링
유지보수
DevOps
탄력적 스케일링
자주 묻는 질문
클러스터
TKE Serverless 클러스터
유지보수
서비스
이미지 레지스트리
원격 터미널
문서Tencent Kubernetes Engine실습 튜토리얼탄력적 스케일링TKE에서 사용자 지정 지표를 사용하여 오토 스케일링 진행

TKE에서 사용자 지정 지표를 사용하여 오토 스케일링 진행

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2023-04-27 18:15:01

작업 시나리오

Custom Metrics API를 기반으로 TKE는 대부분의 HPA 시나리오를 다룰 수 있는 CPU, 메모리, 디스크, 네트워크 및 GPU 관련 메트릭을 포함하여 오토 스케일링을 위한 많은 메트릭을 지원합니다. 자세한 메트릭은 오토 스케일링 메트릭을 참고하십시오. 서비스 단일 복제본 QPS 수를 기반으로 하는 오토 스케일링과 같은 복잡한 시나리오의 경우 prometheus-adapter를 설치하여 오토 스케일링을 구현할 수 있습니다. Kubernetes는 메트릭을 기반으로 오토 스케일링을 수행할 수 있도록 HPA용 Custom Metrics API 및 External Metrics API를 제공하므로, 사용자가 필요에 따라 오토 스케일링을 사용자 지정할 수 있습니다. prometheus-adapter는 위의 두 API를 지원합니다. 실제 환경에서 Custom Metrics API는 대부분의 시나리오를 충족할 수 있습니다. 이 문서는 Custom Metrics API를 통해 오토 스케일링을 위해 사용자 지정 메트릭을 사용하는 방법을 설명합니다.

전제 조건

v1.12 이상의 TKE 클러스터를 생성합니다. 자세한 내용은 클러스터 생성을 참고하십시오.
Prometheus 인스턴스를 배포하고 해당하는 사용자 정의 메트릭을 수집합니다.
Helm을 설치합니다.

작업 단계

모니터링 메트릭 열기

본문은 httpserver_requests_total 메트릭을 열고 HTTP 요청을 기록하는 Golang 서비스 애플리케이션을 예로 듭니다. 이 메트릭은 아래와 같이 서비스 애플리케이션의 QPS 값을 계산하는 데 사용할 수 있습니다.
package main

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"strconv"
)

var (
HTTPRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "httpserver_requests_total",
Help: "Number of the http requests received since the server started",
},
[]string{"status"},
)
)

func init() {
prometheus.MustRegister(HTTPRequests)
}

func main(){
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
code := 200
switch path {
case "/test":
w.WriteHeader(200)
w.Write([]byte("OK"))
case "/metrics":
promhttp.Handler().ServeHTTP(w, r)
default:
w.WriteHeader(404)
w.Write([]byte("Not Found"))
}
HTTPRequests.WithLabelValues(strconv.Itoa(code)).Inc()
})
http.ListenAndServe(":80", nil)
}

서비스 애플리케이션 배포

Deployment를 사용하면 아래와 같이 서비스 응용 프로그램을 TKE 클러스터에 컨테이너화하고 배포할 수 있습니다.
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpserver
namespace: httpserver
spec:
replicas: 1
selector:
matchLabels:
app: httpserver
template:
metadata:
labels:
app: httpserver
spec:
containers:
- name: httpserver
image: registry.imroc.cc/test/httpserver:custom-metrics
imagePullPolicy: Always

---

apiVersion: v1
kind: Service
metadata:
name: httpserver
namespace: httpserver
labels:
app: httpserver
annotations:
prometheus.io/scrape: "true"
prometheus.io/path: "/metrics"
prometheus.io/port: "http"
spec:
type: ClusterIP
ports:
- port: 80
protocol: TCP
name: http
selector:
app: httpserver

Prometheus 인스턴스를 통한 서비스 모니터링 메트릭 수집

Promtheus 인스턴스 수집 규칙 또는 ServiceMonitor를 통해 서비스에서 연 모니터링 메트릭을 수집하도록 Promtheus 인스턴스를 구성할 수 있습니다.

방법1: Promtheus 인스턴스 수집 규칙 구성

아래와 같이 Promtheus 인스턴스 수집 규칙의 구성 파일에 다음 수집 규칙을 추가합니다.
- job_name: httpserver
scrape_interval: 5s
kubernetes_sd_configs:
- role: endpoints
namespaces:
names:
- httpserver
relabel_configs:
- action: keep
source_labels:
- __meta_kubernetes_service_label_app
regex: httpserver
- action: keep
source_labels:
- __meta_kubernetes_endpoint_port_name
regex: http

방법2: ServiceMonitor 구성

prometheus-operator가 설치된 경우 아래와 같이 ServiceMonitor의 CRD 객체를 생성하여 Prometheus 인스턴스를 구성할 수 있습니다.
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: httpserver
spec:
endpoints:
- port: http
interval: 5s
namespaceSelector:
matchNames:
- httpserver
selector:
matchLabels:
app: httpserver

prometheus-adapter 설치

1. Helm을 사용하여 prometheus-adapter를 설치합니다. 설치하기 전에 사용자 정의 메트릭을 확인하고 구성하십시오. 상기 모니터링 메트릭 열기의 예시에 따르면 HTTP 요청을 기록하기 위해 서비스에서 httpserver_requests_total 메트릭을 사용하므로 아래와 같이 다음 PromQL을 통해 각 서비스 Pod의 QPS를 계산할 수 있습니다.
sum(rate(http_requests_total[2m])) by (pod)
2. prometheus-adapter의 구성으로 변환합니다. 다음과 같이 values.yaml을 생성합니다.
rules:
default: false
custom:
- seriesQuery: 'httpserver_requests_total'
resources:
template: <<.Resource>>
name:
matches: "httpserver_requests_total"
as: "httpserver_requests_qps" # PromQL에서 계산한 QPS 메트릭
metricsQuery: sum(rate(<<.Series>>{<<.LabelMatchers>>}[1m])) by (<<.GroupBy>>)
prometheus:
url: http://prometheus.monitoring.svc.cluster.local # Prometheus API 주소 교체(포트 필요 없음)
port: 9090
3. 다음 Helm 명령을 실행하여 아래와 같이 prometheus-adapter를 설치합니다.
주의사항
설치하기 전에 다음 명령을 사용하여 TKE의 등록된 Custom Metrics API를 삭제해야 합니다.
kubectl delete apiservice v1beta1.custom.metrics.k8s.io
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
# Helm 3
helm install prometheus-adapter prometheus-community/prometheus-adapter -f values.yaml
# Helm 2
# helm install --name prometheus-adapter prometheus-community/prometheus-adapter -f values.yaml

테스트 및 검증

올바르게 설치된 경우, 다음 명령을 실행하여 아래와 같이 Custom Metrics API가 반환하는 구성된 QPS 관련 메트릭을 볼 수 있습니다.
$ kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1
{
"kind": "APIResourceList",
"apiVersion": "v1",
"groupVersion": "custom.metrics.k8s.io/v1beta1",
"resources": [
{
"name": "jobs.batch/httpserver_requests_qps",
"singularName": "",
"namespaced": true,
"kind": "MetricValueList",
"verbs": [
"get"
]
},
{
"name": "pods/httpserver_requests_qps",
"singularName": "",
"namespaced": true,
"kind": "MetricValueList",
"verbs": [
"get"
]
},
{
"name": "namespaces/httpserver_requests_qps",
"singularName": "",
"namespaced": false,
"kind": "MetricValueList",
"verbs": [
"get"
]
}
]
}
다음 명령어를 실행하여 아래와 같이 Pod의 QPS 값을 확인합니다.
설명
다음 예시에서 값은 500m이며 이는 QPS 값이 0.5 요청/초임을 의미합니다.
$ kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1/namespaces/httpserver/pods/*/httpserver_requests_qps
{
"kind": "MetricValueList",
"apiVersion": "custom.metrics.k8s.io/v1beta1",
"metadata": {
"selfLink": "/apis/custom.metrics.k8s.io/v1beta1/namespaces/httpserver/pods/%2A/httpserver_requests_qps"
},
"items": [
{
"describedObject": {
"kind": "Pod",
"namespace": "httpserver",
"name": "httpserver-6f94475d45-7rln9",
"apiVersion": "/v1"
},
"metricName": "httpserver_requests_qps",
"timestamp": "2020-11-17T09:14:36Z",
"value": "500m",
"selector": null
}
]
}

HPA 테스트

각 서비스 Pod의 평균 QPS 50 도달 시 스케일 아웃이 트리거되는 경우, 최소 및 최대 복제본 수는 각각 1 및 1000이며 구성 예시는 다음과 같습니다.
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: httpserver
namespace: httpserver
spec:
minReplicas: 1
maxReplicas: 1000
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: httpserver
metrics:
- type: Pods
pods:
metric:
name: httpserver_requests_qps
target:
averageValue: 50
type: AverageValue
다음 명령을 실행하여 서비스를 테스트하고 아래와 같이 스케일 아웃이 트리거되는지 관찰합니다.
$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
httpserver Deployment/httpserver 83933m/50 1 1000 2 18h
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
httpserver-6f94475d45-47d5w 1/1 Running 0 3m41s
httpserver-6f94475d45-7rln9 1/1 Running 0 37h
httpserver-6f94475d45-6c5xm 0/1 ContainerCreating 0 1s
httpserver-6f94475d45-wl78d 0/1 ContainerCreating 0 1s
스케일 아웃이 정상적으로 트리거되면 HPA가 서비스 사용자 지정 메트릭을 기반으로 오토 스케일링을 구현했음을 의미합니다.

도움말 및 지원

문제 해결에 도움이 되었나요?

피드백