Kubernetes 持久化安裝 Prometheus

選摘:https://i4t.com/4132.html


prometheus的方式有很多,為了兼容k8s環境,我們將prometheus搭建在k8s裡,除了使用docker鏡像的方式安裝,還可以使用二進制的方式進行安裝,支持mac、Linux、windows

二進制下載地址:https://prometheus.io/download 下

我們prometheus採用nfs掛載方式來存儲數據,同時使用configMap管理配置文件。並且我們將所有的prometheus存儲在kube-system

#建議將所有的prometheus yaml文件存在一塊
mkdir /opt/prometheus -p && cd /opt/prometheus

#生成配置文件

cat >> prometheus.configmap.yaml <apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: kube-system
data:
prometheus.yml: |
global:
scrape_interval: 15s
scrape_timeout: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
EOF

# 配置文件解釋(這裡的configmap實際上就是prometheus的配置)
上面包含了3個模塊global、rule_files和scrape_configs

其中global模塊控制Prometheus Server的全局配置
scrape_interval:表示prometheus抓取指標數據的頻率,默認是15s,我們可以覆蓋這個值
evaluation_interval:用來控制評估規則的頻率,prometheus使用規則產生新的時間序列數據或者產生警報

rule_files模塊制定了規則所在的位置,prometheus可以根據這個配置加載規則,用於生產新的時間序列數據或者報警信息,當前我們沒有配置任何規則,後期會添加

scrape_configs用於控制prometheus監控哪些資源。由於prometheus通過http的方式來暴露它本身的監控數據,prometheus也能夠監控本身的健康情況。在默認的配置有一個單獨的job,叫做prometheus,它採集prometheus服務本身的時間序列數據。這個job包含了一個單獨的、靜態配置的目標;監聽localhost上的9090端口。
prometheus默認會通過目標的/metrics路徑採集metrics。所以,默認的job通過URL:http://localhost:9090/metrics採集metrics。收集到時間序列包含prometheus服務本身的狀態和性能。如果我們還有其他的資源需要監控,可以直接配置在該模塊下即可

我們這裡暫時只配置了對 prometheus 的監控,然後創建該資源對象:

[root@abcdocker prometheus]# kubectl create -f prometheus.configmap.yaml
configmap/prometheus-config created

創建完畢後,如果我們不放心可以手動的檢查一遍

[root@abcdocker prometheus]# kubectl get configmaps -n kube-system |grep prometheus
prometheus-config 1 2m23s

配置文件創建完成,如果以後我們有新的資源需要被監控,我們只需要將ConfigMap對象更新即可,現在我們開始創建prometheus的Pod資源

cat >>prometheus.deploy.yaml <apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: prometheus
namespace: kube-system
labels:
app: prometheus
spec:
template:
metadata:
labels:
app: prometheus
spec:
serviceAccountName: prometheus
containers:
- image: prom/prometheus:v2.4.3
name: prometheus
command:
- "/bin/prometheus"
args:
- "--config.file=/etc/prometheus/prometheus.yml"
- "--storage.tsdb.path=/prometheus"
- "--storage.tsdb.retention=30d"
- "--web.enable-admin-api" # 控制對admin HTTP API的訪問,其中包括刪除時間序列等功能
- "--web.enable-lifecycle" # 支持熱更新,直接執行localhost:9090/-/reload立即生效
ports:
- containerPort: 9090
protocol: TCP
name: http
volumeMounts:
- mountPath: "/prometheus"
subPath: prometheus
name: data
- mountPath: "/etc/prometheus"
name: config-volume
resources:
requests:
cpu: 100m
memory: 512Mi
limits:
cpu: 100m
memory: 512Mi
securityContext:
runAsUser: 0
volumes:

- name: data
persistentVolumeClaim:
claimName: prometheus
- configMap:
name: prometheus-config
name: config-volume
EOF

這裡稍微講解一下配置參數

我們在啟動程序的時候,除了指定prometheus.yaml(configmap)以外,還通過storage.tsdb.path指定了TSDB數據的存儲路徑、通過storage.tsdb.rentention設置了保留多長時間的數據,還有下面的web.enable-admin-api參數可以用來開啟對admin api的訪問權限,參數web.enable-lifecyle用來開啟支持熱更新,有了這個參數之後,prometheus.yaml(configmap)文件只要更新了,通過執行localhost:9090/-/reload就會立即生效

我們添加了一行securityContext,,其中runAsUser設置為0,這是因為prometheus運行過程中使用的用戶是nobody,如果不配置可能會出現權限問題


NFS搭建步驟,步驟簡單不多說,不會的百度

for i in 10.4.82.138 10.4.82.139 10.4.82.140 10.4.82.142;do ssh root@$i "yum install nfs-utils rpcbind -y";done
接著我們在任意一臺集群上搭建nfs,其他的服務器主要是掛載

我這裡使用10.4.82.138

NFS服務器操作如下
mkdir -p /data/k8s
systemctl start rpcbind

systemctl enable rpcbind
systemctl enable nfs
echo "/data/k8s 10.4.82.0/24(rw,no_root_squash,sync)" >>/etc/exports
#IP改成網段


其他k8s節點直接啟動rpcbind並且掛載目錄就可以
systemctl start rpcbind
systemctl enable rpcbind
mkdir /data/k8s -p
mount -t nfs 10.4.82.138:/data/k8s /data/k8s

#創建完成你們自己測一下就可以了

prometheus.yaml文件對應的ConfigMap對象通過volume的形式掛載進Pod,這樣ConfigMap更新後,對應的pod也會熱更新,然後我們在執行上面的reload請求,prometheus配置就生效了。除此之外,對了將時間數據進行持久化,我們將數據目錄和一個pvc對象進行了綁定,所以我們需要提前創建pvc對象

cat >>prometheus-volume.yaml <apiVersion: v1
kind: PersistentVolume
metadata:
name: prometheus
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
server: 10.4.82.138
path: /data/k8s

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: prometheus

namespace: kube-system
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
EOF

#nfs
server nfs服務器ip
path 掛載點,提前掛在好,確保可以寫入

關於NFS配置掛載目錄的之類的就不介紹了,注意好權限,前期允許所有用戶創建文件

這裡通過一個簡單的NFS作為存儲後端創建一個pv & pvc

[root@abcdocker prometheus]# kubectl create -f prometheus-volume.yaml
persistentvolume/prometheus created
persistentvolumeclaim/prometheus created

創建完,我們可以進行檢查一下

[root@abcdocker prometheus]# kubectl get pvc --all-namespaces
NAMESPACE NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
kube-system prometheus Bound prometheus 10Gi RWO 47s
[root@abcdocker prometheus]# kubectl get pv prometheus
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
prometheus 10Gi RWO Recycle Bound kube-system/prometheus 51s

這裡稍微提示一下,我們創建的pv和pvc大小都是10g,只是測試存儲為10g。線上可以修改為200或者更多,一般prometheus數據保留15-30天就可以,如果數據量過大建議使用TSBD分佈式存儲

我們這裡還需要創建rbac認證,因為prometheus需要訪問k8s集群內部的資源

cat >>prometheus-rbac.yaml <apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
namespace: kube-system
---
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/v1beta1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:

- kind: ServiceAccount
name: prometheus
namespace: kube-system
EOF

由於我們要獲取的資源,在每一個namespace下面都有可能存在,所以我們這裡使用的是ClusterRole的資源對象,nonResourceURLs是用來對非資源型metrics進行操作的權限聲明

創建rbac文件
[root@abcdocker prometheus]# kubectl create -f prometheus-rbac.yaml
serviceaccount/prometheus created
clusterrole.rbac.authorization.k8s.io/prometheus created
clusterrolebinding.rbac.authorization.k8s.io/prometheus created

我們將ConfigMap volume rbac 創建完畢後,就可以創建prometheus.deploy.yaml了,運行prometheus服務

[root@abcdocker prometheus]# kubectl create -f prometheus.deploy.yaml
deployment.extensions/prometheus created

[root@abcdocker prometheus]# kubectl get pod -n kube-system |grep prometheus
prometheus-dd856f675-jn9v2 1/1 Running 0 15s

#這裡1/1 狀態為Running即可

現在我們prometheus服務狀態是已經正常了,但是我們在瀏覽器是無法訪問prometheus的 webui服務。那麼我們還需要創建一個service

cat >>prometeheus-svc.yaml <apiVersion: v1
kind: Service
metadata:
name: prometheus
namespace: kube-system

labels:
app: prometheus
spec:
selector:
app: prometheus
type: NodePort
ports:
- name: web
port: 9090
targetPort: http
EOF

為了方便測試,我這裡使用的是NodePort,我們也可以創建一個Ingress對象使用域名訪問

[root@abcdocker prometheus]# kubectl create -f prometeheus-svc.yaml
service/prometheus created

[root@abcdocker prometheus]# kubectl get svc -n kube-system |grep prometheus
prometheus NodePort 10.101.143.162 <none> 9090:32331/TCP 18s
/<none>

這裡定義的端口為32331,我們直接在瀏覽器上任意節點輸入ip+端口即可

Kubernetes 持久化安裝 Prometheus

image_1ddieti6qjeg12vs8tg17131cmq9.png-77kB

Kubernetes 持久化安裝 Prometheus

我們可以查看一下當前監控規則

默認prometheus會監控自己

Status-->Targets

Kubernetes 持久化安裝 Prometheus

我們查看一下數據,是否收集到數據

Kubernetes 持久化安裝 Prometheus

比如我們這裡就選擇scrape_duration_seconds這個指標,然後點擊Execute,如果這個時候沒有查詢到任何數據,我們可以切換到Graph這個 tab 下面重新選擇下時間,選擇到當前的時間點,重新執行,就可以看到類似於下面的圖表數據了:

一定要設置好時間,否則不出圖

Kubernetes 持久化安裝 Prometheus


分享到:


相關文章: