Kubernetes 1.19.13部署Prometheus 2.30.3 (章节一)
一、Prometheus架构以下是官方提供的架构及其一些相关的生态系统组件二、部署如果想在K8s集群中部署Prometheus监控系统的话,我们需要创建以下资源对象:Namespace:命名空间,为监控系统与业务区分命名空间RBAC认证: Prometheus 需要去访问 Kubernetes相关信息Configmap:保存Prometheus的配置文件Deployment资源对象:管理和部署Po
一、Prometheus架构
以下是官方提供的架构及其一些相关的生态系统组件
二、部署
如果想在K8s集群中部署Prometheus监控系统的话,我们需要创建以下资源对象:
- Namespace:命名空间,为监控系统与业务区分命名空间
- RBAC认证: Prometheus 需要去访问 Kubernetes相关信息
- Configmap:保存Prometheus的配置文件
- Deployment资源对象:管理和部署Pod
- Service资源对象:外部能访问Prometheus UI
2.1 创建Namespace资源对象
[root@k8s-master ~]# kubectl create ns monitor
[root@k8s-master ~]# kubectl get ns
2.2 创建RBAC资源对象YAML文件
[root@k8s-master ~]# mkdir /usr/local/prometheus/ && cd /usr/local/prometheus/
[root@k8s-master prometheus]# vim prometheus_rbac.yaml
# 源YAML文件可到Github下载:https://github.com/shaxiaozz/prometheus
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
namespace: monitor
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups:
- ""
resources:
- nodes
- services
- endpoints
- pods
- nodes/proxy
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- configmaps
- nodes/metrics
verbs:
- get
- nonResourceURLs:
- /metrics
verbs:
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: prometheus
namespace: monitor
[root@k8s-master prometheus]# kubectl apply -f prometheus_rbac.yaml
[root@k8s-master prometheus]# kubectl get ClusterRoleBinding | grep prometheus
[root@k8s-master prometheus]# kubectl get ServiceAccount -n monitor
2.3 创建Configmap资源对象YAML文件
[root@k8s-master prometheus]# vim prometheus_configmap.yaml
# 源YAML文件可到Github下载:https://github.com/shaxiaozz/prometheus
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-configmap
namespace: monitor
data:
prometheus.yml: |
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['127.0.0.1:9090']
- job_name: 'k8s集群'
static_configs:
- targets:
- k8s-master:9100
- k8s-node1:9100
- k8s-node2:9100
[root@k8s-master prometheus]# kubectl apply -f prometheus_configmap.yaml
[root@k8s-master prometheus]# kubectl get cm -n monitor
2.4 创建Deployment资源对象YAML文件
[root@k8s-master prometheus]# vim prometheus_deploy.yaml
# 源YAML文件可到Github下载:https://github.com/shaxiaozz/prometheus
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
namespace: monitor
labels:
name: prometheus
spec:
replicas: 1
selector:
matchLabels:
name: prometheus
# 设置滚动更新策略
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
# 添加注释,configmap更新后,修改版本
annotations:
configmap: v3.2
labels:
name: prometheus
spec:
# 为pod添加角色授权
serviceAccountName: prometheus
serviceAccount: prometheus
# 初始化容器。修改prometheus的数据目录权限
initContainers:
- name: chown-data
image: busybox
command: [chown, -R, "nobody:nobody", /prometheus]
volumeMounts:
- name: prometheus-data
mountPath: /prometheus
# 为容器添加hosts解析
hostAliases:
- ip: 192.168.160.10
hostnames:
- "k8s-master"
- ip: 192.168.160.11
hostnames:
- "k8s-node1"
- ip: 192.168.160.12
hostnames:
- "k8s-node2"
# 固定调度pods
nodeName: k8s-node1
containers:
- name: prometheus
image: prom/prometheus:v2.30.3
imagePullPolicy: IfNotPresent
# 修改容器启动命令
command: ["/bin/prometheus"]
args: ["--config.file=/config/prometheus.yml", "--storage.tsdb.path=/prometheus", "--storage.tsdb.retention=168h", "--web.enable-lifecycle", "--web.enable-admin-api"]
# 容器资源限制
resources:
limits:
memory: 2500Mi
cpu: 800m
requests:
memory: 512Mi
cpu: 200m
# 容器存活健康检测
livenessProbe:
httpGet:
port: 9090
path: /metrics
initialDelaySeconds: 15
periodSeconds: 5
failureThreshold: 3
# 容器就绪健康检测
readinessProbe:
httpGet:
port: 9090
path: /metrics
initialDelaySeconds: 5
periodSeconds: 5
failureThreshold: 3
# 容器路径挂载
volumeMounts:
- mountPath: /prometheus
name: prometheus-data
- name: config
mountPath: /config
readOnly: true
- name: alert-rule-config
mountPath: /config/rules
readOnly: true
# 设置容器端口
ports:
- containerPort: 9090
protocol: TCP
name: http
# 设置configmap资源对象挂载点
volumes:
- name: prometheus-data
hostPath:
path: /usr/local/prometheus/data
- name: config
configMap:
name: prometheus-configmap
注意:
此处的deployment yaml中定义了pod只能在k8s-node1工作节点上运行。需要到k8s-node1服务器上创建prometheus数据的存储路径:/usr/local/prometheus/data。由于目前我们还没有共享存储,因此只能把Pod固定到一个工作节点上。由于此处使用了"nodeName“字段,因此K8s在分配Pod工作节点时,是不需要经过scheduler调度器的。
在容器路径挂载下,由于configmap的挂载模式,默认会以目录的方式挂载进去,因此会覆盖挂载路径下的原有文件。官方推荐使用subPath字段,以文件的方式挂载进去,但是这种挂载进去之后,configmap将无法支持热加载。因此我们每次修改prometheus配置文件后,都需滚动升级一遍prometheus的deployment。(你可以通过命令参数把配置文件放到一个新的目录,并且把subPath字段去了。)目前已更新配置文件路径,取消了subPath字段。
容器启动命令参数说明:
- –config.file=/etc/prometheus/prometheus.yml:prometheus配置文件路径
- –storage.tsdb.path=/prometheus:prometheus数据存储路径
- –storage.tsdb.retention=24h:prometheus数据默认存储多久
- –web.enable-lifecycle:开启热加载配置文件 --web.enable-admin-api:开启HTTP API的管理访问
[root@k8s-master prometheus]# kubectl apply -f prometheus_deploy.yaml
[root@k8s-master prometheus]# kubectl get pods -n monitor -o wide
[root@k8s-master prometheus]# kubectl describe pods prometheus-66f949f988-sbxcl -n monitor
可以看到我们在yaml文件中定义的init容器已经运行完成,并且prometheus也运行完成了。我们可以使用logs查看一下启动日志
[root@k8s-master prometheus]# kubectl logs prometheus-66f949f988-sbxcl -n monitor
2.5 创建Service资源对象YAML文件
[root@k8s-master prometheus]# vim prometheus_service.yaml
# 源YAML文件可到Github下载:https://github.com/shaxiaozz/prometheus
apiVersion: v1
kind: Service
metadata:
name: prometheus
namespace: monitor
labels:
name: prometheus
spec:
selector:
name: prometheus
type: NodePort
ports:
- port: 9090
targetPort: 9090
nodePort: 30090
注意:此次的nodePort为固定的30090,如未指定此参数,则nodeport为随机端口。有需要的朋友也可以将Service的类型设置为Cluster,然后结合Ingress进行外网的暴露。如果修改了Pod的名称,请在标签选择器selector的字段下,对应回来。不然Service资源无法关联到正确的Pod,那流量将无法可达。
[root@k8s-master prometheus]# kubectl apply -f prometheus_service.yaml
[root@k8s-master prometheus]# kubectl get svc,ep -n monitor
我们可以访问任意一个节点的30090端口去查看prometheus的web界面
http://Node_ip:30090/
好了,第一章节我们已经成功将Prometheus部署到K8s集群中,并且也通过configmap资源对象保存配置文件,并对prometheus的监控数据做了持久化存储等等一系列的操作。我们下几节将围绕着怎么样监控K8s相关资源展示说明。如果到了github获取文件的。麻烦点个小星星噢。小小的鼓励。
更多推荐
所有评论(0)