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"