7 記錄

您可以使用 Oracle Cloud Infrastructure (OCI) 或外部工具來設定 Oracle Blockchain Platform Enterprise Edition 的持續性記錄日誌。

概觀

Oracle Blockchain Platform Enterprise Edition 以 Kubernetes 為基礎,每個 Pod 上的日誌都儲存在本機。若要避免在刪除 Pod 時刪除日誌,您必須設定永久記錄日誌,以便將日誌儲存在中央位置。永久記錄日誌有兩種方法。您可以使用外部記錄工具,例如 Fluentd 和 Elastic Stack。或者,如果您在 Oracle Kubernetes Engine 上執行,則可以使用 Oracle Cloud Infrastructure (OCI) 支援的集中式日誌記錄解決方案。

使用 OCI 進行永久日誌記錄

若要使用 OCI 集中儲存日誌,您可以定義日誌群組並設定代理程式來剖析日誌。日誌會儲存在物件儲存服務中。在設定與 OCI 的永久記錄日誌之前,您的部署必須符合下列需求。
  • Kubernetes 區間中的動態群組。若要建立動態群組,請按一下導覽功能表中的識別與安全性。在識別底下,按一下網域,然後按一下建立動態群組。在比對規則區段中新增下列項目至您的動態群組,以區間的 Oracle Cloud ID 取代。
    instance.compartment.id = '<compartment_ocid>'
    舉例而言:
    instance.compartment.id = 'ocid1.compartment.oc1..aaaaaaaa4ws3242343243244nyb423432rwqsxigt2sia'
  • 允許動態群組與日誌記錄服務互動的原則。若要建立原則,請按一下導覽功能表中的識別與安全。在識別底下,按一下原則,然後按一下建立原則
    Allow dynamic-group <my-group> to use log-content in compartment <target_compartment_name>
    舉例而言:
    Allow dynamic-group okelogging to use log-content in compartment BlockchainTeam
完成先決條件之後,請完成下列步驟,以使用 OCI 集中儲存日誌。
  1. 按一下左上角的功能表圖示,搜尋日誌,然後選取日誌
  2. 建立日誌群組。在記錄日誌下,選取日誌群組,然後按一下建立日誌群組
  3. 建立自訂日誌。在記錄下,選取記錄,然後按一下建立自訂記錄以開啟建立自訂記錄精靈。選取您之前建立的日誌群組。
  4. 建立自訂日誌精靈的第二頁,建立自訂日誌的代理程式組態,指定 Kubernetes 區間和動態群組。
  5. 代理程式組態頁面的設定日誌輸入區段中,將代理程式的日誌輸入設定為使用下列檔案路徑 (應用程式容器的預設值)。從輸入類型清單中選取日誌路徑。針對檔案路徑輸入下列檔案路徑。此路徑包含所有容器日誌,包括系統和服務容器。
    /var/log/pods/*/*/*.log
  6. 等待攝取日誌。日誌通常會在 3-5 分鐘內攝取。
  7. 選取日誌,然後導覽至自訂日誌,然後按一下瀏覽日誌。您可以分析、剖析及篩選日誌。
  8. 您也可以使用 OCI 將日誌儲存在物件儲存服務中。
    1. 建立連線器。在記錄下,選取連接器,然後按一下建立連接器。選取記錄日誌作為來源,選取物件儲存作為目標
    2. 視需要設定來源和目標。
    3. 啟用日誌區段底下,將您建立之連線器的啟用日誌設為已啟用。隨即顯示建立日誌面板,內含日誌保留時間的預設值。
    4. 等待攝取日誌。日誌通常會在 3-5 分鐘內攝取。接著,您就可以在連線器日誌中查看讀取和寫入作業。日誌目前正在寫入物件儲存服務。

如需詳細資訊,請參閱使用 OCI 日誌記錄分析監控 Kubernetes 和 OKE 叢集

使用外部工具進行永久記錄日誌

您可以使用 Fluentd 和 Elastic Stack 集中儲存記錄檔。以下步驟已經使用 Fluentd v1.16.2 和 Elasticsearch 7.9.1 進行測試。當您完成這些步驟時,請使用這些版本或更新版本。
  1. 建立一個名為 fluentd 的 Kubernetes 命名空間。
  2. 使用下列命令建立以角色為基礎的存取控制資源。
    kubectl create -f fluentd-rbac.yaml -n fluentd
    搭配使用下列 fluentd-rbac.yaml 檔案與指令。
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: fluentd
      namespace: fluentd
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: fluentd
      namespace: fluentd
    rules:
    - apiGroups:
      - ""
      resources:
      - pods
      - namespaces
      verbs:
      - get
      - list
      - watch
    ---
    kind: ClusterRoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: fluentd
    roleRef:
      kind: ClusterRole
      name: fluentd
      apiGroup: rbac.authorization.k8s.io
    subjects:
    - kind: ServiceAccount
      name: fluentd
      namespace: fluentd
  3. 使用下列命令建立 Fluentd 或 Elastic Stack 的 ConfigMap 物件。
    kubectl create -f fluentd-configmap_removefilter_ascii.yaml -n fluentd
    搭配使用下列 fluentd-configmap_removefilter_ascii.yaml 檔案與指令。
    在下列檔案中,將數字符號 (#) 移除為僅取消註釋下列其中一行。
    • 如果您要寫入 /tmp/obp.log 路徑中的檔案,請取消註釋 @include file-fluent.conf
    • 如果您要寫入 Elasticsearch,請取消註釋 @include elastic-fluent.conf
    下列檔案顯示寫入 /tmp/obp.log 路徑的範例。
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: fluentd-config
      namespace: fluentd
    data:
      fluent.conf: |-
        ################################################################
        # This source gets all logs from local docker host
        #@include pods-kind-fluent.conf
        #@include pods-fluent.conf
        @include pods-nofilter.conf
        @include file-fluent.conf
        #@include elastic-fluent.conf
      pods-nofilter.conf: |-
        <source>
          @type tail
          path /var/log/containers/*.log
          format //^(?<time>.+) (?<stream>stdout|stderr) (?<logtag>.)? (?<log>.*)$/ /
          pos_file /var/log/fluentd-containers.log.pos
          tag kubernetes.*
          read_from_head true
        </source>
        <filter kubernetes.**>
         @type kubernetes_metadata
        </filter>
      file-fluent.conf: |-
         <match kubernetes.var.log.containers.**fluentd**.log>
          @type null
         </match>
         <match kubernetes.var.log.containers.**kube-system**.log>
           @type null
         </match>
         <match kubernetes.**>
           @type file
           path /tmp/obp.log
         </match>
      elastic-fluent.conf: |-
        <match kubernetes.var.log.containers.**fluentd**.log>
          @type null
        </match>
        <match kubernetes.var.log.containers.**kube-system**.log>
          @type null
        </match>
        <match kubernetes.**>
          @type elasticsearch
          host "#{ENV['FLUENT_ELASTICSEARCH_HOST'] || 'elasticsearch.elastic-kibana'}"
          port "#{ENV['FLUENT_ELASTICSEARCH_PORT'] || '9200'}"
          index_name fluentd-k8s-3
          type_name fluentd
          include_timestamp true
        </match>
  4. 使用下列命令建立 Fluentd 的 DaemonSet 物件。此指令會在每個節點上建立 Fluentd Pod。
    kubectl create -f fluentd.yaml -n fluentd
    搭配使用下列 fluentd.yaml 檔案。
    apiVersion: apps/v1
    kind: DaemonSet
    metadata:
      name: fluentd
      namespace: fluentd
      labels:
        k8s-app: fluentd-logging
        version: v1
    spec:
      selector:
        matchLabels:
          k8s-app: fluentd-logging
          version: v1
      template:
        metadata:
          labels:
            k8s-app: fluentd-logging
            version: v1
        spec:
          serviceAccount: fluentd
          serviceAccountName: fluentd
          tolerations:
          - key: node-role.kubernetes.io/master
            effect: NoSchedule
          - key: node-role.kubernetes.io/control-plane
            effect: NoSchedule 
          containers:
          - name: fluentd1
            imagePullPolicy: "Always"
            image: fluent/fluentd-kubernetes-daemonset:v1.16.2-debian-elasticsearch7-1.1
            env:
              - name:  FLUENT_ELASTICSEARCH_HOST
                value: "elasticsearch.elastic-kibana"
              - name:  FLUENT_ELASTICSEARCH_PORT
                value: "9200"
            resources:
              limits:
                memory: 200Mi
              requests:
                cpu: 100m
                memory: 200Mi
            volumeMounts:
            - name: fluentd-config
              mountPath: /fluentd/etc
            - name: logs
              mountPath: /tmp
            - name: varlog
              mountPath: /var/log
            - name: varlibdockercontainers
              mountPath: /var/lib/docker/containers
              readOnly: true
          terminationGracePeriodSeconds: 30
          volumes:
          - name: fluentd-config
            configMap:
              name: fluentd-config
          - name: varlog
            hostPath:
              path: /var/log
          - name: varlibdockercontainers
            hostPath:
              path: /var/lib/docker/containers
          - name: logs
            hostPath:
              path: /tmp
    Oracle Blockchain Platform 日誌可在 Fluentd Pod 或 Kubernetes 節點的 /tmp 目錄中取得。
  5. 若要將日誌傳送至 Elastic Stack,請建立一個名為 elastic-kibana 的 Kubernetes 命名空間。
  6. 使用下列命令建立 Elastic Stack 的部署,並將其顯示為服務。
    kubectl create -f elastic.yaml -n elastic-kibana
    搭配使用下列 elastic.yaml 檔案。
    apiVersion: v1
    kind: Namespace
    metadata:
      name: elastic-kibana
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: elasticsearch
      namespace: elastic-kibana
      labels:
        app: elasticsearch
    spec:
      selector:
        matchLabels:
          app: elasticsearch
      replicas: 1
      template:
        metadata:
          labels:
            app: elasticsearch
        spec:
          initContainers:
          - name: vm-max-fix
            image: busybox
            command: ["sysctl", "-w", "vm.max_map_count=262144"]
            securityContext:
              privileged: true
          containers:
          - name: elasticsearch
            image: elasticsearch:7.9.1
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 9200
            env:
            - name: node.name
              value: "elasticsearch"
            - name: cluster.initial_master_nodes
              value: "elasticsearch"
            - name: bootstrap.memory_lock
              value: "false"
            - name: ES_JAVA_OPTS
              value: "-Xms512m -Xmx512m"
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: elasticsearch
      namespace: elastic-kibana
      labels:
        app: elasticsearch
    spec:
      type: ClusterIP
      selector:
        app: elasticsearch
      ports:
        - protocol: TCP
          name: http
          port: 9200
          targetPort: 9200
  7. 接著,您可以使用下列命令檢查 Elasticsearch 索引中的日誌資料。
    curl -X GET "localhost:9200/_cat/indices/fluentd-k8s-*?v=true&s=index&pretty"
    curl -X GET "localhost:9200/fluentd-k8s-3/_search?pretty=true"
  8. 您也可以使用 Fluentd 將日誌儲存在每個節點的本機區塊磁碟區上。
    1. 為每個節點建立一個區塊磁碟區、附加磁碟區,然後建立一個名為 /u01 的目錄。
    2. 格式化 ext4 檔案系統的連附區塊磁碟區。
    3. 在裝置路徑上掛載 /u01 目錄。
    4. 變更 Fluentd 部署檔案 (fluentd.yaml),讓日誌磁碟區為 /u01,而不是 /tmp,如下列程式碼片段所示。
      - name: logs
              hostPath:
                path: /u01
    5. 執行下列命令以套用 Fluentd 部署。
      kubectl apply -f fluentd.yaml -n fluentd
    6. 日誌現在會顯示在每個節點的 /u01 目錄中。

設定操作員 Pod 的日誌層次

您可以設定 hlf-operatorobp-operator Pod 的日誌層次。設定日誌層次的步驟會根據是否安裝 Oracle Blockchain Platform 而有所不同。如果尚未安裝 Oracle Blockchain Platform Enterprise Edition,請完成下列步驟。

  1. 開啟對應的 deployment.yaml 檔案以進行編輯。hlf-operator Pod 的檔案位於下列位置:
    distribution_package_location/distribution-package/operators/helmcharts/hlf-operator/templates/deployment.yaml
    obp-operator Pod 的檔案位於下列位置:
    distribution_package_location/distribution-package/operators/helmcharts/obp-operator/templates/deployment.yaml
  2. 將下行新增至檔案。如註解所示,您可以將日誌層次設為 debuginfoerror。在下列範例中,日誌層次設為 info
    --zap-log-level=info # debug, info, error
在您編輯檔案之後,檔案的該區段可能會與下列文字類似:
containers:
    - args:
    - --enable-leader-election
    - --zap-log-level=info # debug, info, error

如果已經安裝 Oracle Blockchain Platform ,請完成下列步驟。

  • 請使用下列命令來編輯 hlf-operatorobp-operator Pod 的部署定義。新增或更新設定 Pod 樣板規格下 manager 容器之日誌層次的引數。
    kubectl edit deployment -n obp-cp obp-operator
    kubectl edit deployment -n obp-cp hlf-operator-controller-manager
更新容器引數之後,部署定義可能會與下列文字類似:
containers:
    - args:
    - --enable-leader-election
    - --zap-log-level=info # debug, info, error