K8s 工作流程
K8s 采用声明式管理(用户说"要什么",K8s 负责"怎么做")方式,通过 YAML 文件描述期望的状态,K8s控制平面会自动确保实际状态与期望状态一致。
核心工作流程如下:
用户提交 YAML → API Server → etcd 存储 → 控制器监听 → 调度器分配 → Kubelet 执行 → 运行状态监控
- 用户定义期望状态:通过 YAML 文件定义工作负载
- API Server 接收:验证并存储到 etcd
- 控制器工作:各种控制器监听资源变化
- 调度决策:Scheduler 决定 Pod 运行在哪个节点
- 节点执行:Kubelet 拉取镜像并启动容器
- 持续监控:控制器持续确保实际状态符合期望
Kubernetes Yaml标准结构
实际上用户只用考虑怎么编写yaml文件布置任务。
YAML 基础语法结构
#注释以 # 开头
key: value # 键值对
string_value: "Hello World" # 字符串
number_value: 42 # 数字
boolean_value: true # 布尔值
null_value: null # 空值
#数据结构类型
#对象/映射(Object/Mapping)
person:
name: "张三"
age: 30
city: "北京"
#数组/列表(Array/List)
fruits:
- apple
- banana
- orange
#或者内联格式
fruits: [apple, banana, orange]
#嵌套结构
company:
name: "科技公司"
employees:
- name: "李四"
position: "工程师"
- name: "王五"
position: "设计师"
locations:
- "北京"
- "上海"
Kubernetes YAML 文件标准结构
四大核心部分
#1. API 版本
apiVersion: apps/v1
#e.g.
apiVersion: apps/v1 # Deployment, ReplicaSet, DaemonSet
apiVersion: v1 # Pod, Service, ConfigMap
apiVersion: batch/v1 # Job
apiVersion: batch/v1beta1 # CronJob
#2. 资源类型
kind: Deployment
#e.g.
kind: Deployment # 部署
kind: Service # 服务
kind: ConfigMap # 配置映射
kind: Secret # 密钥
kind: Pod # Pod
#工作负载类型:
#Deployment:长期运行的无状态应用
#StatefulSet:有状态应用(如数据库)
#DaemonSet:每个节点运行一个实例(如日志收集)
#Job/CronJob:一次性任务或定时任务
#3. 元数据
metadata:
name: my-app
namespace: default
labels:
app: my-app
version: v1.0
#e.g.
metadata:
name: my-application # 必需:资源名称
namespace: production # 可选:命名空间
labels: # 可选:标签
app: web-server
environment: production
version: "1.0"
annotations: # 可选:注解
description: "主要的web服务器"
created-by: "开发团队"
#4. 规格定义
spec:
# 具体配置内容
#e.g.
spec:
replicas: 3 # 副本数量
selector: # 选择器
matchLabels:
app: web-server
template: # Pod 模板
metadata:
labels:
app: web-server
spec:
containers: # 容器定义
- name: web-container
image: nginx:1.20
ports:
- containerPort: 80
env: # 环境变量
- name: ENV_VAR
value: "production"
resources: # 资源限制
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
完整的 Deployment 示例
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
labels:
app: nginx
environment: production
annotations:
description: "Nginx web server deployment"
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.20
ports:
- containerPort: 80
name: http
env:
- name: NGINX_PORT
value: "80"
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 5
restartPolicy: Always
实际工作示例
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3 # 安排:运行3个副本
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.20 # 安排:使用nginx镜像
ports:
- containerPort: 80
Kubernetes 收到这个"工作安排"后会:
工作内容是创建 3 个 nginx Pod,如果某个 Pod 失败,自动重新创建,确保始终有 3 个健康的 Pod 运行。