给 Cloud Logging 配置日志记录

本文档介绍如何使用目标工作负载集群上的 ConfigMap 自定义日志记录。

可用日志

默认情况下,Migrate to Containers 会写入来自以下来源的条目:

  • 写入所有 Linux 进程的父级 initstdout 的条目。
  • /var/log/syslog 中的内容。

在 Cloud Logging 中,支持的日志文件中的条目将显示以下形式的日志的一行中。在列出日志文件的 ConfigMap 中定义标签。

date time label log-output

以下示例演示了包含 MySQL 条目的表单:

2019-09-22 12:43:08.681193976 +0000 UTC mysql log-output

您可以选择扩展日志转发功能,以传播在迁移的容器中运行的其他进程和应用的日志。

将应用日志转发到 Cloud Logging

如需从通过 Migate for Anthos 迁移的虚拟机上的应用登录 Cloud Logging,您必须做好以下准备:

  • 在目标工作负载集群上创建一个ConfigMap,用于指定应将其条目写入 Cloud Logging 的文件系统日志文件列表。

    您可以使用下文中包含的示例 ConfigMap,作为涵盖许多常见日志文件的参考。

  • ConfigMap 必须与容器的 DeploymentStatefulSet 位于同一命名空间中。

  • DeploymentStatefulSet 必须引用 ConfigMap

  • 如果您要迁移到 Google Distributed Cloud,请在 Google Distributed Cloud 集群上安装 Connect。如需将数据回传到 Cloud Logging,您必须使用 Connect。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      creationTimestamp: null
      labels:
        app: my-app
        migrate-for-anthos-type: workload
      name: test-ibsc-appenhmutimiy
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: test-ibsc-appenhmutimiy
          migrate-for-anthos-type: workload
      strategy: {}
      template:
        metadata:
          creationTimestamp: null
          labels:
            app: test-ibsc-appenhmutimiy
            migrate-for-anthos-type: workload
        spec:
          containers:
          - image: gcr.io/myproject/my-container-image:v1.0.0
            name: test-ibsc-appenhmutimiy
            readinessProbe:
              exec:
                command:
                - /code/ready.sh
            resources: {}
            securityContext:
              privileged: true
            volumeMounts:
            - mountPath: /sys/fs/cgroup
              name: cgroups
            - mountPath: /code/config/logs/
              name: logs-config
          volumes:
          - hostPath:
              path: /sys/fs/cgroup
              type: Directory
            name: cgroups
          - configMap:
              name: <ConfigMap name>
            name: logs-config

使用 ConfigMap 作为常见应用日志

下文中的参考 ConfigMap YAML 涵盖了一系列常见应用日志文件及其应用标签。您可以按原样复制 ConfigMap YAML,或者修改它以移除条目或添加自定义应用日志。

要编辑日志文件列表,请修改 logs: 行下的元素。日志子元素由以下结构组成:

label:
  - path-with-simple-wildcards

例如:

tomcat:
  - /var/log/tomcat*/catalina.out
  - /var/log/tomcat*/localhost.*.log

参考 ConfigMap 了解常见应用

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: null
  name: <ConfigMap name>
data:
  logs.yaml: |
    logs:
      mysql:
      - /var/log/mysql.log
      - /var/log/mysql/mysql.log
      mongodb:
      - /var/log/mongodb/*.log
      memcached:
      - /var/log/memcached.log
      redis:
      - /var/log/redis*.log
      - /var/log/redis/*.log
      rabbitmq-startup:
      - /var/log/rabbitmq/startup_log
      rabbitmq-startup_err:
      - /var/log/rabbitmq/startup_err
      rabbitmq-sasl:
      - /var/log/rabbitmq/*-sasl.log
      sugarcrm:
      - /var/www/*/sugarcrm.log
      tomcat-localhost_access_log:
      - /var/log/tomcat*/localhost_access_log*.txt
      tomcat:
      - /var/log/tomcat*/catalina.out
      - /var/log/tomcat*/localhost.*.log
      apache-access:
      - /var/log/apache*/access.log
      - /var/log/apache*/access_log
      - /var/log/httpd/access.log
      - /var/log/httpd/access_log
      apache-error:
      - /var/log/apache*/error.log
      - /var/log/apache*/error_log
      - /var/log/httpd/error.log
      - /var/log/httpd/error_log
      cassandra:
      - /var/log/cassandra/system.log
      - /var/log/cassandra/cassandra.log
      - /var/log/cassandra/output.log
      chef-bookshelf:
      - /var/log/chef-server/bookshelf/current
      chef-expander:
      - /var/log/chef-server/chef-expander/current
      chef-pedant-http-traffic:
      - /var/log/chef-server/chef-pedant/http-traffic.log
      chef-server-webui:
      - /var/log/chef-server/chef-server-webui/current
      chef-solr:
      - /var/log/chef-server/chef-solr/current
      chef-erchef-current:
      - /var/log/chef-server/erchef/current
      chef-erchef:
      - /var/log/chef-server/erchef/erchef.log.1
      chef-nginx-access:
      - /var/log/chef-server/nginx/access.log
      chef-nginx-error:
      - /var/log/chef-server/nginx/error.log
      chef-nginx-rewrite-port-80:
      - /var/log/chef-server/nginx/rewrite-port-80.log
      chef-postgresql:
      - /var/log/chef-server/postgresql/current
      chef-rabbitmq:
      - /var/log/chef-server/rabbitmq/current
      postgresql:
      - /var/log/postgres*/*.log
      - /var/log/pgsql/*.log
      puppet-access:
      - /var/log/pe-httpd/access.log
      puppet-puppetmasteraccess:
      - /var/log/pe-httpd/puppetmasteraccess.log
      puppet-activemq:
      - /var/log/pe-activemq/activemq.log
      puppet-activemq-wrapper:
      - /var/log/pe-activemq/wrapper.log
      puppet-mcollective:
      - /var/log/pe-mcollective/mcollective.log
      puppet-mcollective-audit:
      - /var/log/pe-mcollective/mcollective_audit.log
      puppet-puppetdb:
      - /var/log/pe-puppetdb/pe-puppetdb.log
      puppet-dashboard-error:
      - /var/log/pe-httpd/puppetdashboard.error.log
      puppet-dashboard-mcollective-client:
      - /var/log/pe-puppet-dashboard/mcollective_client.log
      puppet-dashboard-production:
      - /var/log/pe-puppet-dashboard/production.log
      puppet-dashboard-event-inspector:
      - /var/log/pe-puppet-dashboard/event-inspector.log
      puppet-dashboard-certificate-manager:
      - /var/log/pe-puppet-dashboard/certificate_manager.log
      puppet-dashboard-live-management:
      - /var/log/pe-puppet-dashboard/live-management.log
      puppet-console-cas-client:
      - /var/log/pe-console-auth/cas_client.log
      puppet-console-auth-cas:
      - /var/log/pe-console-auth/cas.log
      puppet-console-auth:
      - /var/log/pe-console-auth/auth.log
      puppet-dashboard-access:
      - /var/log/pe-httpd/puppetdashboard.access.log
      puppet-dashboard-failed-reports:
      - /var/log/pe-puppet-dashboard/failed_reports.log
      puppet-error:
      - /var/log/pe-httpd/error.log
      puppet-other-vhosts-access:
      - /var/log/pe-httpd/other_vhosts_access.log
      puppet-masterhttp:
      - /var/log/pe-puppet/masterhttp.log
      - /var/log/puppet/masterhttp.log
      puppet-rails:
      - /var/log/pe-puppet/rails.log
      puppet-http:
      - /var/log/puppet/http.log
      gitlab-application:
      - /home/git/gitlab/log/application.log
      gitlab-production:
      - /home/git/gitlab/log/production.log
      gitlab-sidekiq:
      - /home/git/gitlab/log/sidekiq.log
      gitlab-unicorn-stdout:
      - /home/git/gitlab/log/unicorn.stdout.log
      gitlab-unicorn-stderr:
      - /home/git/gitlab/log/unicorn.stderr.log
      gitlab-githost:
      - /home/git/gitlab/log/githost.log
      gitlab-satellites:
      - /home/git/gitlab/log/satellites.log
      gitlab-shell:
      - /home/git/gitlab-shell/gitlab-shell.log
      jenkins:
      - /var/log/jenkins/jenkins.log
      jetty-request:
      - /var/log/jetty/*.request.log
      jetty-stderrout:
      - /var/log/jetty/*.stderrout.log
      jetty-out:
      - /var/log/jetty/out.log
      joomla:
      - /var/www/joomla/logs/*.log
      magento-system:
      - /var/www/magento/var/log/system.log
      magento-exception:
      - /var/www/magento/var/log/exception.log
      magento-report:
      - /var/www/magento/var/report/*
      mediawiki:
      - /var/log/mediawiki/*.log
      nginx-access:
      - /var/log/nginx/access.log
      nginx-error:
      - /var/log/nginx/error.log
      redmine:
      - /var/log/redmine/*.log
      salt-master:
      - /var/log/salt/master
      salt-minion:
      - /var/log/salt/minion
      salt-key:
      - /var/log/salt/key
      salt-syndic:
      - /var/log/salt/syndic.loc
      solr:
      - /var/log/solr/*.log
      sugarcrm:
      - /var/www/*/sugarcrm.log
      tomcat-localhost_access_log:
      - /var/log/tomcat*/localhost_access_log.*.txt
      tomcat:
      - /var/log/tomcat*/catalina.out,/var/log/tomcat*/localhost.*.log

后续步骤