loki分为三部分:
    1、promtail,客户端负责采集日志
    2、loki日志处理,存储和查询部分
    3、grafana界面部分
    介绍可以查看官网,这里几下要注意的几点:
    1、promtail配置:
    daemonset:

    1. apiVersion: apps/v1
    2. kind: DaemonSet
    3. metadata:
    4. name: promtail
    5. namespace: default
    6. spec:
    7. template:
    8. spec:
    9. serviceAccount: promtail-serviceaccount
    10. serviceAccountName: promtail-serviceaccount
    11. containers:
    12. - args:
    13. - -config.file=/etc/promtail/config.yml
    14. - -print-config-stderr=true
    15. env: #这里必须要注意这里的环境变量必须要给上
    16. - name: HOSTNAME
    17. valueFrom:
    18. fieldRef:
    19. apiVersion: v1
    20. fieldPath: spec.nodeName
    21. image: grafana/promtail:2.2.1
    22. name: promtail
    23. volumeMounts: #这里的也要注意
    24. - mountPath: /var/lib/docker/containers
    25. name: dockerlog
    26. - mountPath: /etc/promtail
    27. name: conf
    28. - mountPath: /run/promtail
    29. name: vol1
    30. - mountPath: /var/log/pods
    31. name: pods
    32. volumes:
    33. - hostPath:
    34. path: /var/lib/docker/containers
    35. type: ""
    36. name: dockerlog
    37. - configMap:
    38. defaultMode: 256
    39. items:
    40. - key: promtail
    41. path: config.yml
    42. name: loki
    43. optional: false
    44. name: conf
    45. - hostPath:
    46. path: /var/upa/promtail
    47. type: DirectoryOrCreate
    48. name: vol1
    49. - hostPath:
    50. path: /var/log/pods
    51. type: ""
    52. name: pods

    rabc:
    clusterrole,注意这里的集群的

    1. apiVersion: rbac.authorization.k8s.io/v1
    2. kind: ClusterRole
    3. metadata:
    4. name: promtail-clusterrole
    5. rules:
    6. - apiGroups:
    7. - ""
    8. resources:
    9. - nodes
    10. - services
    11. - pods
    12. verbs:
    13. - get
    14. - watch
    15. - list

    serviceaccount,注意这里是命名空间的

    1. apiVersion: v1
    2. kind: ServiceAccount
    3. metadata:
    4. name: promtail-serviceaccount
    5. namespace: default
    6. secrets:
    7. - name: promtail-serviceaccount-token-xn4v9

    clusterrolebinding 注意这里是集群的

    1. apiVersion: rbac.authorization.k8s.io/v1
    2. kind: ClusterRoleBinding
    3. metadata:
    4. name: promtail-clusterrolebinding
    5. roleRef:
    6. apiGroup: rbac.authorization.k8s.io
    7. kind: ClusterRole
    8. name: promtail-clusterrole
    9. subjects:
    10. - kind: ServiceAccount
    11. name: promtail-serviceaccount
    12. namespace: default

    配置文件:

    1. client:
    2. backoff_config:
    3. min_period: 5s
    4. max_retries: 20
    5. batchsize: 102400
    6. batchwait: 1s
    7. timeout: 10s
    8. url: http://loki:3100/loki/api/v1/push
    9. positions:
    10. filename: /run/promtail/positions.yaml
    11. target_config:
    12. sync_period: 10s
    13. scrape_configs:
    14. - job_name: 'k8s-pods'
    15. kubernetes_sd_configs:
    16. - role: pod
    17. relabel_configs:
    18. - source_labels: [ __meta_kubernetes_pod_label_promtail ] ##筛选要爬取的pod容器
    19. action: keep
    20. regex: true
    21. - source_labels: [ __meta_kubernetes_namespace ]
    22. action: replace
    23. target_label: namespace
    24. - source_labels: [ __meta_kubernetes_pod_name ]
    25. action: replace
    26. target_label: pod
    27. - source_labels: [ __meta_kubernetes_pod_node_name ]
    28. action: replace
    29. target_label: node
    30. - source_labels: [ __meta_kubernetes_pod_container_name ]
    31. action: replace
    32. target_label: name
    33. - replacement: /var/log/pods/*$1/*.log
    34. separator: /
    35. source_labels:
    36. - __meta_kubernetes_pod_uid
    37. - __meta_kubernetes_pod_container_name
    38. target_label: __path__ #这里必须要只想正确的路径

    2、loki使用satefulse

    1. apiVersion: apps/v1
    2. kind: StatefulSet
    3. metadata:
    4. name: loki
    5. namespace: default
    6. spec:
    7. template:
    8. spec:
    9. containers:
    10. - args:
    11. - -config.file=/var/upa/loki/loki.cnf
    12. image: grafana/loki:2.2.0
    13. imagePullPolicy: Always
    14. name: loki
    15. ports:
    16. - containerPort: 3100
    17. name: 3100tcp31002
    18. protocol: TCP
    19. - containerPort: 9096
    20. name: 9096tcp310002
    21. protocol: TCP
    22. volumeMounts:
    23. - mountPath: /var/upa/loki
    24. name: vol1
    25. volumes:
    26. - configMap:
    27. defaultMode: 292
    28. items:
    29. - key: loki
    30. mode: 292
    31. path: loki.cnf
    32. name: loki
    33. optional: false
    34. name: vol1
    35. ##这里要加上日志的映射,这里没加上

    配置文件

    1. auth_enabled: false
    2. server:
    3. http_listen_port: 3100
    4. grpc_listen_port: 9096
    5. ingester:
    6. wal:
    7. enabled: true
    8. dir: /var/upa/loki/data/wal
    9. lifecycler:
    10. address: 127.0.0.1
    11. ring:
    12. kvstore:
    13. store: inmemory
    14. replication_factor: 1
    15. final_sleep: 0s
    16. chunk_idle_period: 1h # Any chunk not receiving new logs in this time will be flushed
    17. max_chunk_age: 1h # All chunks will be flushed when they hit this age, default is 1h
    18. chunk_target_size: 1048576 # Loki will attempt to build chunks up to 1.5MB, flushing first if chunk_idle_period or max_chunk_age is reached first
    19. chunk_retain_period: 30s # Must be greater than index read cache TTL if using an index cache (Default index read cache TTL is 5m)
    20. max_transfer_retries: 0 # Chunk transfers disabled
    21. schema_config:
    22. configs:
    23. - from: 2020-10-24
    24. store: boltdb-shipper
    25. object_store: filesystem
    26. schema: v11
    27. index:
    28. prefix: index_
    29. period: 24h
    30. storage_config:
    31. boltdb_shipper:
    32. active_index_directory: /var/upa/loki/data/boltdb-shipper-active
    33. cache_location: /var/upa/loki/data/boltdb-shipper-cache
    34. cache_ttl: 24h # Can be increased for faster performance over longer query periods, uses more disk space
    35. shared_store: filesystem
    36. filesystem:
    37. directory: /var/upa/loki/data/chunks
    38. compactor:
    39. working_directory: /var/upa/loki/data/boltdb-shipper-compactor
    40. shared_store: filesystem
    41. limits_config:
    42. reject_old_samples: true
    43. reject_old_samples_max_age: 168h
    44. chunk_store_config:
    45. max_look_back_period: 0s
    46. ruler:
    47. storage:
    48. type: local
    49. local:
    50. directory: /var/upa/loki/data/rules
    51. rule_path: /var/upa/loki/data/rules-temp
    52. alertmanager_url: http://localhost:9093
    53. ring:
    54. kvstore:
    55. store: inmemory
    56. enable_api: true
    57. table_manager:
    58. retention_deletes_enabled: true
    59. retention_period: 720h

    3、grafana

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. name: grafana
    5. namespace: default
    6. spec:
    7. template:
    8. spec:
    9. containers:
    10. - env:
    11. - name: GF_LOG_MODE
    12. value: console file
    13. image: grafana/grafana:latest
    14. imagePullPolicy: Always
    15. name: grafana
    16. ports:
    17. - containerPort: 3000
    18. name: http
    19. protocol: TCP
    20. volumeMounts:
    21. - mountPath: /var/lib/grafana
    22. name: vol1
    23. volumes:
    24. - hostPath:
    25. path: /var/upa/grafana
    26. type: DirectoryOrCreate
    27. name: vol1