[TOC]
https://github.com/coreos/kube-prometheus
kubectl create -f manifests/setup
kubectl create -f manifests/
# kubectl create -f manifests/setup -f manifests
# kubectl delete --ignore-not-found=true -f manifests/ -f manifests/setup
kubectl --namespace monitoring port-forward --address 0.0.0.0 svc/prometheus-k8s 9090
kubectl --namespace monitoring port-forward --address 0.0.0.0 svc/grafana 3000
kubectl --namespace monitoring port-forward --address 0.0.0.0 svc/alertmanager-main 9093
chomd +x ./experimental/custom-metrics-api/deploy.sh
cd ./experimental/custom-metrics-api
./deploy.sh
# 删除./teardown.sh
kubectl port-forward --address 0.0.0.0 svc/sample-app 8080
hey -c 20 -n 100000000 http://192.168.1.135:8080/metrics
下载相关部署文件
git clone https://github.com/mgxian/k8s-monitor.git
cd k8s-monitor
# 下载
mkdir k8s-monitor && cd k8s-monitor
git clone https://github.com/kubernetes-incubator/metrics-server.git
# 修改配置(当前版本有bug)
sed -ri 's@gcr.io/google_containers/metrics-server-amd64:(.*)@mirrorgooglecontainers/metrics-server-amd64:\1@g' metrics-server/deploy/1.8+/metrics-server-deployment.yaml
sed -ri 's@--source=kubernetes.summary_api:.*@--source=kubernetes.summary_api:https://kubernetes.default?kubeletHttps=true\&kubeletPort=10250\&insecure=true@' metrics-server/deploy/1.8+/metrics-server-deployment.yaml
# 部署
kubectl create -f metrics-server/deploy/1.8+/
# 查看状态
kubectl get pods -n kube-system
# 测试获取数据
# 由于采集数据间隔为1分钟
# 等待数分钟后查看数据
NODE=$(kubectl get nodes | grep 'Ready' | head -1 | awk '{print $1}')
METRIC_SERVER_POD=$(kubectl get pods -n kube-system | grep 'metrics-server' | awk '{print $1}')
kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes
kubectl get --raw /apis/metrics.k8s.io/v1beta1/pods
kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes/$NODE
kubectl top node $NODE
kubectl top pod $METRIC_SERVER_POD -n kube-system
prometheus-operator是一个整合prometheus和operator的项目,prometheus是一个集数据收集存储,数据查询,数据图表显示于一身的开源监控组件。operator是由coreos开源一套在k8s上管理应用的软件,通过operator可以方便的实现部署,扩容,删除应用等功能。
prometheus-operator利用k8s的CustomResourceDefinitions功能实现了只需要像写原生kubectl支持的yaml文件一样,轻松收集应用数据,配置报警规则等,包含如下CRDs :
# 创建 namespace
kubectl apply -f monitoring-namespace.yaml
# 部署
kubectl apply -f prometheus-operator.yaml
# 查看
kubectl get pods -n monitoring
kubectl get svc -n monitoring
kubectl get crd
# 部署
kubectl apply -f kube-state-metrics.yaml
# 查看
kubectl get pods -n monitoring
kubectl get svc -n monitoring
# 部署
kubectl apply -f node_exporter.yaml
# 查看
kubectl get pods -n monitoring
kubectl get svc -n monitoring
# 部署
kubectl apply -f prometheus.yaml
# 查看
kubectl get pods -n monitoring
kubectl get svc -n monitoring
# 部署
kubectl apply -f kube-servicemonitor.yaml
# 查看
kubectl get servicemonitors -n monitoring
# 查看 nodeport
kubectl get svc -n monitoring | grep prometheus-k8s
# 获取访问链接
# 11.11.11.111 为其中一个node ip
NODE_IP='11.11.11.112'
PROMETHEUS_NODEPORT=$(kubectl get svc -n monitoring | grep prometheus-k8s | awk '{print $(NF-1)}' | cut -d ':' -f 2 | cut -d '/' -f 1)
echo "http://$NODE_IP:$PROMETHEUS_NODEPORT/"
# 部署
kubectl apply -f alertmanager.yaml
# 查看
kubectl get pods -n monitoring
kubectl get svc -n monitoring
# 查看 nodeport
kubectl get svc -n monitoring | grep alertmanager-main
# 获取访问链接
# 11.11.11.111 为其中一个node ip
NODE_IP='11.11.11.112'
ALERTMANAGER_MAIN_NODEPORT=$(kubectl get svc -n monitoring | grep alertmanager-main | awk '{print $(NF-1)}' | cut -d ':' -f 2 | cut -d '/' -f 1)
echo "http://$NODE_IP:$ALERTMANAGER_MAIN_NODEPORT/"
# 部署
kubectl apply -f grafana.yaml
# 查看
kubectl get pods -n monitoring
kubectl get svc -n monitoring
# 查看 nodeport
kubectl get svc -n monitoring | grep grafana
# 获取访问链接
# 11.11.11.111 为其中一个node ip
NODE_IP='11.11.11.112'
GRAFANA_NODEPORT=$(kubectl get svc -n monitoring | grep grafana | awk '{print $(NF-1)}' | cut -d ':' -f 2 | cut -d '/' -f 1)
echo "http://$NODE_IP:$GRAFANA_NODEPORT/"