helm部署redis主從和哨兵模式


 
 
1. helm部署redis主從節點
 
1.1. helm部署redis條件
a. 准備storageClass服務作為永久存儲,名稱為nfs
b. helm Client:"v2.13.1", Server:"v2.13.1"
c. 倉庫源為http://mirror.azure.cn/kubernetes/charts/
d. 確認倉庫里有chart,redis chart版本號為10.5.7
 
[root@ops1 redis]# helm version
Client: &version.Version{SemVer:"v2.13.1", GitCommit:"618447cbf203d147601b4b9bd7f8c37a5d39fbb4", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.13.1", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}
#添加新的遠程倉庫
[root@ops1 redis]# helm repo add stable http://mirror.azure.cn/kubernetes/charts/
[root@ops1 redis]# helm repo list
NAME URL
local           http://127.0.0.1:8879/charts 
stable          http://mirror.azure.cn/kubernetes/charts/
incubator       http://mirror.azure.cn/kubernetes/charts-incubator/
[root@ops1 redis]# helm repo update
[root@ops1 redis]# helm search redis 
NAME CHART VERSION APP VERSION DESCRIPTION
stable/redis 10.5.7 5.0.7 DEPRECATED Open source, advanced key-value store. It is o...
stable/redis-ha 4.4.3 5.0.6 Highly available Kubernetes implementation of Redis 
 
[root@ops1 redis]# helm fetch stable/redis # 拉取代碼到本地,如有問題,查看編寫規則 
 
1.2.部署redis一主二從節點
[root@ops1 redis]# cat <<EOF> my-values.yaml
password: "redis1"
master:
  service:
    type: NodePort
  persistence:
    storageClass: "nfs"
slave:
  service:
    type: NodePort
  persistence:
    storageClass: "nfs"
EOF
[root@ops1 redis]# helm install --name redis-service -f my-values.yaml --namespace=course stable/redis 
NAME: redis-service
LAST DEPLOYED: Sun Mar 29 12:17:33 2020
NAMESPACE: course
STATUS: DEPLOYED
[root@ops1 test]# kubectl get svc | grep redis-service
redis-service-headless ClusterIP None <none> 6379/TCP 3m45s
redis-service-master NodePort 10.96.140.206 <none> 6379:20986/TCP 3m45s
redis-service-slave NodePort 10.96.98.91 <none> 6379:35090/TCP 3m45s
#有時候我們創建的服務不想走 負載均衡,想直接通過 pod-ip 鏈接后端, 怎么辦呢, 使用headless service接可以解決。
所以在微服務相關場景 如果需要直通pod的時候 我們就可以使用headless service 繞過 k8s的轉發機制,直接訪問pod了。
 
[root@ops1 redis]# redis-cli -p 20986 -a redis1
127.0.0.1:20986>
 
 
 
2. 部署redis 高可用哨兵模式
本文中的Redis高可用方案采用Sentinel(哨兵)模式,在集群出現故障的時候自動進行故障轉移,保證集群的可用性。
 
2.1.確認倉庫里有chart,redis chart版本號為10.5.7
a. 准備storageClass服務作為永久存儲,名稱為nfs
b. helm Client:"v2.13.1", Server:"v2.13.1"
c. 倉庫源為http://mirror.azure.cn/kubernetes/charts/
d. 確認倉庫里有chart,redis chart版本號為4.4.3
 
[root@ops1 redis]# helm repo list
NAME URL
local           http://127.0.0.1:8879/charts 
stable          http://mirror.azure.cn/kubernetes/charts/
incubator       http://mirror.azure.cn/kubernetes/charts-incubator/
[root@ops1 redis]# helm repo update
[root@ops1 redis]# helm search redis 
NAME CHART VERSION APP VERSION DESCRIPTION
stable/redis 10.5.7 5.0.7 DEPRECATED Open source, advanced key-value store. It is o...
stable/redis-ha 4.4.3 5.0.6 Highly available Kubernetes implementation of Redis 
 
[root@ops1 redis]# helm fetch stable/redis-ha # 拉取代碼到本地,如有問題,查看編寫規則 
 
2.2. 部署redis 高可用
[root@ops1 redis]# cat <<EOF> my-values.yaml
auth: true
redisPassword: "redis1"
hardAntiAffinity: false #(僅限當replicas > worker node 節點數時修改)
haproxy:
  service:
    type: NodePort
  persistence:
    storageClass: "nfs"
persistentVolume:
  storageClass: "nfs"
EOF
[root@ops1 redis-ha]# helm install --name redis-ha -f my-values.yaml --namespace=course stable/redis-ha
NAME: redis-ha
LAST DEPLOYED: Sun Mar 29 14:40:53 2020
NAMESPACE: course
STATUS: DEPLOYED
 
RESOURCES:
==> v1/ConfigMap
NAME DATA AGE
redis-ha-configmap 4 2s
 
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
redis-ha-server-0 0/2 Init:0/1 0 1s
 
==> v1/Role
NAME AGE
redis-ha 2s
 
==> v1/RoleBinding
NAME AGE
redis-ha 1s
 
==> v1/Secret
NAME TYPE DATA AGE
redis-ha Opaque 1 3s
 
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redis-ha ClusterIP None <none> 6379/TCP,26379/TCP 1s
redis-ha-announce-0 ClusterIP 10.96.208.41 <none> 6379/TCP,26379/TCP 1s
redis-ha-announce-1 ClusterIP 10.96.59.19 <none> 6379/TCP,26379/TCP 1s
redis-ha-announce-2 ClusterIP 10.96.95.191 <none> 6379/TCP,26379/TCP 1s
 
==> v1/ServiceAccount
NAME SECRETS AGE
redis-ha 1 2s
 
==> v1/StatefulSet
NAME READY AGE
redis-ha-server 0/3 1s
 
 
NOTES:
Redis can be accessed via port 6379 and Sentinel can be accessed via port 26379 on the following DNS name from within your cluster:
redis-ha.course.svc.cluster.local
 
To connect to your Redis server:
1. To retrieve the redis password:
   echo $(kubectl get secret redis-ha -o "jsonpath={.data['auth']}" | base64 --decode)
 
2. Connect to the Redis master pod that you can use as a client. By default the redis-ha-server-0 pod is configured as the master:
 
   kubectl exec -it redis-ha-server-0 sh -n course
 
3. Connect using the Redis CLI (inside container):
 
   redis-cli -a <REDIS-PASS-FROM-SECRET>
 
[root@ops1 redis-ha]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/redis-ha-server-0 2/2 Running 0 74s
pod/redis-ha-server-1 2/2 Running 0 63s
pod/redis-ha-server-2 2/2 Running 0 53s
 
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/redis-ha ClusterIP None <none> 6379/TCP,26379/TCP 74s
service/redis-ha-announce-0 ClusterIP 10.96.162.123 <none> 6379/TCP,26379/TCP 74s
service/redis-ha-announce-1 ClusterIP 10.96.198.108 <none> 6379/TCP,26379/TCP 74s
service/redis-ha-announce-2 ClusterIP 10.96.96.97 <none> 6379/TCP,26379/TCP 74s
 
NAME READY AGE
statefulset.apps/redis-ha-server 3/3 74s
 
 
2.3.如果需要暴露給外部使用則需要再部署一個 NodePort Service
[root@ops1 redis]# cat <<EOF> redis-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis-ha-service #名稱:隨意
  labels:
    app: redis-ha #部署的 redis-ha 名稱
spec:
  ports:
  - name: redis-ha #部署的 redis-ha 名稱
    protocol: "TCP" #TCP 協議
    port: 26379
    targetPort: 6379
    nodePort: 30379 #此為外部連接k8s redis-ha 服務的端口
  selector:
    statefulset.kubernetes.io/pod-name: redis-ha-server-0
  type: NodePort
EOF
[root@ops1 redis-ha]# kubectl apply -f redis-service.yaml
[root@ops1 redis-ha]# kubectl get svc | grep redis-ha-service
redis-ha-service NodePort 10.96.152.166 <none> 26379:30379/TCP 65s
[root@ops1 redis-ha]# redis-cli -p 30379 -a redis1
127.0.0.1:30379> info
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM