k8s部署redis-cluster集群

lancelottsang
发布于 2022-4-25 15:37
浏览
0收藏

作者 | 怪兽在此
来源 | 今日头条

1、创建命名空间

redis-namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
	name: vf-redis

2、创建名称空间访问harbor的密钥

redis-harbor-sercet.yaml

apiVersion: v1
kind: Secret
metadata:
	name: harbor-secret
	namespace: vf-redis
	type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: ewoJImF1dGhzIjogewoJCSJoYXJib3J2Zi52Zm5ldHdvcmsuY29tIjogewoJCQkiYXV0aCI6ICJZV1J0YVc0NmMyTnRJVkZCV2pKM2MzZz0iCgkJfQoJfQp9

3、创建redis.conf的configmap

redis-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
	name: redis-conf
	namespace: vf-redis
data:
	redis.conf: |
		cluster-enabled yes
		cluster-config-file /var/lib/redis/nodes.conf
		cluster-node-timeout 10000
		protected-mode no
		daemonize no
		pidfile /var/run/redis.pid
		port 6379
		tcp-backlog 511
		bind 0.0.0.0
		timeout 3600
		tcp-keepalive 1
		loglevel verbose
		logfile /data/redis.log
		databases 16
		save 900 1
		save 300 10
		save 60 10000
		stop-writes-on-bgsave-error yes
		rdbcompression yes
		rdbchecksum yes
		dbfilename dump.rdb
		dir /data
		#requirepass yl123456
		appendonly yes
		appendfilename "appendonly.aof"
		appendfsync everysec
		no-appendfsync-on-rewrite no
		auto-aof-rewrite-percentage 100
		auto-aof-rewrite-min-size 64mb
		lua-time-limit 20000
		slowlog-log-slower-than 10000
		slowlog-max-len 128
		#rename-command FLUSHALL ""
		latency-monitor-threshold 0
		notify-keyspace-events ""
		hash-max-ziplist-entries 512
		hash-max-ziplist-value 64
		list-max-ziplist-entries 512
		list-max-ziplist-value 64
		set-max-intset-entries 512
		zset-max-ziplist-entries 128
		zset-max-ziplist-value 64
		hll-sparse-max-bytes 3000
		activerehashing yes
		client-output-buffer-limit normal 0 0 0
		client-output-buffer-limit slave 256mb 64mb 60
		client-output-buffer-limit pubsub 32mb 8mb 60
		hz 10
		aof-rewrite-incremental-fsync yes

4、创建redis-cluster的StatefulSet:

这里使用nfs共享存储

redis-cluster.yaml

apiVersion: v1
kind: Service
metadata:
	name: redis
	namespace: vf-redis
	labels:
	 app: redis
spec:
  selector:
    app: redis
    appCluster: redis-cluster
  ports:
  - name: redis
    port: 6379
  clusterIP: None
---
apiVersion: v1
kind: Service
metadata:
  name: redis-access
  namespace: vf-redis
  labels:
    app: redis
spec:
  selector:
  app: redis
  appCluster: redis-cluster
  ports:
  - name: redis-access
    protocol: TCP
    port: 6379
    targetPort: 6379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
  namespace: vf-redis
spec:
  serviceName: redis
  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: harborvf.vfnetwork.com/scm/redis-5.0.12@sha256:2764e317cea58379dafc936f7a795e4a6a2995d4c473d355212c97e0bee60eba
	command:
	- "redis-server"
	args:
	- "/etc/redis/redis.conf"
	- "--protected-mode"
	- "no"
	resources:
	requests:
	cpu: "500m"
	memory: "500Mi"
	ports:
	- containerPort: 6379
	name: redis
	protocol: TCP
	- containerPort: 16379
	name: cluster
	protocol: TCP
	volumeMounts:
	- name: conf
	mountPath: /etc/redis
	- name: data
	mountPath: /var/lib/redis
	volumes:
	- name: conf
	configMap:
	name: redis-conf
	items:
	- key: redis.conf
	path: redis.conf
	imagePullSecrets:
	- name: harbor-secret
	volumeClaimTemplates:
	- metadata:
	name: data
	annotations:
	volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
	spec:
	accessModes: [ "ReadWriteMany" ]
	resources:
	requests:
	storage: 10Gi

5、部署:

kubectl apply -f redis-namespace.yaml
kubectl apply -f redis-harbor-sercet.yaml
kubectl apply -f redis-configmap.yaml
kubectl apply -f redis-cluster.yaml 

6、查看部署状态:k8s部署redis-cluster集群-鸿蒙开发者社区

pod状态k8s部署redis-cluster集群-鸿蒙开发者社区

7、初始化集群:

使用Redis-tribe工具进行集群的初始化

kubectl run -it ubuntu --image=ubuntu --restart=Never /bin/bash

进入pod中执行以下操作:

cat > /etc/apt/sources.list << EOF
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
> EOF
apt-get update
apt-get install -y vim wget python2.7 python-pip redis-tools dnsutils
pip install redis-trib==0.5.1
#创建只有master节点的集群:
redis-trib.py create \
`dig +short redis-0.redis.vf-redis.svc.cluster.local`:6379 \
`dig +short redis-1.redis.vf-redis.svc.cluster.local`:6379 \
`dig +short redis-2.redis.vf-redis.svc.cluster.local`:6379
# 为每个master添加slave:
redis-trib.py replicate \
--master-addr `dig +short redis-0.redis.vf-redis.svc.cluster.local`:6379 \
--slave-addr `dig +short redis-3.redis.vf-redis.svc.cluster.local`:6379
redis-trib.py replicate \
--master-addr `dig +short redis-1.redis.vf-redis.svc.cluster.local`:6379 \
--slave-addr `dig +short redis-4.redis.vf-redis.svc.cluster.local`:6379
redis-trib.py replicate \
--master-addr `dig +short redis-2.redis.vf-redis.svc.cluster.local`:6379 \
--slave-addr `dig +short redis-5.redis.vf-redis.svc.cluster.local`:6379

8、连到任意一个Redis Pod检查集群状态:

kubectl exec -it -n vf-redis redis-0 bash

k8s部署redis-cluster集群-鸿蒙开发者社区

分类
标签
收藏
回复
举报
回复
    相关推荐