k8s Helm安装Prometheus Operator

Ubuntu 18 Kubernetes集群的安装和部署 以及Helm的安装完成了k8s的集群和helm的安装,今天我们来看看Prometheus的监控怎么搞。Prometheus Operator 是 CoreOS 开发的基于 Prometheus 的 Kubernete s监控方案,也可能是目前功能最全面的开源方案。更多信息可以查看https://github.com/coreos/prometheus-operator

创建命名空间

为方便管理,创建一个单独的 Namespace monitoring,Prometheus Operator 相关的组件都会部署到这个 Namespace。

kubectl create namespace monitoringk8s Helm安装Prometheus Operator

使用Helm安装Prometheus Operator

Prometheus Operator 所有的组件都打包成 Helm Chart,安装部署非常方便。

helm install --name prometheus-operator --set rbacEnable=true --namespace=monitoring stable/prometheus-operator
#helm install --name prometheus-operator --namespace=monitoring stable/prometheus-operator
#helm del --purge prometheus-operator
#removed CRDS
#kubectl delete crd prometheuses.monitoring.coreos.com
#kubectl delete crd prometheusrules.monitoring.coreos.com
#kubectl delete crd servicemonitors.monitoring.coreos.com
#kubectl delete crd podmonitors.monitoring.coreos.com
#kubectl delete crd alertmanagers.monitoring.coreos.com
kubectl get all -n monitoring #查看创建的资源
helm list #查看安装后的release
kubectl get svc -n monitoring #查看访问类型
# kubectl get svc,pod -n monitoring #查看状态
#kubectl describe pod prometheus-operator-prometheus-node-exporter-xxx -n monitoring
#kubectl get po --all-namespaces -o=jsonpath="{range .items[*]}{.spec.nodeName}{'\t'}{.spec.hostNetwork}{'\t'}{.spec.hostNetwork}{'\t'}{.spec.containers..containerPort}{'\n'}{end}" #查看端口信息

方法1.修改 alertmanager、prometheus、grafana的访问类型为NodePort

kubectl edit svc prometheus-operator-grafana -n monitoring #grafana的访问类型 修改type为NodePort
kubectl edit svc prometheus-operator-alertmanager -n monitoring
kubectl edit svc prometheus-operator-prometheus -n monitoring
kubectl get svc -n monitoring #查看修改后的访问类型

k8s Helm安装Prometheus Operator

修改 kubelet 打开只读端口

prometheus 需要访问 kubelet 的 10255 端口获取 metrics。但是默认情况下 10255 端口是不开放的,会导致 prometheus 上有 unhealthy。打开只读端口需要编辑所有节点的vi /var/lib/kubelet/config.yaml (我这里有图形界面可以用gedit命令)文件,加入以下内容

port:
readOnlyPort: #增加此行

重启 kubelet 服务  systemctl restart kubelet.service
访问地址为http://nodeip:30292/, 默认的用户名/密码为:admin/prom-operator,登陆后如下图:

k8s Helm安装Prometheus Operator

k8s Helm安装Prometheus Operator

如果想和我一样遇到 prometheus-operator-prometheus-node-exporter 1 node(s) didn't match node selector, 2 node(s) didn't have free ports for the requested pod ports., 我这里是因为9100端口被占用,直接修改吧:

k8s Helm安装Prometheus Operator

k8s Helm安装Prometheus Operatork8s Helm安装Prometheus Operator

方法2.通过 Ingress 来暴漏服务

新建文件prometheus-ingress.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
namespace: monitoring
name: prometheus-ingress
spec:
rules:
- host: grafana.domain.com
http:
paths:
- backend:
serviceName: prometheus-operator-grafana
servicePort:
- host: prometheus.domain.com
http:
paths:
- backend:
serviceName: prometheus-operator-prometheus
servicePort:
- host: alertmanager.domain.com
http:
paths:
- backend:
serviceName: prometheus-operator-alertmanager
servicePort:

执行命令

kubectl apply -f prometheus-ingress.yaml

修改hosts文件:

192.168.100.11 grafana.domain.com
192.168.100.11 prometheus.domain.com
192.168.100.11 alertmanager.domain.com
#192.168.100.11 是上一篇文章的边缘服务器

效果如图:

k8s Helm安装Prometheus Operatork8s Helm安装Prometheus Operator

k8s Helm安装Prometheus Operatork8s Helm安装Prometheus Operator

k8s Helm安装Prometheus Operatork8s Helm安装Prometheus Operator

以下修改不确定有效,算是备注吧

prometheus 通过 4001 端口访问 etcd metrics,但是 etcd 默认监听 2379。解决方法是在 /etc/kubernetes/manifests/etcd.yaml

增加k8s-app: etcd-server 然后重启 systemctl restart kubelet.service

k8s Helm安装Prometheus Operatork8s Helm安装Prometheus Operator

由于 kube-controller-manager 和 kube-scheduler 默认监听 127.0.0.1 ,prometheus 无法通过本机地址获取数据,需要修改kube-controller-manager 和 kube-scheduler 监听地址。
解决办法如下:/etc/kubernetes/manifests/kube-controller-manager.yaml 增加 k8s-app: kube-controller-manager

k8s Helm安装Prometheus Operatork8s Helm安装Prometheus Operator

/etc/kubernetes/manifests/kube-scheduler.yaml增加 k8s-app: kube-scheduler

k8s Helm安装Prometheus Operatork8s Helm安装Prometheus Operator

参考:

prometheus-operator

使用 Prometheus Operator 监控 Kubernetes

全手动部署prometheus-operator监控Kubernetes集群遇到的坑

上一篇:LNMP完整安装教程


下一篇:转:Web页面通过URL地址传递参数常见问题及检测方法