如何使用 Minikube 加上 Busybox 驗證 Kubernetes 大全套整合 ELK、Prometheus 和 Grafana
嗨,各位技術同好們!今天我們要來探索一個超有趣的主題:如何在本地的 Minikube 上驗證 Kubernetes 的大全套整合,包括 ELK(Elasticsearch、Logstash 和 Kibana)、Prometheus 和 Grafana。這一切都會在一個方便的小環境裡進行,並且我們還會使用 Busybox 來協助驗證。準備好踏上這趟技術之旅了嗎?讓我們開始吧!
為什麼選擇 Minikube 和 Busybox?
首先,Minikube 是一個超棒的工具,能讓我們在本地環境中快速啟動一個完整的 Kubernetes 叢集。而 Busybox 是一個輕量級的 Linux 工具箱,內含了各種實用的命令,非常適合用來做各種測試和驗證。這兩個工具結合起來,簡直是本地開發和測試的黃金組合!
環境準備
在開始之前,我們需要確保已經安裝並配置好一些基礎工具:
- Minikube
- kubectl
- Helm
- Docker
安裝 Minikube
你可以使用 Homebrew 來安裝 Minikube:
brew install minikube
啟動 Minikube 並分配一些資源:
minikube start --memory 8192 --cpus 4
安裝 kubectl
同樣可以使用 Homebrew 來安裝 kubectl:
brew install kubectl
安裝 Helm
最後,使用 Homebrew 來安裝 Helm:
brew install helm
建立 Kubernetes 命名空間和持久化存儲
我們將為 ELK、Prometheus 和 Grafana 創建獨立的持久化存儲。以下是設定檔案 namespace-and-pv.yaml
:
---
apiVersion: v1
kind: Namespace
metadata:
name: monitoring
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: elasticsearch-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: "/mnt/data/elasticsearch"
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- minikube
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: elasticsearch-pvc
namespace: monitoring
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: local-storage
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: logstash-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: "/mnt/data/logstash"
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- minikube
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: logstash-pvc
namespace: monitoring
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: local-storage
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: kibana-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: "/mnt/data/kibana"
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- minikube
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: kibana-pvc
namespace: monitoring
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: local-storage
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: prometheus-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: "/mnt/data/prometheus"
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- minikube
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: prometheus-pvc
namespace: monitoring
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: local-storage
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: grafana-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: "/mnt/data/grafana"
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- minikube
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: grafana-pvc
namespace: monitoring
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: local-storage
應用這些設定:
kubectl apply -f namespace-and-pv.yaml
安裝 ELK、Prometheus 和 Grafana
1. 添加 Helm 仓库
helm repo add elastic https://helm.elastic.co
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo add grafana https://grafana.github.io/helm-charts
helm repo update
2. 安裝 Elasticsearch
創建一個 elasticsearch-values.yaml
文件:
volumeClaimTemplates:
- metadata:
name: elasticsearch-data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "local-storage"
resources:
requests:
storage: 10Gi
安裝 Elasticsearch:
helm install elasticsearch elastic/elasticsearch --namespace monitoring -f elasticsearch-values.yaml
3. 安裝 Logstash
創建一個 logstash-values.yaml
文件:
logstashConfig:
logstash.yml: |
http.host: "0.0.0.0"
xpack.monitoring.enabled: false
logstash.conf: |
input {
beats {
port => 5044
}
}
output {
elasticsearch {
hosts => ["http://elasticsearch-master:9200"]
}
}
persistence:
enabled: true
existingClaim: logstash-pvc
安裝 Logstash:
helm install logstash elastic/logstash --namespace monitoring -f logstash-values.yaml
4. 安裝 Kibana
創建一個 kibana-values.yaml
文件:
persistence:
enabled: true
existingClaim: kibana-pvc
安裝 Kibana:
helm install kibana elastic/kibana --namespace monitoring -f kibana-values.yaml
5. 安裝
Prometheus
創建一個 prometheus-values.yaml
文件:
server:
persistentVolume:
existingClaim: prometheus-pvc
安裝 Prometheus:
helm install prometheus prometheus-community/prometheus --namespace monitoring -f prometheus-values.yaml
6. 安裝 Grafana
創建一個 grafana-values.yaml
文件:
datasources:
datasources.yaml:
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
url: http://prometheus-server.monitoring.svc.cluster.local
access: proxy
isDefault: true
persistence:
enabled: true
existingClaim: grafana-pvc
安裝 Grafana:
helm install grafana grafana/grafana --namespace monitoring -f grafana-values.yaml
驗證安裝
1. 驗證 Elasticsearch
kubectl get pods --namespace monitoring -l app=elasticsearch
2. 驗證 Logstash
kubectl get pods --namespace monitoring -l app=logstash
3. 驗證 Kibana
kubectl get pods --namespace monitoring -l app=kibana
4. 驗證 Prometheus
kubectl get pods --namespace monitoring -l app=prometheus
5. 驗證 Grafana
kubectl get pods --namespace monitoring -l app.kubernetes.io/name=grafana
訪問 Grafana 和 Kibana
訪問 Grafana
將 Grafana 服務暴露到本地端口:
kubectl port-forward svc/grafana 3000:80 --namespace monitoring
然後在瀏覽器中訪問 http://localhost:3000。
訪問 Kibana
將 Kibana 服務暴露到本地端口:
kubectl port-forward svc/kibana-kibana 5601:5601 --namespace monitoring
然後在瀏覽器中訪問 http://localhost:5601。
部署應用程序
以下是應用程序部署的示例配置文件 deployments.yaml
:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: myapp-service
namespace: default
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 8080
應用這些設定:
kubectl apply -f deployments.yaml
總結
通過本文,我們學會了如何使用 Minikube 和 Busybox 在本地環境中快速驗證 Kubernetes 的 ELK、Prometheus 和 Grafana 整合。我們一步步建立了命名空間和持久化存儲,並使用 Helm 安裝了所有需要的工具。希望這篇文章能幫助你在本地環境中更好地測試和開發 Kubernetes 應用。如果有任何問題或建議,歡迎在下方留言!
Happy Kubernetes-ing! 🚀