promtail对接loki收集日志

Loki的搭建就不作说明,相对简单,只需挂载配置文件与端口即可
如果是自己制作的image,启动loki需要指定配置文件进行启动

promtail搭建

promtail搭建采用导入yaml文件方式进行搭建

注意事项:搭建时注意namespace,镜像,以及loki服务器地址

部署文档

# Source: promtail/templates/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: promtail
    release: calling-quail
  name: promtail
  namespace: loki
---
# Source: promtail/templates/clusterrole.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  labels:
    app: promtail
    release: calling-quail
  name: promtail-clusterrole
  namespace: loki
rules:
- apiGroups: [""] # "" indicates the core API group
  resources:
  - nodes
  - nodes/proxy
  - services
  - endpoints
  - pods
  verbs: ["get", "watch", "list"]
---
# Source: promtail/templates/clusterrolebinding.yaml
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: promtail-clusterrolebinding
  labels:
    app: promtail
    release: calling-quail
subjects:
  - kind: ServiceAccount
    name: promtail
    namespace: loki
roleRef:
  kind: ClusterRole
  name: promtail-clusterrole
  apiGroup: rbac.authorization.k8s.io
---
# Source: promtail/templates/role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: promtail
  namespace: loki
  labels:
    app: promtail
    release: calling-quail
rules:
- apiGroups:      ['extensions']
  resources:      ['podsecuritypolicies']
  verbs:          ['use']
  resourceNames:  [promtail]
---
# Source: promtail/templates/rolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: promtail
  namespace: loki
  labels:
    app: promtail
    release: calling-quail
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: promtail
subjects:
- kind: ServiceAccount
  name: promtail
---
# Source: promtail/templates/daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: promtail
  namespace: loki
  labels:
    app: promtail
    release: calling-quail
  annotations:
    {}

spec:
  selector:
    matchLabels:
      app: promtail
      release: calling-quail
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: promtail
        release: calling-quail
      annotations:
        checksum/config: 674c34bb782c907c837d96c00242c2c953b4b482d2c00475b03145696d4f301f
        prometheus.io/port: http-metrics
        prometheus.io/scrape: "true"

    spec:
      serviceAccountName: promtail
      containers:
        - name: promtail
          image: xxxxxxxxxxx/grafana/promtail:v1.0.0"
          imagePullPolicy: IfNotPresent
          args:
            - "-config.file=/etc/promtail/promtail.yaml"
            - "-client.url=http://xxxxxxx/loki/api/v1/push"
          volumeMounts:
            - name: config
              mountPath: /etc/promtail
            - name: run
              mountPath: /run/promtail
            - mountPath: /var/lib/docker/containers
              name: docker
              readOnly: true
            - mountPath: /var/log/pods
              name: pods
              readOnly: true

          env:
          - name: HOSTNAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          ports:
            - containerPort: 3101
              name: http-metrics
          securityContext:
            readOnlyRootFilesystem: true
            runAsGroup: 0
            runAsUser: 0

          readinessProbe:
            failureThreshold: 5
            httpGet:
              path: /ready
              port: http-metrics
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 1

          resources:
            {}

      nodeSelector:
        {}

      affinity:
        {}

      tolerations:
        - effect: NoSchedule
          key: node-role.kubernetes.io/master
          operator: Exists

      volumes:
        - name: config
          configMap:
            name: promtail-config
        - name: run
          hostPath:
            path: /run/promtail
        - hostPath:
            path: /var/lib/docker/containers
          name: docker
        - hostPath:
            path: /var/log/pods
          name: pods

其中需要进行角色binding,否则promtail无法进入其他namespace进行操作。

其中promtail-configmap文件promtail-config内容如下

client:
  backoff_config:
    maxbackoff: 5s
    maxretries: 20
    minbackoff: 100ms
  batchsize: 102400
  batchwait: 1s
  external_labels: {}
  timeout: 10s
positions:
  filename: /run/promtail/positions.yaml
server:
  http_listen_port: 3101
target_config:
  sync_period: 10s

scrape_configs:
- job_name: kubernetes-pods-name
  pipeline_stages:
    - docker: {}
    
  kubernetes_sd_configs:
  - role: pod
  relabel_configs:
  - source_labels:   
    - __meta_kubernetes_pod_label_name
    target_label: __service__
  - source_labels:             
    - __meta_kubernetes_pod_node_name  
    target_label: __host__
  - action: drop              
    regex: ''
    source_labels:
    - __service__
  - action: replace          
    replacement: $1
    separator: /
    source_labels:     
    - __meta_kubernetes_namespace
    - __service__
    target_label: namespace_service
  - action: replace         
    source_labels:           
    - __meta_kubernetes_namespace
    target_label: namespace
  - action: replace    
    source_labels:                
    - __meta_kubernetes_pod_name
    target_label: pod_name    
  - replacement: /var/log/pods/*$1/*.log   
    separator: /
    source_labels:
    - __meta_kubernetes_pod_uid
    - __meta_kubernetes_pod_container_name
    target_label: __path__
  - action: replace    
    source_labels:                
    - __meta_kubernetes_pod_host_ip
    target_label: pod_host_ip
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_container_name
    target_label: container_name
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_label_site
    target_label: site
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_label_app
    target_label: app
- job_name: kubernetes-pods-app
  pipeline_stages:
    - docker: {}
    
  kubernetes_sd_configs:
  - role: pod
  relabel_configs:
  - action: drop
    regex: .+
    source_labels:
    - __meta_kubernetes_pod_label_name
  - source_labels:
    - __meta_kubernetes_pod_label_app
    target_label: __service__
  - source_labels:
    - __meta_kubernetes_pod_node_name
    target_label: __host__
  - action: drop
    regex: ''
    source_labels:
    - __service__
  - action: replace
    replacement: $1
    separator: /
    source_labels:
    - __meta_kubernetes_namespace
    - __service__
    target_label: namespace_service
  - action: replace
    source_labels:
    - __meta_kubernetes_namespace
    target_label: namespace
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_name
    target_label: pod_name
  - replacement: /var/log/pods/*$1/*.log
    separator: /
    source_labels:
    - __meta_kubernetes_pod_uid
    - __meta_kubernetes_pod_container_name
    target_label: __path__
  - action: replace    
    source_labels:                
    - __meta_kubernetes_pod_host_ip
    target_label: pod_host_ip
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_container_name
    target_label: container_name
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_label_site
    target_label: site
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_label_app
    target_label: app
- job_name: kubernetes-pods-direct-controllers
  pipeline_stages:
    - docker: {}
    
  kubernetes_sd_configs:
  - role: pod
  relabel_configs:
  - action: drop
    regex: .+
    separator: ''
    source_labels:
    - __meta_kubernetes_pod_label_name
    - __meta_kubernetes_pod_label_app
  - action: drop
    regex: '[0-9a-z-.]+-[0-9a-f]{8,10}'
    source_labels:
    - __meta_kubernetes_pod_controller_name
  - source_labels:
    - __meta_kubernetes_pod_controller_name
    target_label: __service__
  - source_labels:
    - __meta_kubernetes_pod_node_name
    target_label: __host__
  - action: drop
    regex: ''
    source_labels:
    - __service__
  - action: replace
    replacement: $1
    separator: /
    source_labels:
    - __meta_kubernetes_namespace
    - __service__
    target_label: namespace_service
  - action: replace
    source_labels:
    - __meta_kubernetes_namespace
    target_label: namespace
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_name
    target_label: pod_name
  - replacement: /var/log/pods/*$1/*.log
    separator: /
    source_labels:
    - __meta_kubernetes_pod_uid
    - __meta_kubernetes_pod_container_name
    target_label: __path__
  - action: replace    
    source_labels:                
    - __meta_kubernetes_pod_host_ip
    target_label: pod_host_ip
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_container_name
    target_label: container_name
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_label_site
    target_label: site
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_label_app
    target_label: app
- job_name: kubernetes-pods-indirect-controller
  pipeline_stages:
    - docker: {}
    
  kubernetes_sd_configs:
  - role: pod
  relabel_configs:
  - action: drop
    regex: .+
    separator: ''
    source_labels:
    - __meta_kubernetes_pod_label_name
    - __meta_kubernetes_pod_label_app
  - action: keep
    regex: '[0-9a-z-.]+-[0-9a-f]{8,10}'
    source_labels:
    - __meta_kubernetes_pod_controller_name
  - action: replace
    regex: '([0-9a-z-.]+)-[0-9a-f]{8,10}'
    source_labels:
    - __meta_kubernetes_pod_controller_name
    target_label: __service__
  - source_labels:
    - __meta_kubernetes_pod_node_name
    target_label: __host__
  - action: drop
    regex: ''
    source_labels:
    - __service__
  - action: replace
    replacement: $1
    separator: /
    source_labels:
    - __meta_kubernetes_namespace
    - __service__
    target_label: namespace_service
  - action: replace
    source_labels:
    - __meta_kubernetes_namespace
    target_label: namespace
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_name
    target_label: pod_name
  - replacement: /var/log/pods/*$1/*.log
    separator: /
    source_labels:
    - __meta_kubernetes_pod_uid
    - __meta_kubernetes_pod_container_name
    target_label: __path__
  - action: replace    
    source_labels:                
    - __meta_kubernetes_pod_host_ip
    target_label: pod_host_ip
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_container_name
    target_label: container_name
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_label_site
    target_label: site
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_label_app
    target_label: app
- job_name: kubernetes-pods-static
  pipeline_stages:
    - docker: {}
    
  kubernetes_sd_configs:
  - role: pod
  relabel_configs:
  - action: drop
    regex: ''
    source_labels:
    - __meta_kubernetes_pod_annotation_kubernetes_io_config_mirror
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_label_component
    target_label: __service__
  - source_labels:
    - __meta_kubernetes_pod_node_name
    target_label: __host__
  - action: drop
    regex: ''
    source_labels:
    - __service__
  - action: replace
    replacement: $1
    separator: /
    source_labels:
    - __meta_kubernetes_namespace
    - __service__
    target_label: namespace_service
  - action: replace
    source_labels:
    - __meta_kubernetes_namespace
    target_label: namespace
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_name
    target_label: pod_name
  - replacement: /var/log/pods/*$1/*.log
    separator: /
    source_labels:
    - __meta_kubernetes_pod_annotation_kubernetes_io_config_mirror
    - __meta_kubernetes_pod_container_name
    target_label: __path__
  - action: replace    
    source_labels:                
    - __meta_kubernetes_pod_host_ip
    target_label: pod_host_ip
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_container_name
    target_label: container_name
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_label_site
    target_label: site
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_label_app
    target_label: app

在配置文件中将label进行转化,换成自己喜欢的label.

promtail启动需要指定配置文件与loki地址,例如:
-config.file=/etc/promtail/promtail.yaml -client.url=http://loki.loki:3100/loki/api/v1/push

目前收集的都是标准输出的,没有针对。所以下一篇针对应用日志收集

你可能感兴趣的:(k8s,Loki)