Cấu hình Prometheus và Grafana để giám sát toàn bộ hệ thống
Bước đầu tiên là triển khai Prometheus để thu thập metrics từ Backstage, Crossplane, ArgoCD và Kubernetes cluster. Chúng ta sẽ sử dụng kube-prometheus-stack qua Helm.
Triển khai Prometheus với cấu hình đặc biệt để scrape các endpoint của Backstage và Crossplane.
helm install monitoring prometheus-community/kube-prometheus-stack \
--namespace monitoring \
--create-namespace \
--set prometheus.prometheusSpec.scrapeConfigSecrets=[{"name": "backstage-scrape-config","matchLabelKey": "prometheus","matchLabelValues": ["backstage"]}] \
--set grafana.adminPassword="admin" \
--set grafana.service.type=LoadBalancer
Kết quả: Namespace "monitoring" được tạo, các Pod Prometheus và Grafana đang ở trạng thái Running.
Tạo Secret chứa cấu hình scrape cho Backstage để Prometheus có thể đọc metrics từ port 7007 của Backstage.
cat
Kết quả: Secret "backstage-scrape-config" được tạo trong namespace monitoring.
Cấu hình Scrape Config cho Crossplane. Crossplane thường chạy metrics trên port 8080, cần thêm endpoint này vào Prometheus.
cat
Kết quả: ServiceMonitor "crossplane" được tạo, Prometheus sẽ tự động phát hiện và scrape metrics từ Crossplane.
Tiếp theo, truy cập Grafana để xem dashboard. Đầu tiên, expose Grafana service ra public hoặc internal network.
EXTERNAL_IP=$(kubectl get svc -n monitoring grafana -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo "Grafana URL: http://$EXTERNAL_IP"
Kết quả: In ra địa chỉ IP của LoadBalancer để truy cập Grafana.
Import dashboard tiêu chuẩn cho Kubernetes và Backstage vào Grafana. Sử dụng Dashboard ID chính thức từ Grafana.com.
# Dashboard Kubernetes Cluster Monitoring (ID: 14589)
curl -s https://grafana.com/api/dashboards/14589/revisions/1/download | \
jq '.dashboard' > /tmp/k8s-dashboard.json
# Dashboard Backstage (ID: 16863 - ví dụ, cần cập nhật ID mới nhất nếu có)
# Giả sử bạn đã có file json dashboard backstage
# curl -s https://raw.githubusercontent.com/backstage/backstage/master/packages/backend/dev/kubernetes/dashboard.json > /tmp/backstage-dashboard.json
# Upload vào Grafana qua API (cần token admin)
# Ở đây dùng kubectl exec để upload file vào container Grafana
kubectl cp /tmp/k8s-dashboard.json monitoring/grafana-:/tmp/k8s-dashboard.json -n monitoring
kubectl exec -it monitoring/grafana- -n monitoring -- /bin/sh -c "cat /tmp/k8s-dashboard.json | /usr/share/grafana/bin/grafana-cli dashboards import"
Kết quả: Dashboard Kubernetes xuất hiện trong thư mục Dashboards của Grafana.
Verify kết quả: Truy cập Grafana qua IP đã lấy, vào menu "Data Sources", đảm bảo Prometheus đang ở trạng thái "Health: OK". Sau đó vào "Dashboards", chọn "Kubernetes / Cluster", thấy các biểu đồ CPU, Memory của Nodes và Pods đang nhảy số.
Phân tích logs và xử lý lỗi phổ biến trong Backstage và Crossplane
Xử lý lỗi kết nối Database trong Backstage
Lỗi phổ biến nhất là Backstage không thể kết nối đến Postgres do sai cấu hình hoặc SSL. Kiểm tra logs của container backend.
kubectl logs -l app=backstage -c backend --tail=50 -n backstage-system
Kết quả mong đợi: Nếu có lỗi "FATAL: no pg_hba.conf entry for host...", nghĩa là Postgres từ chối kết nối.
Giải pháp: Chỉnh sửa Postgres configuration để cho phép kết nối từ bất kỳ host nào trong cluster (trong môi trường dev/test).
cat
Sau đó, mount file config này vào container Postgres (giả sử bạn dùng StatefulSet Postgres). Cần update StatefulSet để thêm volumeMount.
kubectl edit statefulset postgres -n backstage-system
Sửa nội dung StatefulSet thêm volume và volumeMount:
# Thêm vào spec.template.spec.volumes
- name: postgres-config
secret:
secretName: postgres-config
# Thêm vào spec.template.spec.containers[0].volumeMounts
- name: postgres-config
mountPath: /var/lib/postgresql/data/pg_hba.conf
subPath: pg_hba.conf
Kết quả: Backstage khởi động lại và kết nối thành công vào database.
Xử lý lỗi "Resource Not Found" trong Crossplane
Crossplane báo lỗi khi không tìm thấy Provider hoặc XRM (Composite Resource) tương ứng. Kiểm tra logs của Crossplane.
kubectl logs -l control-plane=crossplane -n crossplane-system --tail=50
Kết quả mong đợi: Thông báo "No provider found for APIVersion..." hoặc "Composite resource definition not found".
Giải pháp: Đảm bảo Provider được install và ở trạng thái Healthy. Kiểm tra trạng thái của Provider.
kubectl get provider -n crossplane-system
Nếu Provider đang ở trạng thái "NotReady" hoặc "Failed", kiểm tra logs của Provider deployment.
kubectl get deployment -n crossplane-system | grep provider
kubectl logs -l app=provider-aws -n crossplane-system --tail=50
Trường hợp phổ biến: Thiếu credential. Crossplane cần Secret chứa AWS credentials.
kubectl create secret generic aws-creds \
--from-literal=aws-access-key-id=YOUR_KEY \
--from-literal=aws-secret-access-key=YOUR_SECRET \
-n crossplane-system
Cập nhật ProviderConfig để trỏ vào secret này.
kubectl edit providerconfig aws --namespace crossplane-system
Sửa phần credentialSource thành "Secret" và chỉ định secretName.
spec:
providerConfigRef:
name: aws
credentialSource:
secret:
name: aws-creds
key: aws-access-key-id
secretKeyRef:
name: aws-creds
key: aws-secret-access-key
Kết quả: Provider quay về trạng thái "Healthy", Crossplane bắt đầu reconcile các tài nguyên.
Phân tích lỗi GitOps (ArgoCD)
Khi ArgoCD báo trạng thái "OutOfSync" hoặc "Degraded", cần kiểm tra App và logs của ArgoCD.
kubectl get app -n argocd
kubectl logs -l app.kubernetes.io/name=argocd-server -n argocd --tail=100
Kết quả mong đợi: Nếu thấy lỗi "failed to get config", kiểm tra SSH key hoặc token Git trong ArgoCD.
Giải pháp: Reset hoặc cập nhật Secret chứa Git credentials.
kubectl edit secret argocd-git-ssh-known-hosts-secret -n argocd
kubectl edit secret argocd-git-creds -n argocd
Sau khi sửa, force sync lại ứng dụng.
argocd app sync your-app-name --prune
Kết quả: ArgoCD trạng thái quay về "Synced" và "Healthy".
Verify kết quả: Chạy lệnh kubectl get app -n argocd, tất cả app đều có trạng thái "Synced" và "Healthy". Logs không còn dòng "Error" liên tục.
Chiến lược backup và restore cho catalog và configuration
Backup Database Postgres (Backstage Catalog)
Backstage lưu trữ Catalog (Software Templates, Entities) trong Postgres. Backup database là bắt buộc.
Sử dụng lệnh pg_dump để xuất database ra file SQL.
kubectl exec -it -n backstage-system -- pg_dump -U postgres -d backstage > /tmp/backstage-backup-$(date +%F).sql
Kết quả: File SQL chứa toàn bộ dữ liệu catalog được tạo trên node local.
Tự động hóa backup với CronJob trong Kubernetes để chạy mỗi ngày vào 2h sáng.
cat
Kết quả: CronJob được tạo, sẽ tự động tạo file backup vào PVC.
Backup Configuration (Crossplane & ArgoCD)
Crossplane và ArgoCD lưu cấu hình trong Kubernetes Resources. Backup bằng cách export tất cả resources.
Export toàn bộ resources của Crossplane và ArgoCD ra file YAML.
kubectl get all,cm,secret,provider,providerconfig,composition -n crossplane-system -o yaml > /tmp/crossplane-config-backup.yaml
kubectl get all,cm,secret,application -n argocd -o yaml > /tmp/argocd-config-backup.yaml
Kết quả: Hai file YAML chứa toàn bộ cấu hình của hệ thống GitOps và Crossplane.
Restore dữ liệu khi sự cố xảy ra.
Restore Database Backstage.
kubectl cp /tmp/backstage-backup-2023-10-27.sql :/tmp/restore.sql -n backstage-system
kubectl exec -it -n backstage-system -- psql -U postgres -d backstage -f /tmp/restore.sql
Kết quả: Dữ liệu catalog được khôi phục, Backstage cần khởi động lại để đọc dữ liệu mới.
Restore Configuration Crossplane và ArgoCD.
kubectl apply -f /tmp/crossplane-config-backup.yaml
kubectl apply -f /tmp/argocd-config-backup.yaml
Kết quả: Cấu hình Crossplane và ArgoCD được khôi phục, các resource sẽ được reconcile lại.
Verify kết quả: Sau khi restore, truy cập Backstage UI, kiểm tra xem Catalog còn nguyên dữ liệu. Chạy lệnh kubectl get app -n argocd và kubectl get xr -n crossplane-system để đảm bảo các resource đã quay lại.
Tips nâng cao: Custom plugin, tích hợp external tools và Best practices
Viết Custom Plugin cho Backstage
Để mở rộng Backstage, bạn cần viết plugin riêng. Sử dụng CLI của Backstage để tạo plugin mới.
npx @backstage/create-plugin my-custom-plugin
Kết quả: Thư mục plugin mới được tạo với cấu trúc chuẩn.
Cấu trúc cơ bản của plugin: Component React, Plugin Router, và Plugin Definition.
# src/index.ts
export { myCustomPlugin, MyCustomPage } from './plugin';
# src/plugin.ts
import { createPlugin } from '@backstage/core-plugin-api';
export const myCustomPlugin = createPlugin({
id: 'my-custom-plugin',
routes: {
root: createRoute('my-custom-plugin'),
},
});
export const MyCustomPage = () => {
return Custom Plugin Content;
};
Kết quả: Plugin được build và sẵn sàng để link vào Backstage backend/frontend.
Tích hợp plugin vào Backstage bằng cách cập nhật file catalog-info.yaml của Backstage.
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: backstage
spec:
type: website
lifecycle: experimental
system: backstage
dependsOn:
- my-custom-plugin
Kết quả: Plugin xuất hiện trong menu của Backstage.
Tích hợp với External Tools (Jira, Slack)
Tích hợp Jira để hiển thị ticket trong Backstage. Cài đặt plugin jira-backend.
npm install @backstage/plugin-jira-backend
Cấu hình plugin trong app-config.yaml.
catalog:
import:
jira:
jiraUrl: https://your-domain.atlassian.net
token: YOUR_API_TOKEN
username: YOUR_EMAIL
Kết quả: Backstage có thể sync dữ liệu từ Jira vào Catalog.
Tích hợp Slack để gửi thông báo khi có sự cố hoặc deploy mới.
npm install @backstage/plugin-slack-backend
Cấu hình Slack trong app-config.yaml.
integrations:
slack:
- url: https://hooks.slack.com/services/YOUR/WEBHOOK/URL
token: YOUR_SLACK_TOKEN
Kết quả: Backstage có thể gửi thông báo vào channel Slack.
Best Practices cho IDP
- Security First: Luôn sử dụng mTLS giữa các service trong cluster. Sử dụng OPA Gatekeeper để enforce policy.
- GitOps Discipline: Không bao giờ thay đổi resource trực tiếp trên cluster. Mọi thay đổi phải qua Git commit.
- Monitoring: Đặt alert cho các metric quan trọng như CPU, Memory, Error Rate, Request Latency.
- Documentation: Giữ tài liệu luôn cập nhật trong Backstage Catalog. Sử dụng template để tự động hóa việc tạo tài liệu.
- Testing: Viết unit test và integration test cho custom plugin và Crossplane composition.
Verify kết quả: Hệ thống chạy ổn định, các plugin custom hoạt động, thông báo Slack được gửi đúng lúc, và policy OPA chặn được các config không an toàn.
Điều hướng series:
Mục lục: Series: Series: Xây dựng nền tảng Internal Developer Platform (IDP) với Backstage, Crossplane và GitOps trên Kubernetes để tối ưu trải nghiệm phát triển phần mềm
« Phần 7: Tối ưu hóa hiệu năng và mở rộng quy mô