部署prometheus
创建项目仓库并上传镜像
编写配置文件
[root@k8s2 values]# vim prometheus-values.yaml
alertmanager:alertmanagerSpec:image:repository: prometheus/alertmanagertag: v0.24.0
grafana:enabled: trueimage:repository: grafana/grafanatag: 9.0.6service:type: LoadBalanceradminPassword: prom-operatorsidecar:image:repository: kiwigrid/k8s-sidecartag: 1.19.2
kube-state-metrics:image:repository: kube-state-metrics/kube-state-metricstag: v2.5.0
prometheus-node-exporter:image:repository: prometheus/node-exportertag: v1.3.1
prometheusOperator:admissionWebhooks:patch:enabled: trueimage:repository: ingress-nginx/kube-webhook-certgentag: v1.2.0image:repository: prometheus-operator/prometheus-operatortag: v0.58.0prometheusConfigReloader:image:repository: prometheus-operator/prometheus-config-reloadertag: v0.58.0thanosImage:repository: thanos/thanostag: v0.27.0
prometheus:service:type: LoadBalancerprometheusSpec:image:repository: prometheus/prometheustag: v2.37.0
创建ns
[root@k8s2 helm]# kubectl create namespace monitoring
部署
[root@k8s2 helm]# helm -n monitoring install kube-prometheus-stack -f values/prometheus-values.yaml kube-prometheus-stack-39.11.0.tgz
查看状态
[root@k8s2 helm]# helm -n monitoring get manifest kube-prometheus-stack |kubectl get -f -
访问prometheus监控页面 http://192.168.92.103:9090/
访问grafana监控页面 http://192.168.92.104/
默认用户/密码: admin/prom-operator
切换展示样式
部署nginx监控实例
上传镜像
拉取所需的版本
[root@k8s2 helm]# helm pull my-repo/nginx --version 15.3.3
编写配置文件
[root@k8s2 values]# vim nginx-values.yaml
global:imageRegistry: reg.westos.org
metrics:enabled: trueserviceMonitor:enabled: truenamespace: monitoringlabels:release: kube-prometheus-stack
部署
[root@k8s2 helm]# helm install myapp -f values/nginx-values.yaml nginx-15.3.3.tgz
查看状态
[root@k8s2 ~]# kubectl get pod[root@k8s2 ~]# kubectl get svc
测试
[root@k8s1 ~]# curl 192.168.92.102
prometheus监控指标
部署prometheus-adapter
编写配置文件
[root@k8s2 values]# vim prometheus-adapter-values.yaml
image:repository: prometheus-adapter/prometheus-adaptertag: v0.10.0
prometheus:url: http://kube-prometheus-stack-prometheus.monitoring.svc
部署
[root@k8s2 helm]# helm -n monitoring install prometheus-adapter -f values/prometheus-adapter-values.yaml prometheus-adapter-3.4.0.tgz
查看状态
[root@k8s2 ~]# kubectl -n monitoring get pod
[root@k8s2 ~]# kubectl api-versions
删除原有hpa
[root@k8s2 helm]# kubectl delete hpa --all
新建hpa
[root@k8s2 helm]# vim nginx-hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:name: myapp-nginxnamespace: default
spec:minReplicas: 1maxReplicas: 10metrics:- type: Podspods:metric:name: nginx_http_requeststarget:type: AverageValueaverageValue: 10scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: myapp-nginx
[root@k8s2 helm]# kubectl apply -f nginx-hpa.yaml
压力测试
[root@k8s1 ~]# mv hey /usr/local/bin/[root@k8s1 ~]# chmod +x /usr/local/bin/hey[root@k8s1 ~]# hey -n 10000 -c 5 -q 10 http://myapp.westos.org/index.html
观察hpa动态弹缩
[root@k8s2 ~]# kubectl get hpa -w
prometheus监控指标