日志采集(11)

在Kubernetes等平臺上進(jìn)行任何大型部署時,日志記錄都是主要的挑戰(zhàn)之一,但是配置和維護(hù)用于日志收集的中央存儲庫可以簡化日常操作。為此,F(xiàn)luentd、Elasticsearch和Kibana的組合可以在Kubernetes集群上創(chuàng)建一個強大的日志記錄層

1. 部署es集群

由于前面部署的應(yīng)用大部分都部署到k8s中的,在將es部署在k8s集群中電腦有點扛不住了,所以這里單獨起三臺虛擬機來部署es集群(一般也是單獨部署)。

  1. 準(zhǔn)備
    1. vm配置:1核 1G 20G硬盤

    2. 網(wǎng)絡(luò):

       192.168.241.150 es-node1
       192.168.241.151 es-node2
       192.168.241.152 es-node3
      
    3. 創(chuàng)建一個新的用戶,es不能使用root用戶啟動

       adduser hadoop 
      
  2. 基礎(chǔ)配置(Linux限制設(shè)置)
    1. 修改vm.max_map_count參數(shù)

       sudo vi /etc/sysctl.conf
      
       # elasticsearch config start
       vm.max_map_count=262144
       # elasticsearch config end
      
    2. 修改文件限制和最大線程數(shù)限制

       sudo vi /etc/security/limits.conf
       
       # elasticsearch config start
       * soft nofile 65536
       * hard nofile 131072
       * soft nproc 2048
       * hard nproc 4096
       # elasticsearch config end
      
  3. 下載安裝es
    1. 下載解壓

       curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-linux-x86_64.tar.gz
       sudo tar -zxvf elasticsearch-7.2.0-linux-x86_64.tar.gz -C /opt/
      
    2. 配置es

       vi config/elasticsearch.yml
      
       # ---------------------------------- Cluster -----------------------------------
       #
       # Use a descriptive name for your cluster: 設(shè)置集群的名稱
       #
       cluster.name: cluster
       #
       # ------------------------------------ Node ------------------------------------
       #
       # Use a descriptive name for the node: 設(shè)置當(dāng)前節(jié)點的名稱,其他節(jié)點需要修改
       #
       node.name: node-1
       # ----------------------------------- Paths ------------------------------------
       #
       # Path to directory where to store the data (separate multiple locations by comma): 設(shè)置數(shù)據(jù)目錄
       #
       path.data: /opt/elasticsearch/data
       #
       # Path to log files: 設(shè)置日志目錄
       #
       path.logs: /opt/elasticsearch/logs
       # ---------------------------------- Network -----------------------------------
       #
       # Set the bind address to a specific IP (IPv4 or IPv6): 設(shè)置暴露的IP地址,本機IP地址,其他節(jié)點需要修改
       #
       network.host: 192.168.241.150
       #
       # Set a custom port for HTTP:
       #
       #http.port: 9200
       #
       # For more information, consult the network module documentation.
       #
       # --------------------------------- Discovery ----------------------------------
       #
       # Pass an initial list of hosts to perform discovery when this node is started: 設(shè)置集群中的其它節(jié)點(想對于當(dāng)前節(jié)點而言),單播列表,并不需要配置集群所有節(jié)點
       # The default list of hosts is ["127.0.0.1", "[::1]"]
       #
       discovery.seed_hosts: ["es-node2", "es-node3"]
       #
       # Bootstrap the cluster using an initial set of master-eligible nodes: 候選master節(jié)點,需要和node.name屬性相同
       #
       cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
       #
       # For more information, consult the discovery and cluster formation module documentation.
      
    3. 配置JVM參數(shù)

       vi config/jvm.options
       # Xms represents the initial size of total heap space
       # Xmx represents the maximum size of total heap space
       # Set Xmx and Xms to no more than 50% of your physical RAM.
       
       -Xms512m
       -Xmx512m
      
    4. 啟動

       bin/elasticsearch -d # 后臺啟動
       # 記錄PID并且后臺啟動
       ./bin/elasticsearch -p /tmp/elasticsearch-pid -d
      
    5. 編寫啟動腳本,方便使用

       #!/bin/bash
      
       ES_HOME=/opt/elasticsearch
       ES_PID_FILE=/tmp/elasticsearch-pid
       
       action=$1
       
       case $action in
          'start')
               if [ -e $ES_PID_FILE ]
               then
                   echo 'es already started!'
               else
                   echo 'starting es begin......'
       
                   $ES_HOME/bin/elasticsearch -p /tmp/elasticsearch-pid -d
                   sleep 2
                   echo 'start es success.......'
               fi
          ;;
          'stop')
               if [ -e $ES_PID_FILE ]
               then
                   echo 'stopping es begin......'
       
                   es_pid=`cat /tmp/elasticsearch-pid`
                   kill -9 $es_pid
                   sleep 3
                   rm /tmp/elasticsearch-pid
                   echo 'stop es success........'
               else
                   echo "es dosen't start!!"
               fi
          ;;
          *)
               echo 'not support!!'
          ;;
       esac
      

2. 部署kibana

  1. 下載解壓

     curl -O https://artifacts.elastic.co/downloads/kibana/kibana-7.2.0-linux-x86_64.tar.gz
     sudo tar -zxvf kibana-7.2.0-linux-x86_64.tar.gz -C /opt/
    
  2. 配置

     vi kibana/config/kibana.yml
    
     # Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
     # The default is 'localhost', which usually means remote machines will not be able to connect.
     # To allow connections from remote users, set this parameter to a non-loopback address.
     server.host: "192.168.241.150"
    
     # The URLs of the Elasticsearch instances to use for all your queries.
     elasticsearch.hosts: ["http://es-node1:9200","http://es-node2:9200","http://es-node3:9200"]
    
  3. 啟動

     ./bin/kibana
    
  4. 停止

     # 先獲取PID
     ps -ef | grep java
     # 暫停
     kill -9 PID
    
  5. 訪問:http://kibana.tlh.com:5601

    1.png

3. 再k8s中部署fluentd

  1. 關(guān)于k8s中日志采集的方案可以參照官方文檔說明,這里采用node loggin agent的方式來處理。

    2.png
  2. Fulentd說明

    1. 對容器化的應(yīng)用日志處理建議

      1. The easiest and most embraced logging method for containerized applications is to write to the standard output and standard error streams.
      2. Logs should have a separate storage and lifecycle independent of nodes, pods, or containers.
    2. 日志采集流程

       in_tail -> filter_grep -> out_stdout
      
    3. Event數(shù)據(jù)結(jié)構(gòu)說明:

      1. tag:消息從哪里來的
      2. time:時間
      3. record:log的內(nèi)容
  3. 下載

     git clone https://github.com/fluent/fluentd-kubernetes-daemonset
    
  4. 修改es的鏈接配置,修改fluentd-kubernetes-daemonset文件夾中的fluentd-daemonset-elasticsearch-rbac.yaml的deployment

     spec:
       selector:
         matchLabels:
           k8s-app: fluentd-logging
           version: v1
       template:
         metadata:
           labels:
             k8s-app: fluentd-logging
             version: v1
         spec:
           serviceAccount: fluentd
           serviceAccountName: fluentd
           tolerations:
           - key: node-role.kubernetes.io/master
             effect: NoSchedule
           containers:
           - name: fluentd
             image: fluent/fluentd-kubernetes-daemonset:v1-debian-elasticsearch
             env:
               - name:  FLUENT_ELASTICSEARCH_HOST
                 value: "192.168.241.150"            # 修改為es的主機名或者IP地址
               - name:  FLUENT_ELASTICSEARCH_PORT
                 value: "9200"
               - name: FLUENT_ELASTICSEARCH_SCHEME
                 value: "http"
               # X-Pack Authentication
               # =====================
               - name: FLUENT_ELASTICSEARCH_USER
                 value: "elastic"
               - name: FLUENT_ELASTICSEARCH_PASSWORD
                 value: "changeme"
    
  5. 安裝

     kubectl apply -f fluentd-daemonset-elasticsearch-rbac.yaml
    

4.通過kibanna創(chuàng)建index pattern對采集到的日志進(jìn)行索引

  1. 登陸kibana--->management--->Index Patterns點擊創(chuàng)建

    3.png
  2. 查看

    4.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,488評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,034評論 3 414
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,327評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,554評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,337評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,883評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,975評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,114評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,625評論 1 332
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,555評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,737評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,244評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 43,973評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,362評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,615評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,343評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,699評論 2 370

推薦閱讀更多精彩內(nèi)容