1、配置configmap
kind: ConfigMap
apiVersion: v1
metadata:
name: redis-config
namespace: default
labels:
app: redis
data:
redis.conf: |-
dir /data
port 6379
bind 0.0.0.0
appendonly yes
protected-mode no
requirepass 123
pidfile /data/redis-6379.pid
2、配置nfs目录
cd /k8s
mkdir redis
chmod 777 -R redis/
2、配置pv和pvc
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis-pv
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
nfs:
path: /k8s/redis
readOnly: false
server: 10.10.10.25 # nfs服务器ip
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-pvc
namespace: default
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: ''
volumeName: redis-pv
3、部署deploment
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: default
labels:
app: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
# 进行初始化操作,修改系统配置,解决 Redis 启动时提示的警告信息
initContainers:
- name: system-init
image: busybox:1.32
imagePullPolicy: IfNotPresent
command:
- "sh"
- "-c"
- "echo 2048 > /proc/sys/net/core/somaxconn && echo never > /sys/kernel/mm/transparent_hugepage/enabled"
securityContext:
privileged: true
runAsUser: 0
volumeMounts:
- name: sys
mountPath: /sys
containers:
- name: redis
image: redis:5.0.8
command:
- "sh"
- "-c"
- "redis-server /usr/local/etc/redis/redis.conf"
ports:
- containerPort: 6379
resources:
limits:
cpu: 1000m
memory: 1024Mi
requests:
cpu: 1000m
memory: 1024Mi
livenessProbe:
tcpSocket:
port: 6379
initialDelaySeconds: 300
timeoutSeconds: 1
periodSeconds: 10
successThreshold: 1
failureThreshold: 3
readinessProbe:
tcpSocket:
port: 6379
initialDelaySeconds: 5
timeoutSeconds: 1
periodSeconds: 10
successThreshold: 1
failureThreshold: 3
volumeMounts:
- name: data
mountPath: /data
- name: config
mountPath: /usr/local/etc/redis/redis.conf
subPath: redis.conf
volumes:
- name: data
persistentVolumeClaim:
claimName: redis-pvc
- name: config
configMap:
name: redis-config
- name: sys
hostPath:
path: /sys
4、配置nodeport供外部访问
apiVersion: v1
kind: Service
metadata:
name: redis-front
labels:
app: redis
namespace: default
spec:
selector:
app: redis
type: NodePort
clusterIP: 10.43.0.2 #任意10.43.0.x
ports:
- name: ''
port: 6379
protocol: TCP
targetPort: 6379
nodePort: 30003 #任意30000-32767
sessionAffinity: None