1. 程式人生 > 實用技巧 >k8s建立redis叢集

k8s建立redis叢集

1.首先k8s要有StorangeClass,這裡為data

新建redis-cluster.yaml檔案

apiVersion:v1
kind:ConfigMap
metadata:
name:redis-cluster
data:
update-node.sh:|
#!/bin/sh
REDIS_NODES="/data/nodes.conf"
sed-i-e"/myself/s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/${POD_IP}/"${REDIS_NODES}
exec"$@"
redis.conf:|+
cluster-enabledyes
cluster
-require-full-coverageno cluster-node-timeout15000 cluster-config-file/data/nodes.conf cluster-migration-barrier1 appendonlyyes protected-modeno --- apiVersion:apps/v1 kind:StatefulSet metadata: name:redis-cluster spec: serviceName:redis-cluster replicas:6 selector: matchLabels: app:redis-cluster template: metadata: labels: app:redis
-cluster spec: containers: -name:redis image:redis:6.0.6 ports: -containerPort:6379 name:client -containerPort:16379 name:gossip command:["/conf/update-node.sh","redis-server","/conf/redis.conf"] env: -name:POD_IP valueFrom: fieldRef: fieldPath:status.podIP volumeMounts: -name:conf mountPath:/conf readOnly:
false -name:data mountPath:/data readOnly:false volumes: -name:conf configMap: name:redis-cluster defaultMode:0755 volumeClaimTemplates: -metadata: name:data spec: accessModes:["ReadWriteOnce"] resources: requests: storage:5Gi storageClassName:data #改成NFS的名稱

執行命令:kubectl apply -f redis-cluster.yaml

執行命令檢視:kubectl get all

3主3副的pod已經啟動成功

執行初始化叢集命令:

kubectlexec-itredis-cluster-0--redis-cli--clustercreate--cluster-replicas1$(kubectlgetpods-lapp=redis-cluster-ojsonpath='{range.items[*]}{.status.podIP}:6379') 輸入:yes

這個時候叢集建立完成