redis
是当前最为流行的缓存服务,内存存储,通过IO多路复用型使得即使是单线程,也有非常高的性能。学习redis
时需要有redis
环境,这里介绍四种方式,一种是单机模式,一种是主从模式,一种是哨兵模式,一种是集群模式。
单机模式
安装单机版的方式比较多,为了可以实现数据可持久化,这里使用动态存储。并且pv
是由nfs
作为sc
提供。
pvc.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| apiVersion: v1 kind: PersistentVolumeClaim metadata: annotations: labels: app: redis name: redis-pv-claim spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi storageClassName: nfs-client volumeMode: Filesystem
|
cm.yaml
1 2 3 4 5 6 7 8 9 10 11
| [root@master redis-single]# cat cm.yaml apiVersion: v1 data: redis.conf: | port 6379 timeout 3000 tcp-keepalive 0 databases 99 kind: ConfigMap metadata: name: redis-config
|
sts.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| apiVersion: apps/v1 kind: StatefulSet metadata: name: redis labels: app: redis spec: serviceName: redis replicas: 1 selector: matchLabels: app: redis template: metadata: labels: app: redis spec: containers: - name: redis image: redis:5.0.6 imagePullPolicy: IfNotPresent resources: limits: cpu: 450m memory: 4000Mi requests: cpu: 400m memory: 4000Mi env: - name: TZ value: Asia/Shanghai command: ["redis-server","/usr/local/etc/redis/redis.conf","--appendonly yes"] ports: - containerPort: 6379 volumeMounts: - name: vol-conf mountPath: /usr/local/etc/redis - name: vol-data mountPath: /data volumes: - name: vol-conf configMap: name: redis-config - name: vol-data persistentVolumeClaim: claimName: redis-pv-claim
|
svc.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| apiVersion: v1 kind: Service metadata: labels: app: redis name: redis spec: ports: - name: "6379" port: 6379 protocol: TCP targetPort: 6379 selector: app: redis type: NodePort
|
安装
1 2 3 4
| # kubectl create -f pvc.yaml -n redis # kubectl create -f cm.yaml -n redis # kubectl create -f sts.yaml -n redis # kubectl create -f svc.yaml -n redis
|
1 2 3
| # kubectl get svc -n redis NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE redis NodePort 10.1.86.36 <none> 6379:30325/TCP 116s
|
连接
1
| redis-cli -h 10.1.86.36 -p 6379
|
主从模式
主从版redis
使用bitname
的repo
源,通过chart
直接安装。后端存储使用nfs
提供的的sc
1
| # helm pull bitnami/redis
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| global: storageClass: "nfs-client" architecture: replication master: count: 1 startupProbe: enabled: false livenessProbe: enabled: true readinessProbe: enabled: false service: type: NodePort replica: replicaCount: 3 startupProbe: enabled: true livenessProbe: enabled: true readinessProbe: enabled: false service: type: NodePort
|
安装
1
| # helm install redis-cluster -f values.yaml ./ -n redis
|
使用
1 2
| # redis-cli -h redis-cluster-master # redis-cli -h redis-cluster-replicas
|
哨兵模式
chart
包同主从模式,使用不同的配置即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| global: storageClass: "nfs-client" architecture: replication sentinel: enabled: true startupProbe: enabled: false livenessProbe: enabled: false readinessProbe: enabled: false persistence: enabled: true storageClass: "nfs-client" service: type: ClusterIP
|
安装
1
| # helm install redis-sentinel -f values.yaml ./ -n redis
|
使用
1 2 3 4 5 6 7
| # kubectl get svc -n redis NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE redis-sentinel NodePort 10.1.71.104 <none> 30001:30001/TCP,30000:30000/TCP,26379:30641/TCP,6379:30446/TCP 55s redis-sentinel-headless ClusterIP None <none> 6379/TCP,26379/TCP 7m37s redis-sentinel-node-0 NodePort 10.1.215.86 <none> 30003:30003/TCP,30004:30004/TCP,26379:32280/TCP,6379:30801/TCP 55s redis-sentinel-node-1 NodePort 10.1.79.59 <none> 30005:30005/TCP,30006:30006/TCP,26379:31888/TCP,6379:30775/TCP 55s redis-sentinel-node-2 NodePort 10.1.71.91 <none> 30007:30007/TCP,30008:30008/TCP,26379:30772/TCP,6379:32272/TCP 55s
|
密码
1
| kubectl get secret --namespace redis redis-sentinel -o jsonpath="{.data.redis-password}" | base64 -d
|
集群模式
集群版redis
使用bitname
的repo
源,通过chart
直接安装。后端存储使用nfs提供的的sc
1
| # helm pull bitnami/redis-cluster
|
修改配置
1 2 3 4 5 6 7 8 9 10 11
| global: storageClass: "nfs-client" service: type: NodePort redis: livenessProbe: enabled: false readinessProbe: enabled: false startupProbe: enabled: false
|
安装
1
| # helm install redis-cluster -f values.yaml ./ -n redis
|
密码
1
| kubectl get secret --namespace "redis" redis-cluster -o jsonpath="{.data.redis-password}" | base64 -d
|
查看端口
1 2 3 4
| # kubectl get svc -n redis NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE redis-cluster NodePort 10.1.94.93 <none> 6379:32760/TCP 3m28s redis-cluster-headless ClusterIP None <none> 6379/TCP,16379/TCP 3m28s
|
使用
1
| # redis-cli -c -h 10.1.94.93 -p 6379 -a t1vUbLlQCG
|
验证集群信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 10.1.94.93:6379> CLUSTER info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:44 cluster_stats_messages_pong_sent:44 cluster_stats_messages_sent:88 cluster_stats_messages_ping_received:39 cluster_stats_messages_pong_received:44 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:88
|