简介
这是一个记录收集,他不应该属于一个完整的博客,但是他确实记录了一些有用的东西
该文档主要记录从外部集群对接k8s的各个组件,还有内部集群
场景是这样的:你的监控集群位于外部,不是operator去创建,因此你得手动车权限去对接
安装对接组件(kubestateMetrics)
kube-state-metrics 是一个 Kubernetes 组件,它通过查询 Kubernetes 的 API 服务器,收集关于 Kubernetes 中各种资源(如节点、pod、服务等)的状态信息,并将这些信息转换成 Prometheus 可以使用的指标。
提供下面的指标
- 节点状态信息
- Pod状态信息
- 控制器信息
- Svc信息
- 存储卷信息
- API服务器状态信息
- etcd信息
安装需要下面这些东西
- SA
- Deployment
- ClusterRole/ClusterRoleBind
- ConfigMap
- Service
首先是权限相关得到
- 创建一个SA
- 创建一个ClusterRole(因为监控的是全集群,因此不要限制命名空间)
- 创建一个ClusterRoleBinding,绑定到SA
- 创建一个机密,通过注释,定向到SA上,这样就会产生TOKEN
- 使用命令导出TOKEN,一定要解密 !
apiVersion: v1
kind: ServiceAccount
metadata:
name: kube-state-metrics
namespace: monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: kube-state-metrics-cr
rules:
- apiGroups: [""]
resources:
- configmaps
- secrets
- nodes
- pods
- services
- resourcequotas
- replicationcontrollers
- limitranges
- persistentvolumeclaims
- persistentvolumes
- namespaces
- endpoints
verbs:
- get
- watch
- list
- apiGroups: ["apps"]
resources:
- statefulsets
- daemonsets
- deployments
- replicasets
verbs:
- get
- watch
- list
- apiGroups: ["batch"]
resources:
- horizontalpodautoscalers
verbs:
- get
- watch
- list
- apiGroups: ["*"]
resources: ["*"]
verbs:
- get
- watch
- list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kube-state-metrics-crb
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kube-state-metrics-cr
subjects:
- kind: ServiceAccount
name: kube-state-metrics
namespace: monitoring
---
apiVersion: v1
kind: Secret
metadata:
name: kube-state-metric-secret
namespace: monitoring
annotations:
kubernetes.io/service-account.name: "kube-state-metrics"
type: kubernetes.io/service-account-token
提取token
kubectl get secret -nmonitoring -o yaml | grep token: | cut -f 2 -d ":" | xargs echo | base64 -d
然后记得将token保存到一个文件,后续会用
创建kube-state-mtrics这个服务
需要注意:我们监控的是8081端口,8080端口是给kube-state-mtrics报告自己使用的,因此可以不必将8080端口暴露
apiVersion: apps/v1
kind: Deployment
metadata:
name: kube-state-metrics
namespace: monitoring
spec:
selector:
matchLabels:
app: kube-state-metrics
roles: programe
replicas: 1
template:
metadata:
name: kube-state-metrics
namespace: monitoring
labels:
app: kube-state-metrics
roles: programe
spec:
priorityClassName: system-cluster-critical
serviceAccountName: kube-state-metrics # 细节:必须添加,不添加会出现问题,这是自动挂载SA到POD,后续POD请求K8s就会用这个SA
containers:
- name: kube-state-metrics
image: m.daocloud.io/registry.k8s.io/kube-state-metrics/kube-state-metrics:v2.9.2
ports:
- name: http-metrics
containerPort: 8080
- name: telemetry
containerPort: 8081
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
timeoutSeconds: 5
resources:
limits:
memory: 512Mi
---
apiVersion: v1
kind: Service
metadata:
name: kube-state-metrics-service
namespace: monitoring
labels:
app: kube-state-metrics
role: service
spec:
type: NodePort
selector:
app: kube-state-metrics
roles: programe
ports:
- name: tekemetry
port: 8081
targetPort: 8081
nodePort: 31478
然后就可以apply启动了,启动后这个监控就算上线了
此时修改一下Prometheus的配置文件即可
// 前面省略
scrape_configs:
- job_name: "kube-metrics"
metrics_path: '/metrcs'
static_configs:
- targets: ["127.0.0.1:31478"] // 因为我的Prometheus在一台服务器上,但是没有设置到集群,因此只能用nodePort
监控API-Server
API-Server也是一个核心组件,可以监控下请求比例什么的
我们直接复用刚才得到SA即可,假设你SA是放到文件/root/app/monitoring/token
直接去Prometheus配置文件中添加配置
注意:一定要有SA的Token 还有权限
- 跳过证书
- 指定配置文件
- 指定协议
这里其实有个问题,就是我必须得用管理员权限token,才能正常拿到api-server的指标。。。
scrape_configs:
- job_name: "api-server"
metrics_path: '/metrics'
scheme: https // 指定协议
tls_configs:
insecure_skip_verify: true
bearer_token_file: /root/app/monitoring/token
static_confis:
- targets: [""]
监控controller-manager
监控之前,先去把监听接口改下,controller-manager默认是监听自己,你需要改到控制链路上去才行
- job_name: "controller-manager"
scheme: https
static_configs:
- targets: ["localhost:10257"]
tls_config:
insecure_skip_verify: true
bearer_token_file: /opt/prometheus/prometheus-2.54.0.linux-amd64/token-1.yaml
监控etcd
同理可证
不同的是etcd的metrics是用的80端口,好评
- job_name: "etcd"
static_configs:
- targets: ["localhost:2381"]
cAdvisor
该组件已经内置,无需部署
该组件需要https访问
该组件提供功能如下
- 对资源使用情况进行检测
- 支持Docker等其他容器信息收集
- 检查容器的负载
由于该组件已经内置,准备好一个token即可访问
需要注意,该组件最好不要单独部署,而是采用注册中心的方式部署,他监控的是一台主机的状态,不是所有的主,你得挨个加target
- job_name: cadvisor
metrics_path: "/metrics/cadvisor"
scheme: https
static_configs:
- targets: ["localhost:10250"]
bearer_token_file: /opt/prometheus/prometheus-2.54.0.linux-amd64/token-1.yaml
tls_config:
insecure_skip_verify: true
最后效果
直接看图把