]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: enable log to journald by default
author胡玮文 <huww98@outlook.com>
Wed, 7 Apr 2021 09:53:49 +0000 (17:53 +0800)
committer胡玮文 <huww98@outlook.com>
Wed, 12 May 2021 02:03:00 +0000 (10:03 +0800)
Currently, when deployed with cephadm and podman, logs from ceph daemons are
routed as stderr -> conmon -> journald. We can send logs to journald directly
by connecting to its socket. And this has the following advantages:

- record structured metadata along with log message
  - prettier output from journalctl
    - no duplicated timestamp
    - colorized according to priority
    - multi-line logs are indented properly
  - easier to filter the logs afterward
- theoretically better performance
- workaround https://tracker.ceph.com/issues/49551

This depends on commit 9ef8055. And depends on commit 1a76f47 because cgroup is
needed to associate these logs to the systemd unit.

Signed-off-by: 胡玮文 <huww98@outlook.com>
src/cephadm/cephadm
src/cephadm/tests/test_cephadm.py

index 92d2399f88259dc54fc198e0df244ac5da360c16..7e6c3d57b6d38a712860b3d3eb3949382dc9ade7 100755 (executable)
@@ -116,6 +116,7 @@ class BaseConfig:
         self.env: List[str] = []
         self.memory_request: Optional[int] = None
         self.memory_limit: Optional[int] = None
+        self.log_to_journald: Optional[bool] = None
 
         self.container_init: bool = CONTAINER_INIT
         self.container_engine: Optional[ContainerEngine] = None
@@ -2101,6 +2102,13 @@ def get_legacy_daemon_fsid(ctx, cluster,
     return fsid
 
 
+def should_log_to_journald(ctx):
+    if ctx.log_to_journald is not None:
+        return ctx.log_to_journald
+    return isinstance(ctx.container_engine, Podman) and \
+        ctx.container_engine.version >= CGROUPS_SPLIT_PODMAN_VERSION
+
+
 def get_daemon_args(ctx, fsid, daemon_type, daemon_id):
     # type: (CephadmContext, str, str, Union[int, str]) -> List[str]
     r = list()  # type: List[str]
@@ -2110,14 +2118,29 @@ def get_daemon_args(ctx, fsid, daemon_type, daemon_id):
             '--setuser', 'ceph',
             '--setgroup', 'ceph',
             '--default-log-to-file=false',
-            '--default-log-to-stderr=true',
-            '--default-log-stderr-prefix=debug ',
         ]
+        log_to_journald = should_log_to_journald(ctx)
+        if log_to_journald:
+            r += [
+                '--default-log-to-journald=true',
+                '--default-log-to-stderr=false',
+            ]
+        else:
+            r += [
+                '--default-log-to-stderr=true',
+                '--default-log-stderr-prefix=debug ',
+            ]
         if daemon_type == 'mon':
             r += [
                 '--default-mon-cluster-log-to-file=false',
-                '--default-mon-cluster-log-to-stderr=true',
             ]
+            if log_to_journald:
+                r += [
+                    '--default-mon-cluster-log-to-journald=true',
+                    '--default-mon-cluster-log-to-stderr=false',
+                ]
+            else:
+                r += ['--default-mon-cluster-log-to-stderr=true']
     elif daemon_type in Monitoring.components:
         metadata = Monitoring.components[daemon_type]
         r += metadata.get('args', list())
@@ -2312,6 +2335,9 @@ def get_container_mounts(ctx, fsid, daemon_type, daemon_id,
             crash_dir = '/var/lib/ceph/%s/crash' % fsid
             if os.path.exists(crash_dir):
                 mounts[crash_dir] = '/var/lib/ceph/crash:z'
+            if daemon_type != 'crash' and should_log_to_journald(ctx):
+                journald_sock_dir = '/run/systemd/journal'
+                mounts[journald_sock_dir] = journald_sock_dir
 
     if daemon_type in Ceph.daemons and daemon_id:
         data_dir = get_data_dir(fsid, ctx.data_dir, daemon_type, daemon_id)
index 15b6efcc41d6b0b99a158d2eb2de89353e481662..f5152d69e79649ea12946da1502bcb5f4055e19d 100644 (file)
@@ -524,6 +524,26 @@ docker.io/ceph/daemon-base:octopus
         image = cd._filter_last_local_ceph_image(out)
         assert image == 'docker.io/ceph/ceph:v15.2.5'
 
+    def test_should_log_to_journald(self):
+        ctx = mock.Mock()
+        # explicit
+        ctx.log_to_journald = True
+        assert cd.should_log_to_journald(ctx)
+
+        ctx.log_to_journald = None
+        # enable if podman support --cgroup=split
+        ctx.container_engine = self.mock_podman()
+        ctx.container_engine.version = (2, 1, 0)
+        assert cd.should_log_to_journald(ctx)
+
+        # disable on old podman
+        ctx.container_engine.version = (2, 0, 0)
+        assert not cd.should_log_to_journald(ctx)
+
+        # disable on docker
+        ctx.container_engine = self.mock_docker()
+        assert not cd.should_log_to_journald(ctx)
+
 
 class TestCustomContainer(unittest.TestCase):
     cc: cd.CustomContainer