Kubernetes – Prometheus/Grafana 설치
kube-prometheus 를 이용하여 Prometheus/Grafana 를 동시에 설치합니다.
Prometheus/Grafana 설정이 모두 끝나 있어서 편리하게 사용가능합니다.
설치
git clone https://github.com/coreos/kube-prometheus.git
cd kube-prometheus/
# Create the namespace and CRDs, and then wait for them to be available before creating the remaining resources
# Note that due to some CRD size we are using kubectl server-side apply feature which is generally available since kubernetes 1.22.
# If you are using previous kubernetes versions this feature may not be available and you would need to use kubectl create instead.
kubectl apply --server-side -f manifests/setup
kubectl wait \
--for condition=Established \
--all CustomResourceDefinition \
--namespace=monitoring
kubectl apply -f manifests/
kubectl get pod -n monitoring
Ingress 생성
ingress-nginx 는 기존에 설치되어 있다고 가정합니다.
cd
vi ingress-grafana.yaml
---------------------------
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: grafana
namespace: monitoring
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: k8s-grafana.skyer9.pe.kr
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: grafana
port:
number: 3000
---------------------------
kubectl apply -f ingress-grafana.yaml
아래 networkpolicy 를 추가로 설정해 주지 않으면,
디폴트로 inbound 가 막혀있어서 외부 접속이 불가능하다.
k get networkpolicy -n monitoring
NetworkPolicy 생성
우선 클러스터에 설치되어 있는 Network add-on 이,
NetworkPolicy 를 지원하는지 확인해야 합니다.
(Weave Net works 는 ingress 만 지원합니다.)
vi np-grafana.yaml
---------------------------
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
labels:
app.kubernetes.io/component: grafana
app.kubernetes.io/name: grafana
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 9.0.1
name: grafana
namespace: monitoring
spec:
egress:
- {}
ingress:
- ports:
- port: 3000
protocol: TCP
# 여러개의 아이피 대역을 할당할 수 있다.
# from:
# - ipBlock:
# cidr: 172.17.0.0/16
# except:
# - 172.17.1.0/24
podSelector:
matchLabels:
app.kubernetes.io/component: grafana
app.kubernetes.io/name: grafana
app.kubernetes.io/part-of: kube-prometheus
policyTypes:
- Egress
- Ingress
---------------------------
PC 호스트 파일에 아래 내용을 추가해 줍니다.
<worker node 아이피> k8s-grafana.skyer9.pe.kr
kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.108.205.161 <none> 80:31546/TCP,443:32034/TCP 24h
ingress-nginx-controller-admission ClusterIP 10.103.41.92 <none> 443/TCP 24h
http://k8s-prometheus.skyer9.pe.kr:31546
grafana 가 외부에 노출되었으므로,
추가적인 보안설정이 필요합니다.
grafana 접속 후 좌측메뉴 > Dashboards > browse > Defaults 에 접속하면,
이미 설치되어 있는 많은 Dashboards 를 볼 수 있습니다.
퍼시스턴트볼륨(PV) 생성
데이타가 Pod 내에 저장되므로,
Pod 가 삭제되었을 때 데이타도 같이 사라집니다.
이를 방지하기 위해 PV 생성이 필요합니다.
일단은 Prometheus 가 이중화되어 가동되고,
Grafana 에는 비밀번호를 제외하고 그닥 저장되는 정보가 없어서 skip 합니다.
Grafana 추가설정
좌측메뉴 > Configuration > Preferences > Timezone 을 Browser time 으로 설정하면,
시간대가 KST 가 됩니다.
각각의 대시보드에 시간대를 설정하게 되어 있으면,
각각의 대시보드 설정을 수정해야 합니다.
uninstall
kubectl delete --ignore-not-found=true -f manifests/ -f manifests/setup