Kubernetes 基础
2026/1/15大约 3 分钟
Kubernetes 基础
什么是 Kubernetes
Kubernetes(K8s)是一个开源的容器编排平台,用于自动化部署、扩展和管理容器化应用。
架构概览
┌─────────────────────────────────────────────────────────────────┐
│ Kubernetes 集群 │
├─────────────────────────────────────────────────────────────────┤
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Master Node │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │API Server│ │Scheduler │ │Controller│ │ etcd │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌───────────────────────────┼───────────────────────────────┐ │
│ │ Worker Nodes │ │
│ │ ┌─────────────────┐ ┌─────────────────┐ │ │
│ │ │ Worker Node │ │ Worker Node │ │ │
│ │ │ ┌─────┐ ┌─────┐ │ │ ┌─────┐ ┌─────┐ │ │ │
│ │ │ │ Pod │ │ Pod │ │ │ │ Pod │ │ Pod │ │ │ │
│ │ │ └─────┘ └─────┘ │ │ └─────┘ └─────┘ │ │ │
│ │ │ ┌─────────────┐ │ │ ┌─────────────┐ │ │ │
│ │ │ │ Kubelet │ │ │ │ Kubelet │ │ │ │
│ │ │ │ Kube-proxy │ │ │ │ Kube-proxy │ │ │ │
│ │ │ └─────────────┘ │ │ └─────────────┘ │ │ │
│ │ └─────────────────┘ └─────────────────┘ │ │
│ └───────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘核心组件
| 组件 | 作用 |
|---|---|
| API Server | 集群入口,处理 REST 请求 |
| Scheduler | 调度 Pod 到合适的 Node |
| Controller Manager | 管理各种控制器 |
| etcd | 分布式键值存储,保存集群状态 |
| Kubelet | 管理 Node 上的 Pod |
| Kube-proxy | 实现 Service 的网络代理 |
Pod
Pod 是 K8s 最小的部署单元,包含一个或多个容器。
Pod 定义
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:1.0
ports:
- containerPort: 8080
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
env:
- name: SPRING_PROFILES_ACTIVE
value: "prod"
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5Pod 操作
# 创建 Pod
kubectl apply -f pod.yaml
# 查看 Pod
kubectl get pods
kubectl get pods -o wide
# 查看 Pod 详情
kubectl describe pod myapp-pod
# 查看 Pod 日志
kubectl logs myapp-pod
kubectl logs -f myapp-pod # 实时查看
# 进入 Pod
kubectl exec -it myapp-pod -- /bin/sh
# 删除 Pod
kubectl delete pod myapp-podDeployment
Deployment 用于管理 Pod 的副本,支持滚动更新和回滚。
Deployment 定义
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: myapp
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:1.0
ports:
- containerPort: 8080
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"Deployment 操作
# 创建 Deployment
kubectl apply -f deployment.yaml
# 查看 Deployment
kubectl get deployments
# 扩缩容
kubectl scale deployment myapp-deployment --replicas=5
# 更新镜像
kubectl set image deployment/myapp-deployment myapp=myapp:2.0
# 查看更新状态
kubectl rollout status deployment/myapp-deployment
# 查看历史版本
kubectl rollout history deployment/myapp-deployment
# 回滚
kubectl rollout undo deployment/myapp-deployment
kubectl rollout undo deployment/myapp-deployment --to-revision=2Service
Service 为 Pod 提供稳定的网络访问入口。
Service 类型
┌─────────────────────────────────────────────────────────┐
│ Service 类型 │
├─────────────┬───────────────────────────────────────────┤
│ ClusterIP │ 默认类型,集群内部访问 │
│ NodePort │ 通过 Node IP:Port 访问 │
│ LoadBalancer│ 云厂商负载均衡器 │
│ ExternalName│ 映射外部服务 │
└─────────────┴───────────────────────────────────────────┘Service 定义
# ClusterIP
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
type: ClusterIP
selector:
app: myapp
ports:
- port: 80
targetPort: 8080
---
# NodePort
apiVersion: v1
kind: Service
metadata:
name: myapp-nodeport
spec:
type: NodePort
selector:
app: myapp
ports:
- port: 80
targetPort: 8080
nodePort: 30080
---
# LoadBalancer
apiVersion: v1
kind: Service
metadata:
name: myapp-lb
spec:
type: LoadBalancer
selector:
app: myapp
ports:
- port: 80
targetPort: 8080Service 操作
# 创建 Service
kubectl apply -f service.yaml
# 查看 Service
kubectl get services
kubectl get svc
# 查看 Service 详情
kubectl describe service myapp-service
# 删除 Service
kubectl delete service myapp-serviceNamespace
Namespace 用于隔离资源。
apiVersion: v1
kind: Namespace
metadata:
name: dev# 创建 Namespace
kubectl create namespace dev
# 查看 Namespace
kubectl get namespaces
# 在指定 Namespace 操作
kubectl get pods -n dev
kubectl apply -f app.yaml -n dev
# 设置默认 Namespace
kubectl config set-context --current --namespace=dev常用命令
# 集群信息
kubectl cluster-info
kubectl get nodes
# 资源操作
kubectl get <resource> # 查看资源
kubectl describe <resource> # 查看详情
kubectl apply -f <file> # 创建/更新资源
kubectl delete -f <file> # 删除资源
# 调试
kubectl logs <pod> # 查看日志
kubectl exec -it <pod> -- sh # 进入容器
kubectl port-forward <pod> 8080:80 # 端口转发
# 资源类型简写
# pods -> po
# services -> svc
# deployments -> deploy
# namespaces -> ns
# configmaps -> cm
# secrets -> secret