在容器外搭建
Prometheus在容器外搭建非常简单,只需要下载对应的release,启动二进制文件即可。
下载地址:https://prometheus.io/download/
然后可以直接用下面命令启动:
./prometheus --config.file=prometheus.yml
其中prometheus.yaml是主要的配置文件,主要配置信息如下:
global:scrape_interval: 15sevaluation_interval: 15srule_files:# - "first.rules"# - "second.rules"scrape_configs:- job_name: prometheusstatic_configs:- targets: ['localhost:9090']
上面配置信息主要包括三个模块:global,rule_files,scrape_configs。
(1)、global定义Prometheus server全局配置。
- scrape_interval,定义采集频率
- evaluation_interval,定义评估规则的频率,Prometheus使用规则产生的时间序列数据或者产生的警报
(2)、rule_file,用于指定规则,Prometheus使用规则产生的时间序列数据或者产生的警报
(3)、scrape_configs,用于控制监控的资源
Prometheus默认会通过/metrics路径采集metrics,比如:curl http://localhost:9090/metrics 就可以看到相应的资源对象了。
在容器内搭建
1、创建namespace:
# kubectl create ns kube-ops
2、创建configmap,保存我们的主配置文件prometheus.yaml,这样我们要更新配置文件的话就只需要更新这个configmap即可。
prom-configmap.yaml
apiVersion: v1kind: ConfigMapmetadata:name: prometheus-confignamespace: kube-opsdata:prometheus.yaml: |global:scrape_interval: 15sscrape_timeout: 15sscrape_configs:- job_name: 'prometheus'static_configs:- targets: ['localhost:9090']
创建资源:
# kubectl apply -f prom-configmap.yamlconfigmap/prometheus-config created# kubectl get configmap -n kube-opsNAME DATA AGEprometheus-config 1 16s
(3)、创建prometheus的Pod
prom-deploy.yaml
apiVersion: extensions/v1beta1kind: Deploymentmetadata:name: prometheus-deploynamespace: kube-opslabels:app: prometheusspec:selector:matchLabels:app: prometheusreplicas: 1template:metadata:labels:app: prometheusspec:serviceAccountName: prometheus-sacontainers:- name: prometheusimage: prom/prometheus:v2.14.0imagePullPolicy: IfNotPresentcommand:- "/bin/prometheus"args:- "--config.file=/etc/prometheus/prometheus.yaml"- "--storage.tsdb.path=/data/prometheus"- "--storage.tsdb.retention=24h"- "--web.enable-admin-api"- "--web.enable-lifecycle"ports:- name: httpprotocol: TCPcontainerPort: 9090volumeMounts:- name: datamountPath: "/data/prometheus"subPath: prometheus- name: prometheus-configmountPath: "/etc/prometheus"resources:requests:cpu: 100mmemory: 500Milimits:cpu: 100mmemory: 500MisecurityContext:runAsUser: 0volumes:- name: datapersistentVolumeClaim:claimName: prometheus- name: prometheus-configconfigMap:name: prometheus-config
我们把上面定义的configMap通过挂载的形式挂载到容器中,然后我们还要定义一个持久化PVC。
(4)、创建PV,PVC
prom-pvc.yaml
---apiVersion: v1kind: PersistentVolumemetadata:name: prometheus-pvspec:capacity:storage: 10GiaccessModes:- ReadWriteOncepersistentVolumeReclaimPolicy: Recyclenfs:server: xx.xx.xx.xxpath: /data/k8s/prometheus---apiVersion: v1kind: PersistentVolumeClaimmetadata:name: prometheusnamespace: kube-opsspec:accessModes:- ReadWriteOnceresources:requests:storage: 10Gi
(5)、配置RBAC认证
我们在deploy的模板中定义了serviceAccount,我们就需要定义一个serviceAccount的RBAC。
prom-rbac.yaml
---apiVersion: v1kind: ServiceAccountmetadata:name: prometheus-sanamespace: kube-ops---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:name: prometheusrules:- apiGroups:- ""resources:- nodes- services- endpoints- pods- nodes/proxyverbs:- get- list- watch- apiGroups:- ""resources:- configmaps- nodes/metricsverbs:- get- nonResourceURLs:- /metricsverbs:- get---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: prometheusroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: prometheussubjects:- kind: ServiceAccountname: prometheus-sanamespace: kube-ops
(6)、创建Service,用来暴露promethes服务
prom-service.yaml
apiVersion: v1kind: Servicemetadata:name: prometheus-svcnamespace: kube-opsspec:type: NodePortselector:app: prometheusports:- name: prometheus-webport: 9090targetPort: http
(7)、创建配置清单
创建PVC
# kubectl apply -f prom-pvc.yamlpersistentvolume/prometheus-pv createdpersistentvolumeclaim/prometheus created# kubectl get pv -n kube-opsNAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGEprometheus-pv 10Gi RWO Recycle Bound kube-ops/prometheus 7s# kubectl get pvc -n kube-opsNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEprometheus Bound prometheus-pv 10Gi RWO 13s
创建RBAC
# kubectl apply -f prom-rbac.yamlserviceaccount/prometheus-sa createdclusterrole.rbac.authorization.k8s.io/prometheus createdclusterrolebinding.rbac.authorization.k8s.io/prometheus created# kubectl get clusterrole -n kube-ops | grep prometheusprometheus 35s# kubectl get clusterrolebinding -n kube-ops | grep prometheusprometheus 46s
创建Pod
# kubectl apply -f prom-deploy.yamldeployment.extensions/prometheus-deploy created# kubectl get deploy -n kube-opsNAME READY UP-TO-DATE AVAILABLE AGEprometheus-deploy 1/1 1 0 10s# kubectl get pod -n kube-opsNAME READY STATUS RESTARTS AGEprometheus-deploy-694446b7cb-ssdqm 1/1 Running 0 18s
创建Service
# kubectl apply -f prom-service.yamlservice/prometheus-svc created# kubectl get svc -n kube-opsNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEprometheus-svc NodePort 10.68.254.74 <none> 9090:23050/TCP 6
然后就可以通过浏览器访问WEB界面了
