//参考 Kubernetes 部署Redis主从服务(StatefulSet)_BigQ2020的技术博客_51CTO博客
大体和参考链接内容相同,只是个别需要修改,我的docker镜像是用的ubuntu shell执行用的是bash,相应参数有调整
// launch.sh
#!/bin/bash
PASSWORD=$(cat /etc/redis-passwd/passwd)
if [ ${HOSTNAME} = "redis-0" ]; then
redis-server --bind 0.0.0.0 --requirepass ${PASSWORD}
else
redis-server --slaveof redis-0.redis.cephfs 6379 --masterauth ${PASSWORD} --requirepass ${PASSWORD}
fi
// statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
namespace: cephfs
spec:
serviceName: redis
replicas: 3
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:latest
ports:
- containerPort: 6379
name: redis
volumeMounts:
- name: redis-data
mountPath: /data
- name: script
mountPath: /script/launch.sh
subPath: launch.sh
- name: passwd-volume
mountPath: /etc/redis-passwd
command:
- sh
- -c
- sleep 10 && bash /script/launch.sh //这里用的是bash而不是sh,否则会执行会报错
volumes:
- name: script
configMap:
name: redis-config
defaultMode: 077
- name: passwd-volume
secret:
secretName: redis-passwd
volumeClaimTemplates:
- metadata:
name: redis-data
namespace: cephfs
annotations:
volume.beta.kubernetes.io/storage-class: "cephfs"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
部署完以后可以通过kubectl logs redis-0 -n cephfs 查看是否部署成功。
在k8s中部署的服务要想使用redis集群可以链接无头服务,地址可使用:redis.cephfs:6379