โ๏ธ ํดํด 5๊ธฐ ์์ฆ 1 ์คํฐ๋ : 90DaysOfDevOps ๋ ํฌ์งํ ๋ฆฌ๋ฅผ ๋ฐํ์ผ๋ก ๊ณต๋ถํฉ๋๋ค!
๐ 28 > System monitoring and auditing
90DaysOfDevOps/2023/day28.md at main · MichaelCade/90DaysOfDevOps
This repository started out as a learning in public project for myself and has now become a structured learning map for many in the community. We have 3 years under our belt covering all things Dev...
github.com
๐ System monitoring and auditing
- ์์คํ ๋ชจ๋ํฐ๋ง → ์์คํ ๋ด์์ ๋ฌด์จ ์ผ์ด ์ผ์ด๋๊ณ ์๋์ง์ ๋ํ ๊ฐ์์ฑ ์ ๊ณต→ ๋ณด์ ์ฌ๊ณ ๊ฐ์ง ๋ฐ ๋์
- ๋ฐํ์ ๋ณด์์ ๋ชจ๋ํฐ๋ง์ด ์ค์ํ ์ด์ 5๊ฐ์ง
- Identifying security incidents(๋ณด์ ์ฌ๊ณ ๊ฐ์ง)
- Mitigating risks(์ฌ์ ์กฐ์น → ์ํ ์ํ)
- Complying with regulations(๊ท์ ์ค์)
- Improving incident response(์ฌ๊ณ ๋์ ์ํ)
- Gaining visibility(๊ฐ์์ฑ ํ๋ณด) → ์ฑ๋ฅ ์ต์ ํ ๋ฐ ๋ฌธ์ ํด๊ฒฐ์ ์ธ์ฌ์ดํธ ์ ๊ณต
๐What to monitor and record?
- ๋ชจ๋ํฐ๋ง ํด์ผ ํ๋ ์์๋ค 7๊ฐ์ง
- Control plane logging : ์ธํ๋ผ์ ๋ชจ๋ ์ค์ผ์คํธ๋ ์ด์ ์ด ์ ์ด ํ๋ ์ธ์ ํตํด ์ด๋ค์ง๊ธฐ ๋๋ฌธ
- Operating level logs : ์ด์ ์ฒด์ ์์ค ์ด๋ฒคํธ → ์์คํ ํ๋ ์ถ์ , ์ค๋ฅ์ ๋ณด์ ์ด๋ฒคํธ ๊ฐ์ง
- Network activity : ๋คํธ์ํฌ ํธ๋ํฝ ๋ชจ๋ํฐ๋ง → ๋น์ ์์ , ๋นํ๊ฐ ํ๋ ์๋ณ
- Application activity and performance : ์ ํ๋ฆฌ์ผ์ด์ ์์ค์์ ์ค์๋, ๊ณต๊ฒฉ ๊ฐ์ง
- Resource utilization : CPU, ๋ฉ๋ชจ๋ฆฌ, ๋์คํฌ ๊ณต๊ฐ → ๋ณ๋ชฉ ํ์ ๊ฐ์ ์ฑ๋ฅ ๋ฌธ์ ์๋ณ
- Security configurations : ๋ฐฉํ๋ฒฝ ๊ท์น, ์ฌ์ฉ์ ์ก์ธ์ค ์ ์ด ๋ฑ
- Backup and disaster recovery systems : ๋ฐฑ์ ๋ฐ ์ฌํด ๋ณต๊ตฌ ์์คํ
๐์ค์ ๊ตฌํ ์ค์ต
(1) Control plane monitoring
- ์ฟ ๋ฒ๋คํฐ์ค์ ์ด๋ฒคํธ auditing(๊ฐ์ฌ) ์ธํ๋ผ์ธ auditing log
- Auditing ๊ณต์ ๋ฌธ์
Auditing
Kubernetes auditing provides a security-relevant, chronological set of records documenting the sequence of actions in a cluster. The cluster audits the activities generated by users, by applications that use the Kubernetes API, and by the control plane its
kubernetes.io
- Audit policy : k8s API ์๋ฒ์ ๋ฌด์์ ๊ธฐ๋กํด์ผํ๋์ง ์๋ ค์ฃผ๋ ๊ตฌ์ฑ (ํ์ผ ์ ์ฅ or ์น ํ ์ผ๋ก ์ ์ก)
๐ ์ค์ต
1๏ธโฃ audit-policy ์ค์ ํ minikube ์์
mkdir -p ~/.minikube/files/etc/ssl/certs
cat <<EOF > ~/.minikube/files/etc/ssl/certs/audit-policy.yaml
# Log all requests at the Metadata level.
apiVersion: audit.k8s.io/v1
kind: Policy
rules:
- level: RequestResponse
EOF
minikube start --extra-config=apiserver.audit-policy-file=/etc/ssl/certs/audit-policy.yaml --extra-config=apiserver.audit-log-path=-
โผ Output
2๏ธโฃ log ํ์ผ๋ก ์ด๋ค ๋ด์ฉ์ด ์ ์ฅ๋์ด ์๋์ง ํ์ธ
kubectl logs kube-apiserver-minikube -n kube-system | grep audit.k8s.io/v1
→ ๋ชจ๋ API ๋์์ด stream์ ๋ก๊ทธ๋์ด์ ์ธํ๋ผ ์์ฒญ์ ๋ชจ๋ ์ฃผ์ ์์(๋๊ฐ, ๋ฌด์์ ์ธ์ )๊ฐ ๊ธฐ๋ก๋๋ค.
→ ํ์ผ ์ ์ฅ๋ณด๋ค๋ ๋ก๊น ์์คํ ๋ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ก ์ ๋ฌ
โผ ๊ธฐ๋ณธ k8s์์ Promtail์ ์ฌ์ฉํด์ Prometheus์ ๋ก๊ทธ ์ ๋ฌํ๋ ๊ณผ์
Monitoring and Gathering Metrics from Kubernetes Audit Logs
Monitoring and Gathering Metrics from Kubernetes Audit Logs
Log files, streams and messages provide lots of information about what's going on at runtime. Since Kubernetes 1.7, we've been able to see what's going on inside of our cluster with Kubernetes audit logs.
www.bionconsulting.com
Promtail agent | Grafana Loki documentation
Promtail agent | Grafana Loki documentation
Open source Promtail agent Promtail is an agent which ships the contents of local logs to a private Grafana Loki instance or Grafana Cloud. It is usually deployed to every machine that runs applications which need to be monitored. NotePromtail is feature c
grafana.com



โผ Output
{
"kind":"Event",
"apiVersion":"audit.k8s.io/v1",
"level":"RequestResponse",
"auditID":"940c44ba-867c-4b10-b8d0-d1909439828d",
"stage":"RequestReceived",
"requestURI":"/apis/storage.k8s.io/v1/csidrivers?limit=500\u0026resourceVersion=0",
"verb":"list",
"user":{"username":"system:node:minikube",
"groups":["system:nodes","system:authenticated"]},
"sourceIPs":["192.168.49.2"],
"userAgent":"kubelet/v1.28.3 (linux/arm64) kubernetes/a8a1abc",
"objectRef":{"resource":"csidrivers","apiGroup":"storage.k8s.io","apiVersion":"v1"},
"requestReceivedTimestamp":"2024-03-24T18:51:57.319828Z",
"stageTimestamp":"2024-03-24T18:51:57.319828Z"
}
{"kind":"Event","apiVersion":"audit.k8s.io/v1","level":"RequestResponse","auditID":"2a25f269-a98f-4d38-9968-8fc3ced5d7c4","stage":"RequestReceived","requestURI":"/healthz","verb":"get","user":{"username":"system:kube-controller-manager","groups":["system:authenticated"]},"sourceIPs":["192.168.49.2"],"userAgent":"kube-controller-manager/v1.28.3 (linux/arm64) kubernetes/a8a1abc/shared-informers","requestReceivedTimestamp":"2024-03-24T18:51:57.320857Z","stageTimestamp":"2024-03-24T18:51:57.320857Z"}
{"kind":"Event","apiVersion":"audit.k8s.io/v1","level":"RequestResponse","auditID":"2a25f269-a98f-4d38-9968-8fc3ced5d7c4","stage":"ResponseComplete","requestURI":"/healthz","verb":"get","user":{"username":"system:kube-controller-manager","groups":["system:authenticated"]},"sourceIPs":["192.168.49.2"],"userAgent":"kube-controller-manager/v1.28.3 (linux/arm64) kubernetes/a8a1abc/shared-informers","responseStatus":{"metadata":{},"status":"Failure","message":"forbidden: User \"system:kube-controller-manager\" cannot get path \"/healthz\"","reason":"Forbidden","details":{},"code":403},"responseObject":{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"forbidden: User \"system:kube-controller-manager\" cannot get path \"/healthz\"","reason":"Forbidden","details":{},"code":403},"requestReceivedTimestamp":"2024-03-24T18:51:57.320857Z","stageTimestamp":"2024-03-24T18:51:57.321207Z","annotations":{"authorization.k8s.io/decision":"forbid","authorization.k8s.io/reason":""}}
{"kind":"Event","apiVersion":"audit.k8s.io/v1","level":"RequestResponse","auditID":"b840d8dc-4a8e-4035-aba6-67f313cb95a2","stage":"RequestReceived","requestURI":"/api/v1/nodes?fieldSelector=metadata.name%3Dminikube\u0026limit=500\u0026resourceVersion=0","verb":"list","user":{"username":"system:node:minikube","groups":["system:nodes","system:authenticated"]},"sourceIPs":["192.168.49.2"],"userAgent":"kubelet/v1.28.3 (linux/arm64) kubernetes/a8a1abc","objectRef":{"resource":"nodes","name":"minikube","apiVersion":"v1"},"requestReceivedTimestamp":"2024-03-24T18:51:57.321209Z","stageTimestamp":"2024-03-24T18:51:57.321209Z"}
์ค์ ๋ก๋ ์์๊ฒ ์ถ๋ ฅ์ ํด์ฃผ์ง ์์์... ์ฐ์ ์ฌ์ง์ ์ฒจ๋ถํด๋๋ก ํ๊ฒ ๋ค
(2) Resource monitoring
- Prometheus(๋ก๊น ๋ฐ ์ด๋ฒคํธ ๋ฐ์ดํฐ๋ฒ ์ด์ค) + Grafana(UI ๋ฐ ๋์๋ณด๋) ์กฐํฉ์ด ๐๐ป
- brew install helm ๋ก helm ๋ฏธ๋ฆฌ ์ค์น ํ์ํ๋ค!
๐ ์ค์ต
1๏ธโฃ helm ์ฐจํธ์์ prometheus์ grafana ์ค์น ํ k8s์ ์๋น์ค expose
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo add grafana https://grafana.github.io/helm-charts
helm install prometheus prometheus-community/prometheus
helm install grafana grafana/grafana
kubectl expose service grafana --type=NodePort --target-port=3000 --name=grafana-np
โผ Output
"prometheus-community" has been added to your repositories
"grafana" has been added to your repositories
NAME: prometheus
LAST DEPLOYED: Mon Mar 25 04:10:07 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The Prometheus server can be accessed via port 80 on the following DNS name from within your cluster:
prometheus-server.default.svc.cluster.local
Get the Prometheus server URL by running these commands in the same shell:
export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=prometheus,app.kubernetes.io/instance=prometheus" -o jsonpath="{.items[0].metadata.name}")
kubectl --namespace default port-forward $POD_NAME 9090
The Prometheus alertmanager can be accessed via port 9093 on the following DNS name from within your cluster:
prometheus-alertmanager.default.svc.cluster.local
Get the Alertmanager URL by running these commands in the same shell:
export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=alertmanager,app.kubernetes.io/instance=prometheus" -o jsonpath="{.items[0].metadata.name}")
kubectl --namespace default port-forward $POD_NAME 9093
#################################################################################
###### WARNING: Pod Security Policy has been disabled by default since #####
###### it deprecated after k8s 1.25+. use #####
###### (index .Values "prometheus-node-exporter" "rbac" #####
###### . "pspEnabled") with (index .Values #####
###### "prometheus-node-exporter" "rbac" "pspAnnotations") #####
###### in case you still need it. #####
#################################################################################
The Prometheus PushGateway can be accessed via port 9091 on the following DNS name from within your cluster:
prometheus-prometheus-pushgateway.default.svc.cluster.local
Get the PushGateway URL by running these commands in the same shell:
export POD_NAME=$(kubectl get pods --namespace default -l "app=prometheus-pushgateway,component=pushgateway" -o jsonpath="{.items[0].metadata.name}")
kubectl --namespace default port-forward $POD_NAME 9091
For more information on running Prometheus, visit:
https://prometheus.io/
NAME: grafana
LAST DEPLOYED: Mon Mar 25 04:10:09 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get your 'admin' user password by running:
kubectl get secret --namespace default grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo
2. The Grafana server can be accessed via port 80 on the following DNS name from within your cluster:
grafana.default.svc.cluster.local
Get the Grafana URL to visit by running these commands in the same shell:
export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=grafana" -o jsonpath="{.items[0].metadata.name}")
kubectl --namespace default port-forward $POD_NAME 3000
3. Login with the password from step 1 and the username: admin
#################################################################################
###### WARNING: Persistence is disabled!!! You will lose your data when #####
###### the Grafana pod is terminated. #####
#################################################################################
service/grafana-np exposed
2๏ธโฃ Grafana ๋น๋ฐ๋ฒํธ ์ป๊ธฐ
kubectl get secret --namespace default grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo
- Output : Grafana ๋น๋ฐ๋ฒํธ (e.g : `rsv2K5qMKZSznzjnJXgLXr2b1MMlSBYwm3qvEVxL`)
3๏ธโฃ Grafana URL ์ป๊ธฐ
minikube service grafana-np --url
โผ Output ( โ ๏ธํฐ๋ฏธ๋ ๊ณ์ ์ผ๋ฌ์ผ ํ๋ค!)
4๏ธโฃ Grafana ๋ก๊ทธ์ธํ๊ธฐ
3๏ธโฃ ์์ ์ป๋ URL ์ฃผ์๋ก ๋ค์ด๊ฐ๋ค!
username์๋ admin, Password์๋ 2๏ธโฃ์์ ์๋ ค์ค ๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋ ฅํ๋ฉด ๋ก๊ทธ์ธ์ด ๋๋ค!
๋ก๊ทธ์ธ์ด ์ฑ๊ณตํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ํ๋ฉด์ด ๋ฌ๋ค!
5๏ธโฃ ๊ทธ๋ผํ๋์ ํ๋ก๋ฉํ ์ค์ ์ฐ๊ฒฐํ๊ธฐ
- ๋ฐ์ดํฐ ์์ค(Data sources)์์ prometheus ํด๋ฆญํ๊ณ ์ถ๊ฐํ๋ค.
- Connection์ http://prometheus-server ๋ผ๊ณ ์ ๋ ฅํด์ฃผ๊ณ ๋งจ ์๋๋ก ์คํฌ๋กค ๋ด๋ ค์ Save & Test ํด๋ฆญํด์ ์ ์ฅํ๋ค
6๏ธโฃ ๋์๋ณด๋ ๋ถ๋ฌ์ค๊ธฐ
๋์๋ณด๋(Dashboards) ํญ์ผ๋ก ๊ฐ์ ํ๋ ๋ฒํผ์์ Import ์ ํํ๋ค.
์ค๊ฐ์ ๋ณด์ด๋ Find and import dashboards for common applications ... ์ 6126 ์ ๊ณ Load ํด๋ฆญํ๋ค.
์ด ๋ฒํธ๋ ๊ณต๊ฐ๋ ๋์๋ณด๋ ์์ ์ค ํ๋์ด๋ค.
๋ง์ง๋ง์ผ๋ก ํ๋ก๋ฉํ ์ฐ์ค์ ๋์๋ณด๋๋ฅผ ์ฐ๊ฒฐํ๊ธฐ ์ํด์
DataSource-Prometheus์์ default์ธ prometheus ์ ํ
์ต์ข ๊ฒฐ๊ณผ๋ฌผ์ด ์ด๋ ๊ฒ ๋์ค๋ฉด ์ฑ๊ณต~!
'๐ DevOps & Cloud > 90DaysOfDevOps' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Service Mesh (6) - Ambient Mesh (1) | 2024.04.27 |
---|---|
Service Mesh (5) - Security (1) | 2024.04.27 |
Service Mesh (4) - Observability (0) | 2024.04.26 |
Service Mesh (3) - Traffic Engineering (0) | 2024.04.26 |
Service Mesh (2) - Istio Getting-started (0) | 2024.04.26 |