1.创建redis存储数据目录和配置文件
mkdir -p /data/redis/redis-config/
touch /data/redis/redis-config/redis.conf
cat >> /data/redis/redis-config/redis.conf << EOF
requirepass 123456
bind 0.0.0.0
save 900 1
save 300 10
save 60 10000
EOF
2.编写redis的yaml资源清单
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: default # 默认空间
labels:
name: redis
spec:
replicas: 1
selector:
matchLabels:
name: redis
template:
metadata:
labels:
name: redis
spec:
containers:
- name: redis
image: redis:5.0.5
volumeMounts:
- name: redis-config
mountPath: "/usr/local/etc/redis"
- name: redis-data
mountPath: "/data"
command:
- "redis-server"
args:
- "/usr/local/etc/redis/redis.conf"
volumes:
- name: redis-config
hostPath:
path: /data/redis/redis-config
type: DirectoryOrCreate
- name: redis-data
hostPath:
path: /data/redis/redis-data
type: DirectoryOrCreate
---
apiVersion: v1
kind: Service
metadata:
name: redis
namespace: default # 默认空间
labels:
name: redis
spec:
type: NodePort # 如果需要集群外部访问,这里改为NodePort
ports:
- port: 6379
protocol: TCP
targetPort: 6379
name: redis
nodePort: 32379
selector:
name: redis
3.在K8S中执行redis资源清单
[root@k8s-master redis]# ls
redis.yaml
[root@k8s-master redis]# kubectl apply -f redis.yaml #创建redis容器
deployment.apps/redis created
service/redis created
[root@k8s-master redis]# kubectl get pods,svc #查看pod和svc
NAME READY STATUS RESTARTS AGE
pod/redis-654f4d7694-k2qzm 1/1 Running 0 9s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/redis NodePort 10.101.69.100 <none> 6379:32379/TCP 9s
[root@k8s-master redis]#
4.进入容器测试
[root@k8s-master redis]# kubectl exec -it pod/redis-654f4d7694-k2qzm -- sh
# redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379>