以下の内容はhttps://kazuhira-r.hatenablog.com/entry/2024/10/19/200719より取得しました。


Redis互換のキーバリューストア、ValkeyをUbuntu Linux 22.0.4 LTSにインストールする

これは、なにをしたくて書いたもの?

RedisのフォークであるValkeyが8.0になったので、少し試してみようかなということで。

Redisをフォークした「Valkey」初のメジャーバージョンアップ「Valkey 8.0」正式リリース。非同期I/Oスレッディングによる高速化など強化 - Publickey

Valkey

Valkeyは、ライセンスが変更されたRedisのフォークプロジェクトです。

Redis、クラウドベンダなどによる商用サービスを制限するライセンス変更を発表。今後はRedis社とのライセンス契約が必須に - Publickey

つまり、Redis互換のインメモリーデータストア、キーバリューストアということになります。

ValkeyのWebサイトはこちら。

Valkey

GitHubリポジトリーはこちら。

GitHub - valkey-io/valkey: A flexible distributed key-value datastore that supports both caching and beyond caching workloads.

8.0.0のリリースがアナウンスされていますが、現時点での最新バージョンは8.0.1です。

Valkey · Generally Available: Valkey 8.0.0

ちなみにRedisはというと、7.4.xが最新です。7.4.xからライセンスが変わっていますが。

GitHub - redis/redis-hashes: Redis tarball SHA1 hashes

Valkeyはライセンスが変わる前のRedis 7.2.4からのフォークです。

Redisとは変わらず、Windowsは対象プラットフォームではないようですね。

Valkey can be compiled and used on Linux, OSX, OpenBSD, NetBSD, FreeBSD.

Valkeyのインストール方法なのですが、ダウンロードページを見るとDockerイメージとUbuntu Linux 18.04 LTS、20.04 LTS向けの
バイナリーが案内されています。

Valkey · Download

他のバージョンやディストリビューションの場合は…?となるのですが。

Ubuntu Linuxに関して言うと、24.04 LTS以降ではUbuntu Linuxのパッケージに含まれるようになっています。

Valkey is now included in Ubuntu

valkey package : Ubuntu

Ubuntu Linuxも含め、他のディストリビューション向けにはPerconaが提供しているようです。

New Valkey Packages by Percona

Install from Percona repository - Percona Packages for Valkey

今回はUbuntu Linux 22.04 LTSを使おうと思うので、ソースコードからビルドしてインストールすることにします。

Valkey / Building Valkey

また、ValkeyをターゲットにしたクライアントとしてはValkey GLIDEというものがあるようです。

Valkey GLIDE – Valkey と Redis OSS向けのオープンソースクライアントライブラリのご紹介 | Amazon Web Services ブログ

GitHub - valkey-io/valkey-glide: An open source Valkey client library that supports Valkey and Redis open source 6.2, 7.0 and 7.2. Valkey GLIDE is designed for reliability, optimized performance, and high-availability, for Valkey and Redis OSS based applications. GLIDE is a multi language client library, written in Rust with programming language bindings, such as Java and Python

こちらは別の機会に見てみることにします。

環境

今回の環境はこちら。Ubuntu Linux 22.04 LTSです。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.5 LTS
Release:        22.04
Codename:       jammy


$ uname -srvmpio
Linux 5.15.0-124-generic #134-Ubuntu SMP Fri Sep 27 20:20:17 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Valkeyをソースコードからインストールする

それでは、Valkeyをソースコードからインストールします。手順はこちらですね。

Valkey / Building Valkey

ソースコードからインストールする場合のビルド方法はいくつかあって、通常の方法

$ make

TLSサポートをビルトインで有効にする方法、

$ make BUILD_TLS=yes

TLSをValkeyモジュールとしてビルドする方法、

$ make BUILD_TLS=module

systemdのサポートを有効にしてビルドする方法などがあります。

$ make USE_SYSTEMD=yes

モジュール?と思ったりしますが、ValkeyにはモジュールというValkeyのコードを変更せずに機能を追加する仕組みがあるようです。

Valkey · Valkey Modules 101

まずはシンプルにビルドしてインストールしてみましょう。

build-essential、あとpkg-configは入れておいた方がよさそうです。

$ sudo apt install build-essential
$ sudo apt install pkg-config

ソースコードをダウンロードして展開。

$ curl -LO https://github.com/valkey-io/valkey/archive/refs/tags/8.0.1.tar.gz
$ tar xf 8.0.1.tar.gz
$ cd valkey-8.0.1

ビルド。

$ make

Valkeyサーバーを動かしてみます。

$ src/valkey-server

Valkey / Running Valkey

起動しました。

$ src/valkey-server
12483:C 19 Oct 2024 19:16:16.822 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
12483:C 19 Oct 2024 19:16:16.823 * oO0OoO0OoO0Oo Valkey is starting oO0OoO0OoO0Oo
12483:C 19 Oct 2024 19:16:16.823 * Valkey version=8.0.1, bits=64, commit=00000000, modified=1, pid=12483, just started
12483:C 19 Oct 2024 19:16:16.823 # Warning: no config file specified, using the default config. In order to specify a config file use src/valkey-server /path/to/valkey.conf
12483:M 19 Oct 2024 19:16:16.827 * Increased maximum number of open files to 10032 (it was originally set to 1024).
12483:M 19 Oct 2024 19:16:16.827 * monotonic clock: POSIX clock_gettime
                .+^+.
            .+#########+.
        .+########+########+.           Valkey 8.0.1 (00000000/1) 64 bit
    .+########+'     '+########+.
 .########+'     .+.     '+########.    Running in standalone mode
 |####+'     .+#######+.     '+####|    Port: 6379
 |###|   .+###############+.   |###|    PID: 12483
 |###|   |#####*'' ''*#####|   |###|
 |###|   |####'  .-.  '####|   |###|
 |###|   |###(  (@@@)  )###|   |###|          https://valkey.io
 |###|   |####.  '-'  .####|   |###|
 |###|   |#####*.   .*#####|   |###|
 |###|   '+#####|   |#####+'   |###|
 |####+.     +##|   |#+'     .+####|
 '#######+   |##|        .+########'
    '+###|   |##|    .+########+'
        '|   |####+########+'
             +#########+'
                '+v+'

12483:M 19 Oct 2024 19:16:16.837 * Server initialized
12483:M 19 Oct 2024 19:16:16.837 * Ready to accept connections tcp

別のターミナルでValkeyサーバーに接続してみます。

$ src/valkey-cli
127.0.0.1:6379>

使い方はRedisと同じですね。

127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> get key1
"value1"

コマンドはredis-serverredis-cliではなくvalkey-servervalkey-cliになっています。

Valkeyサーバーは1度Ctrl-cで止めておきます。

インストールはこちら。

$ sudo make install

Valkey / Installing Valkey

これでデフォルトでは/usr/localにインストールされます。

$ which valkey-server
/usr/local/bin/valkey-server


$ which valkey-cli
/usr/local/bin/valkey-cli

互換性のために、redis-serverredis-cliなどもシンボリックリンクとして作成されるようです。

$ ll $(which redis-server)
lrwxrwxrwx 1 root root 13 10月 19 19:28 /usr/local/bin/redis-server -> valkey-server*


$ ll $(which redis-cli)
lrwxrwxrwx 1 root root 10 10月 19 19:28 /usr/local/bin/redis-cli -> valkey-cli*

インストール場所を変更する場合は、PREFIXを指定してビルドします。

$ make PREFIX=/path/to

ところで、Ubuntu LinuxDebian向けには追加でスクリプトを実行すると、daemonとしての設定などを行ってくれるようです。

make install will just install binaries in your system, but will not configure init scripts and configuration files in the appropriate place. This is not needed if you just want to play a bit with Valkey, but if you are installing it the proper way for a production system, we have a script that does this for Ubuntu and Debian systems:

実行してみます。

$ sudo utils/install_server.sh
Welcome to the valkey service installer
This script will help you easily set up a running valkey server

This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!

…怒られました。このスクリプトではsystemdはサポートしていないようです。

1度クリーンして

$ make distclean

systemdのサポートを有効にしてビルド。

$ make USE_SYSTEMD=yes

コンパイルエラーになりました…。

server.h:57:10: fatal error: systemd/sd-daemon.h: そのようなファイルやディレクトリはありません
   57 | #include <systemd/sd-daemon.h>
      |          ^~~~~~~~~~~~~~~~~~~~~

systemd用のパッケージが要るようです。

$ sudo apt install libsystemd-dev

もう1度ビルド。

$ make USE_SYSTEMD=yes

インストール。

$ sudo make install

あとはこういうスクリプトがサンプルとして作成されているので、こちらを使えばよさそうです。

$ find utils -name '*systemd*'
utils/systemd-valkey_server.service
utils/systemd-valkey_multiple_servers@.service

systemd-valkey_server.servicevalkey-server.serviceという名前でコピー。

$ sudo cp utils/systemd-valkey_server.service /etc/systemd/system/valkey-server.service

ちなみに中身はこんな感じですね。

/etc/systemd/system/valkey-server.service

# example systemd service unit file for valkey-server
#
# In order to use this as a template for providing a valkey service in your
# environment, _at the very least_ make sure to adapt the valkey configuration
# file you intend to use as needed (make sure to set "supervised systemd"), and
# to set sane TimeoutStartSec and TimeoutStopSec property values in the unit's
# "[Service]" section to fit your needs.
#
# Some properties, such as User= and Group=, are highly desirable for virtually
# all deployments of valkey, but cannot be provided in a manner that fits all
# expectable environments. Some of these properties have been commented out in
# this example service unit file, but you are highly encouraged to set them to
# fit your needs.
#
# Please refer to systemd.unit(5), systemd.service(5), and systemd.exec(5) for
# more information.

[Unit]
Description=Valkey data structure server
Documentation=https://github.com/valkey-io/valkey-doc
#Before=your_application.service another_example_application.service
#AssertPathExists=/var/lib/valkey
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/local/bin/valkey-server --supervised systemd --daemonize no
## Alternatively, have valkey-server load a configuration file:
#ExecStart=/usr/local/bin/valkey-server /path/to/your/valkey.conf
LimitNOFILE=10032
NoNewPrivileges=yes
#OOMScoreAdjust=-900
#PrivateTmp=yes
Type=notify
TimeoutStartSec=infinity
TimeoutStopSec=infinity
UMask=0077
#User=valkey
#Group=valkey
#WorkingDirectory=/var/lib/valkey

[Install]
WantedBy=multi-user.target

このファイル自体はUSE_SYSTEMD=yesをつけずともソースコードに含まれているものですが、ExecStartの内容を見るとsystemdで
使う時はUSE_SYSTEMD=yesをつけておいた方がよさそうですね。

Valkeyを有効にして起動。

$ sudo systemctl daemon-reload



$ sudo systemctl status valkey-server
○ valkey-server.service - Valkey data structure server
     Loaded: loaded (/etc/systemd/system/valkey-server.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: https://github.com/valkey-io/valkey-doc


$ sudo systemctl enable valkey-server
Created symlink /etc/systemd/system/multi-user.target.wants/valkey-server.service → /etc/systemd/system/valkey-server.service.


$ sudo systemctl start valkey-server


$ sudo systemctl status valkey-server
● valkey-server.service - Valkey data structure server
     Loaded: loaded (/etc/systemd/system/valkey-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2024-10-19 19:56:02 JST; 26s ago
       Docs: https://github.com/valkey-io/valkey-doc
   Main PID: 19434 (valkey-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 3432)
     Memory: 1.9M
        CPU: 151ms
     CGroup: /system.slice/valkey-server.service
             └─19434 "/usr/local/bin/valkey-server *:6379" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

10月 19 19:56:02 my-server valkey-server[19434]: 19434:C 19 Oct 2024 19:56:02.325 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may>
10月 19 19:56:02 my-server valkey-server[19434]: 19434:C 19 Oct 2024 19:56:02.329 * Supervised by systemd. Please make sure you set appropriate values for TimeoutStartSec and >
10月 19 19:56:02 my-server valkey-server[19434]: 19434:C 19 Oct 2024 19:56:02.330 * oO0OoO0OoO0Oo Valkey is starting oO0OoO0OoO0Oo
10月 19 19:56:02 my-server valkey-server[19434]: 19434:C 19 Oct 2024 19:56:02.330 * Valkey version=8.0.1, bits=64, commit=00000000, modified=1, pid=19434, just started

確認。

$ valkey-cli
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> get key1
"value1"

OKですね。

おわりに

Redis互換のキーバリューストア、Valkeyを試してみました。もとがRedisなので、使い方はほぼ変わらないですね。

自分はこういうものは大元の方を使い続けることが多いのですが、Redis/Valkeyに関してはValkeyも扱っておいた方がよさそうかなと思って
試してみることにしました。

今後、このブログで扱うのがRedis寄りになるのかValkey寄りになるのかは微妙なところですが、まずはValkeyの周辺事情も押さえておこうと
思います。




以上の内容はhttps://kazuhira-r.hatenablog.com/entry/2024/10/19/200719より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14