Pangmatagalang imbakan ng data sa Elasticsearch

Pangmatagalang imbakan ng data sa Elasticsearch

Ang pangalan ko ay Igor Sidorenko, ako ay isang teknikal na pinuno sa pangkat ng mga admin na nagpapanatili ng buong imprastraktura ng Domclick.

Gusto kong ibahagi ang aking karanasan sa pagse-set up ng distributed data storage sa Elasticsearch. Titingnan natin kung anong mga setting sa mga node ang responsable para sa pamamahagi ng mga shards, kung paano gumagana at gumagana ang ILM.

Ang mga nagtatrabaho sa mga log, sa isang paraan o iba pa, ay nahaharap sa problema ng pangmatagalang imbakan para sa pagsusuri sa ibang pagkakataon. Sa Elasticsearch, ito ay totoo lalo na, dahil ang lahat ay kapus-palad sa pag-andar ng curator. Ipinakilala ng Bersyon 6.6 ang pag-andar ng ILM. Binubuo ito ng 4 na yugto:

  • Mainit - Ang index ay aktibong ina-update at tinatanong.
  • Warm - Hindi na na-update ang index, ngunit tina-query pa rin.
  • Malamig - Hindi na na-update ang index at bihirang ma-query. Ang impormasyon ay dapat na mahahanap pa rin, ngunit ang mga query ay maaaring mas mabagal.
  • Tanggalin - Hindi na kailangan ang index at maaaring ligtas na matanggal.

Ibinigay

  • Mainit na Data ng Elasticsearch: 24 na processor, 128 GB na memorya, 1,8 TB SSD RAID 10 (8 node).
  • Elasticsearch Data Warm: 24 processor, 64 GB memory, 8 TB NetApp SSD Policy (4 node).
  • Elasticsearch Data Cold: 8 processor, 32 GB memory, 128 TB HDD RAID 10 (4 node).

Layunin

Ang mga setting na ito ay indibidwal, ang lahat ay nakasalalay sa lugar sa mga node, ang bilang ng mga index, mga log, atbp. Mayroon kaming 2-3 TB ng data bawat araw.

  • 5 araw - Mainit na yugto (8 pangunahing / 1 replika).
  • 20 araw - Warm phase (pag-urong-index 4 pangunahing / 1 replika).
  • 90 araw - Cold phase (freeze-index 4 pangunahing / 1 replika).
  • 120 araw - yugto ng pagtanggal.

Pagse-set up ng Elasticsearch

Upang ipamahagi ang isang shard sa mga node, kailangan mo lamang ng isang parameter:

  • mainit-node:
    ~]# cat /etc/elasticsearch/elasticsearch.yml | grep attr
    # Add custom attributes to the node:
    node.attr.box_type: hot
  • Mainit-node:
    ~]# cat /etc/elasticsearch/elasticsearch.yml | grep attr
    # Add custom attributes to the node:
    node.attr.box_type: warm
  • Malamig-node:
    ~]# cat /etc/elasticsearch/elasticsearch.yml | grep attr
    # Add custom attributes to the node:
    node.attr.box_type: cold

Pagse-set up ng Logstash

Paano gumagana ang lahat ng ito at paano namin ipinatupad ang tampok na ito? Magsimula tayo sa pagkuha ng mga log sa Elasticsearch. Mayroong dalawang paraan:

  1. Kinukuha ng Logstash ang mga log mula sa Kafka. Maaaring kunin ang malinis o i-convert sa iyong tabi.
  2. May nagsusulat mismo sa Elasticsearch, halimbawa, isang APM server.

Isaalang-alang ang isang halimbawa ng pamamahala ng mga index sa pamamagitan ng Logstash. Lumilikha ito ng isang index at nalalapat dito pattern ng index at katumbas ILM.

k8s-ingress.conf

input {
    kafka {
        bootstrap_servers => "node01, node02, node03"
        topics => ["ingress-k8s"]
        decorate_events => false
        codec => "json"
    }
}

filter {
    ruby {
        path => "/etc/logstash/conf.d/k8s-normalize.rb"
    }
    if [log] =~ "[warn]" or [log] =~ "[error]" or [log] =~ "[notice]" or [log] =~ "[alert]" {
        grok {
            match => { "log" => "%{DATA:[nginx][error][time]} [%{DATA:[nginx][error][level]}] %{NUMBER:[nginx][error][pid]}#%{NUMBER:[nginx][error][tid]}: *%{NUMBER:[nginx][error][connection_id]} %{DATA:[nginx][error][message]}, client: %{IPORHOST:[nginx][error][remote_ip]}, server: %{DATA:[nginx][error][server]}, request: "%{WORD:[nginx][error][method]} %{DATA:[nginx][error][url]} HTTP/%{NUMBER:[nginx][error][http_version]}", (?:upstream: "%{DATA:[nginx][error][upstream][proto]}://%{DATA:[nginx][error][upstream][host]}:%{DATA:[nginx][error][upstream][port]}/%{DATA:[nginx][error][upstream][url]}", )?host: "%{DATA:[nginx][error][host]}"(?:, referrer: "%{DATA:[nginx][error][referrer]}")?" }
            remove_field => "log"
        }
    }
    else {
        grok {
            match => { "log" => "%{IPORHOST:[nginx][access][host]} - [%{IPORHOST:[nginx][access][remote_ip]}] - %{DATA:[nginx][access][remote_user]} [%{HTTPDATE:[nginx][access][time]}] "%{WORD:[nginx][access][method]} %{DATA:[nginx][access][url]} HTTP/%{NUMBER:[nginx][access][http_version]}" %{NUMBER:[nginx][access][response_code]} %{NUMBER:[nginx][access][bytes_sent]} "%{DATA:[nginx][access][referrer]}" "%{DATA:[nginx][access][agent]}" %{NUMBER:[nginx][access][request_lenght]} %{NUMBER:[nginx][access][request_time]} [%{DATA:[nginx][access][upstream][name]}] (?:-|%{IPORHOST:[nginx][access][upstream][addr]}:%{NUMBER:[nginx][access][upstream][port]}) (?:-|%{NUMBER:[nginx][access][upstream][response_lenght]}) %{DATA:[nginx][access][upstream][response_time]} %{DATA:[nginx][access][upstream][status]} %{DATA:[nginx][access][request_id]}" }
            remove_field => "log"
        }
    }
}
output {
    elasticsearch {
        id => "k8s-ingress"
        hosts => ["node01", "node02", "node03", "node04", "node05", "node06", "node07", "node08"]
        manage_template => true # Π²ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ ΡƒΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ шаблонами
        template_name => "k8s-ingress" # имя примСняСмого шаблона
        ilm_enabled => true # Π²ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ ΡƒΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ ILM
        ilm_rollover_alias => "k8s-ingress" # alias для записи Π² индСксы, Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±Ρ‹Ρ‚ΡŒ ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹ΠΌ
        ilm_pattern => "{now/d}-000001" # шаблон для создания индСксов, ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΠ°ΠΊ "{now/d}-000001" Ρ‚Π°ΠΊ ΠΈ "000001"
        ilm_policy => "k8s-ingress" # ΠΏΠΎΠ»ΠΈΡ‚ΠΈΠΊΠ° прикрСпляСмая ΠΊ индСксу
        index => "k8s-ingress-%{+YYYY.MM.dd}" # Π½Π°Π·Π²Π°Π½ΠΈΠ΅ создаваСмого индСкса, ΠΌΠΎΠΆΠ΅Ρ‚ ΡΠΎΠ΄Π΅Ρ€ΠΆΠ°Ρ‚ΡŒ %{+YYYY.MM.dd}, зависит ΠΎΡ‚ ilm_pattern
    }
}

Pag-setup ng Kibana

Mayroong base pattern na nalalapat sa lahat ng bagong index. Itinatakda nito ang pamamahagi ng mga maiinit na index, ang bilang ng mga shards, replika, atbp. Ang bigat ng template ay tinutukoy ng opsyon order. Ino-override ng mga template na may mas mataas na timbang ang mga kasalukuyang parameter ng template o magdagdag ng mga bago.

Pangmatagalang imbakan ng data sa Elasticsearch
Pangmatagalang imbakan ng data sa Elasticsearch

GET _template/default

{
  "default" : {
    "order" : -1, # вСс шаблона
    "version" : 1,
    "index_patterns" : [
      "*" # примСняСм ΠΊΠΎ всСм индСксам
    ],
    "settings" : {
      "index" : {
        "codec" : "best_compression", # ΡƒΡ€ΠΎΠ²Π΅Π½ΡŒ сТатия
        "routing" : {
          "allocation" : {
            "require" : {
              "box_type" : "hot" # распрСдСляСм Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΏΠΎ горячим Π½ΠΎΠ΄Π°ΠΌ
            },
            "total_shards_per_node" : "8" # максимальноС количСство ΡˆΠ°Ρ€Π΄ΠΎΠ² Π½Π° Π½ΠΎΠ΄Ρƒ ΠΎΡ‚ ΠΎΠ΄Π½ΠΎΠ³ΠΎ индСкса
          }
        },
        "refresh_interval" : "5s", # ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π» обновлСния индСкса
        "number_of_shards" : "8", # количСство ΡˆΠ°Ρ€Π΄ΠΎΠ²
        "auto_expand_replicas" : "0-1", # количСство Ρ€Π΅ΠΏΠ»ΠΈΠΊ Π½Π° Π½ΠΎΠ΄Ρƒ ΠΎΡ‚ ΠΎΠ΄Π½ΠΎΠ³ΠΎ индСкса
        "number_of_replicas" : "1" # количСство Ρ€Π΅ΠΏΠ»ΠΈΠΊ
      }
    },
    "mappings" : {
      "_meta" : { },
      "_source" : { },
      "properties" : { }
    },
    "aliases" : { }
  }
}

Pagkatapos ay ilapat ang pagmamapa sa mga index k8s-ingress-* gamit ang isang template na may mas mataas na timbang.

Pangmatagalang imbakan ng data sa Elasticsearch
Pangmatagalang imbakan ng data sa Elasticsearch

GET _template/k8s-ingress

{
  "k8s-ingress" : {
    "order" : 100,
    "index_patterns" : [
      "k8s-ingress-*"
    ],
    "settings" : {
      "index" : {
        "lifecycle" : {
          "name" : "k8s-ingress",
          "rollover_alias" : "k8s-ingress"
        },
        "codec" : "best_compression",
        "routing" : {
          "allocation" : {
            "require" : {
              "box_type" : "hot"
            }
          }
        },
        "number_of_shards" : "8",
        "number_of_replicas" : "1"
      }
    },
    "mappings" : {
      "numeric_detection" : false,
      "_meta" : { },
      "_source" : { },
      "dynamic_templates" : [
        {
          "all_fields" : {
            "mapping" : {
              "index" : false,
              "type" : "text"
            },
            "match" : "*"
          }
        }
      ],
      "date_detection" : false,
      "properties" : {
        "kubernetes" : {
          "type" : "object",
          "properties" : {
            "container_name" : {
              "type" : "keyword"
            },
            "container_hash" : {
              "index" : false,
              "type" : "keyword"
            },
            "host" : {
              "type" : "keyword"
            },
            "annotations" : {
              "type" : "object",
              "properties" : {
                "value" : {
                  "index" : false,
                  "type" : "text"
                },
                "key" : {
                  "index" : false,
                  "type" : "keyword"
                }
              }
            },
            "docker_id" : {
              "index" : false,
              "type" : "keyword"
            },
            "pod_id" : {
              "type" : "keyword"
            },
            "labels" : {
              "type" : "object",
              "properties" : {
                "value" : {
                  "type" : "keyword"
                },
                "key" : {
                  "type" : "keyword"
                }
              }
            },
            "namespace_name" : {
              "type" : "keyword"
            },
            "pod_name" : {
              "type" : "keyword"
            }
          }
        },
        "@timestamp" : {
          "type" : "date"
        },
        "nginx" : {
          "type" : "object",
          "properties" : {
            "access" : {
              "type" : "object",
              "properties" : {
                "agent" : {
                  "type" : "text"
                },
                "response_code" : {
                  "type" : "integer"
                },
                "upstream" : {
                  "type" : "object",
                  "properties" : {
                    "port" : {
                      "type" : "keyword"
                    },
                    "name" : {
                      "type" : "keyword"
                    },
                    "response_lenght" : {
                      "type" : "integer"
                    },
                    "response_time" : {
                      "index" : false,
                      "type" : "text"
                    },
                    "addr" : {
                      "type" : "keyword"
                    },
                    "status" : {
                      "index" : false,
                      "type" : "text"
                    }
                  }
                },
                "method" : {
                  "type" : "keyword"
                },
                "http_version" : {
                  "type" : "keyword"
                },
                "bytes_sent" : {
                  "type" : "integer"
                },
                "request_lenght" : {
                  "type" : "integer"
                },
                "url" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword"
                    }
                  }
                },
                "remote_user" : {
                  "type" : "text"
                },
                "referrer" : {
                  "type" : "text"
                },
                "remote_ip" : {
                  "type" : "ip"
                },
                "request_time" : {
                  "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis||dd/MMM/YYYY:H:m:s Z",
                  "type" : "date"
                },
                "host" : {
                  "type" : "keyword"
                },
                "time" : {
                  "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis||dd/MMM/YYYY:H:m:s Z",
                  "type" : "date"
                }
              }
            },
            "error" : {
              "type" : "object",
              "properties" : {
                "server" : {
                  "type" : "keyword"
                },
                "upstream" : {
                  "type" : "object",
                  "properties" : {
                    "port" : {
                      "type" : "keyword"
                    },
                    "proto" : {
                      "type" : "keyword"
                    },
                    "host" : {
                      "type" : "keyword"
                    },
                    "url" : {
                      "type" : "text",
                      "fields" : {
                        "keyword" : {
                          "type" : "keyword"
                        }
                      }
                    }
                  }
                },
                "method" : {
                  "type" : "keyword"
                },
                "level" : {
                  "type" : "keyword"
                },
                "http_version" : {
                  "type" : "keyword"
                },
                "pid" : {
                  "index" : false,
                  "type" : "integer"
                },
                "message" : {
                  "type" : "text"
                },
                "tid" : {
                  "index" : false,
                  "type" : "keyword"
                },
                "url" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword"
                    }
                  }
                },
                "referrer" : {
                  "type" : "text"
                },
                "remote_ip" : {
                  "type" : "ip"
                },
                "connection_id" : {
                  "index" : false,
                  "type" : "keyword"
                },
                "host" : {
                  "type" : "keyword"
                },
                "time" : {
                  "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis||dd/MMM/YYYY:H:m:s Z",
                  "type" : "date"
                }
              }
            }
          }
        },
        "log" : {
          "type" : "text"
        },
        "@version" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "ignore_above" : 256,
              "type" : "keyword"
            }
          }
        },
        "eventtime" : {
          "type" : "float"
        }
      }
    },
    "aliases" : { }
  }
}

Pagkatapos ilapat ang lahat ng mga template, inilalapat namin ang patakaran ng ILM at sinimulan naming subaybayan ang buhay ng mga index.

Pangmatagalang imbakan ng data sa Elasticsearch

Pangmatagalang imbakan ng data sa Elasticsearch

Pangmatagalang imbakan ng data sa Elasticsearch

GET _ilm/policy/k8s-ingress

{
  "k8s-ingress" : {
    "version" : 14,
    "modified_date" : "2020-06-11T10:27:01.448Z",
    "policy" : {
      "phases" : {
        "warm" : { # тСплая Ρ„Π°Π·Π°
          "min_age" : "5d", # срок ΠΆΠΈΠ·Π½ΠΈ индСкса послС Ρ€ΠΎΡ‚Π°Ρ†ΠΈΠΈ Π΄ΠΎ наступлСния Ρ‚Π΅ΠΏΠ»ΠΎΠΉ Ρ„Π°Π·Ρ‹
          "actions" : {
            "allocate" : {
              "include" : { },
              "exclude" : { },
              "require" : {
                "box_type" : "warm" # ΠΊΡƒΠ΄Π° ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π°Π΅ΠΌ индСкс
              }
            },
            "shrink" : {
              "number_of_shards" : 4 # ΠΎΠ±Ρ€Π΅Π·Π°Π½ΠΈΠ΅ индСксов, Ρ‚.ΠΊ. Ρƒ нас 4 Π½ΠΎΠ΄Ρ‹
            }
          }
        },
        "cold" : { # холодная Ρ„Π°Π·Π°
          "min_age" : "25d", # срок ΠΆΠΈΠ·Π½ΠΈ индСкса послС Ρ€ΠΎΡ‚Π°Ρ†ΠΈΠΈ Π΄ΠΎ наступлСния Ρ…ΠΎΠ»ΠΎΠ΄Π½ΠΎΠΉ Ρ„Π°Π·Ρ‹
          "actions" : {
            "allocate" : {
              "include" : { },
              "exclude" : { },
              "require" : {
                "box_type" : "cold" # ΠΊΡƒΠ΄Π° ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π°Π΅ΠΌ индСкс
              }
            },
            "freeze" : { } # Π·Π°ΠΌΠΎΡ€Π°ΠΆΠΈΠ²Π°Π΅ΠΌ для ΠΎΠΏΡ‚ΠΈΠΌΠΈΠ·Π°Ρ†ΠΈΠΈ
          }
        },
        "hot" : { # горячая Ρ„Π°Π·Π°
          "min_age" : "0ms",
          "actions" : {
            "rollover" : {
              "max_size" : "50gb", # ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ Ρ€Π°Π·ΠΌΠ΅Ρ€ индСкса Π΄ΠΎ Ρ€ΠΎΡ‚Π°Ρ†ΠΈΠΈ (Π±ΡƒΠ΄Π΅Ρ‚ Ρ…2, Ρ‚.ΠΊ. Π΅ΡΡ‚ΡŒ 1 Ρ€Π΅ΠΏΠ»ΠΈΠΊΠ°)
              "max_age" : "1d" # ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ срок ΠΆΠΈΠ·Π½ΠΈ индСкса Π΄ΠΎ Ρ€ΠΎΡ‚Π°Ρ†ΠΈΠΈ
            },
            "set_priority" : {
              "priority" : 100
            }
          }
        },
        "delete" : { # Ρ„Π°Π·Π° удалСния
          "min_age" : "120d", # ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ срок ΠΆΠΈΠ·Π½ΠΈ послС Ρ€ΠΎΡ‚Π°Ρ†ΠΈΠΈ ΠΏΠ΅Ρ€Π΅Π΄ ΡƒΠ΄Π°Π»Π΅Π½ΠΈΠ΅ΠΌ
          "actions" : {
            "delete" : { }
          }
        }
      }
    }
  }
}

Mga Problema

Nagkaroon ng mga problema sa yugto ng pag-setup at pag-debug.

Mainit na yugto

Para sa tamang pag-ikot ng mga indeks, ang presensya sa dulo ay kritikal index_name-date-000026 mga numero ng format 000001. May mga linya sa code na nagsusuri ng mga index gamit ang isang regular na expression para sa pagkakaroon ng mga numero sa dulo. Kung hindi, magkakaroon ng error, walang mga patakarang ilalapat sa index, at palagi itong nasa mainit na yugto.

Mainit na yugto

Pag-urong (cutoff) β€” binabawasan ang bilang ng mga shards, dahil mayroon kaming 4 na node sa mainit at malamig na yugto. Ang dokumentasyon ay naglalaman ng mga sumusunod na linya:

  • Ang index ay dapat na read-only.
  • Ang isang kopya ng bawat shard sa index ay dapat na nasa parehong node.
  • Ang kalagayan ng kalusugan ng cluster ay dapat na berde.

Upang putulin ang isang index, inililipat ng Elasticsearch ang lahat ng pangunahing shards sa isang node, kino-duplicate ang pinutol na index gamit ang mga kinakailangang parameter, at pagkatapos ay tatanggalin ang luma. Parameter total_shards_per_node dapat na katumbas o mas malaki kaysa sa bilang ng mga pangunahing shards upang magkasya sa isang node. Kung hindi, magkakaroon ng mga notification at ang mga shards ay hindi lilipat sa tamang mga node.

Pangmatagalang imbakan ng data sa Elasticsearch
Pangmatagalang imbakan ng data sa Elasticsearch

GET /shrink-k8s-ingress-2020.06.06-000025/_settings

{
  "shrink-k8s-ingress-2020.06.06-000025" : {
    "settings" : {
      "index" : {
        "refresh_interval" : "5s",
        "auto_expand_replicas" : "0-1",
        "blocks" : {
          "write" : "true"
        },
        "provided_name" : "shrink-k8s-ingress-2020.06.06-000025",
        "creation_date" : "1592225525569",
        "priority" : "100",
        "number_of_replicas" : "1",
        "uuid" : "psF4MiFGQRmi8EstYUQS4w",
        "version" : {
          "created" : "7060299",
          "upgraded" : "7060299"
        },
        "lifecycle" : {
          "name" : "k8s-ingress",
          "rollover_alias" : "k8s-ingress",
          "indexing_complete" : "true"
        },
        "codec" : "best_compression",
        "routing" : {
          "allocation" : {
            "initial_recovery" : {
              "_id" : "_Le0Ww96RZ-o76bEPAWWag"
            },
            "require" : {
              "_id" : null,
              "box_type" : "cold"
            },
            "total_shards_per_node" : "8"
          }
        },
        "number_of_shards" : "4",
        "routing_partition_size" : "1",
        "resize" : {
          "source" : {
            "name" : "k8s-ingress-2020.06.06-000025",
            "uuid" : "gNhYixO6Skqi54lBjg5bpQ"
          }
        }
      }
    }
  }
}

Malamig na yugto

Magyelo (freeze) - Ni-freeze namin ang index upang i-optimize ang mga query sa dating data.

Ang mga paghahanap na isinagawa sa mga nakapirming indeks ay gumagamit ng maliit, nakatuon, search_throttled na threadpool upang kontrolin ang bilang ng mga kasabay na paghahanap na tumama sa mga nakapirming shard sa bawat node. Nililimitahan nito ang dami ng dagdag na memorya na kinakailangan para sa lumilipas na mga istruktura ng data na tumutugma sa mga nakapirming shards, na dahil dito ay pinoprotektahan ang mga node laban sa labis na pagkonsumo ng memorya.
Ang mga frozen na indeks ay read-only: hindi ka maaaring mag-index sa mga ito.
Ang mga paghahanap sa mga nakapirming indeks ay inaasahang mabagal. Ang mga frozen na indeks ay hindi inilaan para sa mataas na load ng paghahanap. Posible na ang paghahanap ng isang nakapirming index ay maaaring tumagal ng ilang segundo o minuto upang makumpleto, kahit na ang parehong mga paghahanap ay nakumpleto sa millisecond kapag ang mga indeks ay hindi na-freeze.

Mga resulta ng

Natutunan namin kung paano maghanda ng mga node para sa pagtatrabaho sa ILM, mag-set up ng template para sa pamamahagi ng mga shards sa mga maiinit na node, at mag-set up ng ILM para sa isang index sa lahat ng yugto ng buhay.

Kapaki-pakinabang na mga link

Pinagmulan: www.habr.com