redis集羣有三種模式,主從,sentinel(哨兵),cluster。關於這三種模式的介紹和部署,其它文章也有相關介紹,本人蔘考了他人的博客完成了三種模式的部署,博客地址:https://blog.csdn.net/miss1181248983/article/details/90056960。非常感謝此大佬的分享
目前redis使用主從模式,集羣信息如下:
[root@master redis]# redis-cli -h 192.168.122.7 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.122.8,port=6379,state=online,offset=28,lag=0
slave1:ip=192.168.122.9,port=6379,state=online,offset=28,lag=1
master_replid:950b3c2b60b56148577e566ac53af00911527c4e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
[root@master redis]#
[root@master ~]#
以上信息可以看出,此時集羣爲主從模式,192.168.122.7爲master節點,192.168.122.8和192.168.122.9爲slave節點。關於redis的具體操作這兒不做闡述,相關文章很多。下面步驟就是如何利用redis-exporter收集redis性能監控。
1 創建redis-exporter
使用下面的yaml文件創建一個redis-exporter的Deployment‘
[root@master redis-local]# cat redis-exporter.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-exporter001
namespace: monitoring
labels:
k8s-app: redis-exporter001
spec:
selector:
matchLabels:
k8s-app: redis-exporter001
template:
metadata:
labels:
k8s-app: redis-exporter001
spec:
containers:
- name: redis-exporter001
image: oliver006/redis_exporter:latest
args: ["-redis.addr", "redis://192.168.122.7:6379", "-redis.password", "123456"]
ports:
- containerPort: 9121
name: http
[root@master redis-local]# kubectl apply -f redis-exporter.yaml
deployment.apps/redis-exporter001 created
[root@master redis-local]# kubectl logs -f -n monitoring redis-exporter001-5dc78569b5-5lmtf
time="2020-05-26T02:49:21Z" level=info msg="Redis Metrics Exporter v1.6.1 build date: 2020-05-13-02:16:26 sha1: 34c779deb5f9a1e023166b5cf3717d53cdc97053 Go: go1.14.2 GOOS: linux GOARCH: amd64"
time="2020-05-26T02:49:21Z" level=info msg="Providing metrics at :9121/metrics"
此時redis-exporter創建完成
2 創建redis-exporter服務
使用下面的yaml文件創建redis-exporter服務
[root@master redis-local]# cat redis-exporter-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: redis-exporter001
name: redis-exporter001
namespace: monitoring
spec:
type: NodePort
ports:
- name: http
port: 9121
nodePort: 30019
targetPort: http
selector:
k8s-app: redis-exporter001
[root@master redis-local]# kubectl apply -f redis-exporter-service.yaml
service/redis-exporter001 created
[root@master redis-local]# kubectl describe svc -n monitoring redis-exporter001
Name: redis-exporter001
Namespace: monitoring
Labels: k8s-app=redis-exporter001
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"k8s-app":"redis-exporter001"},"name":"redis-exporter001","name...
Selector: k8s-app=redis-exporter001
Type: NodePort
IP: 10.10.126.113
Port: http 9121/TCP
TargetPort: http/TCP
NodePort: http 30019/TCP
Endpoints: 10.124.187.225:9121
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@master redis-local]#
使用nodeip:nodeport進行訪問
此時redis的metrics部署完成
3創建redis-exporter-serverMonitor
使用下面的文件創建redis-exporter-serverMonitor
[root@master redis-local]# cat redis-exporter-serviceMonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
k8s-app: redis-exporter001
name: redis-exporter001
namespace: monitoring
spec:
endpoints:
- interval: 30s
port: http
jobLabel: k8s-app
selector:
matchLabels:
k8s-app: redis-exporter001
[root@master redis-local]# kubectl apply -f redis-exporter-serviceMonitor.yaml
servicemonitor.monitoring.coreos.com/redis-exporter001 created
[root@master redis-local]# kubectl describe servicemonitors.monitoring.coreos.com -n monitoring redis-exporter001
Name: redis-exporter001
Namespace: monitoring
Labels: k8s-app=redis-exporter001
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"monitoring.coreos.com/v1","kind":"ServiceMonitor","metadata":{"annotations":{},"labels":{"k8s-app":"redis-exporter001"},"na...
API Version: monitoring.coreos.com/v1
Kind: ServiceMonitor
Metadata:
Creation Timestamp: 2020-05-26T02:53:46Z
Generation: 1
Resource Version: 574501
Self Link: /apis/monitoring.coreos.com/v1/namespaces/monitoring/servicemonitors/redis-exporter001
UID: 065dc9cc-a872-4630-a387-f4b11193aab6
Spec:
Endpoints:
Interval: 30s
Port: http
Job Label: k8s-app
Selector:
Match Labels:
k8s-app: redis-exporter001
Events: <none>
最後創建一個serviceaccount
[root@master yaml]# kubectl create serviceaccount -n monitoring redis-exporter001
serviceaccount/redis-exporter001 created
以上部署完成後,在prometheus頁面中查看對應的targets
4 grafana面板創建dashboard來監控redis集羣
點擊Import
監控效果如上圖
5 redis寫入數據進行測試
[root@master redis-local]# cat 123.txt |redis-cli -h 192.168.122.7 -a 123456
批量插入數據 ,查看grafana監控
此時redis性能使用率已經在上升。
關於如何監控redis集羣——主從模式到此完成。關於如何監控另外兩種模式,後續文檔會繼續更新。