在 Kubernetes 上安装
如何在 Kubernetes 上安装 RedisInsight
本教程演示如何在 Kubernetes (K8s) 上安装 RedisInsight。这是将 RedisInsight 与 Redis Enterprise K8s 部署 配合使用的简单方法。
创建 RedisInsight 部署和服务
以下是带注释的 YAML 文件,它将在 K8s 集群中创建一个 RedisInsight 部署和一个服务。
- 使用以下内容创建一个名为
redisinsight.yaml
的新文件。
# RedisInsight service with name 'redisinsight-service'
apiVersion: v1
kind: Service
metadata:
name: redisinsight-service # name should not be 'redisinsight'
# since the service creates
# environment variables that
# conflicts with redisinsight
# application's environment
# variables `RI_APP_HOST` and
# `RI_APP_PORT`
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 5540
selector:
app: redisinsight
---
# RedisInsight deployment with name 'redisinsight'
apiVersion: apps/v1
kind: Deployment
metadata:
name: redisinsight #deployment name
labels:
app: redisinsight #deployment label
spec:
replicas: 1 #a single replica pod
selector:
matchLabels:
app: redisinsight #which pods is the deployment managing, as defined by the pod template
template: #pod template
metadata:
labels:
app: redisinsight #label for pod/s
spec:
containers:
- name: redisinsight #Container name (DNS_LABEL, unique)
image: redis/redisinsight:latest #repo/image
imagePullPolicy: IfNotPresent #Installs the latest RedisInsight version
volumeMounts:
- name: redisinsight #Pod volumes to mount into the container's filesystem. Cannot be updated.
mountPath: /data
ports:
- containerPort: 5540 #exposed container port and protocol
protocol: TCP
volumes:
- name: redisinsight
emptyDir: {} # node-ephemeral volume https://kubernetes.ac.cn/docs/concepts/storage/volumes/#emptydir
- 创建 RedisInsight 部署和服务
kubectl apply -f redisinsight.yaml
- 在部署和服务成功应用并完成后,访问 RedisInsight。这可以通过使用我们创建的服务的
<external-ip>
来访问 RedisInsight 来实现。
$ kubectl get svc redisinsight-service
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redisinsight-service <cluster-ip> <external-ip> 80:32143/TCP 1m
- 如果您正在使用 minikube,请运行
minikube list
来列出服务,并在http://<minikube-ip>:<minikube-service-port>
访问 RedisInsight。
$ minikube list
|-------------|----------------------|--------------|---------------------------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-------------|----------------------|--------------|---------------------------------------------|
| default | kubernetes | No node port | |
| default | redisinsight-service | 80 | http://<minikube-ip>:<minikubeservice-port> |
| kube-system | kube-dns | No node port | |
|-------------|----------------------|--------------|---------------------------------------------|
使用持久存储创建 RedisInsight 部署
以下是带注释的 YAML 文件,它将在 K8s 集群中创建一个 RedisInsight 部署。它将分配一个从卷声明模板创建的持久卷。对容器的写访问权限在初始化容器中进行配置。在将部署与持久可写卷一起使用时,最好将策略设置为 Recreate
。否则,您可能会发现两个 Pod 尝试使用同一卷。
- 使用以下内容创建一个名为
redisinsight.yaml
的新文件。
# RedisInsight service with name 'redisinsight-service'
apiVersion: v1
kind: Service
metadata:
name: redisinsight-service # name should not be 'redisinsight'
# since the service creates
# environment variables that
# conflicts with redisinsight
# application's environment
# variables `RI_APP_HOST` and
# `RI_APP_PORT`
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 5540
selector:
app: redisinsight
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redisinsight-pv-claim
labels:
app: redisinsight
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
storageClassName: default
---
# RedisInsight deployment with name 'redisinsight'
apiVersion: apps/v1
kind: Deployment
metadata:
name: redisinsight #deployment name
labels:
app: redisinsight #deployment label
spec:
replicas: 1 #a single replica pod
strategy:
type: Recreate
selector:
matchLabels:
app: redisinsight #which pods is the deployment managing, as defined by the pod template
template: #pod template
metadata:
labels:
app: redisinsight #label for pod/s
spec:
volumes:
- name: redisinsight
persistentVolumeClaim:
claimName: redisinsight-pv-claim
initContainers:
- name: init
image: busybox
command:
- /bin/sh
- '-c'
- |
chown -R 1001 /data
resources: {}
volumeMounts:
- name: redisinsight
mountPath: /data
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
containers:
- name: redisinsight #Container name (DNS_LABEL, unique)
image: redis/redisinsight:latest #repo/image
imagePullPolicy: IfNotPresent #Always pull image
volumeMounts:
- name: redisinsight #Pod volumes to mount into the container's filesystem. Cannot be updated.
mountPath: /data
ports:
- containerPort: 5540 #exposed container port and protocol
protocol: TCP
- 创建 RedisInsight 部署和服务。
kubectl apply -f redisinsight.yaml
创建没有服务的 RedisInsight 部署。
以下是带注释的 YAML 文件,它将在 K8s 集群中创建一个 RedisInsight 部署。
- 使用以下内容创建一个名为 redisinsight.yaml 的新文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: redisinsight #deployment name
labels:
app: redisinsight #deployment label
spec:
replicas: 1 #a single replica pod
selector:
matchLabels:
app: redisinsight #which pods is the deployment managing, as defined by the pod template
template: #pod template
metadata:
labels:
app: redisinsight #label for pod/s
spec:
containers:
- name: redisinsight #Container name (DNS_LABEL, unique)
image: redis/redisinsight:latest #repo/image
imagePullPolicy: IfNotPresent #Always pull image
env:
# If there's a service named 'redisinsight' that exposes the
# deployment, we manually set `RI_APP_HOST` and
# `RI_APP_PORT` to override the service environment
# variables.
- name: RI_APP_HOST
value: "0.0.0.0"
- name: RI_APP_PORT
value: "5540"
volumeMounts:
- name: redisinsight #Pod volumes to mount into the container's filesystem. Cannot be updated.
mountPath: /data
ports:
- containerPort: 5540 #exposed container port and protocol
protocol: TCP
livenessProbe:
httpGet:
path : /healthcheck/ # exposed RI endpoint for healthcheck
port: 5540 # exposed container port
initialDelaySeconds: 5 # number of seconds to wait after the container starts to perform liveness probe
periodSeconds: 5 # period in seconds after which liveness probe is performed
failureThreshold: 1 # number of liveness probe failures after which container restarts
volumes:
- name: redisinsight
emptyDir: {} # node-ephemeral volume https://kubernetes.ac.cn/docs/concepts/storage/volumes/#emptydir
- 创建 RedisInsight 部署
kubectl apply -f redisinsight.yaml
注意
如果部署将通过名称为“redisinsight”的服务公开,请设置 `RI_APP_HOST` 和 `RI_APP_PORT` 环境变量以覆盖服务创建的环境变量。
- 在部署成功应用并完成部署后,访问 RedisInsight。这可以通过将部署公开为 K8s 服务或使用端口转发来实现,如下例所示
kubectl port-forward deployment/redisinsight 5540
打开浏览器并指向 https://127.0.0.1:5540