一、准备配置文件
Redis配置文件
[root@master1 redis]# cat redis.conf
bind 0.0.0.0
daemonize yes
logfile ""
masterauth 123456
requirepass 123456
appendonly yes
Sentinel 配置文件
[root@master1 redis]# cat sentinel.conf
bind 0.0.0.0
protected-mode no
port 26379
logfile ""
sentinel monitor mymaster redis-0.redis 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000
二、准备容器执行脚本
[root@master1 redis]# cat redis.sh
#!/bin/bash
pod_seq=`echo $POD_NAME | awk -F"-" '{print $2}'`
if [ $pod_seq -eq 0 ];then
ip=`hostname -I`
hostname -I > /usr/local/etc/redis/ip.txt
sed -ri "/^bind/c bind $ip" /usr/local/etc/redis/redis.conf
sed -ri "/^bind/c bind $ip" /usr/local/etc/redis/sentinel.conf
sed -ri "/2$/c sentinel monitor mymaster $ip 6379 2" /usr/local/etc/redis/sentinel.conf
redis-server /usr/local/etc/redis/redis.conf
redis-sentinel /usr/local/etc/redis/sentinel.conf
else
ip=`hostname -I`
i=`cat /usr/local/etc/redis/ip.txt`
sed -ri "/^bind/c bind $ip" /usr/local/etc/redis/redis.conf
sed -ri "/^bind/c bind $ip" /usr/local/etc/redis/sentinel.conf
sed -ri "/2$/c sentinel monitor mymaster $i 6379 2" /usr/local/etc/redis/sentinel.conf
echo "\nreplicaof redis-0.redis 6379" >> /usr/local/etc/redis/redis.conf
redis-server /usr/local/etc/redis/redis.conf
redis-sentinel /usr/local/etc/redis/sentinel.conf
fi
注意:这里是有个换行符的,之前没加入这个,直接加到在/usr/local/etc/redis/redis.conf后面去了,造成了参数无法识别
三、编写initContainer的Dockerfile
[root@master1 redis]# cat Dockerfile
FROM alpine
COPY ["redis.conf","redis.sh","sentinel.conf","/redis/"]
这里我已经上传镜像到docker.hub仓库了,想部署redis哨兵的其实上面没有必要自己制作镜像,直接拉取我的就行
docker build -t dockerliuliang/redis:v3 .
docker push dockerliuliang/redis:v3
四、编写yaml文件
注意:我这里用的是local存储,你也可以用NFS存储啥的,那就需要改动一点了,自己去找资料吧,都差不多,就不展示了
[root@master1 yaml]# cat redis.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: xiaoliu-redis-local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
namespace: xiaoliu
name: redis-local-pvc
labels:
app: redis
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: xiaoliu-redis-local-storage
resources:
requests:
storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: vm3a-xiaoliu-local-pv-redis
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: xiaoliu-redis-local-storage
local:
path: /data/xiaoliu/redis
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- vm3a
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
spec:
serviceName: redis
replicas: 3
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
initContainers:
- image: dockerliuliang/redis:v3
name: conf
command: ["cp","/redis/redis.sh","/redis/redis.conf","/redis/sentinel.conf","/conf"]
volumeMounts:
- mountPath: /conf
name: redis-conf
containers:
- name: redis
image: redis
volumeMounts:
- mountPath: /usr/local/etc/redis
name: redis-conf
command: ["sh","/usr/local/etc/redis/redis.sh"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
ports:
- containerPort: 6379
name: redis
volumes:
- name: redis-conf
persistentVolumeClaim:
claimName: redis-local-pvc
---
apiVersion: v1
kind: Service
metadata:
name: redis
labels:
app: redis
spec:
selector:
app: redis
clusterIP: None
ports:
- name: redis
port: 6379
五、使用k8s按yaml文件进行创建
webedit@vm3a:~$ kubectl apply -f redis.yaml
webedit@vm3a:~$ kubectl get pod
六、进入容器内进行验证
1、验证集群是否正常
webedit@vm3a:~$ kubectl exec -it redis-0 /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@redis-0:/data# info replication
bash: info: command not found
root@redis-0:/data# redis-cli -h redis-0.redis -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
redis-0.redis:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.244.15.35,port=6379,state=online,offset=29191,lag=1
slave1:ip=10.244.15.36,port=6379,state=online,offset=29191,lag=1
master_failover_state:no-failover
master_replid:daf3989bf9d40ac6123a31dcccfb9889fcda16bb
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:29191
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:29191
redis-0.redis:6379> set name xiaoliu
OK
webedit@vm3a:~$ kubectl exec -it redis-1 /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@redis-1:/data# redis-cli -h redis-1.redis -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
redis-1.redis:6379> ping
PONG
redis-1.redis:6379> info replication
# Replication
role:slave
master_host:10.244.15.34
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:15888
slave_repl_offset:15888
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:daf3989bf9d40ac6123a31dcccfb9889fcda16bb
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:15888
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:594
repl_backlog_histlen:15295
redis-1.redis:6379> get name
"xiaoliu"