k8s 使用官方 compose 部署 plausible

主要记录使用 kompose 转换部署 compose 程序的思路,具体过程根据情况调整。

首先获取官方 compose 配置:

git clone -b v2.1.4 --single-branch https://github.com/plausible/community-edition plausible-ce
cd plausible-ce

调整配置

之后根据需要定制配置文件:

$ touch .env
$ echo "BASE_URL=https://plausible.example.com" >> .env
$ echo "SECRET_KEY_BASE=$(openssl rand -base64 48)" >> .env

# 服务暴露根据需要配置
# 这里采用反代的方法
$ echo "HTTP_PORT=8000" >> .env

之后定制一下 compose.yaml,其中有一些不合理的地方,直接使用 kompose 转换会出问题:

diff --git a/compose.yml b/compose.yml
index 2fe91e5..dd42713 100644
--- a/compose.yml
+++ b/compose.yml
@@ -1,16 +1,18 @@
 services:
-  plausible_db:
+  plausible-db:
     image: postgres:16-alpine
     restart: always
     volumes:
       - db-data:/var/lib/postgresql/data
     environment:
       - POSTGRES_PASSWORD=postgres
+    ports:
+      - 5432:5432
     healthcheck:
       test: ["CMD-SHELL", "pg_isready -U postgres"]
       start_period: 1m
 
-  plausible_events_db:
+  plausible-events-db:
     image: clickhouse/clickhouse-server:24.3.3.102-alpine
     restart: always
     volumes:
@@ -24,6 +26,8 @@ services:
       nofile:
         soft: 262144
         hard: 262144
+    ports:
+      - 5432:5432
     healthcheck:
       test: ["CMD-SHELL", "wget --no-verbose --tries=1 -O - http://127.0.0.1:8123/ping || exit 1"]
       start_period: 1m
@@ -33,9 +37,9 @@ services:
     restart: always
     command: sh -c "/entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh run"
     depends_on:
-      plausible_db:
+      plausible-db:
         condition: service_healthy
-      plausible_events_db:
+      plausible-events-db:
         condition: service_healthy
     volumes:
       - plausible-data:/var/lib/plausible
@@ -43,6 +47,9 @@ services:
       nofile:
         soft: 65535
         hard: 65535
+    env_file: ".env"
+    ports:
+      - 8000:8000
     environment:
       - TMPDIR=/var/lib/plausible/tmp
       # required: https://github.com/plausible/community-edition/wiki/configuration#required
@@ -54,28 +61,28 @@ services:
       - DISABLE_REGISTRATION
       - ENABLE_EMAIL_VERIFICATION
       # web: https://github.com/plausible/community-edition/wiki/configuration#web
-      - HTTP_PORT
+      - HTTP_PORT=8000
       - HTTPS_PORT
       # databases: https://github.com/plausible/community-edition/wiki/configuration#database
-      - DATABASE_URL
-      - CLICKHOUSE_DATABASE_URL
+      - DATABASE_URL=postgres://postgres:postgres@plausible_db:5432/plausible_db
+      - CLICKHOUSE_DATABASE_URL=http://plausible_events_db:8123/plausible_events_db

之后需要对 clickhouse 做一些配置,暂时解决 Application: Disabling cgroup memory observer because of an error during initialization: Code: 107. DB::Exception: Cannot find cgroups v1 or v2 current memory file 问题:

diff --git a/clickhouse/ipv4-only.xml b/clickhouse/ipv4-only.xml
index 7eb0a5c..4c8222d 100644
--- a/clickhouse/ipv4-only.xml
+++ b/clickhouse/ipv4-only.xml
@@ -1,3 +1,4 @@
 <clickhouse>
     <listen_host>0.0.0.0</listen_host>
+    <cgroups_memory_usage_observer_wait_time>0</cgroups_memory_usage_observer_wait_time>
 </clickhouse>

转换资源清单

一切就绪后转换:

kompose convert -f compose.yaml  --controller statefulset -o plausible.yaml

检查一下文件,不喜欢的可以再调整,比如条整 pvc 大小。

最后使用 ingress 暴露:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-plausible
  namespace: plausible
  annotations:
    cert-manager.io/cluster-issuer: issuer01
spec:
  ingressClassName: nginx
  rules:
  - host: example.com
    http:
      paths:
      - backend:
          service:
            name: plausible
            port:
              number: 8000
        path: /
        pathType: Prefix
  tls:
  - hosts:
      - example.com
    secretName: plausible-example-com-tls

检查 pod 状态,检查服务状态,最后访问,结束。

问题解决

pv 权限问题

如果出现这个故障:

Attaching to plausible
plausible  | Loading plausible..
plausible  | Starting dependencies..
plausible  | Starting repos..
plausible  | Plausible.Repo database already exists
plausible  | Plausible.IngestRepo database already exists
plausible  | Creation of Db successful!
plausible  | Loading plausible..
plausible  | Starting dependencies..
plausible  | Starting repos..
plausible  | Runtime terminating during boot (terminating)
plausible  | 09:55:19.599 [notice] Application tzdata exited: exited in: Tzdata.App.start(:normal, [])
plausible  |     ** (EXIT) an exception was raised:
plausible  |         ** (MatchError) no match of right hand side value: {:error, {:shutdown, {:failed_to_start_child, Tzdata.EtsHolder, {%File.Error{reason: :enoent, path: "/var/lib/plausible/tzdata_data/release_ets", action: "list directory"}, [{File, :ls!, 1, [file: ~c"lib/file.ex", line: 1700]}, {Tzdata.EtsHolder, :release_files_for_dir, 1, [file: ~c"lib/tzdata/ets_holder.ex", line: 127]}, {Tzdata.EtsHolder, :make_sure_a_release_is_on_file, 0, [file: ~c"lib/tzdata/ets_holder.ex", line: 90]}, {Tzdata.EtsHolder, :init, 1, [file: ~c"lib/tzdata/ets_holder.ex", line: 16]}, {:gen_server, :init_it, 2, [file: ~c"gen_server.erl", line: 2057]}, {:gen_server, :init_it, 6, [file: ~c"gen_server.erl", line: 2012]}, {:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 329]}]}}}}
plausible  |             (tzdata 1.1.2) lib/tzdata/tzdata_app.ex:13: Tzdata.App.start/2
plausible  |             (kernel 10.0) application_master.erl:295: :application_master.start_it_old/4
plausible  |
plausible exited with code 1

找到 pv 位置执行:

chown -R 999:nogroup /mnt/hdd-mirror/docker/plausible/data/

之后重启 plausible pod 即可。

References

Kubernetes

最后修改:2024 年 11 月 11 日
如果觉得我的文章对你有用,请随意赞赏