|NO.Z.00280|——————————|^^ 部署 ^^|——|KuberNetes&运维.V02|——|EFK架构.v02|部署ElasticSearch|
一、部署ElasticSearch
### --- 创建ElasticSearch-service.yaml文件
[root@k8s-master01 EFK]# cat es-service.yaml
apiVersion: v1
kind: Service
metadata:
name: elasticsearch-logging
namespace: public-service
labels:
k8s-app: elasticsearch-logging
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
kubernetes.io/name: "Elasticsearch"
spec:
ports:
- port: 9200
protocol: TCP
targetPort: db
selector:
k8s-app: elasticsearch-logging
二、创建ElasticSearch-service
### --- 创建ElasticSearch-service
[root@k8s-master01 EFK]# kubectl create -f es-service.yaml
service/elasticsearch-logging created
### --- 查看创建的ElasticSearch-service
[root@k8s-master01 EFK]# kubectl get svc -n public-service -owide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
elasticsearch-logging ClusterIP 10.107.157.176 <none> 9200/TCP 114s k8s-app=elasticsearch-logging
[root@k8s-master01 EFK]# kubectl get ep -n public-service -owide
NAME ENDPOINTS AGE
elasticsearch-logging 172.27.14.195:9200 3m
三、创建ElasticSearch-StatefulSet.yaml
### --- 创建ElasticSearch-StatefulSet的yaml文件
[root@k8s-master01 EFK]# cat es-statefulset.yaml
# RBAC authn and authz
apiVersion: v1
kind: ServiceAccount
metadata:
name: elasticsearch-logging
namespace: public-service
labels:
k8s-app: elasticsearch-logging
addonmanager.kubernetes.io/mode: Reconcile
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: elasticsearch-logging
labels:
k8s-app: elasticsearch-logging
addonmanager.kubernetes.io/mode: Reconcile
rules:
- apiGroups:
- ""
resources:
- "services"
- "namespaces"
- "endpoints"
verbs:
- "get"
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: public-service
name: elasticsearch-logging
labels:
k8s-app: elasticsearch-logging
addonmanager.kubernetes.io/mode: Reconcile
subjects:
- kind: ServiceAccount
name: elasticsearch-logging
namespace: public-service
apiGroup: ""
roleRef:
kind: ClusterRole
name: elasticsearch-logging
apiGroup: ""
---
# Elasticsearch deployment itself
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: elasticsearch-logging
namespace: public-service
labels:
k8s-app: elasticsearch-logging
version: v7.4.2
addonmanager.kubernetes.io/mode: Reconcile
spec:
serviceName: elasticsearch-logging
replicas: 1
selector:
matchLabels:
k8s-app: elasticsearch-logging
version: v7.4.2
template:
metadata:
labels:
k8s-app: elasticsearch-logging
version: v7.4.2
spec:
serviceAccountName: elasticsearch-logging
containers:
- image: quay.io/fluentd_elasticsearch/elasticsearch:v7.4.2
name: elasticsearch-logging
imagePullPolicy: IfNotPresent
resources:
# need more cpu upon initialization, therefore burstable class
limits:
cpu: 500m
memory: 2Gi
requests:
cpu: 100m
memory: 2Gi
ports:
- containerPort: 9200
name: db
protocol: TCP
- containerPort: 9300
name: transport
protocol: TCP
# livenessProbe:
# tcpSocket:
# port: transport
# initialDelaySeconds: 5
# timeoutSeconds: 10
# readinessProbe:
# tcpSocket:
# port: transport
# initialDelaySeconds: 5
# timeoutSeconds: 10
volumeMounts:
- name: elasticsearch-logging
mountPath: /data
env:
- name: "NAMESPACE"
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumes:
- name: elasticsearch-logging
emptyDir: {}
# Elasticsearch requires vm.max_map_count to be at least 262144.
# If your OS already sets up this number to a higher value, feel free
# to remove this init container.
initContainers:
- image: alpine:3.6
command: ["/sbin/sysctl", "-w", "vm.max_map_count=262144"]
name: elasticsearch-logging-init
securityContext:
privileged: true
### --- 注:
~~~ # 注释一:namespace确定
metadata:
name: elasticsearch-logging
namespace: public-service
~~~ # 注释二:serviceName:
spec:
serviceAccountName: elasticsearch-logging
~~~ # 注释三:副本数:设置为1
spec:
serviceName: elasticsearch-logging
replicas: 1 // 副本数
~~~ # 注释四:绑定宿主机节点
spec:
serviceAccountName: elasticsearch-logging // 此行下添加如下参数
nodeSelector:
kubernetes.io/hostname: k8s-master03
~~~ # 注释五:健康检查关闭
~~~ 注:它是使用k8s集群发现的机制来创建EFK的集群,
~~~ 若是使用健康检查机制,它的健康检查一直通不过。集群一直就起不来。
# livenessProbe:
# tcpSocket:
# port: transport
# initialDelaySeconds: 5
# timeoutSeconds: 10
# readinessProbe:
# tcpSocket:
# port: transport
# initialDelaySeconds: 5
# timeoutSeconds: 10
~~~ # 注释六:es集群
~~~ 测试环境是没有挂es的数据盘的,生成环境下,是会有自己的es集群的,es集群规模比较大,有可能是几百台或上千台宿主机的
~~~ 所以es集群是不建议部署在k8s集群中的,因为es是非常消耗资源的。
~~~ 若是有k8s专用的节点也是可以的
volumes:
- name: elasticsearch-logging
emptyDir: {}
四、创建ElasticSearch-StatefulSet
### --- 创建ElasticSearch-StatefulSet
[root@k8s-master01 EFK]# kubectl create -f es-statefulset.yaml
serviceaccount/elasticsearch-logging created
clusterrole.rbac.authorization.k8s.io/elasticsearch-logging created
clusterrolebinding.rbac.authorization.k8s.io/elasticsearch-logging created
statefulset.apps/elasticsearch-logging created
### --- 查看创建日志
[root@k8s-master01 EFK]# kubectl logs -f pod/elasticsearch-logging-0 -n public-service
+ export NODE_NAME=elasticsearch-logging-0
+ NODE_NAME=elasticsearch-logging-0
+ export NODE_MASTER=true
+ NODE_MASTER=true
+ export NODE_DATA=true
+ NODE_DATA=true
+ export HTTP_PORT=9200
+ HTTP_PORT=9200
+ export TRANSPORT_PORT=9300
+ TRANSPORT_PORT=9300
+ export MINIMUM_MASTER_NODES=2
+ MINIMUM_MASTER_NODES=2
+ chown -R elasticsearch:elasticsearch /data
+ ./bin/elasticsearch_logging_discovery
### --- 查看创建后结果
[root@k8s-master01 EFK]# kubectl get po -n public-service -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
elasticsearch-logging-0 1/1 Running 0 20s 172.27.14.195 k8s-node02 <none> <none>
五、验证ElasticSearch的实例
### --- 查看创建ElasticSearch的pod实例
[root@k8s-master01 EFK]# kubectl get po -n public-service -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
elasticsearch-logging-0 1/1 Running 0 20s 172.27.14.195 k8s-node02 <none> <none>
### --- 查看创建ElasticSearch的svc实例
[root@k8s-master01 EFK]# kubectl get svc -n public-service -owide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
elasticsearch-logging ClusterIP 10.107.157.176 <none> 9200/TCP 114s k8s-app=elasticsearch-logging
### --- 查看创建ElasticSearch的ep实例
[root@k8s-master01 EFK]# kubectl get ep -n public-service -owide
NAME ENDPOINTS AGE
elasticsearch-logging 172.27.14.195:9200 3m
### --- 验证创建ElasticSearch的集群是否正常
~~~ 显示green状态说明正常
[root@k8s-master01 EFK]# curl 10.107.157.176:9200/_cluster/health?pretty
{
"cluster_name" : "kubernetes-logging",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了