别着急,坐和放宽
示例部署资源清单:
apiVersion: apps/v1
kind: Deployment
metadata:
name: whoami-deploy
spec:
replicas: 1
selector:
matchLabels:
app: whoami
release: canary
template:
metadata:
labels:
app: whoami
release: canary
spec:
containers:
- name: whoami
image: traefik/whoami:v1.10
ports:
- name: http
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: whoami
spec:
type: NodePort
selector:
app: whoami
release: canary
ports:
- name: http
port: 80
targetPort: 80 # pod port
nodePort: 30080
部署:
kubectl apply -f xxx.yaml
我来为您解释这个 Kubernetes 配置文件中的关键参数:
apiVersion: apps/v1 # K8s API 版本
kind: Deployment # 资源类型为 Deployment
metadata:
name: whoami-deploy - Deployment 的名称spec:
replicas: 1 - 指定要运行的 Pod 副本数量selector - 用于选择要管理哪些 Pod
matchLabels - Pod 必须包含这些标签才会被该 Deployment 管理
app: whoamirelease: canarytemplate: - Pod 的模板定义
metadata/labels - 为创建的 Pod 添加标签containers - 容器配置
image: traefik/whoami:v1.10 - 使用的容器镜像containerPort: 80 - 容器内部暴露的端口kind: Service # 资源类型为 Service
type: NodePort # Service 类型为 NodePort,允许从集群外访问
selector: - 选择要暴露哪些 Pod
app: whoamirelease: canaryports:
port: 80 - Service 的端口targetPort: 80 - Pod 的目标端口nodePort: 30080 - 节点上暴露的端口(可以通过 <节点IP>:30080 访问)这是一个完整的应用部署配置,包含了 Deployment(管理 Pod)和 Service(暴露服务)两部分
标签(Labels)在这里起到了关键作用:
端口映射关系:
NodePort 类型的 Service 允许从集群外部访问服务,端口范围通常在 30000-32767 之间