一、摆设es
1.下载安装
- #下载安装
- https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.13.2-linux-x86_64.tar.gz
- #解压
- [root@es software]# tar xf elasticsearch-8.13.2-linux-x86_64.tar.gz
- #创建运行elasticsearch服务用户并修改权限
- [root@es software]# useradd liux
- [root@es software]# chown liux:liux /data/software/elasticsearch-8.13.2 -R
复制代码 2.修改文件形貌符
最大及进程数目打开上限 (不修改启动会报错)- [root@es ~]# cat > /etc/security/limits.d/es.conf <<EOF
- * soft nofile 65535
- * hard nofile 131070
- * hard nproc 8192
- EOF
复制代码 3.修改内核参数
- [root@es elasticsearch-8.13.2]$ sysctl -q vm.max_map_count
- vm.max_map_count = 65530
- [root@es elasticsearch-8.13.2]# cat > /etc/sysctl.d/es.conf <<EOF
- vm.max_map_count=262144
- EOF
- [root@es elasticsearch-8.13.2]# sysctl -f /etc/sysctl.d/es.conf
- vm.max_map_count = 262144
复制代码 4.修改设置文件
- [liux@es elasticsearch-8.13.2]$ egrep -v "^#|^$" config/elasticsearch.yml
- network.host: 0.0.0.0
- discovery.seed_hosts: ["192.168.91.30"]
- cluster.initial_master_nodes: ["192.168.91.30"]
- xpack.security.enabled: false
复制代码 5.设置启动文件
- [liux@es elasticsearch-8.13.2]$ cat > startES.sh <<EOF
- #!/bin/bash
- # 设置Elasticsearch的安装目录 ES_HOME="/data/software/elasticsearch-8.13.2"
- pid=`ps -ef | grep elasticsearch-8.13.2 | grep -v grep | grep '/data/software/elasticsearch-8.13.2/jdk/bin/ja
- va' | awk '{print $2}'`
- # 检查Elasticsearch是否已经在运行
- if ps -ef | grep elasticsearch | grep -v grep > /dev/null; then echo "Elasticsearch is running. Stopping..."
- kill $pid; echo "Elasticsearch stopped."
- fi
- sleep 5s
- # 使用nohup重新启动Elasticsearch
- nohup "$ES_HOME/bin/elasticsearch" -d > "$ES_HOME/es.log" 2>&1 &
- echo "Elasticsearch is starting in the background..."
- EOF
- [liux@es elasticsearch-8.13.2]$ sh startES.sh
复制代码 6.如下所示代表安装乐成
- [liux@es elasticsearch-8.13.2]$ curl http://192.168.91.30:9200
- {
- "name" : "es",
- "cluster_name" : "elasticsearch",
- "cluster_uuid" : "f337MT9oRvmL16xom5muBQ",
- "version" : {
- "number" : "8.13.2",
- "build_flavor" : "default",
- "build_type" : "tar",
- "build_hash" : "16cc90cd2d08a3147ce02b07e50894bc060a4cbf",
- "build_date" : "2024-04-05T14:45:26.420424304Z",
- "build_snapshot" : false,
- "lucene_version" : "9.10.0",
- "minimum_wire_compatibility_version" : "7.17.0",
- "minimum_index_compatibility_version" : "7.0.0"
- },
- "tagline" : "You Know, for Search"
- }
复制代码 7.Elasticsearch天生 TLS 证书
- #生成 CA 证书
- [root@es elasticsearch-8.13.2]# bin/elasticsearch-certutil ca --pem --out config/certs/ca.zip
- [root@es elasticsearch-8.13.2]# unzip config/certs/ca.zip -d config/certs/
- # 生成节点证书(包含 SAN)
- [root@es elasticsearch-8.13.2]# bin/elasticsearch-certutil cert --pem \
- --ca-cert config/certs/ca/ca.crt \
- --ca-key config/certs/ca/ca.key \
- --name "es" \
- --dns localhost,es,127.0.0.1 \
- --ip 192.168.91.30 \
- --out config/certs/es-node.zip
- [root@es elasticsearch-8.13.2]# unzip config/certs/es-node.zip -d config/certs/
复制代码 8.修改elasticsearch设置文件
- [root@es elasticsearch-8.13.2]# cat config/elasticsearch.yml
- # 集群和节点名称
- cluster.name: my-es
- node.name: es-node
- # 网络绑定
- network.host: 0.0.0.0
- http.port: 9200
- # 安全配置
- xpack.security.enabled: true
- #配置 HTTP 层 TLS(HTTPS)
- xpack.security.http.ssl:
- enabled: true
- key: certs/es/es.key
- certificate: certs/es/es.crt
- certificate_authorities: certs/ca/ca.crt
- verification_mode: full
- #配置 Transport 层 TLS(节点间通信)
- xpack.security.transport.ssl:
- enabled: true
- key: certs/es/es.key
- certificate: certs/es/es.crt
- certificate_authorities: certs/ca/ca.crt
- verification_mode: full
- # 初始主节点
- cluster.initial_master_nodes: ["192.168.91.30"]
复制代码 9.设置文件权限
- [root@es elasticsearch-8.13.2]# chown -R liux:liux /data/software/elasticsearch-8.13.2
- [root@es elasticsearch-8.13.2]# chmod 600 /data/software/elasticsearch-8.13.2/config/certs/**/*.key
- [root@es elasticsearch-8.13.2]# chmod 644 /data/software/elasticsearch-8.13.2/config/certs/**/*.crt
复制代码 10.启动(以非root账号)
- [liux@es elasticsearch-8.13.2]$ ./startES.sh
复制代码 11.为内用用户设置暗码
- [root@es elasticsearch-8.13.2]# ./bin/elasticsearch-reset-password -u elastic
- This tool will reset the password of the [elastic] user to an autogenerated value.
- The password will be printed in the console.
- Please confirm that you would like to continue [y/N]y
- Password for the [elastic] user successfully reset.
- New value: CxL+02hUBIERrlwaXefD
复制代码 12.验证 HTTPS 访问
利用 curl 或浏览器验证 HTTPS 是否收效- [liux@es elasticsearch-8.13.2]$ curl -k -u elastic:qgXT6yy*Vmj8FQhO2ein https://192.168.91.30:9200
- {
- "name" : "es",
- "cluster_name" : "es8",
- "cluster_uuid" : "f337MT9oRvmL16xom5muBQ",
- "version" : {
- "number" : "8.13.2",
- "build_flavor" : "default",
- "build_type" : "tar",
- "build_hash" : "16cc90cd2d08a3147ce02b07e50894bc060a4cbf",
- "build_date" : "2024-04-05T14:45:26.420424304Z",
- "build_snapshot" : false,
- "lucene_version" : "9.10.0",
- "minimum_wire_compatibility_version" : "7.17.0",
- "minimum_index_compatibility_version" : "7.0.0"
- },
- "tagline" : "You Know, for Search"
- }
复制代码
13.注意事项
验证模式
verification_mode: certificate:仅验证证书有用性(开发环境适用)。
verification_mode: full:严酷验证证书和主机名(生产环境必须)。
防火墙和网络
确保防火墙开放端口 9200(HTTPS)和 9300(节点间通讯)。
集群设置
多节点集群需确保全部节点利用相同的 CA 证书,并在设置中指定 discovery.seed_hosts。
二、摆设kibana
1.下载安装
- https://artifacts.elastic.co/downloads/kibana/kibana-8.13.2-x86_64.rpm
- [root@es software]# rpm -ivh kibana-8.13.2-x86_64.rpm
复制代码 2.设置kibana用户暗码
- [root@es elasticsearch-8.13.2]# ./bin/elasticsearch-reset-password -u kibana_system
- This tool will reset the password of the [kibana_system] user to an autogenerated value.
- The password will be printed in the console.
- Please confirm that you would like to continue [y/N]y
- Password for the [kibana_system] user successfully reset.
- New value: qqjW5tkngBhbWuEydDGW
复制代码 3.修改kibana设置文件
- [root@es kibana]# cat kibana.yml
- server.port: 5601
- server.host: "192.168.91.30"
- server.publicBaseUrl: "http://192.168.91.30:5601"
- # Elasticsearch 安全连接
- elasticsearch.hosts: ["https://192.168.91.30:9200"]
- elasticsearch.ssl.certificateAuthorities: ["/data/software/elasticsearch-8.13.2/config/certs/ca/ca.crt"]
- elasticsearch.ssl.verificationMode: full
- elasticsearch.username: "kibana_system"
- elasticsearch.password: "*q-Bc0=m5d*Ev-AcGShm"
- # 启用 Kibana 安全
- #用于加密和解密敏感数据的密钥
- xpack.encryptedSavedObjects.encryptionKey: "1f57078d76aa5c992171a342d7a64d33e56f3a1b832967160bcc568ae43dcf8d"
- # 如果二进制安装可用 ./bin/kibana-encryption-keys generate生成上面32位的随机字符串
- # 也可以用openssl rand -hex 32 生成
复制代码 4.设置文件权限
- [root@es kibana]# chown -R kibana:kibana /etc/kibana
- # 确保 Kibana 可读 CA 证书
- [root@es kibana]# chmod 644 /data/software/elasticsearch-8.13.2/config/certs/ca/ca.crt
复制代码 5.启动
- [root@es software]# systemctl enable --now kibana
- [root@es software]# systemctl status kibana
- [root@es kibana]# cat /usr/lib/systemd/system/kibana.service
- [Unit]
- Description=Kibana
- Documentation=https://www.elastic.co
- Wants=network-online.target
- After=network-online.target
- [Service]
- Type=simple
- User=kibana
- Group=kibana
- PrivateTmp=true
- Environment=KBN_HOME=/usr/share/kibana
- Environment=KBN_PATH_CONF=/etc/kibana
- EnvironmentFile=-/etc/default/kibana
- EnvironmentFile=-/etc/sysconfig/kibana
- ExecStart=/usr/share/kibana/bin/kibana
- Restart=on-failure
- RestartSec=3
- StartLimitBurst=3
- StartLimitInterval=60
- WorkingDirectory=/usr/share/kibana
- StandardOutput=journal
- StandardError=inherit
- [Install]
- WantedBy=multi-user.target
- #页面访问
- http://192.168.91.30:5601
复制代码 三、摆设filebeat
1.下载filebeat镜像
- [root@node-1 ~]# docker pull docker.elastic.co/beats/filebeat:8.13.2
复制代码 2.编写RBAC文件
- [root@master-1 logs]# vim filebeat-sa.yaml
- #创建filebeat-sa的服务账号
- apiVersion: v1
- kind: ServiceAccount
- metadata:
- name: filebeat-sa
- namespace: kube-system
- labels:
- k8s-app: filebeat
- ---
- #集群级权限(ClusterRole)
- apiVersion: rbac.authorization.k8s.io/v1
- kind: ClusterRole
- metadata:
- name: filebeat
- labels:
- k8s-app: filebeat
- rules:
- - apiGroups: [""] # "" indicates the core API group
- resources: ["namespaces","pods","nodes"] #用于自动发现容器
- verbs: ["get","watch","list"]
- - apiGroups: ["apps"] #用于关联 Pod 与 ReplicaSet
- resources:
- - replicasets
- verbs: ["get", "list", "watch"]
-
- ---
- #集群级权限(ClusterRoleBinding)
- #将角色与服务账号绑定
- apiVersion: rbac.authorization.k8s.io/v1
- kind: ClusterRoleBinding
- metadata:
- name: filebeat
- subjects:
- - kind: ServiceAccount
- name: filebeat-sa
- namespace: kube-system
- roleRef:
- kind: ClusterRole
- name: filebeat
- apiGroup: rbac.authorization.k8s.io
-
- ---
- #访问 kubeadm-config 的权限(Role + RoleBinding)
- #读取 kube-system 命名空间中名为 kubeadm-config 的 ConfigMap(用于获取集群配置信息)
- #如需要获取data.kubernetes.container.name
- apiVersion: rbac.authorization.k8s.io/v1
- kind: Role
- metadata:
- name: filebeat-kubeadm-config
- namespace: kube-system
- labels:
- k8s-app: filebeat
- rules:
- - apiGroups: [""]
- resources:
- - configmaps
- resourceNames:
- - kubeadm-config
- verbs: ["get"]
- ---
- apiVersion: rbac.authorization.k8s.io/v1
- kind: RoleBinding
- metadata:
- name: filebeat-kubeadm-config
- namespace: kube-system
- subjects:
- - kind: ServiceAccount
- name: filebeat-sa
- namespace: kube-system
- roleRef:
- kind: Role
- name: filebeat-kubeadm-config
- apiGroup: rbac.authorization.k8s.io
- [root@master-1 logs]# kubectl apply -f filebeat-sa.yaml
复制代码 3.编写filebeat-config资源
- [root@master-1 logs]# vim filebeat-config.yaml
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: filebeat-config
- namespace: kube-system
- labels:
- k8s-app: filebeat
- data:
- filebeat.yml: |-
- # To enable hints based autodiscover, remove `filebeat.inputs` configuration and uncomment this:
- filebeat.autodiscover:
- providers:
- - type: kubernetes
- node: ${NODE_NAME}
- hints.enabled: true
- conditions:
- equals:
- data.kubernetes.container.name: "nginx" # 关键条件字段,只收集容器为nginx日志
 - hints.default_config:
- type: container
- paths:
- - /var/log/containers/*${data.kubernetes.container.id}.log
- symlinks: true
- processors:
- - add_host_metadata:
- output.elasticsearch:
- hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}']
- username: ${ELASTICSEARCH_USERNAME}
- password: ${ELASTICSEARCH_PASSWORD}
- # 定义索引名称格式:nginx-logs-当前日期
- index: "nginx-logs-%{+yyyy.MM.dd}"
- setup.ilm.enabled: false # 禁用索引生命周期管理
- setup.template.enabled: false # 禁用默认索引模板,开启该选项日志
默认会收集到以filebeat+版本号的 数据流模板中 - cloud.id: "" # 清空云ID配置
- cloud.auth: ""
- [root@master-1 logs]# kubectl apply -f filebeat-config.yaml
复制代码 4.编写filebeat的DaemonSet资源
5.设置 Filebeat 利用 TLS 连接es
注:es如果没有开启tls认证,该步骤可以忽略- #1. 通过secret存储es证书
- [root@master-1 logs]# kubectl create secret generic es-ca-cert -n kube-system --from-file=ca.crt=/root/ca.crt
- #2.在Filebeat 的 Deployment/DaemonSet 中挂载证书filebeat-ds.yaml
- volumeMounts:
- - name: es-ca-cert
- mountPath: /etc/filebeat/certs
- readOnly: true
- volumes:
- - name: es-ca-cert
- secret:
- secretName: es-ca-cert
- #3.配置 Filebeat 的 filebeat-config.yml
- #修改 Filebeat 的 Elasticsearch 输出配置,启用 TLS 并指定 CA 证书路径
- #hosts需要改为https
- hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}']
- username: ${ELASTICSEARCH_USERNAME}
- password: ${ELASTICSEARCH_PASSWORD}
- # 禁用默认数据流,使用自定义索引
- #data_stream.enabled: false
- # 定义索引名称格式:nginx-logs-当前日期
- index: "nginx-logs-%{+yyyy.MM.dd}"
- ssl:
- enabled: true
- certificate_authorities: ["/etc/filebeat/certs/ca.crt"] # 挂载的 CA 证书路径
- verification_mode: "full" # 严格验证证书(生产环境推荐)
复制代码 6.利用Secret 存储es用户暗码(更安全)
- #1. 使用kubectl命令创建secret
- [root@master-1 logs]# kubectl create secret generic es-credentials -n kube-system \
- --from-literal=username=elastic \
- --from-literal=password=CxL+02hUBIERrlwaXefD
- #也可通过yaml方式创建
- [root@master-1 logs]# kubectl apply -f es-credentials.yaml
- apiVersion: v1
- kind: Secret
- metadata:
- name: es-credentials
- namespace: kube-system
- type: Opaque
- data:
- password: Q3hMKzAyaFVCSUVScmx3YVhlZkQ= # echo -n "CxL+02hUBIERrlwaXefD" | base64
- username: ZWxhc3RpYw== # echo -n "elastic" | base64
- #2.将secret通过环境变量注入pod中
- # 示例:Filebeat DaemonSet 配置片段
- spec:
- template:
- spec:
- containers:
- - name: filebeat
- env:
- - name: ELASTICSEARCH_USERNAME
- valueFrom:
- secretKeyRef:
- name: es-credentials
- key: username
- - name: ELASTICSEARCH_PASSWORD
- valueFrom:
- secretKeyRef:
- name: es-credentials
- key: password
- #3.filebeat-config从环境变量中获取
- output.elasticsearch:
- hosts: ["https://es:9200"]
- protocol: "https"
- username: ${ELASTICSEARCH_USERNAME} # 从环境变量读取
- password: ${ELASTICSEARCH_PASSWORD} # 从环境变量读取
复制代码 7.汇总版本(tls+secret)
8.测试效果以及排查题目
- [root@master-1 logs]# kubectl apply -f filebeat-sa.yaml
- [root@master-1 logs]# kubectl apply -f filebeat-config.yaml
- [root@master-1 logs]# kubectl apply -f filebeat-ds.yaml
- #以下表示部署成功
- [root@master-1 logs]# kubectl get pods -A -o wide
- NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
- kube-system filebeat-49g94 1/1 Running 0 16m 192.168.91.22 node-2 <none> <none>
- kube-system filebeat-rgf4f 1/1 Running 0 16m 192.168.91.21 node-1 <none> <none>
- #kibana中查看索引Stack Management-->Index Management
- nginx-logs
- #查看filebeat日志
- [root@master-1 logs]# kubectl logs -f filebeat-49g94 -n kube-system
- #调试 Filebeat,输出更详细的日志,帮助排查问题
- [root@master-1 logs]# kubectl exec -it filebeat-49g94 -n kube-system -- filebeat -e -d "*" -c /etc/filebeat.yml
- # 遇到被锁的情况 进入Filebeat Pod手动删除锁文件
- [root@master-1 logs]# kubectl exec -it filebeat-49g94 -n kube-system -- rm -f /usr/share/filebeat/data/filebeat.lock
- #查看es连通性
- [root@master-1 logs]# kubectl exec -it filebeat-49g94 -n kube-system -- curl -u elastic:CxL+02hUBIERrlwaXefD http://192.168.91.30:9200
复制代码 EFK 日记系统为 Kubernetes 提供了从日记网络、存储到分析的全链路本领,生产环境中需重点关注 长期化存储、资源配额 和 安全设置(如 TLS 加密、RBAC 权限)。
|