k8s部署prometheus监控
时间: 2023-09-02 12:13:59 浏览: 78
1. 创建命名空间
首先我们需要创建一个命名空间,用于部署prometheus和其他相关组件。
```
kubectl create namespace monitoring
```
2. 创建configmap
接下来我们需要创建一个configmap,用于存储prometheus的配置文件。在这个例子中,我们将使用以下配置文件:
```
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['localhost:9090']
- job_name: node-exporter
static_configs:
- targets: ['localhost:9100']
```
我们将这个配置文件保存为prometheus.yaml,并使用以下命令创建configmap。
```
kubectl create configmap prometheus-config --namespace=monitoring --from-file=prometheus.yaml
```
3. 创建RBAC
我们需要为prometheus创建一些RBAC规则,以便它可以在Kubernetes集群中获取监控数据。
首先,我们需要创建一个ServiceAccount:
```
kubectl create serviceaccount prometheus --namespace=monitoring
```
接下来,我们需要创建一个ClusterRole和一个ClusterRoleBinding:
```
kubectl create -f https://raw.githubusercontent.com/coreos/prometheus-operator/master/example/rbac/prometheus-cluster-role.yaml
kubectl create -f https://raw.githubusercontent.com/coreos/prometheus-operator/master/example/rbac/prometheus-cluster-role-binding.yaml
```
最后,我们需要创建一个Role和一个RoleBinding:
```
kubectl create -f https://raw.githubusercontent.com/coreos/prometheus-operator/master/example/rbac/prometheus-role.yaml
kubectl create -f https://raw.githubusercontent.com/coreos/prometheus-operator/master/example/rbac/prometheus-role-binding.yaml
```
4. 部署prometheus-operator
现在我们可以开始部署prometheus-operator了。prometheus-operator是一个自动化部署prometheus的工具。
kubectl apply -f https://raw.githubusercontent.com/coreos/prometheus-operator/master/bundle.yaml
5. 部署prometheus
接下来我们需要部署prometheus本身。我们将使用prometheus-operator来完成自动化部署。
首先,我们需要创建一个Prometheus对象,它将自动创建一个Deployment和Service。
```
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
labels:
prometheus: prometheus
spec:
replicas: 1
serviceAccountName: prometheus
serviceMonitorSelector:
matchLabels:
app: prometheus
ruleSelector:
matchLabels:
prometheus: prometheus
alerting:
alertmanagers:
- namespace: monitoring
name: alertmanager
port: web
```
将上面的配置保存为prometheus.yaml,并使用以下命令创建Prometheus对象。
```
kubectl apply -f prometheus.yaml
```
6. 部署alertmanager
现在我们需要部署alertmanager,它将用于发送警报通知。
首先,我们需要创建一个configmap,用于存储alertmanager的配置文件。在这个例子中,我们将使用以下配置文件:
```
global:
resolve_timeout: 5m
route:
receiver: 'default-receiver'
group_by:
- alertname
receivers:
- name: 'default-receiver'
email_configs:
- to: 'your-email@example.com'
from: 'alertmanager@example.com'
smarthost: smtp.gmail.com:587
auth_username: 'your-email@example.com'
auth_identity: 'your-email@example.com'
auth_password: 'your-password'
```
我们将这个配置文件保存为alertmanager.yaml,并使用以下命令创建configmap。
```
kubectl create configmap alertmanager-config --namespace=monitoring --from-file=alertmanager.yaml
```
接下来,我们需要创建一个alertmanager对象,它将自动创建一个Deployment和Service。
```
apiVersion: monitoring.coreos.com/v1
kind: Alertmanager
metadata:
name: alertmanager
labels:
alertmanager: alertmanager
spec:
replicas: 1
serviceAccountName: prometheus
configSecret:
name: alertmanager-config
```
将上面的配置保存为alertmanager.yaml,并使用以下命令创建alertmanager对象。
```
kubectl apply -f alertmanager.yaml
```
7. 部署node-exporter
最后,我们需要部署node-exporter,它将用于收集节点的监控数据。
首先,我们需要创建一个DaemonSet,它将自动在每个节点上部署一个node-exporter。
```
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
labels:
app: node-exporter
spec:
selector:
matchLabels:
app: node-exporter
template:
metadata:
labels:
app: node-exporter
spec:
containers:
- name: node-exporter
image: prom/node-exporter:v1.0.1
ports:
- containerPort: 9100
name: http
resources:
limits:
cpu: 200m
memory: 200Mi
requests:
cpu: 100m
memory: 100Mi
volumeMounts:
- name: proc
mountPath: /host/proc
readOnly: true
- name: sys
mountPath: /host/sys
readOnly: true
- name: docker
mountPath: /host/var/lib/docker
readOnly: true
hostNetwork: true
hostPID: true
volumes:
- name: proc
hostPath:
path: /proc
- name: sys
hostPath:
path: /sys
- name: docker
hostPath:
path: /var/lib/docker
```
将上面的配置保存为node-exporter.yaml,并使用以下命令创建DaemonSet。
```
kubectl apply -f node-exporter.yaml
```
现在,您已经成功部署了prometheus、alertmanager和node-exporter。您可以使用以下命令查看它们的状态:
```
kubectl get all -n monitoring
```