三、实战案例之Redis 服务

1.Dockfile文件信息
[root@localhost7C redis]# ll
-rwxr-xr-x 1 root root     132 4月   3 13:49 build-command.sh
-rw-r--r-- 1 root root     489 4月   3 13:51 Dockerfile
-rw-r--r-- 1 root root 1740967 4月   7 2020 redis-4.0.14.tar.gz
-rw-r--r-- 1 root root   58783 4月   3 13:44 redis.conf
-rwxr-xr-x 1 root root      85 4月   7 2020 run_redis.sh


[root@localhost7C redis]# cat Dockerfile 
#Redis Image
FROM harbor.zzhz.com/baseimages/magedu-centos-base:7.6.1810
MAINTAINER zhangshijie "zhangshijie@magedu.net"
ADD redis-4.0.14.tar.gz /usr/local/src
RUN ln -sv /usr/local/src/redis-4.0.14 /usr/local/redis && cd /usr/local/redis && make && cp src/redis-cli /usr/sbin/ && cp src/redis-server  /usr/sbin/ && mkdir -pv /data/redis-data 
ADD redis.conf /usr/local/redis/redis.conf 
ADD run_redis.sh /usr/local/redis/run_redis.sh
EXPOSE 6379
CMD ["/usr/local/redis/run_redis.sh"]


[root@localhost7C redis]# cat run_redis.sh 
#!/bin/bash
/usr/sbin/redis-server /usr/local/redis/redis.conf
tail -f  /etc/hosts



[root@localhost7C redis]# grep -Ev '^[ ]*$|^#'  redis.conf 
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 5 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis-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
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
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 ""
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@localhost7C redis]# cat build-command.sh 
#!/bin/bash
TAG=$1
docker build -t harbor.zzhz.com/linux39/redis:${TAG} .
sleep 3
docker push  harbor.zzhz.com/linux39/redis:${TAG}


[root@localhost7C redis]# chmod +x  ./*.sh
[root@localhost7C redis]# ./build-command.sh v1

 

2.安装nfs服务器,基于PV和PVC作为后端存储
[root@localhost7B ]# cat  /etc/exports
/data/k8sdata  *(rw,no_root_squash)

[root@localhost7B ]# mkdir /data/k8sdata/magedu/redis-datadir-1

[root@localhost7B ]# systemctl  restart  nfs-server.service

#写数据测试
mount -t nfs 192.168.80.110:/data/k8sdata/magedu/redis-datadir-1   /mnt
umount  /mnt/

[root@localhost7C redis]# tree 
.
├── pv
│   ├── redis-persistentvolumeclaim.yaml
│   └── redis-persistentvolume.yaml
└── redis.yaml


#创建PV/PVC保存数据,实现k8s中运行Redis服务
[root@localhost7C redis]# cat pv/redis-persistentvolume.yaml 
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-datadir-pv-1
  namespace: magedu
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    path: /data/k8sdata/magedu/redis-datadir-1 
    server: 192.168.80.110



[root@localhost7C redis]# cat pv/redis-persistentvolumeclaim.yaml 
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-datadir-pvc-1 
  namespace: magedu
spec:
  volumeName: redis-datadir-pv-1 
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

 

3. 清单
[root@localhost7C redis]# cat redis.yaml 
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
  labels:
    app: devops-redis 
  name: deploy-devops-redis
  namespace: magedu
spec:
  replicas: 1 
  selector:
    matchLabels:
      app: devops-redis
  template:
    metadata:
      labels:
        app: devops-redis
    spec:
      containers:
        - name: redis-container
          image: harbor.zzhz.com/linux39/redis:v1
          imagePullPolicy: Always
          volumeMounts:
          - mountPath: "/data/redis-data/"
            name: redis-datadir
      volumes:
        - name: redis-datadir
          persistentVolumeClaim:
            claimName: redis-datadir-pvc-1 

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: devops-redis
  name: srv-devops-redis
  namespace: magedu
spec:
  type: NodePort
  ports:
  - name: http
    port: 6379 
    targetPort: 6379
    nodePort: 36379 
  selector:
    app: devops-redis
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 10800



#登录测试

 

4.部署测试
[root@localhost7C redis]# kubectl apply   -f  ./


[root@localhost7C redis]# kubectl exec -it -n magedu deploy-devops-redis-7556995d45-cxs58  bash
[root@deploy-devops-redis-7556995d45-cxs58 /]# redis-cli  -h 127.0.0.1 -p 6379  -a 123456
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6379> set key1 vv1
OK

127.0.0.1:6379> set key2 v2
OK
127.0.0.1:6379> set key3 v3
OK
127.0.0.1:6379> set key4 v4
OK
127.0.0.1:6379> set key5 v5
OK
127.0.0.1:6379> get key1
"vv1"



验证nfs服务器Redis的快照数据:
[root@localhost7B ~]# ll  /data/k8sdata/magedu/redis-datadir-1/
-rw-r--r-- 1 root root 144 4月   3 14:31 dump.rdb



验证Redis数据高可用:
删除redis的pod,然后重新创建pod验证新生成的pod中是否有之前的数据,可能有丢失数据的几率,取决于是否
开启AOF或者dump数据的功能及设置:

[root@localhost7C redis]# kubectl delete  -f redis.yaml 
[root@localhost7C redis]# kubectl get pod -A -o wide
NAMESPACE              NAME                                         READY   STATUS        RESTARTS   AGE    IP               NODE             NOMINATED NODE   READINESS GATES
kube-system            metrics-server-ccccb9bb6-22ssw               1/1     Running       1          5d3h   10.20.6.18       192.168.80.150   <none>           <none>
kubernetes-dashboard   dashboard-metrics-scraper-74bbb59f48-t6jj2   1/1     Running       1          6d4h   10.20.6.19       192.168.80.150   <none>           <none>
kubernetes-dashboard   kubernetes-dashboard-bc4695695-t44sv         1/1     Running       3          5d3h   10.20.6.17       192.168.80.150   <none>           <none>
magedu                 deploy-devops-redis-7556995d45-cxs58         1/1     Terminating   0          19m    10.20.5.47       192.168.80.160   <none>           <none>


[root@localhost7C redis]# kubectl apply  -f redis.yaml 
[root@localhost7C redis]# kubectl exec -it -n magedu deploy-devops-redis-7556995d45-j6v4l bash
[root@deploy-devops-redis-7556995d45-j6v4l /]# redis-cli  -h 127.0.0.1 -p 6379  -a 123456
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> get key4
"v4"
posted @ 2023-03-31 10:29  yuanbangchen  阅读(19)  评论(0编辑  收藏  举报