Deploy redis on Kubernetes
Environmental preparation
First you need a Kubernetes environment, please refer to the article I wrote:https://blog.csdn.net/m0_51510236/article/details/130842122
Create namespace
We can choose the following two ways to create a namespace. The name of the namespace I created this time isdeploy-test
, which can be changed. This is just a demonstration:
- Command to create directly
kubectl create namespace deploy-test
- yaml creation (recommended)
apiVersion: v1
kind: Namespace
metadata:
name: deploy-test
spec: {
}
status: {
}
Prepare PV and PVC
I explained what PV and PVC are in my previous article:https://blog.csdn.net/m0_51510236/article/details/132482351 a>
Install nfs
This article is used directly. The preparation this time is nfs for storage, so three servers need to be installed. nfs-utils
:
yum install -y nfs-utils
As shown in the figure, all three servers have been installed:
Then we execute these two lines of commands on the main server or nfs server:
mkdir -p /data/nfs/redis
cat >> /etc/exports << EOF
/data/nfs/redis *(rw,sync,no_root_squash)
EOF
This line of command is exposed/data/nfs/redis
to the directory accessed by the nfs client, which is used to store redis data and logs. As a result, we need to open the nfs server and execute the following line Command:
systemctl enable --now nfs-server
Check the exposure results after exposure:
showmount -e nfs服务端地址
The test is as shown in the figure:
Okay, next nfs will be installed.
Prepare for PV
Upload yaml directly and modify it according to the prompt content:
apiVersion: v1
kind: PersistentVolume
metadata:
name: deploy-redis-nfs-pv # PV的名字
namespace: deploy-test # 命名空间
spec:
capacity:
storage: 1Gi # 申请的大小
accessModes:
- ReadWriteMany # 访问模式为多节点读写
nfs:
server: 192.168.1.160 # nfs服务器地址,注意修改为你自己的
path: /data/nfs/redis # nfs的远程目录
storageClassName: "nfs" # 存储类名字为nfs
The creation results show:
Prepare PVC
Go directly to yaml. There is nothing to modify other than changing the name:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: deploy-redis-nfs-pvc # pvc名字
namespace: deploy-test # 命名空间
spec:
accessModes:
- ReadWriteMany # 多节点读写
storageClassName: "nfs" # 存储类类型为nfs
resources:
requests:
storage: 1Gi # 申请存储空间的大小
volumeName: deploy-redis-nfs-pv # 对应上面的pv名字
View the creation of pvc:
View the status of both (kubectl get pv,pvc -o wide -n deploy-test
):
Deploy redis
Create a redis configuration file
First, let’s specify the content of the configuration file (be sure to change your password):
# 关闭保护模式
protected-mode no
# redis链接密码
requirepass redis
# 日志级别
loglevel warning
# 日志存放目录
logfile "/data/redis.log"
# 数据文件目录
dir /data
dbfilename dump.rdb
# 数据库数量
databases 16
# 保存数据库到数据文件
save 900 1
save 300 10
save 60 10000
Note uploading this configuration file to the server:
Then we create a configmap for this file:
kubectl create configmap deploy-redis-config -n deploy-test --from-file=redis.conf --dry-run=client -o yaml
The above line of command will generate a yaml file based on redis.conf
:
So the yaml file that creates this configmap is:
apiVersion: v1
data:
redis.conf: |
# 关闭保护模式
protected-mode no
# redis链接密码
requirepass redis
# 日志级别
loglevel warning
# 日志存放目录
logfile "/data/redis.log"
# 数据文件目录
dir /data
dbfilename dump.rdb
# 数据库数量
databases 16
# 保存数据库到数据文件
save 900 1
save 300 10
save 60 10000
kind: ConfigMap
metadata:
name: deploy-redis-config
namespace: deploy-test
Create a configmap as shown in the figure:
department script
apiVersion: v1
kind: Service
metadata:
name: deploy-redis-svc
namespace: deploy-test
labels:
app: redis
spec:
ports:
- port: 6379
name: redis
targetPort: 6379
nodePort: 30379
selector:
app: redis
type: NodePort
sessionAffinity: ClientIP
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: deploy-redis
namespace: deploy-test
spec:
selector:
matchLabels:
app: redis # 必须匹配 .spec.template.metadata.labels
serviceName: "deploy-redis-svc"
replicas: 1
template:
metadata:
labels:
app: redis # 必须匹配 .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- command:
- "redis-server"
- "/usr/local/etc/redis.conf"
name: redis
image: redis:5.0.14
ports:
- containerPort: 6379
name: redis
volumeMounts:
- name: redis-data
mountPath: /data
- name: redis-config
mountPath: /usr/local/etc
readOnly: true
volumes:
- name: redis-data
persistentVolumeClaim:
claimName: deploy-redis-nfs-pvc
- name: redis-config
configMap:
name: deploy-redis-config
items:
- key: redis.conf
path: redis.conf
Next, I will explain the more important scripts inside.
Mount data directory
There is a string of code in StatefulSet.spec.template.spec.volumes
:
- name: redis-data
persistentVolumeClaim:
claimName: deploy-redis-nfs-pvc
This string of code is to use deploy-redis-nfs-pvc
this pvc for data storage, and give this mount a name redis-data
, and then a>StatefulSet.spec.template.spec.containers
There is a string of code:
- name: redis-data
mountPath: /data
This string of code means to use the data mount named redis-data
and mount it in the /data directory of the pod
Mount configuration file
There is a string of code in StatefulSet.spec.template.spec.volumes
:
- name: redis-config
configMap:
name: deploy-redis-config
items:
- key: redis.conf
path: redis.conf
This string of code means to read the configmapdeploy-redis-config
and name it redis-config
, and then get the redis.conf
inside Configuration file, named redis.conf
, and then there is a string of code in StatefulSet.spec.template.spec.containers
:
- name: redis-config
mountPath: /usr/local/etc
readOnly: true
This string of code means to use the configuration file named redis-config
and mount it in the /usr/local/etc of the pod in a read-only manner
Start redis through the specified configuration file
There is this line of code in `StatefulSet.spec.template.spec.containers:
- command:
- "redis-server"
- "/usr/local/etc/redis.conf"
This line of code means to use the/usr/local/etc/redis.conf
configuration file to start redis. This configuration file is configured in configmap. When we execute the deployed yaml, we can view the results:
Execute the following command to check the deployment progress:
kubectl get all -o wide -n deploy-test
Seeing this means the deployment is complete:
Access within the cluster
The access address of the StatefulSet application is:
<pod名称>.<service名称>.<命名空间名称>.svc.cluster.local
Then the pod access address exposed this time is:
deploy-redis-0.deploy-redis-svc.deploy-test.svc.cluster.local
Try to parse and verify:
External linkRedis
We can try linking directly using IntelliJ IDEA
or other linking tools:
Display link is successful (if there is a password, please enter the password):
And the operation is no problem:
We come to the data directory and check that the data in the container has been successfully exposed to the nfs directory:
At this point, the installation of redis on Kubernetes is complete!