前提摘要
目录结构
-
1. 编写 redis-master-rc.yaml (ReplicationController)
-
2. 编写 redis-slave-rc.yaml(ReplicationController)
-
3. 编写 redis-svc.yaml (Service)
-
4. 部署安装集群
-
5. 验证主从同步
-
6. 集群手动伸缩扩容
-
7. 集群自动伸缩扩容
1. 编写 redis-master-rc.yaml (ReplicationController)
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-master
labels:
name: redis-master
spec:
replicas: 1
selector:
name: redis-master
template:
metadata:
labels:
name: redis-master
spec:
containers:
- name: redis-master
image: 980315926pxm/redis-master:2.8.19
imagePullPolicy: IfNotPresent
ports:
- containerPort: 6379
2. 编写 redis-slave-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
replicas: 1
selector:
name: redis-slave
template:
metadata:
labels:
name: redis-slave
spec:
containers:
- name: redis-slave
image: 980315926pxm/redis-slave:3.0.3
imagePullPolicy: IfNotPresent
resources:
requests:
memory: 128Mi
cpu: 100m
limits:
memory: 256Mi
cpu: 200m
ports:
- containerPort: 6379
env:
- name: GET_HOSTS_FROM
value: env
-
redis-slave 镜像中启动脚本 /run.sh 内容:
root@redis-slave-68tcm:~# cat /run.sh
if [[ ${GET_HOSTS_FROM:-dns} == "env" ]]; then
redis-server --slaveof ${REDIS_MASTER_SERVICE_HOST} 6379
else
redis-server --slaveof redis-master 6379
fi
3. 编写 redis-svc.yaml (Service)
apiVersion: v1
kind: Service
metadata:
name: redis-master
labels:
name: redis-master
spec:
type: NodePort
ports:
- port: 6379
targetPort: 6379
nodePort: 30009
selector:
name: redis-master
---
apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
type: NodePort
ports:
- port: 6379
nodePort: 30099
selector:
name: redis-slave
-
master 主机外网连接端口 30009 (读写)
-
slave 从机外网连接端口 30099(读)
4. 部署安装集群
kubectl create -f redis-master-rc.yaml
kubectl create -f redis-slave-rc.yaml
kubectl create -f redis-svc.yaml
5. 验证主从同步
root@VM-0-6-ubuntu:~# kubectl get po
NAME READY STATUS RESTARTS AGE
mysql-master-99mpz 1/1 Running 0 128m
mysql-slave-rfc45 1/1 Running 0 128m
redis-master-hxnqs 1/1 Running 0 15m
redis-slave-v6tcm 1/1 Running 0 26m
root@VM-0-6-ubuntu:~# kubectl exec -it redis-master-hxnqs -- bash
[ root@redis-master-hxnqs:/data ]$ redis-cli
127.0.0.1:6379> set test 123456
OK
root@VM-0-6-ubuntu:~# kubectl exec -it redis-slave-v6tcm -- bash
root@redis-slave-v6tcm:/data# redis-cli
127.0.0.1:6379> get test
"123456"
6. 集群手动伸缩扩容
root@VM-0-6-ubuntu:~# kubectl scale rc redis-slave --replicas=3
replicationcontroller/redis-slave scaled
root@VM-0-6-ubuntu:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-master-99mpz 1/1 Running 0 142m
mysql-slave-rfc45 1/1 Running 0 142m
redis-master-hxnqs 1/1 Running 0 30m
redis-slave-68v4c 1/1 Running 0 58s
redis-slave-t9zkc 1/1 Running 0 58s
redis-slave-v6tcm 1/1 Running 0 40m
7. 集群自动伸缩扩容
root@VM-0-6-ubuntu:~# kubectl autoscale rc redis-slave --min=1 --max=10 --cpu-percent=50
horizontalpodautoscaler.autoscaling/redis-slave autoscaled
root@VM-0-6-ubuntu:~# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
redis-slave ReplicationController/redis-slave 0%/50% 1 10 1 7m20s
-
kubectl autoscale rc redis-slave --min=1 --max=10 --cpu-percent=50
--min (容器数量下限)
--max (容器数量上限)
--cpu-percent (cpu使用率达到指定百分比)
容器 cpu 使用率上升至 50% 以上时,自动扩充容器数量
容器 cpu 使用率下降至 50% 以上时,自动缩减容器数量