Docker 简明教程
How to Setup and Run Redis in Docker?
Redis 是一种开源且内存中的数据结构存储,以处理字符串、哈希、列表、集合等各种数据类型而著称。Redis 最初开发为一种缓存解决方案,但现在已经发展成为一种用于实时分析、消息代理、会话管理和队列系统的强大工具。
Redis is an open-source and in-memory data structure store that is widely known for its excellent performance in handling various data types such as strings, hashes, lists, sets, etc. Redis was originally developed as a caching solution, but it has evolved into a powerful tool for real-time analytics, message brokering, session management, and queuing systems.
但是,部署和管理 Redis 实例可能很困难,尤其是在关注可伸缩性、一致性和资源利用的环境中。Docker 的轻量级容器化功能在此发挥作用。当您在 Docker 容器中运行 Redis 时,您可以在多个环境中持续部署、扩展和管理 Redis 实例。
However, deploying and managing Redis instances can be difficult, especially in environments that are concerned with scalability, consistency, and resource utilization. This is where Docker’s lightweight containerization capabilities come into the picture. When you run Redis in Docker containers, you can consistently deploy, scale, and manage Redis instances across multiple environments.
以下是在 Docker 容器中设置和运行 Redis 的主要方法:
Here are the major ways to set up and run Redis inside Docker containers −
-
Pulling the Redis Docker base image from the official Docker Hub repository and customizing Redis container settings via Dockerfile.
-
Creating a Docker Compose file to define and manage Redis container configuration.
-
Utilizing Kubernetes for orchestrating Redis containers in a clustered environment.
在本节中,我们将分步详细讨论使用这 3 种方法在 Docker 容器中设置和运行 Redis 的方法,并提供示例和 Docker 命令。
In this chapter, let’s discuss how to setup and run Redis inside Docker containers using all these 3 approaches, in a detailed, step-by-step manner with examples and Docker commands.
How to Setup and Run Redis in Docker using Dockerfile?
以下是如何使用 Dockerfile 设置和运行 Redis 的分步指南:
Here’s a step-by-step guide on how to set up and run Redis using a Dockerfile:
Step 1: Create a Dockerfile
首先在项目目录中创建一个 Dockerfile。此文件将包含用于构建已预装 Redis 的 Docker 映像的说明和命令。
Start by creating a Dockerfile
in your project directory. This file will contain the instructions and commands to build the Docker image with Redis pre-installed.
# Use the official Redis image as the base image
FROM redis:latest
# Set metadata for the container
LABEL maintainer="Your Name <your@email.com>"
# Expose Redis default port
EXPOSE 6379
-
The
FROM
instruction specifies the base image that we will use. In this case, we will use the latest version of the official Redis image from Docker Hub. -
The
LABEL
instruction adds metadata to the image. Here, we have added the maintainer’s name and email. -
The
EXPOSE
instruction exposes Redis’s default port6379
. This allows it to accept connections from other containers or the host machine.
Step 2: Build the Docker Image
导航到创建 Dockerfile 的目录,并运行以下 Docker 构建命令来创建 Docker 映像。
Navigate to the directory where you have created your Dockefile and run the Docker build command below to create the Docker image.
docker build -t my-redis-image .
-
docker build
is the command that can be used to build a Docker image. -
-t my-redis-image
adds a tag to the image for easy reference. -
.
specifies the build context. It indicates that theDockerfile
is located in the current directory.
Step 3: Run the Redis Container
现在,在构建 Docker 镜像后,可以使用 Docker 运行命令运行与该镜像关联的容器。
Now that you have your Docker image built, you can run a container associated to that image using the Docker run command.
docker run --name my-redis-container -d my-redis-image
docker run
是我们用于运行 Docker 容器的命令。 --name my-redis-container
用于为运行的容器指定一个名称以便于识别。 -d
标志用于以分离模式运行容器,这意味着它在后台运行。然后我们指定要用于创建容器的 Docker 镜像的镜像名称 my-redis-image
。
The docker run
is the command that we have used to run a Docker container. The --name my-redis-container
is used to assign a name to the running container for easy identification. The -d
flag is used to run the container in detached mode, meaning it runs in the background. Then we specify the image name my-redis-image
of the Docker image to be used for creating the container.
Step 4: Verify the Container
如果您要确保 Redis 容器正在成功运行,可以使用以下命令列出所有正在运行的容器 -
If you want to ensure that the Redis container is running successfully, you can use the below command to list all running containers −
docker ps
这将显示有关正在运行的 Redis 容器的信息,包括其容器 ID、名称、状态和端口。
This will display information about the running Redis container, including its container ID, name, status, and ports.
Step 5: Access Redis
在验证 Redis 容器正在运行后,您现在可以使用 Redis 客户端工具(如 RedisInsight)访问它或从其他应用程序和服务连接到它。默认情况下,Redis 将在 Dockerfile 中提到的端口 6379
上可访问。
After verifying that the Redis container is running, you can now access it using Redis client tools like RedisInsight or connect to it from other applications and services. By default, Redis will be accessible on port 6379
that we mentioned in the Dockerfile.
How to Run Redis in Docker using Docker Compose?
Docker Compose 简化了定义和管理多容器 Docker 应用程序的过程。下面介绍如何在 Docker 中使用 Docker Compose 运行 Redis −
Docker Compose simplifies the process of defining and managing multi-container Docker applications. Here’s how to run Redis in Docker using Docker Compose −
Step 1: Create a Docker Compose File
首先,你可以创建一个名为 docker-compose.yml
的新文件放在项目目录中。
You can start by creating a new file named docker-compose.yml
in your project directory.
version: '3.8'
services:
redis:
image: redis:latest
container_name: my-redis-container
ports:
- "6379:6379"
在这个文件中,我们指定了正在使用的 Docker Compose 语法的 version: '3.8'
版本。然后,在 services
下面,我们定义了 Redis 服务: image: redis:latest
指定要从 Docker Hub 拉取和使用的 Redis 镜像。接下来,我们使用属性 container_name: my-redis-container
定义容器名称。最后,已使用 ports
指定要公开的端口,该端口将主机上的端口 6379
映射到容器中的端口 6379
,从而允许访问 Redis。
In this file, we have specified the version: '3.8'
version of Docker Compose syntax being used. Then, under services
, we have defined the Redis service: The image: redis:latest
specifies the Redis image to be pulled and used from Docker Hub. Next, we have defined the container name using the property container_name: my-redis-container
. Finally, the port to be exposed has been specified using ports
which maps port 6379
on the host machine to port 6379
in the container, allowing access to Redis.
Step 2: Run Docker Compose
接下来,你可以运行 Docker Compose 命令来启动安装了 Redis 的容器。导航至已创建 compose yml 文件的目录,然后运行以下命令。
Next, you can run the Docker compose command to start the container which has Redis installed in it. Navigate to the directory where you have created the compose yml file and run the below command.
docker-compose up -d
-
docker-compose up
command is used to create and start Docker containers using the configurations defined in thedocker-compose.yml
file. -
-d
flag is used to run the containers in detached mode, meaning they run in the background.
Step 3: Verify and access Redis from the Container
你可以使用以下命令列出所有正在运行的容器,以验证 Redis Docker 容器是否正在运行。
You can list all the running containers using the below command to verify if the Redis Docker container is running or not.
docker ps
在 Redis 容器运行时,你现在可以使用 Redis 客户端工具(例如 RedisInsight)访问它,或从其他应用程序连接到它。默认情况下,Redis 可以通过端口 6379
访问。
With the Redis container running, you can now access it using Redis client tools like RedisInsight or connect to it from other applications. By default, Redis is accessible on port 6379
.
How to run Redis in Docker Containers using Kubernetes?
Kubernetes 是一个流行的容器编排平台,它简化了容器化应用程序的部署、扩展和管理。以下是如何使用 Kubernetes 在集群环境中编排 Redis 容器的分步指南。
Kubernetes is a popular container orchestration platform that simplifies the deployment, scaling, and management of containerized applications. Here is a step-by-step guide on how you can utilize Kubernetes to orchestrate Redis containers in a clustered environment.
Step 1: Setup Kubernetes Cluster
你需要做的第一件事是设置 Kubernetes 集群。你可以使用托管式 Kubernetes 服务,例如 Google Kubernetes Engine (GKE) 和 Amazon Elastic Kubernetes Service (EKS),也可以使用 Minikube 或 kind 等工具在本地部署 Kubernetes。
The first thing you need to do is set up a Kubernetes cluster. You can use managed Kubernetes services like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or deploy Kubernetes locally using tools like Minikube or kind.
Step 2: Create a Redis Deployment YAML
接下来,你可以创建一个名为 redis-deployment.yaml
的 YAML 文件,在其中你可以定义 Redis 部署配置。
Next, you can create a YAML file called redis-deployment.yaml
where you can define the Redis deployment configuration.
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
replicas: 3
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: 'redis:latest'
ports:
- containerPort: 6379
在这个 YAML 文件中,我们定义了一个名为 redis
的 Kubernetes Deployment 对象,其中有 3 个副本。每个副本将运行一个基于 Docker Hub 中最新 Redis 镜像的 Redis 容器。容器将在端口 6379
上侦听。
In this YAML file, we have defined a Kubernetes Deployment object named redis
with 3 replicas. Each replica will run a Redis container based on the latest Redis image from Docker Hub. The container will listen on port 6379
.
Step 3: Apply the Redis Deployment
接下来,你必须将 Redis 部署配置应用于你的 Kubernetes 集群。
Next, you have to apply the Redis Deployment configuration to your Kubernetes cluster.
kubectl apply -f redis-deployment.yaml
此命令将帮助你创建 Redis Deployment 并根据定义的副本启动指定数量的 Redis pod(容器)。
This command will help you to create the Redis Deployment and start the specified number of Redis pods (containers) as per the defined replicas.
Step 4: Expose Redis Service
接下来,你可以创建一个 Kubernetes Service,以在 Kubernetes 集群内部公开 Redis Deployment。
Next, you can create a Kubernetes Service to expose the Redis Deployment internally within the Kubernetes cluster.
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
selector:
app: redis
ports:
- protocol: TCP
port: 6379
targetPort: 6379
在这个 YAML 文件中,我们定义了一个名为 redis
的 Kubernetes Service,它根据 app: redis
标签选择 Redis pod。然后,它在 Kubernetes 集群内公开端口 6379
。
In this YAML file, we have defined a Kubernetes Service named redis
that selects the Redis pods based on the app: redis
label. It then exposes port 6379
within the Kubernetes cluster.
Step 5: Apply the Redis Service
最后一步是使用以下命令应用服务配置。
The last step is to apply the Service configuration using the following command.
kubectl apply -f redis-service.yaml
该命令可用于为 Redis Deployment 创建 Kubernetes Service。这将允许集群内的其他 pod 使用服务名称 redis
访问 Redis。
This command can be used to create a Kubernetes Service for the Redis Deployment. This will allow other pods within the cluster to access Redis using the service name redis
.
Conclusion
综上所述,当我们利用 Docker 和 Kubernetes 部署和管理 Redis 容器时,这在简单性、可扩展性和可靠性方面提供了显著优势。在 Docker 的帮助下,设置 Redis 非常简单。此外,Kubernetes 提供鲁棒的容器管理功能。这使得能够在生产环境中无缝地部署和扩展 Redis 集群。
In conclusion, when we leverage Docker and Kubernetes for deploying and orchestrating Redis containers, it offers significant advantages in terms of simplicity, scalability, and reliability. With the help of Docker, it is very straightforward to set up Redis. Furthermore, Kubernetes provides robust container orchestration capabilities. This enables the seamless deployment and scaling of Redis clusters in a production environment.
Frequently Asked Questions
Q1. Can I persist Redis data when running it in Docker?
是的,您在 Docker 中运行 Redis 时可以使用 Docker 卷来保存 Redis 数据。可以通过 Docker 卷将数据存储在容器的文件系统之外,这保证了在停止或删除容器时不会丢失数据。
Yes, you can use Docker volumes to persist Redis data when running it in Docker. Data can be stored outside of the container’s filesystem using Docker volumes, which guarantee that the data will not be lost in the event that the container is stopped or removed.
您可以创建一个 Docker 卷并将其挂载到 Redis 容器中包含数据的相关目录,以保存 Redis 数据。在发生容器故障或重新启动事件时,这可以确保在 Redis 数据中所做的任何修改都会保留在主机系统上,从而实现数据的持久性和恢复。
You can make a Docker volume and mount it to the relevant directory inside the Redis container containing the data in order to persist Redis data. In the event of a container failure or restart, this guarantees that any modifications made to the Redis data are preserved on the host system, enabling data durability and recovery.
Q2. How can I configure Redis settings when running it in Docker?
Redis 在 Docker 容器中运行时能够使用环境变量或自定义 Redis 配置文件进行配置。当通过 Docker 将环境变量传给 Redis 容器时,Redis 容器可以修改默认参数,如 Redis 端口、密码和最大内存限制。
Redis can be configured using environment variables or a custom Redis configuration file when it is run in a Docker container. The Redis container can alter default parameters like the Redis port, password, and maximum memory limit when environment variables are passed to it via Docker.
此外,您可以在 Docker 文件中通过 COPY
命令使用 Docker 卷或将自定义 Redis 配置文件(包含您需要的参数)挂载到容器中。通过基于应用的需求微调 Redis 配置,这使您能够优化性能和安全性。
Moreover, you can use a Docker volume or the COPY
command in a Dockerfile to mount a customized Redis configuration file inside the container with the parameters you want. This lets you optimize performance and security by fine-tuning Redis configurations based on your application’s needs.
Q3. Is it possible to run multiple Redis instances in Docker for different purposes?
是的,可以通过为每个 Redis 实例构建不同的 Docker 容器,以便在 Docker 中出于不同的目的运行 Redis 的多个实例。借助 Docker,您可以同时运行多个容器,每个容器都具有自己独立的网络、存储和配置设置环境。
Yes, by building distinct Docker containers for each Redis instance, it is possible to run numerous instances of Redis in Docker for various purposes. With Docker, you can operate several containers at once, each with its own separate networking, storage, and configuration settings environment.
可以为不同的用途创建多个 Redis 容器,包括缓存、会话管理或排队,并且具有不同的名称、端口和配置。通过在 Docker 中运行多个 Redis 实例,您可以根据应用的不同需求,改善资源隔离、可扩展性和管理 Redis 基础架构时的灵活性。
Multiple Redis containers can be created for various uses, including caching, session management, or queuing, with unique names, ports, and configurations. You may improve resource isolation, scalability, and flexibility in managing your Redis infrastructure to match the various needs of your apps by running several Redis instances in Docker.