一、Redis简介
Redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。
Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。存盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。
Redis的官网地址,非常好记,是redis.io。(域名后缀io属于国家域名,是british Indian Ocean territory,即英属印度洋领地),Vmware在资助着redis项目的开发和维护。
从2010年3月15日起,Redis的开发工作由VMware主持。从2013年5月开始,Redis的开发由Pivotal赞助。
二、创建StorageClass、Namespace
1、创建StorageClass
[root@cmp-k8s-dev-master01 ~]# mkdir /opt/kube-redis && cd /opt/kube-redis
[root@cmp-k8s-dev-master01 kube-redis]# vim kube-redis-storageclass.yaml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: nfs-redis
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
[root@cmp-k8s-dev-master01 kube-redis]# kubectl apply -f kube-redis-storageclass.yaml
storageclass.storage.k8s.io/nfs-redis created
2、创建Namespace
[root@cmp-k8s-dev-master01 kube-redis]# vim kube-redis-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: kube-redis
labels:
app: redis
[root@cmp-k8s-dev-master01 kube-redis]# kubectl apply -f kube-redis-namespace.yaml
namespace/kube-redis created
三、部署Redis
1、创建Redis的PV
注意:这里采用的是NFS,前提需要部署NFS,这里就不演示了
[root@cmp-k8s-dev-master01 kube-redis]# showmount -e 10.10.50.126
Export list for 10.10.50.126:
/data/redis-6 10.10.50.0/24
/data/redis-5 10.10.50.0/24
/data/redis-4 10.10.50.0/24
/data/redis-3 10.10.50.0/24
/data/redis-2 10.10.50.0/24
/data/redis-1 10.10.50.0/24
[root@cmp-k8s-dev-master01 kube-redis]# vim kube-redis-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis-pv1
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-redis
nfs:
server: 10.10.50.126
path: "/data/redis-1"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis-pv2
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-redis
nfs:
server: 10.10.50.126
path: "/data/redis-2"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis-pv3
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-redis
nfs:
server: 10.10.50.126
path: "/data/redis-3"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis-pv4
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-redis
nfs:
server: 10.10.50.126
path: "/data/redis-4"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis-pv5
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-redis
nfs:
server: 10.10.50.126
path: "/data/redis-5"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis-pv6
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-redis
nfs:
server: 10.10.50.126
path: "/data/redis-6"
[root@cmp-k8s-dev-master01 kube-redis]# kubectl apply -f kube-redis-pv.yaml
persistentvolume/redis-pv1 created
persistentvolume/redis-pv2 created
persistentvolume/redis-pv3 created
persistentvolume/redis-pv4 created
persistentvolume/redis-pv5 created
persistentvolume/redis-pv6 created
2、创建Redis的ConfigMap
[root@cmp-k8s-dev-master01 kube-redis]# vim kube-redis-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-configmap
namespace: kube-redis
labels:
app: redis-configmap
data:
redis.conf: |
bind 0.0.0.0
port 6379
dir /data
cluster-enabled yes
cluster-config-file /data/nodes.conf
cluster-node-timeout 15000
cluster-announce-bus-port 16379
appendonly yes
masterauth P8eUNqTvkf35CXT2
requirepass P8eUNqTvkf35CXT2
[root@cmp-k8s-dev-master01 kube-redis]# kubectl apply -f kube-redis-configmap.yaml
configmap/redis-configmap created
3、创建Redis的statefulSet
[root@cmp-k8s-dev-master01 kube-redis]# vim kube-redis-statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis-cluster
namespace: kube-redis
spec:
serviceName: redis-cluster
replicas: 6
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
#image: bitnami/redis:6.0.16
image: redis:6.0.16
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- |
set -x
redis-server /config/redis.conf --cluster-announce-ip "$(MY_POD_IP)"
env:
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
ports:
- name: redis
containerPort: 6379
- name: cluster
containerPort: 16379
volumeMounts:
- name: config
mountPath: /config
- name: redis-data
mountPath: /data
volumes:
- name: config
configMap:
name: redis-configmap
defaultMode: 0755
items:
- key: redis.conf
path: redis.conf
volumeClaimTemplates:
- metadata:
name: redis-data
spec:
accessModes: [ "ReadWriteMany" ]
storageClassName: "nfs-redis"
resources:
requests:
storage: 10Gi
selector:
matchLabels:
app: redis
[root@cmp-k8s-dev-master01 kube-redis]# kubectl apply -f kube-redis-statefulset.yaml
statefulset.apps/redis-cluster created
4、创建Redis的Service
[root@cmp-k8s-dev-master01 kube-redis]# vim kube-redis-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-service
namespace: kube-redis
labels:
app: redis-service
spec:
type: NodePort
ports:
- name: redis
protocol: TCP
port: 6379
targetPort: redis
nodePort: 30079
selector:
app: redis
[root@cmp-k8s-dev-master01 kube-redis]# kubectl apply -f kube-redis-svc.yaml
service/redis-service created
5、查看是否创建成功
[root@cmp-k8s-dev-master01 kube-redis]# kubectl get pod -n kube-redis
NAME READY STATUS RESTARTS AGE
redis-cluster-0 1/1 Running 0 6m15s
redis-cluster-1 1/1 Running 0 6m14s
redis-cluster-2 1/1 Running 0 5m58s
redis-cluster-3 1/1 Running 0 5m44s
redis-cluster-4 1/1 Running 0 5m28s
redis-cluster-5 1/1 Running 0 5m15s
四、配置Redis集群
1、初始化集群
注:目前Redis还不支持以hostname方式初始化集群,所以先获取每个节点的IP地址
[root@cmp-k8s-dev-master01 kube-redis]# kubectl get pod -n kube-redis -o wide
2、执行初始化命令
# 一共有6个节点,当选项--cluster-replicas指定为1时,表示为每个Master节点分配一个Slave节点,这样集群中刚好3个Master节点和3个Slave节点
[root@cmp-k8s-dev-master01 kube-redis]# kubectl exec -it -n kube-redis redis-cluster-1 -- bash
root@redis-cluster-1:/data# redis-cli -a P8eUNqTvkf35CXT2 --cluster create 172.20.67.166:6379 172.20.108.219:6379 172.20.38.83:6379 172.20.235.82:6379 172.20.106.72:6379 172.20.203.114:6379 --cluster-replicas 1
# 输出类似以下信息表示初始化成功
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
3、登录Redis节点
# 使用Redis,现在进入集群中的任意一个Pod中都可以访问Redis服务,前面我们创建了一个headless类型的Service,kubernetes集群会为该服务分配一个DNS A记录,格式为my-svc.my-namespace.svc.cluster-domain.example,每次访问该服务名时,将随机解析到其中一个Redis节点上
[root@cmp-k8s-dev-master01 kube-redis]# kubectl exec -it -n kube-redis redis-cluster-1 -- bash
root@redis-cluster-1:/data# redis-cli -a P8eUNqTvkf35CXT2 -c -h redis-service.kube-redis.svc.cluster.local -p 6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
redis-service.kube-redis.svc.cluster.local:6379> set test v1
-> Redirected to slot [6918] located at 172.20.108.219:6379
OK
172.20.108.219:6379> get test
"v1"
至此,K8s搭建Redis集群完毕。
若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!
评论