k8s redis-cluster 6节点(3主3从)

网友投稿 277 2022-09-12

k8s redis-cluster 6节点(3主3从)

此创建仅为简单创建,无PV、PVC挂载一、 把redis.conf配置文件引入configmapcat redis.confappendonly yes cluster-enabled yes cluster-config-file /var/lib/redis/nodes.conf cluster-node-timeout 5000 dir /var/lib/redis port 6379kubectl create configmap redis-conf --from-file=redis.conf二、 创建HeadlessServiceapiVersion: 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三、 创建statefulset## 如果看到 apps/v1beta1 不要惊讶,方法附在apiVersion: apps/v1 kind: StatefulSet metadata:   name: redis-app spec:   serviceName: "redis-service"   replicas: 6   selector:     matchLabels:       app: redis       appCluster: redis-cluster   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: "registry.cn-qingdao.aliyuncs.com/gold-faas/gold-redis:1.0"         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"       - name: "redis-data"         emptyDir: {}四、service创建,跨ns访问apiVersion: v1 kind: Service metadata:   name: gold-redis   labels:     app: redis spec:   ports:   - name: redis-port     protocol: "TCP"     port: 6379     targetPort: 6379   selector:     app: redis     appCluster: redis-cluster五、 开始 初始化redis集群kubectl run -i --tty redis-cluster-manager --image=ubuntu --restart=Never /bin/bash## 进入容器操作如下(此操作,由本人实测)   2  apt update   3  apt install wget dnsutils        # 这里make的时候可能会出来时区选择,我这里先选择的是 Asia (亚洲),                                                       后选择的Shanghai(上海)   4  wget   5  tar -xzf redis-5.0.3.tar.gz    6  cd redis-5.0.3   9  apt install -y make gcc   13  apt install -y gcc-c++          # 可能会报错,不装了,忽略   15  make MALLOC=libc             # 直接make,如果不行,使用这条                                                     # 这里报错如下,所以添加参数 MALLOC=libc                                                       In file included from adlist.c:34:                                                       zmalloc.h:50:10: fatal error: jemalloc/jemalloc.h: No such file or directory                                                          50 | #include                                                             |          ^~~~~~~~~~~~~~~~~~~~~                                                       compilation terminated.                                                       make[1]: *** [Makefile:248: adlist.o] Error 1                                                       make[1]: Leaving directory '/redis-5.0.3/src'                                                       make: *** [Makefile:6: all] Error  16  cd src/ && cp redis-cli /usr/local/bin/          # cp redis-cli 到 /usr/local/bin/ , 这里的主目录为/redis-5.0.3/src/两种方法做集群### 如果报错,删除掉生成的db文件及配置文件其一、变量找ipkubectl get pods -l appCluster=redis-cluster -n default -o jsonpath='{range.items[*]}{.status.podIP}:6379 ' | awk '{print $1,$2,$3,$4,$5,$6 }'然后,构建集群kubectl -n default exec -ti redis-app-0 "redis-cli --cluster create $(kubectl get pods -l appCluster=redis-cluster -n default -o jsonpath='{range.items[*]}{.status.podIP}:6379 ' | awk '{print $1,$2,$3,$4,$5,$6 }') --cluster-replicas 0"其二、# nslookup 找到ip地址  23  nslookup redis-app-0.redis-service.default   24  nslookup redis-app-1.redis-service.default   25  nslookup redis-app-2.redis-service.default   26  nslookup redis-app-3.redis-service.default   27  nslookup redis-app-4.redis-service.default   28  nslookup redis-app-5.redis-service.default  # 解析除6个ip,分3主3从,如下     3主:192.168.120.71:6379  192.168.161.31:6379  192.168.135.253:6379     3从:192.168.176.111:6379  192.168.148.249:6379  192.168.121.27:6379  # 初始化redis集群的 3个主master   29  redis-cli --cluster create 192.168.120.71:6379 192.168.161.31:6379 192.168.135.253:6379       #返回状态,如下       >>> Performing Cluster Check (using node 192.168.120.71:6379)        M: e6ee1d55ca9ad00d170a8b9db81a4d37a7a5f91f 192.168.120.71:6379           slots:[0-5460] (5461 slots) master        M: 0e94eede524b103d18224cfb1104b6e8de0d0afd 192.168.161.31:6379           slots:[5461-10922] (5462 slots) master        M: 36699e577b79fce5883832897c98cac3f1fa6d31 192.168.135.253:6379           slots:[10923-16383] (5461 slots) master        [OK] All nodes agree about slots configuration.        >>> Check for open slots...        >>> Check slots coverage...        [OK] All 16384 slots covered.  #  附加对应的slave节点,add-node 后首先跟的是要添加的slave节点ip,然后是主master节点ip,最最后面是master的cluster-master-id   30  redis-cli --cluster add-node 192.168.176.111:6379 192.168.120.71:6379 --cluster-slave --cluster-master-id e6ee1d55ca9ad00d170a8b9db81a4d37a7a5f91f   31  redis-cli --cluster add-node 192.168.148.249:6379 192.168.161.31:6379 --cluster-slave --cluster-master-id 0e94eede524b103d18224cfb1104b6e8de0d0afd   32  redis-cli --cluster add-node 192.168.121.27:6379 192.168.135.253:6379 --cluster-slave --cluster-master-id 36699e577b79fce5883832897c98cac3f1fa6d31  # 最后记得测试   34  redis-cli -c -h 192.168.161.31   35  redis-cli -c -h redis-app-0.redis-service.default        set a 1        info        ping  # 等等去测试

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【k8s中无法使用jstack和arthas的解决方案】1: Unable to get pid of LinuxThreads manager thread
下一篇:PR人:小红书孤芳自赏!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~