1.制作redis docker镜像
1.1 准备alpine基础镜像
# 下载
docker pull alpine:3.13
# 更改tag
docker tag alpine:3.13 192.168.1.110/base/alpine:3.13
# 上传到本地harbor
docker push 192.168.1.110/base/alpine:3.13
1.2 准备redis安装包及配置文件
下载redis安装包
wget http://download.redis.io/releases/redis-4.0.14.tar.gz
修改redis.conf
tar xf redis-4.0.14.tar.gz
cp redis-4.0.14/redis.conf .
vim redis.conf
...
save 5 1
save 900 1
save 300 10
save 60 10000
dbfilename dump.rdb
dir /data/
...
镜像中redis的启动脚本
cat run_redis.sh
#!/bin/bash
/usr/local/redis/bin/redis-server /etc/redis.conf
tail -f /etc/hosts
1.3 编写Dockerfile
FROM 192.168.1.110/base/alpine:3.13
ADD redis-4.0.14.tar.gz /tmp/
ADD run_redis.sh /usr/local/redis/
ADD redis.conf /etc/redis.conf
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk add gcc g++ make libffi-dev openssl-dev libtool \
&& mkdir /data/redisdata -p \
&& cd /tmp/redis-4.0.14 \
&& make PREFIX=/usr/local/redis install \
&& rm -rf /tmp/redis-4.0.14
EXPOSE 6379
CMD ["/usr/local/redis/run_redis.sh"]
1.4 制作镜像并上传到本地harbor
# ls -l
总用量 1772
-rw-r--r-- 1 root root 471 10月 22 14:37 Dockerfile
-rw-r--r-- 1 root root 1740967 10月 22 13:58 redis-4.0.14.tar.gz
-rw-r--r-- 1 root root 58792 10月 22 16:30 redis.conf
-rwxr-xr-x 1 root root 82 10月 22 14:15 run_redis.sh
# docker build -t 192.168.1.110/base/alpine3.13-redis4.0.14:v20211022-1631 .
# docker push 192.168.1.110/base/alpine3.13-redis4.0.14:v20211022-1631
2.为redis数据存储部署PV/PVC
2.1 NFS server创建目录
mkdir /data/k8s-data/redis/datadir-pv-1 -p
2.2 编写pv/pvc yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis-datadir-pv-1
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
nfs:
server: 192.168.2.10
path: /data/k8s-data/redis/datadir-pv-1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-datadir-pvc-1
namespace: yan-test
spec:
accessModes:
- ReadWriteOnce
volumeName: redis-datadir-pv-1
resources:
requests:
storage: 10Gi
2.3 创建pv/pvc并验证
创建namespace
# kubectl create ns yan-test
创建pv/pvc
# kubectl apply -f redis_pv_pvc.yaml
验证
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
redis-datadir-pv-1 20Gi RWO Retain Bound yan-test/redis-datadir-pvc-1 78m
# kubectl get pvc -n yan-test
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
redis-datadir-pvc-1 Bound redis-datadir-pv-1 20Gi RWO 78m
3.在k8s上部署redis
3.1 编写yaml
# cat redis_svc_deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-deployment
labels:
app: redis
namespace: yan-test
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis-container
image: 192.168.1.110/base/alpine3.13-redis4.0.14:v20211022-1631
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: /data/
name: redis-data
volumes:
- name: redis-data
persistentVolumeClaim:
claimName: redis-datadir-pvc-1
---
apiVersion: v1
kind: Service
metadata:
name: redis-svc
namespace: yan-test
spec:
ports:
- name: http
port: 6379
targetPort: 6379
nodePort: 36379
type: NodePort
selector:
app: redis
3.2 创建redis
# kubectl apply -f redis_svc_deploy.yaml
3.3 验证是否创建成功
root@k8-deploy:~/k8s-yaml/redis# kubectl get svc -n yan-test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redis-svc NodePort 10.0.12.138 <none> 6379:36379/TCP 36m
root@k8-deploy:~/k8s-yaml/redis# kubectl get pod -n yan-test
NAME READY STATUS RESTARTS AGE
redis-deployment-75b9b94b64-rtj95 1/1 Running 0 36m
4.验证redis服务及数据存储是否正常
# 通过Nodeport端口连接redis
root@k8-deploy:~/k8s-yaml/redis# redis-cli -h 192.168.2.17 -p 36379
192.168.2.17:36379> INFO
# Server
redis_version:4.0.14
redis_git_sha1:00000000
...
# 写入数据测试
192.168.2.17:36379> DBSIZE
(integer) 0
192.168.2.17:36379> set a 1
OK
# 查看NFSsever上redis的目录是否有redis的数据文件
root@k8-deploy:~/k8s-yaml/redis# ls /data/k8s-data/redis/datadir-pv-1/
dump.rdb redis.log