Step1: 准备yaml
创建 es-sts.yaml :
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: es-cluster
namespace: elk
spec:
serviceName: es-cluster
replicas: 3
selector:
matchLabels:
app: es-cluster
template:
metadata:
name: es-cluster
labels:
app: es-cluster
spec:
initContainers:
- name: init-sysctl
image: busybox
command:
- sysctl
- '-w'
- vm.max_map_count=262144
imagePullPolicy: IfNotPresent
securityContext:
privileged: true
containers:
- image: 'docker.elastic.co/elasticsearch/elasticsearch:7.17.5'
name: es
env:
- name: node.name
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: cluster.name
value: my-cluster
- name: cluster.initial_master_nodes
value: 'es-cluster-0,es-cluster-1,es-cluster-2'
- name: discovery.seed_hosts
value: es-cluster
- name: network.host
value: _site_
- name: ES_JAVA_OPTS
value: '-Xms512m -Xmx512m'
volumeMounts:
- name: es-cluster-data
mountPath: /usr/share/elasticsearch/data
volumes:
- name: es-cluster-data
emptyDir: {}
创建负载服务 svc.yaml :
apiVersion: v1
kind: Service
metadata:
name: es-cluster
namespace: elk
spec:
clusterIP: None
ports:
- name: http
port: 9200
- name: tcp
port: 9300
selector:
app: es-cluster
创建外网访问 nodePort.yaml
apiVersion: v1
kind: Service
metadata:
name: es-cluster-nodeport
namespace: elk
spec:
type: NodePort
selector:
app: es-cluster
ports:
- name: http
port: 9200
targetPort: 9200
nodePort: 31200
- name: tcp
port: 9300
targetPort: 9300
nodePort: 31300
Step2: 创建
创建以上的yaml文件:
kubectl apply -f es-sts.yaml
kubectl apply -f svc.yaml
或者使用kubebiz的yaml源:
kubectl apply -f https://www.kubebiz.com/raw/KubeBiz/Elasticsearch/7.17.5/all?namespace=elk
Step3: 验证
查看状态:
kubectl get pods -n elk
NAME READY STATUS RESTARTS AGE
es-cluster-0 1/1 Running 0 3m
es-cluster-1 1/1 Running 0 3m
es-cluster-2 1/1 Running 0 3m
等待所有pod的状态为Running
。
通过创建的nodePort访问,通过 <nodepot-ip>
:31200
在游览器访问,返回:
{
"name" : "es-cluster-2",
"cluster_name" : "my-cluster",
"cluster_uuid" : "_na_",
"version" : {
"number" : "7.17.5",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "8d61b4f7ddf931f219e3745f295ed2bbc50c8e84",
"build_date" : "2022-06-23T21:57:28.736740635Z",
"build_snapshot" : false,
"lucene_version" : "8.11.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
安装完成。