一、创建ConfigMap
(1)首先创建命名空间
kubectl create namespace ospp
(2)编写redis-config.yaml文件,内容如下:
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-config
namespace: ospp
labels:
app: redis
data:
redis.conf: |-
dir /srv
port 6379
bind 0.0.0.0
appendonly yes
daemonize no
#protected-mode no
requirepass test
pidfile /srv/redis-6379.pid
(3)使用如下命令创建configmap
[root@master ospp]# kubectl apply -f redis-config.yaml
configmap/redis-config created
[root@master ospp]#
二、创建Redis
(1)编写redis.yaml文件,内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: ospp
labels:
app: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:5.0.7
command:
- "sh"
- "-c"
- "redis-server /usr/local/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: config
mountPath: /usr/local/redis/redis.conf
subPath: redis.conf
volumes:
- name: config
configMap:
name: redis-config
---
apiVersion: v1
kind: Service
metadata:
name: service-redis
namespace: ospp
spec:
ports:
- port: 6379
protocol: TCP
targetPort: 6379
nodePort: 30120
selector:
app: redis
type: NodePort
(2)使用如下命令创建redis
[root@master ospp]# kubectl apply -f redis.yaml
deployment.apps/redis created
service/service-redis unchanged
[root@master ospp]#
(3)查看创建资源如下:
[root@master ospp]# kubectl get service,deploy,pod -n ospp -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/service-redis NodePort 10.100.68.76 <none> 6379:30120/TCP 2m8s app=redis
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/redis 1/1 1 1 36s redis redis:5.0.7 app=redis
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/redis-748df49b99-kdhx5 1/1 Running 0 36s 10.244.2.146 node2 <none> <none>
[root@master ospp]#
三、验证redis
(1)若为安装redis-cli,先安装redis-cli
wget http://download.redis.io/redis-stable.tar.gz
tar -zxvf redis-stable.tar.gz
mv redis-stable /usr/local/src/
cd /usr/local/src
cd redis-stable/
make redis-cli
cp src/redis-cli /usr/local/bin/
(2)然后链接redis,如下表示OK
[root@master redis-stable]# redis-cli -h 192.168.16.40 -p 30120
192.168.16.40:30120> keys *
(error) NOAUTH Authentication required.
192.168.16.40:30120> auth test
OK
192.168.16.40:30120> exit
[root@master redis-stable]#