1、首先,通过Config Map来对容器中redis应用的配置进行管理,如自定义配置文件、密码、日志路径等
redis-standalone-conf.yml
apiVersion: v1 kind: ConfigMap metadata: name: redis-standalone-conf data: redis.conf: | bind 0.0.0.0 port 6379 requirepass 111111 appendonly yes cluster-config-file nodes-6379.conf pidfile /redis/log/redis-6379.pid cluster-config-file /redis/conf/redis.conf dir /redis/data/ logfile /redis/log/redis-6379.log cluster-node-timeout 5000 protected-mode no
创建configmaps
kubectl apply -f redis-standalone-conf.yml
2、上面部署的redis,数据是存储在当前部署节点的本地的文件系统,现在讲存储改成NFS,创建pv和pvc
首先创建NFS
注:预先在NFS服务器上创建目录:/home/data/nfs/pai/redisstandalone
# 创建挂载目录 mkdir /home/data/nfs/pai/redisstandalone -p cat >>/etc/exports <<EOF /home/data/nfs/pai/redisstandalone *(rw,sync,no_root_squash) EOF exportfs -r systemctl restart rpcbind && systemctl restart nfs-server
redis-standalone-pv.yml
--- apiVersion: v1 kind: PersistentVolume metadata: name: redis-standalone-pv spec: capacity: storage: 200G accessModes: - ReadWriteMany storageClassName: nfsredis nfs: server: 192.168.33.157 # NFS服务器地址 path: "/home/data/nfs/pai/redisstandalone" # NFS目录 --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: redis-standalone-pvc spec: volumeName: redis-standalone-pv accessModes: - ReadWriteMany resources: requests: storage: 100Gi storageClassName: nfsredis
创建pv和pvc
kubectl apply -f redis-standalone-pv.yml
3、由于redis是一个有状态服务,因此,现需要创建一个StatefulSet,并把数据挂载到宿主机上。
redis-standalone-statefulset.yml
apiVersion: apps/v1 kind: StatefulSet metadata: name: redis-standalone spec: replicas: 1 serviceName: redis-standalone selector: matchLabels: name: redis-standalone template: metadata: labels: name: redis-standalone spec: #nodeName: gggd12-111-128-105 # 指定调度节点 initContainers: - name: init-redis-standalone image: busybox command: ['sh', '-c', 'mkdir -p /redis/log/;mkdir -p /redis/conf/;mkdir -p /redis/data/'] volumeMounts: - name: redis-standalone-pvc mountPath: /redis/ containers: - name: redis-standalone image: redis:5.0.6 imagePullPolicy: IfNotPresent command: - sh - -c - "exec redis-server /redis/conf/redis.conf" ports: - containerPort: 6379 name: redis protocol: TCP volumeMounts: - name: redis-config mountPath: /redis/conf/ - name: redis-standalone-pvc mountPath: /redis/ volumes: - name: redis-config configMap: name: redis-standalone-conf - name: redis-standalone-pvc persistentVolumeClaim: claimName: redis-standalone-pvc
4、通过创建service,提供对外访问pod的服务接口
redis-standalone-service.yml
kind: Service apiVersion: v1 metadata: labels: name: redis-standalone name: redis-standalone spec: type: NodePort ports: - name: redis port: 6379 targetPort: 6379 nodePort: 31379 selector: name: redis-standalone
创建service
kubectl apply -f redis-standalone-service.yml
5、验证redis是否部署成功
查看pod、pv、pvc、service、ConfigMap运行状态
kubectl get pod,svc,pv,pvc,cm
测试远程连接
注:这里的host为宿主机IP
-h 指定 远程redis主机的IP地址(pod所在的k8s节点)
-p 指定 远程redis连接的端口 (service暴露的端口)
-a 指定 远程redis连接的密码 (ConfigMap文件中设置的密码)
redis-cli -h 192.168.33.244 -p 31379 -a 111111
最后,更新完了重新验证redis即可。
登录NFS服务器,查看相应的文件挂载信息
[root@k8s-master redis]# tree /home/data/nfs/pai/redisstandalone/ /home/data/nfs/pai/redisstandalone/ ├── conf ├── data │ └── appendonly.aof └── log ├── redis-6379.log └── redis-6379.pid 3 directories, 3 files