0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OS起動時、Kong GatewayのData Planeコンテナを自動起動する

Last updated at Posted at 2025-06-05

Kong Gatewayの起動についてはsystemdを使ったやり方がControl Kong Gateway through systemdで記載されているが、コンテナ版がなかったので作ってみる。

ここではsystemd経由でData Planeの起動・停止を行うようにする。
単に再起動させたいだけなら、dockerの引数に--restart=alwaysを足せば問題なさそうだが、細かな設定をしたい場合とかはsystemdを使うといいかとおもう
なお、「Data Plane」としたのはKonnect向けを意識しているからであり、Enterprise版でControl Planeも同様に自動起動したい人はアレンジすれば自動起動できると思う。

前提

以下の前提で進める。

  • Kong GatewayのData Planeをコンテナで起動している
  • Data Planeの起動時の引数(docker run ...)が手元にある
  • systemdが利用可能

手順

自作系は/etc/systemd/systemに置くのが一般的だと思うので、こちらにUnitファイルとしてkong-gateway-dp.serviceを作成する。
書き込み権限が必要なので、最初にrootになっておく。

sudo su -

次にUnitファイルの作成だが、Unitファイルにdocker run ...部分をベタ書きしようとすると、証明書のところが上手く解釈されないので、コマンドをスクリプト化して1クッション挟む形でコンテナを起動するようにする。
なお、証明書の部分をファイル化するのでもいいのだが、ここでは起動コマンド自体をスクリプト化することで対応した。
以下のような感じでスクリプトを/usr/local/bin以下に作成する。なお、コンテナに名前をつけたかったので--name kong-gateway-dpを足している。

cat <<EOF > /usr/local/bin/run-kong-gateway-dp.sh
#!/bin/bash
docker run -d \
--name kong-gateway-dp \
-e "KONG_ROLE=data_plane" \
-e "KONG_DATABASE=off" \
-e "KONG_VITALS=off" \
:(省略)
-p 8000:8000 \
-p 8443:8443 \
kong/kong-gateway:3.10
EOF

実行権限を付与する。

chmod +x /usr/local/bin/run-kong-gateway-dp.sh

Unitファイルを作成する。コンテナに名前をつけた関係上、削除処理を入れている。また起動していない場合を考慮して=-としてエラーが出ても問題ないようにしている。

/etc/systemd/system/kong-gateway-dp.service
cat <<'EOF' > /etc/systemd/system/kong-gateway-dp.service
[Unit]
Description=Kong Gateway
After=docker.service
Requires=docker.service
[Service]
ExecStartPre=-/usr/bin/docker stop kong-gateway-dp
ExecStartPre=-/usr/bin/docker rm kong-gateway-dp
ExecStart=/usr/local/bin/run-kong-gateway-dp.sh
[Install]
WantedBy=multi-user.target
EOF

作成したsystemdのサービスを有効化し起動する。

chmod 644 /etc/systemd/system/kong-gateway-dp.service
systemctl daemon-reload
systemctl enable kong-gateway-dp
systemctl start kong-gateway-dp

動作確認する。

curl localhost:8000

問題なければ以下が返ってくる。

{
  "message":"no Route matched with those values",
  "request_id":"858b840264c4e906cb70e00dd781ab58"
}

再起動しても動作することを確認する。
再起動後、docker psで確認する。

$ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS                             PORTS                                                                                                                NAMES
3ae96f7a3b37   kong/kong-gateway:3.10   "/entrypoint.sh kong…"   42 seconds ago   Up 41 seconds (health: starting)   0.0.0.0:8000->8000/tcp, :::8000->8000/tcp, 8001-8004/tcp, 0.0.0.0:8443->8443/tcp, :::8443->8443/tcp, 8444-8447/tcp   kong-gateway-dp

念の為、curlでも確認しておく。

$ curl localhost:8000
{
  "message":"no Route matched with those values",
}

問題なさそうだ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?