Redis
Redis 简介
Redis 是完全[[开源]]的,遵守 BSD 协议,是一个高性能的 key-value 数据库。
Redis 与其他 key - value 缓存产品有以下三个特点:
Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
Redis支持数据的备份,即master-slave模式的数据备份。
Redis客户端
Redis K8S集群部署
版本:redis 6.2 k8s 1.28.1 文件
[[#pv.yaml]]
[[#pvc.yaml]]
[[#redis-cluster.yaml]]
[[#roles.sh]]
pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: data0
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/data0
storageClassName: local-storage
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: data1
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/data1
storageClassName: local-storage
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: data2
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/data2
storageClassName: local-storage
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: data3
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/data3
storageClassName: local-storage
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: data4
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/data4
storageClassName: local-storage
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: data5
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/data5
storageClassName: local-storagepvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-redis-0
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 1Gi
volumeName: data0
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-redis-1
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 1Gi
volumeName: data1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-redis-2
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 1Gi
volumeName: data2
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-redis-3
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 1Gi
volumeName: data3
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-redis-4
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 1Gi
volumeName: data4
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-redis-5
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 1Gi
volumeName: data5redis-cluster.yaml
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START gke_manifests_redis_cluster_statefulset_redis]
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
spec:
serviceName: "redis-cluster"
replicas: 6
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
appCluster: redis-cluster
spec:
terminationGracePeriodSeconds: 20
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- redis
topologyKey: kubernetes.io/hostname
containers:
- name: redis
image: "172.25.0.50/k8s/redis:6.2"
command:
- "redis-server"
args:
- "/conf/redis.conf"
- "--protected-mode"
- "no"
resources:
requests:
cpu: "100m"
memory: "100Mi"
ports:
- name: redis
containerPort: 6379
protocol: "TCP"
- name: cluster
containerPort: 16379
protocol: "TCP"
# [START gke_manifests_redis_cluster_statefulset_redis_probes]
startupProbe:
periodSeconds: 5
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 20
tcpSocket:
port: redis
livenessProbe:
periodSeconds: 5
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 5
exec:
command: ["sh", "-c", "/probes/liveness.sh"]
readinessProbe:
periodSeconds: 5
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 5
exec:
command: ["sh", "-c", "/probes/readiness.sh"]
# [END gke_manifests_redis_cluster_statefulset_redis_probes]
volumeMounts:
- name: conf
mountPath: /conf
readOnly: false
- name: data
mountPath: /data
readOnly: false
- name: probes
mountPath: /probes
readOnly: true
volumes:
- name: conf
configMap:
name: redis-cluster
defaultMode: 0755
- name: probes
configMap:
name: redis-probes
defaultMode: 0555
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
# [END gke_manifests_redis_cluster_statefulset_redis]
---
# [START gke_manifests_redis_configmap_configmap_redis_cluster]
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-cluster
data:
redis.conf: |+
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file /data/nodes.conf
appendonly yes
protected-mode no
dir /data
port 6379
---
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-probes
data:
# [START gke_manifests_redis_configmap_configmap_redis_cluster_probes]
readiness.sh: |-
#!/bin/sh
pingResponse="$(redis-cli -h localhost ping)"
if [ "$?" -eq "124" ]; then
echo "PING timed out"
exit 1
fi
if [ "$pingResponse" != "PONG"]; then
echo "$pingResponse"
exit 1
fi
liveness.sh: |-
#!/bin/sh
pingResponse="$(redis-cli -h localhost ping | head -n1 | awk '{print $1;}')"
if [ "$?" -eq "124" ]; then
echo "PING timed out"
exit 1
fi
if [ "$pingResponse" != "PONG"] && [ "$pingResponse" != "LOADING" ] && [ "$pingResponse" != "MASTERDOWN" ]; then
echo "$pingResponse"
exit 1
fi
# [END gke_manifests_redis_configmap_configmap_redis_cluster_probes]
# [END gke_manifests_redis_configmap_configmap_redis_cluster]
---
apiVersion: v1
kind: Service
metadata:
name: redis-cluster
spec:
clusterIP: 172.24.11.151
ports:
- name: redis-port
port: 6379
protocol: TCP
targetPort: 6379
selector:
app: redis
appCluster: redis-cluster
sessionAffinity: None
type: ClusterIP
# [END gke_manifests_redis_service_service_redis_cluster]
---
# [START gke_quickstarts_hello_app_redis_manifests_pbd_minavailable]
# apiVersion: policy/v1
# kind: PodDisruptionBudget
# metadata:
# name: redis-pdb
# spec:
# minAvailable: 3
# selector:
# matchLabels:
# app: redis
# [END gke_quickstarts_hello_app_redis_manifests_pbd_minavailable]roles.sh
当所有pod启动后,执行以下脚本用来创建集群
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START gke_quickstarts_hello_app_redis_manifests_roles]
#!/bin/bash
# Usage: ./roles.sh
urls=$(kubectl get pods -l app=redis -o jsonpath='{range.items[*]}{.status.podIP} ')
command="kubectl exec -it redis-0 -- redis-cli --cluster create --cluster-replicas 1 "
for url in $urls
do
command+=$url":6379 "
done
echo "Executing command: " $command
$command
# [END gke_quickstarts_hello_app_redis_manifests_roles]redis集群报错:(error) MOVED 解决方法
在使用 redis-cli 连接 redis 集群,进行数据操作时,有报错
./redis-cli -h 172.8.5.202
192.24.54.1:6379> get name
(error) MOVED 5798 172.8.5.202:6379解决方法: 这种情况一般是因为启动 redis-cli 时没有设置集群模式所导致。
启动时使用 -c 参数来启动集群模式,命令如下:
./redis-cli -h 172.8.5.202 -c最后更新于