1创建Configmap
配置文件redis.conf
如下:
appendonly yes
cluster-enabled yes
cluster-config-file /var/lib/redis/nodes.conf
cluster-node-timeout 5000
dir /var/lib/redis
port 6379
kubectl create configmap redis-conf --from-file=redis.conf -n redis
2创建Headless service
headless-service.yml
如下:
apiVersion: v1
kind: Service
metadata:
name: redis-service
labels:
app: redis
spec:
ports:
- name: redis-port
port: 6379
clusterIP: None
selector:
app: redis
appCluster: redis-cluster
kubectl create -f headless-service.yml -n redis
3创建Redis 集群节点
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: redis-app
spec:
serviceName: "redis-service"
replicas: 6
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: "redis:3.2.8"
command:
- "redis-server"
args:
- "/etc/redis/redis.conf"
- "--protected-mode"
- "no"
resources:
requests:
cpu: "100m"
memory: "100Mi"
ports:
- name: redis
containerPort: 6379
protocol: "TCP"
- name: cluster
containerPort: 16379
protocol: "TCP"
volumeMounts:
- name: "redis-conf"
mountPath: "/etc/redis"
- name: "redis-data"
mountPath: "/var/lib/redis"
volumes:
- name: "redis-conf"
configMap:
name: "redis-conf"
items:
- key: "redis.conf"
path: "redis.conf"
volumeClaimTemplates:
- metadata:
name: redis-data
spec:
accessModes: [ "ReadWriteMany" ]
storageClassName: rook-block2
resources:
requests:
storage: 200Mi
kubectl create -f redis-statefulset.yaml -n redis
4初始化Redis集群
kubectl run -i --tty ubuntu --image=ubuntu --restart=Never /bin/bash
apt-get update
apt-get install -y vim wget python2.7 python-pip redis-tools dnsutils
pip install redis-trib==0.5.1
redis-trib.py create \
`dig +short redis-app-0.redis-service.redis.svc.cluster.local`:6379 \
`dig +short redis-app-1.redis-service.redis.svc.cluster.local`:6379 \
`dig +short redis-app-2.redis-service.redis.svc.cluster.local`:6379
redis-trib.py replicate \
--master-addr `dig +short redis-app-0.redis-service.redis.svc.cluster.local`:6379 \
--slave-addr `dig +short redis-app-3.redis-service.redis.svc.cluster.local`:6379
redis-trib.py replicate \
--master-addr `dig +short redis-app-1.redis-service.redis.svc.cluster.local`:6379 \
--slave-addr `dig +short redis-app-4.redis-service.redis.svc.cluster.local`:6379
redis-trib.py replicate \
--master-addr `dig +short redis-app-2.redis-service.redis.svc.cluster.local`:6379 \
--slave-addr `dig +short redis-app-5.redis-service.redis.svc.cluster.local`:6379
[root@dev7 redis-cluster]# kubectl exec -it redis-app-2 -n redis /bin/bash
root@redis-app-2:/data# redis-cli -c
127.0.0.1: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:3
cluster_my_epoch:3
cluster_stats_messages_sent:1879
cluster_stats_messages_received:1879
5创建用于访问Service
piVersion: v1
kind: Service
metadata:
name: redis-access-service
labels:
app: redis
spec:
ports:
- name: redis-port
protocol: "TCP"
port: 6379
targetPort: 6379
type: NodePort
selector:
app: redis
appCluster: redis-cluster