在 Kubernetes 上安装

如何在 Kubernetes 上安装 Redis Insight

本教程展示了如何在 Kubernetes (K8s) 上安装 Redis Insight。这是使用 Redis Insight 与 Redis Enterprise K8s 部署 的一种简便方法。

创建 Redis Insight 部署和服务

以下是将创建一个 Redis Insight 部署和一个 K8s 集群中的服务的带注释的 YAML 文件。

  1. 创建一个名为 redisinsight.yaml 的新文件,内容如下。
# Redis Insight 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
---
# Redis Insight 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 Redis Insight 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
  1. 创建 Redis Insight 部署和服务
kubectl apply -f redisinsight.yaml
  1. 部署和服务成功应用并完成之后,访问 Redis Insight。这可以通过使用我们创建的服务的 <external-ip> 来访问 Redis Insight 来实现。
$ kubectl get svc redisinsight-service
NAME                   CLUSTER-IP       EXTERNAL-IP      PORT(S)         AGE
redisinsight-service   <cluster-ip>     <external-ip>    80:32143/TCP    1m
  1. 如果使用的是 minikube,请运行 minikube list 列出服务并访问 http://<minikube-ip>:<minikube-service-port> 上的 Redis Insight。
$ 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 |                                             |
|-------------|----------------------|--------------|---------------------------------------------|

使用持久存储创建 Redis Insight 部署

以下是将在 K8s 集群中创建 Redis Insight 部署的带注释的 YAML 文件。它将分配从卷声明模板创建的持久卷。对容器的写入访问权限是在一个 init 容器中配置的。当使用具有持久可写卷的部署时,最好将策略设置为 Recreate。否则,您可能会发现有两个 pod 尝试使用同一个卷。

  1. 创建一个新文件 redisinsight.yaml,其内容如下。
# Redis Insight 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
---
# Redis Insight 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 1000 /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
  1. 创建 Redis Insight 部署和服务。
kubectl apply -f redisinsight.yaml

创建没有服务的 Redis Insight 部署。

以下是将在 K8s 集群中创建 Redis Insight 部署的带注释的 YAML 文件。

  1. 创建一个新文件 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
  1. 创建 Redis Insight 部署
kubectl apply -f redisinsight.yaml
注意
如果部署将由名为“redisinsight”的服务公开,请设置 RI_APP_HOSTRI_APP_PORT 环境变量以覆盖由服务创建的环境变量。

运行 Redis Insight

部署成功应用且部署完成后,访问 Redis Insight。这可以通过将部署公开为 K8s 服务或使用端口转发来完成,如下面的示例所示。

kubectl port-forward deployment/redisinsight 5540

打开浏览器并指向 http://localhost:5540

RATE THIS PAGE
Back to top ↑