Kubernetes云原生部署实战:从ECS到K8s集群


阿里云特惠 - 新用户专享

Kubernetes云原生部署实战

越来越多的企业选择将业务从传统的虚拟机架构迁移到Kubernetes容器编排平台。本文介绍如何在阿里云上快速搭建K8s集群,并部署首个容器化应用。

一、阿里云ACK托管集群

ACK(Container Service for Kubernetes)是阿里云托管的K8s服务,我们只需管理Worker节点:

  • 托管版:Master节点由阿里云托管,免运维,推荐生产使用
  • 专有版:自建Master节点,完全控制,适合有K8s运维能力的团队
  • Serverless(ASK):无需管理节点,按Pod使用量计费,适合轻量级工作负载

二、快速创建集群

# 使用阿里云CLI创建托管集群
aliyun cs POST /clusters --body '{
  "cluster_type": "ManagedKubernetes",
  "name": "my-prod-cluster",
  "vpcid": "vpc-xxxxx",
  "vswitch_ids": ["vsw-xxxxx"],
  "container_cidr": "172.20.0.0/16",
  "service_cidr": "172.21.0.0/20",
  "master_instance_type": "ecs.g6.large",
  "worker_instance_type": "ecs.c6.large",
  "worker_nodes_count": 3
}'

# 获取集群凭证
aliyun cs GET /clusters/cluster-xxxxx/kubeconfig --file ~/.kube/config

三、部署首个应用

# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-web
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.25
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80

# 部署
kubectl apply -f nginx-deployment.yaml

四、弹性伸缩配置

# 安装阿里云控件管理器
helm install ack-autoscaler ack-community/autoscaler   --set autoScaler.clusterId=cluster-xxxxx

# 配置HPA(水平Pod自动伸缩)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx-web
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60

五、日志与监控

  • 日志服务SLS:接入ACK日志收集,分析Pod日志和容器事件
  • ARMS应用监控:无侵入式APM监控,跟踪K8s内服务调用链路
  • Prometheus + Grafana:开源方案,监控集群和Workload资源使用

总结

K8s带来了声明式的部署模式和强大的自愈、弹性能力,但同时也增加了学习成本。建议从非核心业务开始,逐步积累经验后再迁移关键业务。

发表评论