k8s 安装redis-cluster集群,文件如下
[root@k8s-1 redis-cluster]# cat redis-cluster.yaml
#redis配置文件
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-conf
data:
redis.conf: |
port 6379
masterauth haoke.com
requirepass haoke.com
appendonly yes
dir /var/lib/redis
cluster-enabled yes
cluster-config-file /var/lib/redis/nodes.conf
cluster-node-timeout 5000
---
#redis-proxy配置文件
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-proxy
data:
proxy.conf: |
cluster redis-cluster:6379
bind 0.0.0.0
port 7777
threads 8
daemonize no
auth haoke.com
enable-cross-slot yes
log-level error
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis-node
annotations:
reloader.stakater.com/auto: "true"
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: redis
command:
- "redis-server"
args:
- "/etc/redis/redis.conf"
- "--protected-mode"
- "no"
- "--cluster-announce-ip"
- "$(POD_IP)"
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
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","ReadWriteOnce"]
resources:
requests:
storage: 1G
storageClassName: nfs-storage
---
apiVersion: v1
kind: Service
metadata:
name: redis-cluster
labels:
app: redis
spec:
ports:
- name: redis-port
port: 6379
clusterIP: None
selector:
app: redis
appCluster: redis-cluster
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-proxy
annotations:
reloader.stakater.com/auto: "true"
spec:
replicas: 1
selector:
matchLabels:
app: redis-proxy
template:
metadata:
labels:
app: redis-proxy
spec:
imagePullSecrets:
- name: harbor
containers:
- name: redis-proxy
image: nuptaxin/redis-cluster-proxy:v1.0.0
imagePullPolicy: IfNotPresent
command: ["redis-cluster-proxy"]
args:
- -c
- /data/proxy.conf # 指定启动配置文件
ports:
- name: redis-proxy
containerPort: 7777
protocol: TCP
volumeMounts:
- name: redis-proxy-conf
mountPath: /data/
volumes:
- name: redis-proxy-conf
configMap:
name: redis-proxy
---
apiVersion: 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
---
#redis外网访问
apiVersion: v1
kind: Service
metadata:
name: redis-proxy-service
labels:
name: redis-proxy
spec:
type: NodePort
ports:
- port: 7777
protocol: TCP
targetPort: 7777
name: http
nodePort: 30939
selector:
app: redis-proxy
初始化集群
kubectl -n db exec -it redis-node-0 -- redis-cli -a haoke.com --cluster create --cluster-replicas 1 $(kubectl -n db get pods -l app=redis -o jsonpath='{range.items[*]}{.status.podIP}:6379 {end}')
验证集群
root@redis-node-0:/data# redis-cli -a haoke.com --cluster check 127.0.0.1:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379 (a3c725ce...) -> 0 keys | 5461 slots | 1 slaves.
10.244.13.69:6379 (c07c80e9...) -> 0 keys | 5462 slots | 1 slaves.
10.244.200.202:6379 (d81f519c...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: a3c725ce0c5cd031b933f387871dd908e99d049a 127.0.0.1:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: aa232b5d2bd620d7b4261fd498a1c2f98edd6b2f 10.244.13.75:6379
slots: (0 slots) slave
replicates d81f519c00f099cb6a6f707daab84718c6636e12
M: c07c80e9334a8fa405d3ce15a878ce4f78c3f16d 10.244.13.69:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 2fe6a80fc3e5d50b066085e8f7756a9acf9b4e7f 10.244.13.70:6379
slots: (0 slots) slave
replicates c07c80e9334a8fa405d3ce15a878ce4f78c3f16d
S: 61960bf08c6f2d1f0fb1c5f71d0ab519ba0e9a6f 10.244.200.204:6379
slots: (0 slots) slave
replicates a3c725ce0c5cd031b933f387871dd908e99d049a
M: d81f519c00f099cb6a6f707daab84718c6636e12 10.244.200.202:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
文章参考:https://www.kubebiz.com/lwllwl/redis-cluster?namespace=db