基于 K8S 搭建自己的 ELK 服务

打印 上一主题 下一主题

主题 986|帖子 986|积分 2960


基于 K8S(K3S) 搭建自己的 ELK 服务
对应的 Yaml 资源在 https://github.com/nicelizhi/k8s-elk
elasticsearch 服务

Service
  1. kind: Service
  2. apiVersion: v1
  3. metadata:
  4.   name: elasticsearch
  5. spec:
  6.   ports:
  7.     - name: elasticsearch
  8.       protocol: TCP
  9.       port: 9200
  10.       targetPort: 9200
  11.   selector:
  12.     app: elasticsearch
  13.   type: ClusterIP
  14.   sessionAffinity: None
复制代码
ConfigMap
  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4.   name: elasticsearch-config
  5. data:
  6.   elasticsearch.yml: |
  7.     network.host: 0.0.0.0
  8.     xpack.monitoring.collection.enabled: true
  9.     http.cors.enabled: true
  10.     http.cors.allow-origin: "*"
  11.     xpack.security.enabled: true
  12.     xpack.security.authc.api_key.enabled: true
复制代码
Deployment
  1. kind: Deployment
  2. apiVersion: apps/v1
  3. metadata:
  4.   name: elasticsearch
  5. spec:
  6.   replicas: 1
  7.   selector:
  8.     matchLabels:
  9.       app: elasticsearch
  10.   template:
  11.     metadata:
  12.       labels:
  13.         app: elasticsearch
  14.     spec:
  15.       volumes:
  16.         - name: config
  17.           configMap:
  18.             name: elasticsearch-config
  19.             defaultMode: 420
  20.         - name: es-data
  21.           hostPath:
  22.             path: /data/es
  23.       initContainers:
  24.         - name: increase-vm-max-map
  25.           image: busybox
  26.           command:
  27.             - sysctl
  28.             - '-w'
  29.             - vm.max_map_count=262144
  30.           securityContext:
  31.             privileged: true
  32.       containers:
  33.         - name: elasticsearch
  34.           image: 'docker.elastic.co/elasticsearch/elasticsearch:7.16.0'
  35.           resources:
  36.             requests:
  37.               memory: 1524Mi
  38.               cpu: 500m
  39.             limits:
  40.               memory: 1824Mi
  41.               cpu: 1
  42.           ports:
  43.             - containerPort: 9200
  44.               protocol: TCP
  45.             - containerPort: 9300
  46.               protocol: TCP
  47.           env:
  48.             - name: ES_JAVA_OPTS
  49.               value: '-Xms256m -Xmx256m'
  50.             - name: discovery.type
  51.               value: single-node
  52.           volumeMounts:
  53.             - name: config
  54.               mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
  55.               subPath: elasticsearch.yml
  56.             - mountPath: /usr/share/elasticsearch/data/
  57.               name: es-data
复制代码
上面的资源有硬盘挂载与 config 类型的使用
kibana 服务

Service
  1. kind: Service
  2. apiVersion: v1
  3. metadata:
  4.   name: kibana
  5. spec:
  6.   ports:
  7.     - name: kibana
  8.       protocol: TCP
  9.       port: 5601
  10.       targetPort: 5601
  11.   selector:
  12.     component: kibana
  13.   type: LoadBalancer
复制代码
ConfigMap
  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4.   name: kibana-config
  5. data:
  6.   kibana.yml: >
  7.     server.name: kibana
  8.     server.host: 0.0.0.0
  9.     elasticsearch.hosts: ["http://elasticsearch:9200" ]
  10.    
  11.     elasticsearch.username: "elastic"
  12.     monitoring.ui.container.elasticsearch.enabled: true
复制代码
Deployment
  1. kind: Deployment
  2. apiVersion: apps/v1
  3. metadata:
  4.   name: kibana
  5. spec:
  6.   replicas: 1
  7.   selector:
  8.     matchLabels:
  9.       component: kibana
  10.   template:
  11.     metadata:
  12.       labels:
  13.         component: kibana
  14.     spec:
  15.       volumes:
  16.         - name: config
  17.           configMap:
  18.             name: kibana-config
  19.             defaultMode: 420
  20.         - name: secrets
  21.           secret:
  22.             secretName: es-user-pass
  23.             defaultMode: 0400
  24.       containers:
  25.         - name: elk-kibana
  26.           image: 'docker.elastic.co/kibana/kibana:7.16.0'
  27.           resources:
  28.             requests:
  29.               memory: 512Mi
  30.               cpu: 200m
  31.             limits:
  32.               memory: 1Gi
  33.               cpu: 1
  34.           ports:
  35.             - name: kibana
  36.               containerPort: 5601
  37.               protocol: TCP
  38.           env:
  39.             - name: KIBANA_SYSTEM_PASSWORD
  40.               valueFrom:
  41.                 secretKeyRef:
  42.                   name: es-user-pass
  43.                   key: password
  44.             - name: ELASTICSEARCH_PASSWORD
  45.               valueFrom:
  46.                 secretKeyRef:
  47.                   name: es-user-pass
  48.                   key: password
  49.           volumeMounts:
  50.             - name: config
  51.               mountPath: /usr/share/kibana/config/kibana.yml
  52.               subPath: kibana.yml
复制代码
configMap 的配置使用与 Secret内容的使用
logstash 服务

Deployment
  1. kind: Deployment
  2. apiVersion: apps/v1
  3. metadata:
  4.   name: logstash
  5. spec:
  6.   replicas: 1
  7.   selector:
  8.     matchLabels:
  9.       app: logstash
  10.   template:
  11.     metadata:
  12.       labels:
  13.         app: logstash
  14.     spec:
  15.       volumes:
  16.         - name: config
  17.           configMap:
  18.             name: logstash-config
  19.             defaultMode: 420
  20.         - name: pipelines
  21.           configMap:
  22.             name: logstash-pipelines
  23.             defaultMode: 420
  24.       containers:
  25.         - name: logstash
  26.           image: 'docker.elastic.co/logstash/logstash:7.16.0'
  27.           resources:
  28.             requests:
  29.               memory: 512Mi
  30.               cpu: 500m
  31.             limits:
  32.               memory: 1024Mi
  33.               cpu: 1
  34.           ports:
  35.             - containerPort: 5044
  36.               protocol: TCP
  37.             - containerPort: 5000
  38.               protocol: TCP
  39.             - containerPort: 5000
  40.               protocol: UDP
  41.             - containerPort: 9600
  42.               protocol: TCP
  43.           env:
  44.             - name: ELASTICSEARCH_HOST
  45.               value: 'http://elasticsearch:9200'
  46.             - name: LS_JAVA_OPTS
  47.               value: '-Xms512m -Xmx512m'
  48.           volumeMounts:
  49.             - name: pipelines
  50.               mountPath: /usr/share/logstash/pipeline
  51.             - name: config
  52.               mountPath: /usr/share/logstash/config/logstash.yml
  53.               subPath: logstash.yml
复制代码
Service
  1. kind: Service
  2. apiVersion: v1
  3. metadata:
  4.   name: logstash
  5. spec:
  6.   ports:
  7.     - name: logstash
  8.       protocol: TCP
  9.       port: 10000
  10.       targetPort: 9600
  11.     - name: filebeat
  12.       protocol: TCP
  13.       port: 5044
  14.       targetPort: 5044
  15.   selector:
  16.     app: logstash
  17.   type: LoadBalancer
  18.   sessionAffinity: None
复制代码
ConfigMap
  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4.   name: logstash-config
  5.   namespace: default
  6. data:
  7.   logstash.yml: >
  8.     http.host: "0.0.0.0"
  9.     xpack.monitoring.enabled: true
  10.     config.reload.automatic: true
  11.     xpack.monitoring.elasticsearch.hosts: ["elasticsearch:9200" ]
  12.     xpack.monitoring.elasticsearch.username: "elastic"
  13.     xpack.monitoring.elasticsearch.password: "abc123456"
复制代码
ConfigMap piple
  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4.   name: logstash-pipelines
  5. data:
  6.   logstash.conf: |
  7.     input {
  8.       syslog {
  9.         type => "syslog"
  10.         port => 5044
  11.       }
  12.     }
  13.     filter {
  14.        grok {
  15.           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}"]
  16.         }
  17.         mutate { rename => ["heroku_message", "message"] }
  18.         kv { source => "message" }
  19.         mutate { convert => ["sample#memory-free", "integer"]}
  20.         mutate { convert => ["sample#memory-total", "integer"]}
  21.         mutate { convert => ["sample#memory-redis", "integer"]}
  22.         mutate { convert => ["sample#memory-cached", "integer"]}
  23.         mutate { convert => ["sample#load-avg-5m", "float"]}
  24.         mutate { convert => ["sample#load-avg-1m", "float"]}
  25.         mutate { convert => ["sample#load-avg-15m", "float"]}
  26.         syslog_pri { syslog_pri_field_name => "syslog5424_pri" }
  27.     }
  28.     output {
  29.       elasticsearch {
  30.         hosts => ["http://elasticsearch:9200"]
  31.         "user" => "elastic"
  32.         "password" => "abc123456"
  33.         "index" => "logstash-%{heroku_dyno}"
  34.          template_overwrite => true
  35.       }
  36.     }
复制代码
pipe 日志处理了 Heroku 平台日志收集。并且配置了pipe的自动加载动作,这块也是实际应用中经常应用到的功能
针对与日志收集的过程中可以使用 grok 去做必要的格式转化,从而使的日志安装您的要求保存到ES 服务器中使用。
上面示例是运行在一台 2核 4G 的服务器上面,所以我们使用的是K3S 架构,并且在 ES 上只使用了一个节点数据,这些在正式的使用过程中需要多留意。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81428

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表