loki分为三部分:
1、promtail,客户端负责采集日志
2、loki日志处理,存储和查询部分
3、grafana界面部分
介绍可以查看官网,这里几下要注意的几点:
1、promtail配置:
daemonset:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: promtail
namespace: default
spec:
template:
spec:
serviceAccount: promtail-serviceaccount
serviceAccountName: promtail-serviceaccount
containers:
- args:
- -config.file=/etc/promtail/config.yml
- -print-config-stderr=true
env: #这里必须要注意这里的环境变量必须要给上
- name: HOSTNAME
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: spec.nodeName
image: grafana/promtail:2.2.1
name: promtail
volumeMounts: #这里的也要注意
- mountPath: /var/lib/docker/containers
name: dockerlog
- mountPath: /etc/promtail
name: conf
- mountPath: /run/promtail
name: vol1
- mountPath: /var/log/pods
name: pods
volumes:
- hostPath:
path: /var/lib/docker/containers
type: ""
name: dockerlog
- configMap:
defaultMode: 256
items:
- key: promtail
path: config.yml
name: loki
optional: false
name: conf
- hostPath:
path: /var/upa/promtail
type: DirectoryOrCreate
name: vol1
- hostPath:
path: /var/log/pods
type: ""
name: pods
rabc:
clusterrole,注意这里的集群的
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: promtail-clusterrole
rules:
- apiGroups:
- ""
resources:
- nodes
- services
- pods
verbs:
- get
- watch
- list
serviceaccount,注意这里是命名空间的
apiVersion: v1
kind: ServiceAccount
metadata:
name: promtail-serviceaccount
namespace: default
secrets:
- name: promtail-serviceaccount-token-xn4v9
clusterrolebinding 注意这里是集群的
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: promtail-clusterrolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: promtail-clusterrole
subjects:
- kind: ServiceAccount
name: promtail-serviceaccount
namespace: default
配置文件:
client:
backoff_config:
min_period: 5s
max_retries: 20
batchsize: 102400
batchwait: 1s
timeout: 10s
url: http://loki:3100/loki/api/v1/push
positions:
filename: /run/promtail/positions.yaml
target_config:
sync_period: 10s
scrape_configs:
- job_name: 'k8s-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [ __meta_kubernetes_pod_label_promtail ] ##筛选要爬取的pod容器
action: keep
regex: true
- source_labels: [ __meta_kubernetes_namespace ]
action: replace
target_label: namespace
- source_labels: [ __meta_kubernetes_pod_name ]
action: replace
target_label: pod
- source_labels: [ __meta_kubernetes_pod_node_name ]
action: replace
target_label: node
- source_labels: [ __meta_kubernetes_pod_container_name ]
action: replace
target_label: name
- replacement: /var/log/pods/*$1/*.log
separator: /
source_labels:
- __meta_kubernetes_pod_uid
- __meta_kubernetes_pod_container_name
target_label: __path__ #这里必须要只想正确的路径
2、loki使用satefulse
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: loki
namespace: default
spec:
template:
spec:
containers:
- args:
- -config.file=/var/upa/loki/loki.cnf
image: grafana/loki:2.2.0
imagePullPolicy: Always
name: loki
ports:
- containerPort: 3100
name: 3100tcp31002
protocol: TCP
- containerPort: 9096
name: 9096tcp310002
protocol: TCP
volumeMounts:
- mountPath: /var/upa/loki
name: vol1
volumes:
- configMap:
defaultMode: 292
items:
- key: loki
mode: 292
path: loki.cnf
name: loki
optional: false
name: vol1
##这里要加上日志的映射,这里没加上
配置文件
auth_enabled: false
server:
http_listen_port: 3100
grpc_listen_port: 9096
ingester:
wal:
enabled: true
dir: /var/upa/loki/data/wal
lifecycler:
address: 127.0.0.1
ring:
kvstore:
store: inmemory
replication_factor: 1
final_sleep: 0s
chunk_idle_period: 1h # Any chunk not receiving new logs in this time will be flushed
max_chunk_age: 1h # All chunks will be flushed when they hit this age, default is 1h
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
chunk_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 disabled
schema_config:
configs:
- from: 2020-10-24
store: boltdb-shipper
object_store: filesystem
schema: v11
index:
prefix: index_
period: 24h
storage_config:
boltdb_shipper:
active_index_directory: /var/upa/loki/data/boltdb-shipper-active
cache_location: /var/upa/loki/data/boltdb-shipper-cache
cache_ttl: 24h # Can be increased for faster performance over longer query periods, uses more disk space
shared_store: filesystem
filesystem:
directory: /var/upa/loki/data/chunks
compactor:
working_directory: /var/upa/loki/data/boltdb-shipper-compactor
shared_store: filesystem
limits_config:
reject_old_samples: true
reject_old_samples_max_age: 168h
chunk_store_config:
max_look_back_period: 0s
ruler:
storage:
type: local
local:
directory: /var/upa/loki/data/rules
rule_path: /var/upa/loki/data/rules-temp
alertmanager_url: http://localhost:9093
ring:
kvstore:
store: inmemory
enable_api: true
table_manager:
retention_deletes_enabled: true
retention_period: 720h
3、grafana
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafana
namespace: default
spec:
template:
spec:
containers:
- env:
- name: GF_LOG_MODE
value: console file
image: grafana/grafana:latest
imagePullPolicy: Always
name: grafana
ports:
- containerPort: 3000
name: http
protocol: TCP
volumeMounts:
- mountPath: /var/lib/grafana
name: vol1
volumes:
- hostPath:
path: /var/upa/grafana
type: DirectoryOrCreate
name: vol1