创建本地目录用于redis持久化
创建持久化文件mkdir -p /Users/renzhengxin/IdeaProjects/k8s/redis/v30/data
创建持久卷
创建持久卷定义vim redis-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis30-pv
labels:
pv: redis30-pv
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /Users/renzhengxin/IdeaProjects/k8s/redis/v30/data
执行创建持久卷:kubectl create -f redis-pv.yaml 查看持久卷:kubectl get pv
创建持久卷声明
创建持久卷声明定义vim redis-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: redis30-pvc
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
selector:
matchLabels:
pv: redis30-pv
执行创建持久卷声明:kubectl create -f redis-pvc.yaml 查看持久卷声明:kubectl get pvc 查看持久卷:kubectl get pv
创建配置文件
创建redis配置文件:vim redis-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: redis30-conf
data:
redis30.conf: |
bind 0.0.0.0
port 6379
pidfile .pid
appendonly yes
cluster-config-file nodes-6379.conf
pidfile /data/redis/log/redis30.pid
cluster-config-file /data/redis/conf/redis30.conf
dir /data/redis/data/
logfile "/data/redis/log/redis30.log"
cluster-node-timeout 5000
创建configMap: kubectl create -f redis-cm.yaml 查看configMap: kubectl describe cm redis30-conf
创建StatefulSet
创建StatefulSet定义vim redis-ss.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis30-app
spec:
replicas: 1
serviceName: redis30-svc
selector:
matchLabels:
name: redis30
template:
metadata:
labels:
name: redis30
spec:
initContainers:
- name: init-redis
image: busybox:1.35
command: ['sh', '-c', 'mkdir -p /data/redis/log/;mkdir -p /data/redis/conf/;mkdir -p /data/redis/data/']
volumeMounts:
- name: data
mountPath: /data/redis/
containers:
- name: redis
image: amd64/redis:3.0
imagePullPolicy: IfNotPresent
command:
- sh
- -c
- "exec redis-server /data/redis/conf/redis30.conf"
ports:
- containerPort: 6379
name: redis
protocol: TCP
volumeMounts:
- name: redis-config
mountPath: /data/redis/conf/
- name: data
mountPath: /data/redis/
volumes:
- name: redis-config
configMap:
name: redis30-conf
- name: data
persistentVolumeClaim:
claimName: redis30-pvc
执行创建StatefulSet:kubectl create -f redis-ss.yaml 查看StatefulSet:kubectl get statefulSet 查看Pod:kubectl get po
创建服务
创建服务定义vim redis-svc.yaml
kind: Service
apiVersion: v1
metadata:
labels:
name: redis30-svc
name: redis30-svc
spec:
type: NodePort
ports:
- name: redis
port: 6379
targetPort: 6379
nodePort: 30030
selector:
name: redis30
执行创建服务:kubectl create -f redis-svc.yaml 查看服务:kubectl get svc
访问集群
如本地已安装redis客户端,使用以下命令: redis-cli -h localhost -p 30030 查看节点信息使用以下指令: info
参考文档: kubernetes部署redis5.0.6单机版 自定义配置文件、密码、日志路径等
|