前言
- K8S的环境部署
- 主机清单
- 创建NFS持久化目录
- 安装NFS
- 创建PV&PVC
- K8S中的部署
- Redis配置文件:configmap
- Deployment:redis
- Service
- k8s验证
- 数据迁移
- 数据备份
- 数据还原
- 关于appendonly.aof和dump.rdb
- Mysql
K8S的环境部署
我这里已经部署完毕了,这里就不演示了
主机清单
20.0.0.21 | master1 | centos 7.4 |
20.0.0.22 | master2 | centos 7.4 |
20.0.0.23 | node1 | centos 7.4 |
20.0.0.24 | NFS |
创建NFS持久化目录
安装NFS
20.0.0.24:
yum install -y nfs-utils rpcbind
systemctl restart nfs
systemctl restart rpcbind
systemctl enable nfs
systemctl enable rpcbind
[root@localhost ]# vim /etc/exports
/data/nfs/ *(rw,no_root_squash)
mkdir -p /data/nfs/redis
chmod 755 /data/nfs/redis
systemctl restart rpcbind
systemctl restart nfs
#在k8s所有节点安装nfs客户端,或者指定节点安装即可
yum install nfs-utils -y
showmount -e 20.0.0.24
创建PV&PVC
#pv不用指定命名空间
#pvc需要指定命名空间,默认为default
#若有配置hosts映射,可使用映射名代替
#pv相当于逻辑卷,pvc是将多个卷合并为一使用
[root@localhost ]# vim 1-pv_pvc.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis-pv
namespace: qt
spec:
capacity:
storage: 30Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: redis-nfs
nfs:
path: /data/nfs/redis
server: 20.0.0.24
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-pvc
namespace: qt
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 30Gi
storageClassName: redis-nfs
[root@localhost ]# kubectl apply -f 1-pv_pvc.yaml
K8S中的部署
Redis配置文件:configmap
[root@localhost ]# vim 2-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: redis
namespace: qt
data:
redis.conf: |+
requirepass 123456
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events Ex
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
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
[root@localhost ]# kubectl apply -f 2-configmap.yaml
#ConfigMap生成的配置文件放置于容器内/etc/redis/redis.conf
#使挂载的ConfigMap生效:command: ["redis-server","/etc/redis/redis.conf"]
#将容器的/data持久化到redis-pvc,即20.0.0.24机器的/data/nfs/redis下
Deployment:redis
[root@localhost ]# vim 3-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: qt
labels:
app: redis
spec:
strategy:
type: Recreate
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
annotations:
version/date: "20210703"
version/author: "lc"
spec:
containers:
- name: redis
image: redis
imagePullPolicy: Always
command: ["redis-server","/etc/redis/redis.conf"]
ports:
- containerPort: 6379
volumeMounts:
- name: redis-config
mountPath: /etc/redis/redis.conf
subPath: redis.conf
- name: redis-persistent-storage
mountPath: /data
volumes:
- name: redis-config
configMap:
name: redis
items:
- key: redis.conf
path: redis.conf
- name: redis-persistent-storage
persistentVolumeClaim:
claimName: redis-pvc
nodeName: node1
[root@localhost ]# kubectl apply -f 3-deployment.yaml
Service
[root@localhost ]# vim 4-service.yaml
---------------------------------
kind: Service
apiVersion: v1
metadata:
name: redis
namespace: qt
spec:
type: NodePort
selector:
app: redis
ports:
- port: 6379
targetPort: 6379
nodePort: 30379
root@localhost ]# kubectl apply -f 4-service.yaml
k8s验证
连接20.0.0.21:30379
随便输入一个key
#第一步:删除redis的pod
[root@master1 redis]# ll
-rw-r--r-- 1 root root 450 7月 2 16:00 1-pv_pvc.yaml
-rw-r--r-- 1 root root 1629 7月 2 16:01 2-configmap.yaml
-rw-r--r-- 1 root root 991 7月 3 14:48 3-deployment.yaml
-rw-r--r-- 1 root root 168 7月 2 16:02 4-service.yaml
[root@master1 redis]# kubectl delete -f 3-deployment.yaml
#第二步:重新生成redis的pod
[root@master1 redis]# kubectl apply -f 3-deployment.yaml
#第三步:连接redis查看key是否还在,存在即完成持久化
数据迁移
数据备份
若源redis配置有持久化,直接拷贝持久化目录下的dump.rdb和appendonly.aof即可
若源redis不支持持久化,则进入容器生成dump.rdb并拷出
进入容器:kubectl exec -it pod redis-xxx -- /bin/sh
进入redis命令台:redis-cli -a 123456
保存数据:save
退出redis命令台:quit
退出容器:exit
从容器中取出数据:kubectl cp -n namespace Pod_Name:/data/dump.rdb ./
传输至远程主机:scp dump.rdb root@目标服务器:/目录
数据还原
停止redis,直接删除创建的deployment
拷贝dump.rdb和appendonly.aof至目标redis的持久化目录下(注:将覆盖目标redis的数据)
重启pod:kubectl apply -f 3-deployment.yaml
关于appendonly.aof和dump.rdb
参考点击连接即可:
AOF:会将每次执行的命令及时保存到硬盘中,实时性更好,丢失的数据更少
AOF会保存服务器执行的所有写操作到日志文件中,在服务重启以后,会执行这些命令来恢复数据
RDB:会根据指定的规则定时将内存中的数据保存到硬盘中
RDB 持久化生成的 RDB 文件是一个经过压缩的二进制文件,也可以称之为快照文件,
通过该文件可以还原生成 RDB 文件时的数据库状态
需要注意的是:
如果你使用了SSD的话,appendfsync设置always会可能导致
write amplification从而很大影响SSD的寿命;
另外就是aof一般是比rdb文件较大,恢复时间较长,因为要重新执行所有的写操作
需要注意的是:
RDB如果分配的不合理,每次快照和下一次快照的时间间隔中如果down机
那么上次快照与下一次快照之间的数据也会丢失
Mysql
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
app: mysql
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql
name: mysql
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 4
memory: 6Gi
limits:
cpu: 4
memory: 12Gi
env:
- name: MYSQL_ROOT_PASSWORD
value: abc123
---
apiVersion: v1
kind: Service
metadata:
labels:
app: mysql
name: mysql
spec:
type: NodePort
ports:
- port: 3306
nodePort: 30006
protocol: TCP
targetPort: 3306
selector:
app: mysql