您好,登錄后才能下訂單哦!
小編給大家分享一下k8s如何部署elasticsearch集群,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
Elastic Cloud on Kubernetes,這是一款基于 Kubernetes Operator 模式的新型編排產品,用戶可使用該產品在 Kubernetes 上配置、管理和運行 Elasticsearch 集群。ECK 的愿景是為 Kubernetes 上的 Elastic 產品和解決方案提供 SaaS 般的體驗。
ECK使用 Kubernetes Operator模式構建而成,需要安裝在Kubernetes集群內,ECK用于部署,且更專注于簡化所有后期運行工作:
Kubernetes目前是容器編排領域的領頭羊,而Elastic社區發布ECK,使Elasticsearch更容易的跑在云上,也是為云原生技術增磚添瓦,緊跟時代潮流。
部署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
過幾分鐘查看elastic-operator是否運行正常,ECK中只有一個elastic-operator pod:
# kubectl get pods -n elastic-system NAME READY STATUS RESTARTS AGE elastic-operator-0 1/1 Running 1 2m55s
我們測試情況使用1臺master節點和1臺data節點來部署集群,生產環境建議使用3+臺master節點。如下的manifest中,對實例的heap大小,容器的可使用內存,容器的虛擬機內存都進行了配置,可以根據集群需要做調整:
# 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
過段時間,查看elasticsearch集群的狀態
# 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的狀態,我們可以看到申請的pv已經創建和綁定成功:
# 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
默認集群開啟了basic認證,用戶名為elastic,密碼可以通過secret獲取。默認集群也開啟了自簽名證書https訪問。我們可以通過service資源來訪問elasticsearch:
# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE quickstart-es-data-nodes ClusterIP None
不停服,擴容一臺data節點:修改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
不停服,縮容一臺data節點,會自動進行數據同步:修改es.yaml中data-nodes中count的value為1,然后apply下es.yaml即可。
由于默認kibana也開啟了自簽名證書的https訪問,我們可以選擇關閉,我們來使用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中添加一個四層代理,提供對外訪問服務:
# 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
默認kibana訪問elasticsearch的用戶名為elastic,密碼獲取方式如下
# kubectl get secret quickstart-es-elastic-user -o=jsonpath='{.data.elastic}' | base64 --decode; echo
通過瀏覽器訪問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應用的包管理工具helm。Helm是用來封裝 Kubernetes原生應用程序的YAML文件,可以在你部署應用的時候自定義應用程序的一些metadata,helm依賴chart實現了應用程序的在k8s上的分發。helm和chart主要實現了如下功能:
# 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
通過helm安裝cerebro:
# helm install stable/cerebro --version 1.1.4 --generate-name
查看cerebro的狀態:
# kubectl get pods|grep cerebro cerebro-1591777586-7fd87f7d48-hmlp7 1/1 Running 0 11m
由于默認ECK部署的elasticsearch開啟了自簽名證書的https服務,故可以在cerebro配置忽略https證書認證(也可以在cerebro中添加自簽名證書的ca證書來識別自簽名證書),并重啟cerebro:
1,導出cerebro的configmap:
# kubectl get configmap cerebro-1591777586 -o yaml > cerebro.yaml
2,替換configmap中cerebro的hosts相關配置為如下(其中quickstart-es-http為elasticsarch的service資源名字):
play.ws.ssl.loose.acceptAnyCertificate = true hosts = [ { host = "https://quickstart-es-http.default.svc:9200" name = "k8s elasticsearch" } ]
注意:
每一個service在k8s集群dns中多有一個對應的記錄,客戶端訪問service的時候可以通過FQDN來訪問,默認FQDN的格式為servicename.namespace.svc.cluster.local。其中servicename對用server的名字,namespace對應所在的命名空間,svc.cluster.local是k8s集群可以配置集群域后綴。客戶端和服務端在同一個namespace下,可以直接使用servicename訪問,如果在不同namespace下,需要使用servername.namespace進行訪問。當然,如果直接使用完成的FQDN訪問就不用區分namespace了。
3,應用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 -
先確認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",然后通過覽器訪問:
刪除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
看完了這篇文章,相信你對k8s如何部署elasticsearch集群有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。