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
 
 
 
posted @ 2020-03-29 18:11  会跑的蚂蚁  阅读(2542)  评论(0编辑  收藏  举报