Redis基础篇02 — 多方式部署 Redis

概述

Redis 的安装方式有多种:

  • 容器化方式安装(从 dockerhub 中拉取镜像生成容器实例)
  • 从存储库中安装
  • 源代码编译安装(推荐)

为了最大化的发挥性能,推荐您使用源代码安装。截至本文撰写前,最新的稳定版本为 7.2.3。

使用 Docker 安装 Redis

由于你懂的原因,dockerhub 的网站目前已经在中国大陆无法访问,因此阅读镜像的文档就需要你懂的方式。笔者这里的教程适用于 Rocky Linux 8.x 版本。

# 导入docker官方的存储库,安装 docker 的最新版
Shell > dnf config-manager --enable  --add-repo  https://download.docker.com/linux/centos/docker-ce.repo

# 安装 docker 需要的一些软件包
Shell > dnf  -y install docker-ce docker-ce-cli containerd.io

# 启动 docker,并测试基本的功能
Shell > systemctl start docker.service
Shell > docker version
Client: Docker Engine - Community
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        afdd53b
 Built:             Thu Oct 26 09:09:18 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.10
  Git commit:       311b9ff
  Built:            Thu Oct 26 09:08:20 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.26
  GitCommit:        3dd1e886e55dd695541fdcd67420c2888645a495
 runc:
  Version:          1.1.10
  GitCommit:        v1.1.10-0-g18a0cb0
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Shell > docker run hello-world:latest
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:ac69084025c660510933cca701f615283cdbb3aa0963188770b54c31c8962493
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

# 查阅容器ID和镜像ID
Shell > docker ps -a && docker images -a
CONTAINER ID   IMAGE                COMMAND    CREATED         STATUS                     PORTS     NAMES
9ed9664e7c8a   hello-world:latest   "/hello"   4 minutes ago   Exited (0) 4 minutes ago             zealous_burnell
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d2c94e258dcb   7 months ago   13.3kB

# 删除生成的容器和相关镜像
Shell > docker rm 9ed9664e7c8a && docker rmi d2c94e258dcb
# 拉取 redis 稳定版本的镜像
Shell > docker pull redis:latest
Shell > docker images -a
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
redis        latest    e40e2763392d   2 weeks ago   138MB

# 查阅 redis 的镜像版本
Shell > docker inspect redis:latest  | grep -i redis_version
                "REDIS_VERSION=7.2.3",

# 利用镜像生成容器
## 准备好要使用的目录
Shell > mkdir -p /container-redis/config/  && mkdir -p /container-redis/data/
## 获取 redis.conf 配置文件
Shell > cd /container-redis/config/ && wget http://download.redis.io/redis-stable/redis.conf
## 创建容器实例, \ 表示命令还未键入结束
Shell > docker run -d --name redis-stable --restart always  -p 6379:6379  \
-v /container-redis/config/:/usr/local/etc/redis/:rw  -v /container-redis/data/:/data/:rw \
--privileged=true  redis:latest \
--appendonly yes  --requirepass "MyPassword"
3878129531ad514cc6d0a511837ef4eb0489609002744112daafa396e670774c

# 查询启动的相关信息
Shell > docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS                                       NAMES
3878129531ad   redis:latest   "docker-entrypoint.s…"   20 seconds ago   Up 19 seconds   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   redis-stable
Shell > docker inspect redis-stable
Shell > docker logs redis-stable

创建容器实例使用的选项的说明如下:

  • -d - 以后台守护进程的方式启动 reids
  • -p 6379:6379 - 端口映射,左边是宿主机端口,右边是容器实例的端口
  • -v /container-redis/config/:/usr/local/etc/redis/:rw - 容器数据卷,左边是宿主机的目录,右边是容器实例的目录,权限为读写(rw)
  • --privileged=true - 使用真正的 root 用户进行启动
  • --appendonly yes - redis 里的功能,开启 redis 的持久化,默认启用的是 RDB 持久化方案
  • --requirepass "MyPassword" - 指定密码

至此,docker 容器版的 redis 安装完成。请注意!你需要在宿主机的 /container-redis/config/redis.conf 文件中修改相关参数才能正常使用 redis。

从存储库中安装

使用的是应用程序流,默认情况下只开启了 5 版本的流。

Shell > dnf module list | grep -i redis
redis                5 [d]           common [d]                               Redis persistent key-value database
redis                6               common [d]                               Redis persistent key-value database

如果您需要开启 6 版本的 流,则可以键入以下命令:

Shell > dnf -y module enable redis:6
Shell > dnf module info redis:6
Name             : redis
Stream           : 6 [e] [a]
Version          : 8070020221108110224
Context          : 3b9f49c4
Architecture     : x86_64
Profiles         : common [d]
Default profiles : common
Repo             : appstream
Summary          : Redis persistent key-value database
Description      : redis 6 module
Requires         : platform:[el8]
Artifacts        : redis-0:6.2.7-1.module+el8.7.0+1105+8815ce78.src
                 : redis-0:6.2.7-1.module+el8.7.0+1105+8815ce78.x86_64
                 : redis-debuginfo-0:6.2.7-1.module+el8.7.0+1105+8815ce78.x86_64
                 : redis-debugsource-0:6.2.7-1.module+el8.7.0+1105+8815ce78.x86_64
                 : redis-devel-0:6.2.7-1.module+el8.7.0+1105+8815ce78.x86_64
                 : redis-doc-0:6.2.7-1.module+el8.7.0+1105+8815ce78.noarch
提示 : [d]默认, [e]启用, [x]禁用, [i]已安装的, [a]活跃的
# 安装 redis 6.2.7,配置集为 common
Shell > dnf -y module install redis:6/common

源代码编译安装

# 下载源代码
Shell > wget https://github.com/redis/redis/archive/7.2.3.tar.gz
# 解压压缩包
Shell > tar -zvxf redis-7.2.3.tar.gz -C /usr/local/src/

有关源代码构建和安装的详细说明,请查阅解压后的 README.md 自述文件。

# 开启源代码编译且安装
## jemalloc 是一个用来高效管理内存的内存分配器。 jemalloc-devel 软件包在 epel 存储库中才有
## 您的操作系统中可能还需要 gcc-c++、make、automake 等依赖项的包
Shell > dnf config-manager --set-enabled powertools && dnf -y install epel-release
Shell > dnf -y install jemalloc-devel make gcc-c++ python3
Shell > mkdir -p /usr/local/redis/
Shell > cd  /usr/local/src/redis-7.2.3/ &&  make  &&  make  PREFIX=/usr/local/redis/  install

一些构建说明:

  • 如果在 make 阶段出现类似这样的错误 —— "../deps/jemalloc/lib/libjemalloc.a:No such file or directory",则表示您缺少相关的包。
  • 如果 make 阶段失败,可在 /usr/local/src/redis-7.2.3/ 目录中使用 make disclean 命令清除历史的构建记录。

make install 阶段成功后,在 /usr/local/redis/bin/ 会有一些相关的二进制文件,包括主要的:

  • redis-server:指 redis 服务器本身
  • redis-cli:命令行界面管理 redis 的工具
Shell > ls -l /usr/local/redis/bin/
总用量 38392
-rwxr-xr-x 1 root root  9455480 10月  4 10:51 redis-benchmark
lrwxrwxrwx 1 root root       12 10月  4 10:51 redis-check-aof -> redis-server
lrwxrwxrwx 1 root root       12 10月  4 10:51 redis-check-rdb -> redis-server
-rwxr-xr-x 1 root root 10185736 10月  4 10:51 redis-cli
lrwxrwxrwx 1 root root       12 10月  4 10:51 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 19668128 10月  4 10:51 redis-server
用一杯咖啡支持我们,每一篇 [文档] 都经过我们实操,并非从网上一味的copy,期间花费了大量的心思,希望能够帮忙到您。
暂无评论

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇