loki分为三部分:
1、promtail,客户端负责采集日志
2、loki日志处理,存储和查询部分
3、grafana界面部分
介绍可以查看官网,这里几下要注意的几点:
1、promtail配置:
daemonset:
apiVersion: apps/v1kind: DaemonSetmetadata:name: promtailnamespace: defaultspec:template:spec:serviceAccount: promtail-serviceaccountserviceAccountName: promtail-serviceaccountcontainers:- args:- -config.file=/etc/promtail/config.yml- -print-config-stderr=trueenv: #这里必须要注意这里的环境变量必须要给上- name: HOSTNAMEvalueFrom:fieldRef:apiVersion: v1fieldPath: spec.nodeNameimage: grafana/promtail:2.2.1name: promtailvolumeMounts: #这里的也要注意- mountPath: /var/lib/docker/containersname: dockerlog- mountPath: /etc/promtailname: conf- mountPath: /run/promtailname: vol1- mountPath: /var/log/podsname: podsvolumes:- hostPath:path: /var/lib/docker/containerstype: ""name: dockerlog- configMap:defaultMode: 256items:- key: promtailpath: config.ymlname: lokioptional: falsename: conf- hostPath:path: /var/upa/promtailtype: DirectoryOrCreatename: vol1- hostPath:path: /var/log/podstype: ""name: pods
rabc:
clusterrole,注意这里的集群的
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:name: promtail-clusterrolerules:- apiGroups:- ""resources:- nodes- services- podsverbs:- get- watch- list
serviceaccount,注意这里是命名空间的
apiVersion: v1kind: ServiceAccountmetadata:name: promtail-serviceaccountnamespace: defaultsecrets:- name: promtail-serviceaccount-token-xn4v9
clusterrolebinding 注意这里是集群的
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: promtail-clusterrolebindingroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: promtail-clusterrolesubjects:- kind: ServiceAccountname: promtail-serviceaccountnamespace: default
配置文件:
client:backoff_config:min_period: 5smax_retries: 20batchsize: 102400batchwait: 1stimeout: 10surl: http://loki:3100/loki/api/v1/pushpositions:filename: /run/promtail/positions.yamltarget_config:sync_period: 10sscrape_configs:- job_name: 'k8s-pods'kubernetes_sd_configs:- role: podrelabel_configs:- source_labels: [ __meta_kubernetes_pod_label_promtail ] ##筛选要爬取的pod容器action: keepregex: true- source_labels: [ __meta_kubernetes_namespace ]action: replacetarget_label: namespace- source_labels: [ __meta_kubernetes_pod_name ]action: replacetarget_label: pod- source_labels: [ __meta_kubernetes_pod_node_name ]action: replacetarget_label: node- source_labels: [ __meta_kubernetes_pod_container_name ]action: replacetarget_label: name- replacement: /var/log/pods/*$1/*.logseparator: /source_labels:- __meta_kubernetes_pod_uid- __meta_kubernetes_pod_container_nametarget_label: __path__ #这里必须要只想正确的路径
2、loki使用satefulse
apiVersion: apps/v1kind: StatefulSetmetadata:name: lokinamespace: defaultspec:template:spec:containers:- args:- -config.file=/var/upa/loki/loki.cnfimage: grafana/loki:2.2.0imagePullPolicy: Alwaysname: lokiports:- containerPort: 3100name: 3100tcp31002protocol: TCP- containerPort: 9096name: 9096tcp310002protocol: TCPvolumeMounts:- mountPath: /var/upa/lokiname: vol1volumes:- configMap:defaultMode: 292items:- key: lokimode: 292path: loki.cnfname: lokioptional: falsename: vol1##这里要加上日志的映射,这里没加上
配置文件
auth_enabled: falseserver:http_listen_port: 3100grpc_listen_port: 9096ingester:wal:enabled: truedir: /var/upa/loki/data/wallifecycler:address: 127.0.0.1ring:kvstore:store: inmemoryreplication_factor: 1final_sleep: 0schunk_idle_period: 1h # Any chunk not receiving new logs in this time will be flushedmax_chunk_age: 1h # All chunks will be flushed when they hit this age, default is 1hchunk_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 firstchunk_retain_period: 30s # Must be greater than index read cache TTL if using an index cache (Default index read cache TTL is 5m)max_transfer_retries: 0 # Chunk transfers disabledschema_config:configs:- from: 2020-10-24store: boltdb-shipperobject_store: filesystemschema: v11index:prefix: index_period: 24hstorage_config:boltdb_shipper:active_index_directory: /var/upa/loki/data/boltdb-shipper-activecache_location: /var/upa/loki/data/boltdb-shipper-cachecache_ttl: 24h # Can be increased for faster performance over longer query periods, uses more disk spaceshared_store: filesystemfilesystem:directory: /var/upa/loki/data/chunkscompactor:working_directory: /var/upa/loki/data/boltdb-shipper-compactorshared_store: filesystemlimits_config:reject_old_samples: truereject_old_samples_max_age: 168hchunk_store_config:max_look_back_period: 0sruler:storage:type: locallocal:directory: /var/upa/loki/data/rulesrule_path: /var/upa/loki/data/rules-tempalertmanager_url: http://localhost:9093ring:kvstore:store: inmemoryenable_api: truetable_manager:retention_deletes_enabled: trueretention_period: 720h
3、grafana
apiVersion: apps/v1kind: Deploymentmetadata:name: grafananamespace: defaultspec:template:spec:containers:- env:- name: GF_LOG_MODEvalue: console fileimage: grafana/grafana:latestimagePullPolicy: Alwaysname: grafanaports:- containerPort: 3000name: httpprotocol: TCPvolumeMounts:- mountPath: /var/lib/grafananame: vol1volumes:- hostPath:path: /var/upa/grafanatype: DirectoryOrCreatename: vol1
