小編給大家分享一下k8s如何部署elasticsearch集群,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)專(zhuān)注于聞喜網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供聞喜營(yíng)銷(xiāo)型網(wǎng)站建設(shè),聞喜網(wǎng)站制作、聞喜網(wǎng)頁(yè)設(shè)計(jì)、聞喜網(wǎng)站官網(wǎng)定制、微信小程序定制開(kāi)發(fā)服務(wù),打造聞喜網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供聞喜網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。Elastic Cloud on Kubernetes,這是一款基于 Kubernetes Operator 模式的新型編排產(chǎn)品,用戶可使用該產(chǎn)品在 Kubernetes 上配置、管理和運(yùn)行 Elasticsearch 集群。ECK 的愿景是為 Kubernetes 上的 Elastic 產(chǎn)品和解決方案提供 SaaS 般的體驗(yàn)。
ECK使用 Kubernetes Operator模式構(gòu)建而成,需要安裝在Kubernetes集群內(nèi),ECK用于部署,且更專(zhuān)注于簡(jiǎn)化所有后期運(yùn)行工作:
Kubernetes目前是容器編排領(lǐng)域的領(lǐng)頭羊,而Elastic社區(qū)發(fā)布ECK,使Elasticsearch更容易的跑在云上,也是為云原生技術(shù)增磚添瓦,緊跟時(shí)代潮流。
部署ECK并查看日志是否正常:
# kubectl apply -f https://download.elastic.co/downloads/eck/1.1.2/all-in-one.yaml # kubectl -n elastic-system logs -f statefulset.apps/elastic-operator
過(guò)幾分鐘查看elastic-operator是否運(yùn)行正常,ECK中只有一個(gè)elastic-operator pod:
# kubectl get pods -n elastic-system NAME READY STATUS RESTARTS AGE elastic-operator-0 1/1 Running 1 2m55s
我們測(cè)試情況使用1臺(tái)master節(jié)點(diǎn)和1臺(tái)data節(jié)點(diǎn)來(lái)部署集群,生產(chǎn)環(huán)境建議使用3+臺(tái)master節(jié)點(diǎn)。如下的manifest中,對(duì)實(shí)例的heap大小,容器的可使用內(nèi)存,容器的虛擬機(jī)內(nèi)存都進(jìn)行了配置,可以根據(jù)集群需要做調(diào)整:
# vim es.yaml apiVersion: elasticsearch.k8s.elastic.co/v1 kind: Elasticsearch metadata: name: quickstart spec: version: 7.7.1 nodeSets: - name: master-nodes count: 1 config: node.master: true node.data: false podTemplate: spec: initContainers: - name: sysctl securityContext: privileged: true command: ['sh', '-c', 'sysctl -w vm.max_map_count=262144'] containers: - name: elasticsearch env: - name: ES_JAVA_OPTS value: -Xms1g -Xmx1g resources: requests: memory: 2Gi limits: memory: 2Gi volumeClaimTemplates: - metadata: name: elasticsearch-data spec: accessModes: - ReadWriteOnce resources: requests: storage: 5Gi storageClassName: rook-ceph-block - name: data-nodes count: 1 config: node.master: false node.data: true podTemplate: spec: initContainers: - name: sysctl securityContext: privileged: true command: ['sh', '-c', 'sysctl -w vm.max_map_count=262144'] containers: - name: elasticsearch env: - name: ES_JAVA_OPTS value: -Xms1g -Xmx1g resources: requests: memory: 2Gi limits: memory: 2Gi volumeClaimTemplates: - metadata: name: elasticsearch-data spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi storageClassName: rook-ceph-block # kubectl apply -f es.yaml
過(guò)段時(shí)間,查看elasticsearch集群的狀態(tài)
# kubectl get pods quickstart-es-data-nodes-0 1/1 Running 0 54s quickstart-es-master-nodes-0 1/1 Running 0 54s # kubectl get elasticsearch NAME HEALTH NODES VERSION PHASE AGE quickstart green 2 7.7.1 Ready 73s
查看pv的狀態(tài),我們可以看到申請(qǐng)的pv已經(jīng)創(chuàng)建和綁定成功:
# kubectl get pv pvc-512cc739-3654-41f4-8339-49a44a093ecf 10Gi RWO Retain Bound default/elasticsearch-data-quickstart-es-data-nodes-0 rook-ceph-block 9m5s pvc-eff8e0fd-f669-448a-8b9f-05b2d7e06220 5Gi RWO Retain Bound default/elasticsearch-data-quickstart-es-master-nodes-0 rook-ceph-block 9m5s
默認(rèn)集群開(kāi)啟了basic認(rèn)證,用戶名為elastic,密碼可以通過(guò)secret獲取。默認(rèn)集群也開(kāi)啟了自簽名證書(shū)https訪問(wèn)。我們可以通過(guò)service資源來(lái)訪問(wèn)elasticsearch:
# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE quickstart-es-data-nodes ClusterIP None
不停服,擴(kuò)容一臺(tái)data節(jié)點(diǎn):修改es.yaml中data-nodes中count的value為2,然后apply下es.yaml即可。
# kubectl apply -f es.yaml # kubectl get pods quickstart-es-data-nodes-0 1/1 Running 0 24m quickstart-es-data-nodes-1 1/1 Running 0 8m22s quickstart-es-master-nodes-0 1/1 Running 0 24m # kubectl get elasticsearch NAME HEALTH NODES VERSION PHASE AGE quickstart green 3 7.7.1 Ready 25m
不停服,縮容一臺(tái)data節(jié)點(diǎn),會(huì)自動(dòng)進(jìn)行數(shù)據(jù)同步:修改es.yaml中data-nodes中count的value為1,然后apply下es.yaml即可。
由于默認(rèn)kibana也開(kāi)啟了自簽名證書(shū)的https訪問(wèn),我們可以選擇關(guān)閉,我們來(lái)使用ECK部署kibana:
# vim kibana.yaml apiVersion: kibana.k8s.elastic.co/v1 kind: Kibana metadata: name: quickstart spec: version: 7.7.1 count: 1 elasticsearchRef: name: quickstart http: tls: selfSignedCertificate: disabled: true # kubectl apply -f kibana.yaml # kubectl get pods NAME READY STATUS RESTARTS AGE quickstart-es-data-nodes-0 1/1 Running 0 31m quickstart-es-data-nodes-1 1/1 Running 1 15m quickstart-es-master-nodes-0 1/1 Running 0 31m quickstart-kb-6558457759-2rd7l 1/1 Running 1 4m3s # kubectl get kibana NAME HEALTH NODES VERSION AGE quickstart green 1 7.7.1 4m27s
為kibana在ingress中添加一個(gè)四層代理,提供對(duì)外訪問(wèn)服務(wù):
# vim tsp-kibana.yaml apiVersion: k8s.nginx.org/v1alpha1 kind: GlobalConfiguration metadata: name: nginx-configuration namespace: nginx-ingress spec: listeners: - name: kibana-tcp port: 5601 protocol: TCP --- apiVersion: k8s.nginx.org/v1alpha1 kind: TransportServer metadata: name: kibana-tcp spec: listener: name: kibana-tcp protocol: TCP upstreams: - name: kibana-app service: quickstart-kb-http port: 5601 action: pass: kibana-app # kubectl apply -f tsp-kibana.yaml
默認(rèn)kibana訪問(wèn)elasticsearch的用戶名為elastic,密碼獲取方式如下
# kubectl get secret quickstart-es-elastic-user -o=jsonpath='{.data.elastic}' | base64 --decode; echo
通過(guò)瀏覽器訪問(wèn)kibana:
刪除elasticsearch和kibana以及ECK
# kubectl get namespaces --no-headers -o custom-columns=:metadata.name \ | xargs -n1 kubectl delete elastic --all -n # kubectl delete -f https://download.elastic.co/downloads/eck/1.1.2/all-in-one.yaml
先安裝Kubernetes應(yīng)用的包管理工具h(yuǎn)elm。Helm是用來(lái)封裝 Kubernetes原生應(yīng)用程序的YAML文件,可以在你部署應(yīng)用的時(shí)候自定義應(yīng)用程序的一些metadata,helm依賴chart實(shí)現(xiàn)了應(yīng)用程序的在k8s上的分發(fā)。helm和chart主要實(shí)現(xiàn)了如下功能:
# wget https://get.helm.sh/helm-v3.2.3-linux-amd64.tar.gz # tar -zxvf helm-v3.0.0-linux-amd64.tar.gz # mv linux-amd64/helm /usr/local/bin/helm # helm repo add stable https://kubernetes-charts.storage.googleapis.com
通過(guò)helm安裝cerebro:
# helm install stable/cerebro --version 1.1.4 --generate-name
查看cerebro的狀態(tài):
# kubectl get pods|grep cerebro cerebro-1591777586-7fd87f7d48-hmlp7 1/1 Running 0 11m
由于默認(rèn)ECK部署的elasticsearch開(kāi)啟了自簽名證書(shū)的https服務(wù),故可以在cerebro配置忽略https證書(shū)認(rèn)證(也可以在cerebro中添加自簽名證書(shū)的ca證書(shū)來(lái)識(shí)別自簽名證書(shū)),并重啟cerebro:
1,導(dǎo)出cerebro的configmap:
# kubectl get configmap cerebro-1591777586 -o yaml > cerebro.yaml
2,替換configmap中cerebro的hosts相關(guān)配置為如下(其中quickstart-es-http為elasticsarch的service資源名字):
play.ws.ssl.loose.acceptAnyCertificate = true hosts = [ { host = "https://quickstart-es-http.default.svc:9200" name = "k8s elasticsearch" } ]
注意:
每一個(gè)service在k8s集群dns中多有一個(gè)對(duì)應(yīng)的記錄,客戶端訪問(wèn)service的時(shí)候可以通過(guò)FQDN來(lái)訪問(wèn),默認(rèn)FQDN的格式為servicename.namespace.svc.cluster.local。其中servicename對(duì)用server的名字,namespace對(duì)應(yīng)所在的命名空間,svc.cluster.local是k8s集群可以配置集群域后綴??蛻舳撕头?wù)端在同一個(gè)namespace下,可以直接使用servicename訪問(wèn),如果在不同namespace下,需要使用servername.namespace進(jìn)行訪問(wèn)。當(dāng)然,如果直接使用完成的FQDN訪問(wèn)就不用區(qū)分namespace了。
3,應(yīng)用cerebro的configmap并重啟cerebro pod:
# kubectl apply -f cerebro.yaml # kubectl get pods|grep cerebro cerebro-1591777586-7fd87f7d48-hmlp7 1/1 Running 0 11m # kubectl get pod cerebro-1591777586-7fd87f7d48-hmlp7 -o yaml | kubectl replace --force -f -
先確認(rèn)cerebro的service資源,然后配置ingress為cerebro添加7層代理:
# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE cerebro-1591777586 ClusterIP 10.111.107.171
在本地pc的/etc/hosts文件添加host綁定"172.18.2.175 cerebro.myk8s.com",然后通過(guò)覽器訪問(wèn):
刪除cerebro
# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION cerebro-1591777586 default 1 2020-06-10 16:26:30.419723417 +0800 CST deployed cerebro-1.1.4 0.8.4 # heml delete name cerebro-1591777586
看完了這篇文章,相信你對(duì)k8s如何部署elasticsearch集群有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!