首页
/ Kubernetes 手册 —— 快速上手与实践指南

Kubernetes 手册 —— 快速上手与实践指南

2024-08-11 06:58:57作者:尤辰城Agatha

1. 项目介绍

《Kubernetes Handbook》是由 RootsongJC 开发并维护的一份开源电子书,致力于提供详尽的 Kubernetes 学习资料和实践指导。这本书旨在帮助读者掌握 Kubernetes 的核心概念、操作技巧以及最新的发展动态。内容覆盖了从基础部署到高级应用场景的多个方面,适合对容器编排有兴趣或正在使用 Kubernetes 的开发者和运维人员。

项目链接:https://github.com/rootsongjc/kubernetes-handbook.git

2. 项目快速启动

安装 Minikube(本地快速搭建 Kubernetes 环境)

首先确保你的机器上安装了 kubectlminikube

# 安装 kubectl
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/

# 安装 minikube
curl -LO "https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64" && chmod +x minikube-darwin-amd64
sudo mv minikube-darwin-amd64 /usr/local/bin/minikube

# 启动 Minikube
minikube start --driver=docker

部署第一个应用

创建一个名为 hello-world.yaml 的 YAML 文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-app
        image: gcr.io/google-samples/hello-app:1.0
        ports:
          - containerPort: 8080

部署应用:

kubectl apply -f hello-world.yaml

暴露服务:

kubectl expose deployment hello-world --type=LoadBalancer --port=8080

访问应用:

kubectl get svc | grep hello-world # 获取服务外部IP
curl $(kubectl get svc hello-world -o jsonpath='{.status.loadBalancer.ingress[0].ip}') # 访问应用

3. 应用案例和最佳实践

  • 滚动更新:通过修改 Deployment 中的镜像版本,Kubernetes 会平滑地将旧版本替换为新版本。
kubectl set image deployment/hello-world hello-app=gcr.io/google-samples/hello-app:2.0
  • 自动扩展:设置 Horizontal Pod Autoscaler (HPA),让 Kubernetes 根据资源利用率自动调整副本数量。
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: hello-world-hpa
spec:
  maxReplicas: 5
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: hello-world
  targetCPUUtilizationPercentage: 50
  • 服务网格:利用 Istio 或 Linkerd 提供微服务间的流量管理和安全策略。

4. 典型生态项目

  • Helm:Kubernetes 的包管理工具,用于安装、升级和管理应用程序。
  • Flux:持续交付工具,自动化 Helm Chart 的版本控制和部署。
  • Prometheus:监控和警报系统,集成 Kubernetes 以收集各种指标。
  • Jaeger:分布式跟踪系统,用于性能优化和故障排查。
  • Velero:备份和恢复工具,提供 Kubernetes 集群的数据安全性。

通过这些模块,你可以快速地了解并开始使用 Kubernetes。更多细节和深入内容,请参考项目提供的完整手册。祝你在 Kubernetes 之旅中一切顺利!

登录后查看全文
热门项目推荐