loki 进程包含 四种角色
能够经过loki二进制的 -target参数指定运行角色
wget https://github.com/grafana/loki/releases/download/v2.2.1/loki-linux-amd64.zip wget https://github.com/grafana/loki/releases/download/v2.2.1/promtail-linux-amd64.zip
mkdir /opt/app/{promtail,loki} -pv # promtail配置文件 cat <<EOF> /opt/app/promtail/promtail.yaml server: http_listen_port: 9080 grpc_listen_port: 0 positions: filename: /var/log/positions.yaml # This location needs to be writeable by promtail. client: url: http://localhost:3100/loki/api/v1/push scrape_configs: - job_name: system pipeline_stages: static_configs: - targets: - localhost labels: job: varlogs # A `job` label is fairly standard in prometheus and useful for linking metrics and logs. host: yourhost # A `host` label will help identify logs from this machine vs others __path__: /var/log/*.log # The path matching uses a third party library: https://github.com/bmatcuk/doublestar EOF # service文件 cat <<EOF >/etc/systemd/system/promtail.service [Unit] Description=promtail server Wants=network-online.target After=network-online.target [Service] ExecStart=/opt/app/promtail/promtail -config.file=/opt/app/promtail/promtail.yaml StandardOutput=syslog StandardError=syslog SyslogIdentifier=promtail [Install] WantedBy=default.target EOF systemctl daemon-reload systemctl restart promtail systemctl status promtail
mkdir /opt/app/{promtail,loki} -pv # promtail配置文件 cat <<EOF> /opt/app/loki/loki.yaml auth_enabled: false server: http_listen_port: 3100 grpc_listen_port: 9096 ingester: wal: enabled: true dir: /opt/app/loki/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: /opt/app/loki/boltdb-shipper-active cache_location: /opt/app/loki/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: /opt/app/loki/chunks compactor: working_directory: /opt/app/loki/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 table_manager: retention_deletes_enabled: false retention_period: 0s ruler: storage: type: local local: directory: /opt/app/loki/rules rule_path: /opt/app/loki/rules-temp alertmanager_url: http://localhost:9093 ring: kvstore: store: inmemory enable_api: true EOF # service文件 cat <<EOF >/etc/systemd/system/loki.service [Unit] Description=loki server Wants=network-online.target After=network-online.target [Service] ExecStart=/opt/app/loki/loki -config.file=/opt/app/loki/loki.yaml StandardOutput=syslog StandardError=syslog SyslogIdentifier=loki [Install] WantedBy=default.target EOF systemctl daemon-reload systemctl restart loki systemctl status loki
查看日志
rate({job="message"} |="kubelet"
前端
算qps
rate({job="message"} |="kubelet" [1m])
linux
以前屡次提到loki和es最大的不一样是 loki只对标签进行索引而不对内容索引
下面咱们举例来看下
以简单的promtail配置举例
scrape_configs: - job_name: system pipeline_stages: static_configs: - targets: - localhost labels: job: message __path__: /var/log/messages
job="syslog"
/var/log/messages
,会以一个名为filename的固定标签{job="syslog"}
git
scrape_configs: - job_name: system pipeline_stages: static_configs: - targets: - localhost labels: job: syslog __path__: /var/log/syslog - job_name: system pipeline_stages: static_configs: - targets: - localhost labels: job: apache __path__: /var/log/apache.log
{job=~”apache|syslog”}
进行多job匹配和prometheus一致,相同标签对应的是一个流github
prometheus 处理series的模式
prometheus中标签一致对应的同一个hash值和refid(正整数递增的id),也就是同一个seriesweb
loki处理日志的模式
和prometheus一致,loki一组标签值会生成一个streamexpress
由于这种根据标签算哈希在倒排中查找id,对应找到存储的块在prometheus中已经被验证过了apache
因此有了上述知识,那么就得谈谈动态标签的问题了
何为动态标签:说白了就是标签的value不固定api
何为高基数标签:说白了就是标签的value可能性太多了,达到10万,100万甚至更多缓存
好比apache的access日志服务器
11.11.11.11 - frank [25/Jan/2000:14:00:01 -0500] "GET /1986.js HTTP/1.1" 200 932 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6"
在promtail中使用regex想要匹配 action
和status_code
两个标签
job_name: system
pipeline_stages:
- regex: expression: "^(?P<ip>\\S+) (?P<identd>\\S+) (?P<user>\\S+) \\[(?P<timestamp>[\\w:/]+\\s[+\\-]\\d{4})\\] \"(?P<action>\\S+)\\s?(?P<path>\\S+)?\\s?(?P<protocol>\\S+)?\" (?P<status_code>\\d{3}|-) (?P<size>\\d+|-)\\s?\"?(?P<referer>[^\"]*)\"?\\s?\"?(?P<useragent>[^\"]*)?\"?$"
static_configs:
targets:
labels:
job: apache
env: dev
__path__: /var/log/apache.log
那么对应action=get/post 和status_code=200/400则对应4个流
11.11.11.11 - frank [25/Jan/2000:14:00:01 -0500] "GET /1986.js HTTP/1.1" 200 932 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6" 11.11.11.12 - frank [25/Jan/2000:14:00:02 -0500] "POST /1986.js HTTP/1.1" 200 932 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6" 11.11.11.13 - frank [25/Jan/2000:14:00:03 -0500] "GET /1986.js HTTP/1.1" 400 932 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6" 11.11.11.14 - frank [25/Jan/2000:14:00:04 -0500] "POST /1986.js HTTP/1.1" 400 932 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6"
Loki的超级能力是将查询分解为小块并并行分发,以便您能够在短期内查询大量日志数据
以上边提到的ip字段为例
使用过滤器表达式查询
{job="apache"} |= "11.11.11.11"