MLOps/Kubernetes

Kubernetes Dashboard(Grafana / Prometheus)

ai-notes 2024. 7. 5. 08:58
반응형

이번 글에서는 helm을 이용한 grafana와 prometheus의 설치 과정에 대해 설명할 예정입니다.
prometheus는 minio의 console monitoring 탭에서 Info외에 Usage / Traffic / Resources의 정보를 보는 것에도 관여합니다.

Kubernetes에서 minio를 설치하고 사용하는데 속도가 느린 원인을 파악하기 위해 설치했습니다.


helm 설치

 grafana와 prometheus는 helm을 사용하면 설치가 매우 쉬우므로, helm을 이용하여 설치하기 위해 helm을 먼저 설치한다.

curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh

 

Prometheus 설치

Prometheus는 오픈소스 모니터링 및 알림 도구로, Kubernetes 클러스터의 성능 메트릭을 수집, 저장, 분석하는데 주로 사용된다.

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/prometheus -f values.yaml

#helm show values prometheus-community/prometheus > values.yaml

prometheus는 미리 준비한 values.yaml 파일을 이용해서 설치를 진행한다.

# values.yaml
alertmanager:
    persistentVolume:
        enabled: false

server:
    persistentVolume:
        enabled: false
    service:
      type: NodePort
      nodePort: 30240

pushgateway:
    persistentVolume:
        enabled: false

 

persistentVolume을 false로 지정하지 않으면, PV를 직접 생성해서 매칭해줘야 하는듯 한데, 그 부분은 잘 안됐었다.

*service에서 NodePort가 적용이 안될 수 있다. svc를 확인해 본 후 적응이 되지 않았다면, 아래 명령어를 통해 직접 수정한다.

kubectl edit -n monitoring svc/prometheus-server

Grafana 설치

Grafana는 다양한 데이터 소스(Prometheus, InfluxDB 등)에서 데이터를 가져와 대시보드 형태로 시각화할 수 있다.

https://grafana.com/grafana/dashboards/?search=kubernetes 에서 다양한 템플릿을 제공하니, 설치 후 골라서 쓰면 된다.

helm repo add grafana <https://grafana.github.io/helm-charts>
helm repo update
helm install grafana grafana/grafana --namespace monitoring --create-namespace

#helm get notes grafana -n monitoring

설치 이후 Grafana에 접속하기 위한 비밀번호는 아래 명령어를 통해 알 수 있다. (기본 계정명 admin)

kubectl get secret --namespace monitoring grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo

Kubernetes 포트포워딩을 통해 접속을 해도 되지만, 편의성을 위해 svc의 type을 NodePort로 변경하여 외부에서도 접속할 수 있도록 수정한다.

# type 을 NodePort로 변경
kubectl edit -n monitoring svc/grafana

# 이후 Port를 확인하여 접속
kubectl get -n monitoring svc

웹을 통해 Grafana에 접속 및 로그인 후, 왼쪽탭의 Connections - Data sources에서 Prometheus를 등록해주면 설정이 완료된다.

Prometheus server URL에 실행중인 Prometheus service 주소를 입력하면 된다.

[prometheus가 실행중인 PC IP]:nodeport
or
http://prometheus-server.monitoring.svc.cluster.local

이렇게 prometheus 연결을 마치고, 홈페이지에서 json파일 혹은 ID를 가져와 등록하고, 실행하면 아래와 같은 화면을 볼 수 있다.

 

Kubernetes Dashboard 설치

helm repo add kubernetes-dashboard https://kubernetes.github.io/dashboard/
helm upgrade --install kubernetes-dashboard kubernetes-dashboard/kubernetes-dashboard --create-namespace --namespace kubernetes-dashboard

Kubernetes에서 실행중인 Node, Pod, Svc등을 관리하기 위한 Dashboard. helm으로 설치를 마치면 다음과 같은 화면이 나타난다.

Web으로 접근하기 위해서는 Https로 접속해야 한다. 설정하는 과정이 번거로워서 nohup을 이용하여 port-forward를 진행했다.

nohup kubectl -n kubernetes-dashboard port-forward svc/kubernetes-dashboard-kong-proxy 8443:443 &

 

서버에서 진행하고 있으므로, 사용하고 있는 데스크톱에서 아래 명령어를 통해 연결하고,

ssh -N -L 8443:localhost:8443 id@serverip

 

localhost를 통해 접속을 하면 아래와 같은 화면이 뜬다.

접속에 필요한 토큰은 아래 설정파일을 등록함으로써(apply) 생성할 수 있다.

apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard
 ---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kubernetes-dashboard
kubectl -n kubernetes-dashboard create token admin-user

이렇게 하면 토큰이 생성되는데, 생성된 토큰을 복사해서 로그인하면 Dashboard를 확인할 수 있다.

 

마치며

kubernetes-dashboard의 경우, 오래 사용하지 않으면 접속이 끊기는데 매번 Token을 생성하고 붙여넣는것이 불편하여 잘 쓰고 있지 못합니다. 혹시 영구적으로 등록한다거나 스킵하는 방법을 알고계신다면 알려주세요!

반응형

'MLOps > Kubernetes' 카테고리의 다른 글

쿠버네티스 Multi Control-plane  (0) 2024.11.18
쿠버네티스 실행  (1) 2024.07.01
쿠버네티스 개요 및 설치  (0) 2024.06.17