1. 程式人生 > >k8s efk mysql慢日誌例子

k8s efk mysql慢日誌例子

應用部署檔案

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: alpaca
  labels:
    app: alpaca
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: alpaca
    spec:
      terminationGracePeriodSeconds: 60
      nodeSelector:
        deploy: app
      containers:
      - name: alpaca
        image: 192.168.1.225:5000/mysql:5.7-youben
        imagePullPolicy: Always
        readinessProbe:
          tcpSocket:
            port: 3306
          initialDelaySeconds: 60
          timeoutSeconds: 5
        livenessProbe:
          tcpSocket:
            port: 3306
          initialDelaySeconds: 60
          timeoutSeconds: 5
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: root
        ports:
        - containerPort: 3306
          name: tcp
        volumeMounts:
        - name: alpaca
          mountPath: /var/lib/mysql
      - name: fluentd-es
        image: 192.168.1.225:5000/fluentd-elasticsearch:v2.0.4-youben
        imagePullPolicy: Always
        command: ["/bin/sh"]
        args: ["-c", "/run.sh $FLUENTD_ARGS"]
        env:
        - name: FLUENTD_ARGS
          value: --no-supervisor -q
        volumeMounts:
        - name: config-volume
          mountPath: /etc/fluent/config.d
        - name: alpaca
          mountPath: /var/lib/mysql
      volumes:
      - name: alpaca
        persistentVolumeClaim:
          claimName: pvc-alpaca-withlog
      - name: config-volume
        configMap:
          name: fluentd-es-config-mysql-alpaca
---
apiVersion: v1
kind: Service
metadata:
  name: alpaca
  labels:
    app: alpaca
spec:
  ports:
  - port: 3306
    targetPort: 3306
  selector:
    app: alpaca

配置檔案:

kind: ConfigMap
apiVersion: v1
metadata:
  name: fluentd-es-config-mysql-alpaca
  labels:
    addonmanager.kubernetes.io/mode: Reconcile
data:
  system.conf: |-
    <system>
      root_dir /tmp/fluentd-buffers/
    </system>

  input.conf: |- 
    <source>
      @type mysql_slow_query
      path /var/lib/mysql/slow_query.log
      pos_file /root/mysql.log.slow.pos
      tag mysql.alpaca.slow.query
      <parse>
        @type none
     </parse>
    </source>
    
    <source>
      @id mysql.error.log
      @type tail
      path /var/log/mysql/error.log
      format multiline
      format_firstline /^\d{6} \d\d:\d\d:\d\d/
      format1 /(?<time>\d{6} \d\d:\d\d:\d\d) (?<error_msg>.*)/
      time_format %y%m%d %H:%M:%S
      pos_file /root/mysql.log.error.pos
      tag mysql.alpaca.error
    </source>
    
  output.conf: |-
    <match mysql.alpaca.slow.query>
      @id elasticsearch.slow
      @type elasticsearch
      @log_level info
      include_tag_key true
      host elasticsearch-logging.kube-system.svc.cluster.local
      port 9200
      logstash_format true
      logstash_prefix docker.mysql.slow
      logstash_dateformat %Y-%m-%d
      type_name docker_mysql_alpaca_log_slow
      <buffer>
        @type file
        path /var/log/fluentd-buffers/kubernetes.system.buffer.slow
        flush_mode interval
        retry_type exponential_backoff
        flush_thread_count 2
        flush_interval 5s
        retry_forever
        retry_max_interval 30
        chunk_limit_size 2M
        queue_limit_length 8
        overflow_action block
      </buffer>
    </match>
    
    <match mysql.alpaca.error>
      @id elasticsearch.error
      @type elasticsearch
      @log_level info
      include_tag_key true
      host elasticsearch-logging.kube-system.svc.cluster.local
      port 9200
      logstash_format true
      logstash_prefix docker.mysql.error
      logstash_dateformat %Y-%m-%d
      type_name docker_mysql_alpaca_log_error
      <buffer>
        @type file
        path /var/log/fluentd-buffers/kubernetes.system.buffer.error
        flush_mode interval
        retry_type exponential_backoff
        flush_thread_count 2
        flush_interval 5s
        retry_forever
        retry_max_interval 30
        chunk_limit_size 2M
        queue_limit_length 8
        overflow_action block
      </buffer>
    </match>

記得裝上mysql慢日誌外掛

已經打包好外掛:https://github.com/13567436138/kubernetes-yamls/tree/master/docker