diff --git a/kubernetes-monitoring/nginx/Dockerfile b/kubernetes-monitoring/nginx/Dockerfile new file mode 100644 index 0000000..0a484f1 --- /dev/null +++ b/kubernetes-monitoring/nginx/Dockerfile @@ -0,0 +1,6 @@ +FROM nginx:1.14.2 +RUN rm /etc/nginx/conf.d/default.conf +ADD nginx.conf /etc/nginx/conf.d/default.conf +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/kubernetes-monitoring/nginx/deployment.yaml b/kubernetes-monitoring/nginx/deployment.yaml new file mode 100644 index 0000000..685de37 --- /dev/null +++ b/kubernetes-monitoring/nginx/deployment.yaml @@ -0,0 +1,32 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx + namespace: homework + labels: + app: nginx +spec: + replicas: 1 + selector: + matchLabels: + app: nginx + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nvyakimov/my-nginx:v1.0.0 + ports: + - containerPort: 80 + - name: nginx-exporter + image: 'nginx/nginx-prometheus-exporter:1.1.0' + args: + - '-nginx.scrape-uri=http://homework.otus/nginx-metrics' + ports: + - containerPort: 9113 \ No newline at end of file diff --git a/kubernetes-monitoring/nginx/nginx.conf b/kubernetes-monitoring/nginx/nginx.conf new file mode 100644 index 0000000..9b631c8 --- /dev/null +++ b/kubernetes-monitoring/nginx/nginx.conf @@ -0,0 +1,22 @@ +server { + listen 80; + server_name localhost; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + } + + location /nginx-metrics { + stub_status on; + } + + #error_page 404 /404.html; + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } +} \ No newline at end of file diff --git a/kubernetes-monitoring/nginx/service.yaml b/kubernetes-monitoring/nginx/service.yaml new file mode 100644 index 0000000..81b91b9 --- /dev/null +++ b/kubernetes-monitoring/nginx/service.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: Service +metadata: + name: nginx-service + namespace: homework + labels: + app: nginx +spec: + type: ClusterIP + selector: + app: nginx + ports: + - name: nginx-port + protocol: TCP + port: 80 + targetPort: 80 + - name: nginx-exporter-port + protocol: TCP + port: 9113 + targetPort: 9113 \ No newline at end of file diff --git a/kubernetes-monitoring/prometheus-stack/serviceMonitor.yaml b/kubernetes-monitoring/prometheus-stack/serviceMonitor.yaml new file mode 100644 index 0000000..b6a8c7f --- /dev/null +++ b/kubernetes-monitoring/prometheus-stack/serviceMonitor.yaml @@ -0,0 +1,16 @@ +apiVersion: monitoring.coreos.com/v1 +kind: ServiceMonitor +metadata: + name: nginx-exporter-sm + namespace: prometheus + labels: + release: prometheus-stack +spec: + namespaceSelector: + any: true + selector: + matchLabels: + app: nginx + endpoints: + - port: nginx-exporter-port + scheme: http \ No newline at end of file diff --git a/kubernetes-monitoring/prometheus-stack/values.yaml b/kubernetes-monitoring/prometheus-stack/values.yaml new file mode 100644 index 0000000..3867713 Binary files /dev/null and b/kubernetes-monitoring/prometheus-stack/values.yaml differ diff --git a/kubernetes-monitoring/readme.md b/kubernetes-monitoring/readme.md new file mode 100644 index 0000000..a137a05 --- /dev/null +++ b/kubernetes-monitoring/readme.md @@ -0,0 +1,32 @@ +1) запустить миникуб minikube start --registry-mirror=https://c.163.com +2) запустить деплоймент с кастомным образом nginx и экспортером: kubectl apply -f .\nginx\deployment.yaml +3) включить сервис для него: kubectl apply -f .\nginx\service.yaml +4) создать неймспейс для prometheus-operator: kubectl create ns prometheus +5) установить prometheus-operator. Для этого выполнить команды: +- helm repo add prometheus-community https://prometheus-community.github.io/helm-charts +- helm repo update +- helm -n prometheus install prometheus-stack prometheus-community/kube-prometheus-stack + +6) добавить настройку для скейпинга метрик nginx: + вариант №1: + получить values из чарта: helm show values prometheus-community/kube-prometheus-stack > .\prometheus-stack\values.yaml + + в values добавить + additionalServiceMonitors: + - name: "nginx-exporter" + namespaceSelector: + any: true + selector: + matchLabels: + app: nginx + endpoints: + - port: "nginx-exporter-port" + - targetPort: "nginx-exporter-port" + scheme: http + и выполнить helm -n prometheus upgrade -f .\prometheus-stack\values.yaml prometheus-stack prometheus-community/kube-prometheus-stack + + вариант №2: применить настройку из файла serviceMonitor.yaml + выполнить kubectl apply -f .\prometheus-stack\serviceMonitor.yaml + +7) пробросить порт для графаны: kubectl -n prometheus port-forward svc/prometheus-stack-grafana 8080:80 + ипортировать дашборд 12708 , уюбедиться, что на нем есть информация о работе nginx \ No newline at end of file