配置文件如下:
1. 首先,通过ConfigMap来对容器中redis应用的配置进行管理,如自定义配置文件、密码、日志路径等
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-standalone-conf
data:
redis.conf: |
bind 0.0.0.0
port 6379
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
2.由于redis是一个有状态服务,因此,现需要创建一个StatefulSet,并把数据挂载到宿主机上。
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:
initContainers:
- name: init-redis-standalone
image: busybox
command: ['sh', '-c', 'mkdir -p /redis/log/;mkdir -p /redis/conf/;mkdir -p /redis/data/']
volumeMounts:
- name: data
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: data
mountPath: /redis/
volumes:
- name: redis-config
configMap:
name: redis-standalone-conf
- name: data
hostPath:
path: /redis/
3.创建statefulset
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
连接redis的31379端口即可
参考地址:https://zhuanlan.zhihu.com/p/160959892