首页 > 其他分享 >基于 K8S 搭建自己的 ELK 服务

基于 K8S 搭建自己的 ELK 服务

时间:2023-10-05 16:22:21浏览次数:43  
标签:ELK name metadata kibana elasticsearch K8S config logstash 搭建

基于 K8S(K3S) 搭建自己的 ELK 服务

对应的 Yaml 资源在 https://github.com/nicelizhi/k8s-elk

elasticsearch 服务

Service

kind: Service
apiVersion: v1
metadata:
  name: elasticsearch
spec:
  ports:
    - name: elasticsearch
      protocol: TCP
      port: 9200
      targetPort: 9200
  selector:
    app: elasticsearch
  type: ClusterIP
  sessionAffinity: None

ConfigMap

kind: ConfigMap
apiVersion: v1
metadata:
  name: elasticsearch-config
data:
  elasticsearch.yml: |
    network.host: 0.0.0.0
    xpack.monitoring.collection.enabled: true
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    xpack.security.enabled: true
    xpack.security.authc.api_key.enabled: true

Deployment

kind: Deployment
apiVersion: apps/v1
metadata:
  name: elasticsearch
spec:
  replicas: 1
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:
      volumes:
        - name: config
          configMap:
            name: elasticsearch-config
            defaultMode: 420
        - name: es-data
          hostPath:
            path: /data/es
      initContainers:
        - name: increase-vm-max-map
          image: busybox
          command:
            - sysctl
            - '-w'
            - vm.max_map_count=262144
          securityContext:
            privileged: true
      containers:
        - name: elasticsearch
          image: 'docker.elastic.co/elasticsearch/elasticsearch:7.16.0'
          resources:
            requests:
              memory: 1524Mi
              cpu: 500m
            limits:
              memory: 1824Mi
              cpu: 1
          ports:
            - containerPort: 9200
              protocol: TCP
            - containerPort: 9300
              protocol: TCP
          env:
            - name: ES_JAVA_OPTS
              value: '-Xms256m -Xmx256m'
            - name: discovery.type
              value: single-node
          volumeMounts:
            - name: config
              mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
              subPath: elasticsearch.yml
            - mountPath: /usr/share/elasticsearch/data/
              name: es-data

上面的资源有硬盘挂载与 config 类型的使用

kibana 服务

Service

kind: Service
apiVersion: v1
metadata:
  name: kibana
spec:
  ports:
    - name: kibana
      protocol: TCP
      port: 5601
      targetPort: 5601
  selector:
    component: kibana
  type: LoadBalancer

ConfigMap

kind: ConfigMap
apiVersion: v1
metadata:
  name: kibana-config
data:
  kibana.yml: >
    server.name: kibana

    server.host: 0.0.0.0

    elasticsearch.hosts: ["http://elasticsearch:9200" ]
    
    elasticsearch.username: "elastic"

    monitoring.ui.container.elasticsearch.enabled: true

Deployment

kind: Deployment
apiVersion: apps/v1
metadata:
  name: kibana
spec:
  replicas: 1
  selector:
    matchLabels:
      component: kibana
  template:
    metadata:
      labels:
        component: kibana
    spec:
      volumes:
        - name: config
          configMap:
            name: kibana-config
            defaultMode: 420
        - name: secrets
          secret:
            secretName: es-user-pass
            defaultMode: 0400
      containers:
        - name: elk-kibana
          image: 'docker.elastic.co/kibana/kibana:7.16.0'
          resources:
            requests:
              memory: 512Mi
              cpu: 200m
            limits:
              memory: 1Gi
              cpu: 1
          ports:
            - name: kibana
              containerPort: 5601
              protocol: TCP
          env:
            - name: KIBANA_SYSTEM_PASSWORD
              valueFrom: 
                secretKeyRef: 
                  name: es-user-pass
                  key: password
            - name: ELASTICSEARCH_PASSWORD
              valueFrom: 
                secretKeyRef: 
                  name: es-user-pass
                  key: password
          volumeMounts:
            - name: config
              mountPath: /usr/share/kibana/config/kibana.yml
              subPath: kibana.yml

configMap 的配置使用与 Secret内容的使用

logstash 服务

Deployment

kind: Deployment
apiVersion: apps/v1
metadata:
  name: logstash
spec:
  replicas: 1
  selector:
    matchLabels:
      app: logstash
  template:
    metadata:
      labels:
        app: logstash
    spec:
      volumes:
        - name: config
          configMap:
            name: logstash-config
            defaultMode: 420
        - name: pipelines
          configMap:
            name: logstash-pipelines
            defaultMode: 420
      containers:
        - name: logstash
          image: 'docker.elastic.co/logstash/logstash:7.16.0'
          resources:
            requests:
              memory: 512Mi
              cpu: 500m
            limits:
              memory: 1024Mi
              cpu: 1
          ports:
            - containerPort: 5044
              protocol: TCP
            - containerPort: 5000
              protocol: TCP
            - containerPort: 5000
              protocol: UDP
            - containerPort: 9600
              protocol: TCP
          env:
            - name: ELASTICSEARCH_HOST
              value: 'http://elasticsearch:9200'
            - name: LS_JAVA_OPTS
              value: '-Xms512m -Xmx512m'
          volumeMounts:
            - name: pipelines
              mountPath: /usr/share/logstash/pipeline
            - name: config
              mountPath: /usr/share/logstash/config/logstash.yml
              subPath: logstash.yml

Service

kind: Service
apiVersion: v1
metadata:
  name: logstash
spec:
  ports:
    - name: logstash
      protocol: TCP
      port: 10000
      targetPort: 9600
    - name: filebeat
      protocol: TCP
      port: 5044
      targetPort: 5044
  selector:
    app: logstash
  type: LoadBalancer
  sessionAffinity: None

ConfigMap

kind: ConfigMap
apiVersion: v1
metadata:
  name: logstash-config
  namespace: default
data:
  logstash.yml: >
    http.host: "0.0.0.0"

    xpack.monitoring.enabled: true

    config.reload.automatic: true

    xpack.monitoring.elasticsearch.hosts: ["elasticsearch:9200" ]

    xpack.monitoring.elasticsearch.username: "elastic"

    xpack.monitoring.elasticsearch.password: "abc123456"

ConfigMap piple

kind: ConfigMap
apiVersion: v1
metadata:
  name: logstash-pipelines
data:
  logstash.conf: |
    input {
      syslog {
        type => "syslog"
        port => 5044
      }
    }
    filter {
       grok {
          match => ["message", "%{SYSLOG5424PRI}%{NONNEGINT:syslog5424_ver} +(?:%{TIMESTAMP_ISO8601:timestamp}|-) +(?:%{HOSTNAME:heroku_drain_id}|-) +(?:%{WORD:heroku_source}|-) +(?:%{DATA:heroku_dyno}|-) +(?:%{WORD:syslog5424_msgid}|-) +(?:%{SYSLOG5424SD:syslog5424_sd}|-|) +%{GREEDYDATA:heroku_message}"]
        }
        mutate { rename => ["heroku_message", "message"] }
        kv { source => "message" }
        mutate { convert => ["sample#memory-free", "integer"]}
        mutate { convert => ["sample#memory-total", "integer"]}
        mutate { convert => ["sample#memory-redis", "integer"]}
        mutate { convert => ["sample#memory-cached", "integer"]}
        mutate { convert => ["sample#load-avg-5m", "float"]}
        mutate { convert => ["sample#load-avg-1m", "float"]}
        mutate { convert => ["sample#load-avg-15m", "float"]}
        syslog_pri { syslog_pri_field_name => "syslog5424_pri" }
    }
    output {
      elasticsearch {
        hosts => ["http://elasticsearch:9200"]
        "user" => "elastic"
        "password" => "abc123456"
        "index" => "logstash-%{heroku_dyno}"
         template_overwrite => true
      }
    }

pipe 日志处理了 Heroku 平台日志收集。并且配置了pipe的自动加载动作,这块也是实际应用中经常应用到的功能
针对与日志收集的过程中可以使用 grok 去做必要的格式转化,从而使的日志安装您的要求保存到ES 服务器中使用。

上面示例是运行在一台 2核 4G 的服务器上面,所以我们使用的是K3S 架构,并且在 ES 上只使用了一个节点数据,这些在正式的使用过程中需要多留意。

标签:ELK,name,metadata,kibana,elasticsearch,K8S,config,logstash,搭建
From: https://www.cnblogs.com/cnphp/p/howto-deployment-elk-service-use-k83.html

相关文章

  • Ubuntu 20.04 搭建 Timemachine
    创建一个目录,作为TimeMachine保存数据的目录。$sudomkdir/usr/local/timemachine$sudochownnobody:nogroup/usr/local/timemachine$sudochmod777/usr/local/timemachine安装netatalk服务和avahi-daemon服务。$sudoaptinstallnetatalkavahi-daemon编辑net......
  • redis主从复制基础上搭建哨兵模式
    假如156和157是不同的两台服务器两台redis主从复制基础上搭建哨兵模式如下156redis.confmasterauth123456bind0.0.0.0requirement123456daemonizeyessentinel.confsentinelauth-passmymaster123456sentinelmonitormymaster10.190.107.15663792157......
  • 使用ensp搭建路由拓扑,并使用isis协议实现网络互通实操
    转载请注明出处:1.通过拓扑搭建如下拓扑:               其中R7、R8为L1,R6为L1/2,R9为L2。2.配置isis实现网络互通R7配置如下:[Huawei]isis1[Huawei-isis-1]dith#isis1is-levellevel-1network-entity10.0000.0000.0001.00#r......
  • MySQL-8.0.18的搭建
    一、安装mysql-8.0.18(1)下载安装包#下载前先卸载mariadb的一些东西yumremove-ymariadb*#然后下载安装包mkdir/usr/local/soft&&cd/usr/local/softwget-chttps://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz(2)......
  • Docker搭建Mysql主从机制
    Mysql主从复制1基础准备由于家境贫寒没有那么多的云资源供我操作,只能使用docker进行模拟了。拉取镜像简单得很就先不谈了。直接开整。以下操作基于mysql:5.7进行一主二从配置。2主库配置运行容器dockerrun-p3306:3306--namemysql-slaver-2-eMYSQL_ROOT_PASSWOR......
  • 搭建Springboot+Vue+Element的简单系统流程
    今天研究了一下如何把Springboot+Mybatis和Vue+Element结合起来使用详细写一篇博客来记录一下流程吧,因为途中发现了很多的问题首先,创建Springboot项目,惯例添加依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="htt......
  • 免费服务器搭建网盘教程,给电脑挂载500G磁盘
    免费服务器搭建网盘教程,给电脑挂载500G磁盘免费搭建网盘教程,给电脑挂载500G磁盘请勿注册下载,注册下载是空白文件,使用免登录下载免费搭建网盘教程,给电脑挂载500G磁盘教程截图:其他按照下载教程操作教程代码:下载下来的文件pancn文件拖到您创建的容器手机的话点击newfile上传文件按......
  • 主从+哨兵搭建注意实现
    Redis主从复制模式下,一旦主节点出现了故障不可达,需要人工干预进行故障转移,无论对于Redis的应用方还是运维方都带来了很大的不便。对于应用方来说无法及时感知到主节点的变化,必然会造成一定的写数据丢失和读数据错误,甚至可能造成应用方服务不可用。对于Redis的运维方来说,整个故障......
  • k8s1.25安装
    环境初始化yuminstallbash-completionvimntpdateiptableslrzszepel-release-y&&execbashsystemctlstopfirewalldsystemctldisabledfirewalldsetenforce0sed-i's/=enforcing/=disabled/g'/etc/selinux/configdocker#step1:安......
  • 使用J4125主机搭建个人微型服务器
    使用J4125主机搭建个人微型服务器对于个人开发者而言,一个稳定可靠的服务器通常是不可或缺的。然而,云服务器的价格却让许多人望而却步。我曾通过白嫖阿里云服务提供给学生的六个月(?)免费公网服务器搭建WEB服务,在其已然过期许久的今天,我选择了一个经济且足够运行虚拟化的解决方案—......