# Install Prometheus and Grafana on Kubenetes cluster ###### tags: `Kubernetes` > You can find the code from [charlie6679/k8s-prometheus](https://github.com/charlie6679/k8s-prometheus) ## Step 1 Install node-expoter > create a namespace "monitoring" ``` kubectl apply -f prometheus-ns.yml ``` > create node-exporter daemonset ``` kubectl apply -f node-exporter-daemonset.yml ``` > create node-exporter service ``` kubectl apply -f node-exporter-service.yml ``` Now you can visit <node_ip>:9100 to check. ## Step 2 Install kube-state-metrics > create ServiceAccount ``` kubectl apply -f kube-state-metrics-ServiceAccount.yml ``` > create kube-state-metrics deployment ``` kubectl apply -f kube-state-metrics-deploy.yml ``` > create kube-state-metrics service ``` kubectl apply -f kube-state-metrics-service.yml ``` ## Step 3 Install Prometheus > create ServiceAccount ``` kubectl apply -f prometheus-k8s-ServiceAccount.yml ``` > create configmap ``` kubectl apply -f prometheus-config-configmap.yml ``` > create Prometheus Server Deployment ``` kubectl apply -f prometheus-deploy.yml ``` > create Prometheus Server Service ``` kubectl apply -f prometheus-service.yml ``` > check the Node_Port ``` kubectl get service -n monitoring ``` Now can visit <Node_IP>:<Node_Port> to see Prometheus. ## Step 4 Install Grafana > create Grafana Server Deployment ``` kubectl apply -f grafana-deploy.yml ``` > create Grafana Server Service ``` kubectl apply -f grafana-service.yml ``` > check the Nose_Port ``` kubectl get service -n monitoring ``` Now can visit <Node_IP>:<Node_Port> to see Grafana. ## Step 5 Install Nginx ingress-controller ``` kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.34.1/deploy/static/provider/baremetal/deploy.yaml ``` ## Create ingress > Create Grafana Service Ingress ``` kubectl apply -f grafana-ingress.yml ``` > get the ingress IP ``` kubrctl get ing ``` > set the addreee to the host /etc/hosts > chek the 80 port mapping to which port ``` kubectl get service -n ingress-nginx ``` > use <host_name><Node_port> to visit grafana ## Design your dashboard. > Click configuration -> data sources. > > Search prometheus and select. > > Type the "http://<master ip>:<prometheus service mapping port>" on URL than click "Save & Test". > > Click Create -> dashboard to add panel. > > In query you can choose the data. > > Or you can go google "Grafana dashboard" to fin the ID. > > Click Create -> import to import the dashboard. ## Reference ### Prometheus and Grafana : > https://github.com/xingao0803/Prometheus > > https://www.servicemesher.com/blog/prometheus-monitor-k8s-2/ > ### Nginx-ingress controller : > https://kubernetes.github.io/ingress-nginx/deploy/#bare-metal