目录:
Deployment 配置示例:
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-deployment
spec:
replicas: 2 # 指定同时运行 2 个相同的 Pod 副本,提供基本的高可用
selector:
matchLabels:
app: frontend # 这个选择器必须与下面 Pod 模板的标签匹配
template:
metadata:
labels:
app: frontend # 给 Pod 打上标签,Service 和 Ingress 通过此标签找到它们
spec:
containers:
- name: frontend-container
image: my-frontend-app:1.0 # 使用我们刚刚构建的镜像
imagePullPolicy: Never # 显式设置拉取策略
ports:
- containerPort: 80 # 容器内部监听的端口
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "200m"
配置文件以 apiVersion 和 kind 字段开头,指明所使用的 Kubernetes API 版本和资源类型。对于 Deployment,通常使用 apps/v1。
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment # 必需
namespace: my-namespace # 可选,默认为 default
labels: # 可选,用于识别和选择
app: my-app
env: production
annotations: # 可选,用于记录附加信息
description: "This is my web server deployment."
prometheus.io/scrape: "true"
🎈 name 即 Deployment 的名称。
🎈 namespace
命名空间,比如一个应用有开发、测试和生产三个环境,一种方式是三个 deployment 的 name 不一样,比如 my-app-dev、my-app-test 和 my-app-live,另一种方式是允许 deployment 的 name 一样,通过添加 namespace 来区分不同环境。
🎈 labels
是以键值对形式定义的标签,用于 K8s 系统内部对资源进行识别、筛选和组织。例如,一个 Service 可以通过 selector 匹配 Pod 的 labels 来找到其代理的后端。
🎈 annotations
注解,与 labels 类似,annotations 也是键值对,主要用途是存储非识别性的元数据,供人类或其他外部工具(如 Ingress Controller、监控系统)查看和使用,常用于记录描述信息、配置第三方工具扩展功能等。
🎈 uid
唯一标识符,这是由 Kubernetes 系统自动生成的、在整个集群范围内真正唯一的 ID,用于在底层唯一标识对象。它通常不需要也不应该在 YAML 文件中手动配置。
spec 部分是 Deployment 配置的核心,定义了应用的实际运行规格。
🎈 replicas: 指定运行的 Pod 副本数量,决定了应用的冗余和负载能力。例如,replicas: 2 会确保始终有 2 个 Pod 实例在运行。
🎈 selector: 定义 Deployment 如何找到 Pod。通过 matchLabels 指定一组标签选择器,必须与 Pod 模板中定义的标签一致。
🎈 template: 定义了 Pod 的模板,这是创建实际 Pod 的蓝图。
示例如下:
spec: # <- Deployment
replicas: 2 # 指定同时运行 2 个相同的 Pod 副本,提供基本的高可用
selector:
matchLabels:
app: my-app # 这个选择器必须与下面 Pod 模板的标签匹配
template: # <- Pod
metadata:
labels:
app: my-app # 给 Pod 打上标签,Deployment、Service 和 Ingress 通过此标签找到它们
在 spec.template.spec.containers 中,可以定义一个或多个容器。每个容器通常需要配置:
🎈 name: 容器名称;
🎈 ports: 容器暴露的端口列表,通过 containerPort 指定。
🎈 image: 容器镜像地址及标签。避免使用 latest 标签,应指定明确的版本号以提高可追溯性和稳定性;
🎈 imagePullPolicy: 镜像拉取策略
containers:
- name: frontend-container
image: my-frontend-app:1.0 # 使用我们刚刚构建的镜像
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80 # 容器内部监听的端口
可以为容器设置 CPU 和内存的请求值/最小值(requests)和上限值(limits)。
一个完整的示例如下:
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
为了确保应用的高可用性,应配置存活探针(livenessProbe)和就绪探针(readinessProbe)。
🎈 livenessProbe: 用于检测容器是否正在运行。如果检查失败,kubelet 会重启容器。
🎈 readinessProbe: 用于检测容器是否已准备好接收流量。如果检查失败,会将 Pod 从S ervice 的负载均衡端点中移除。
常见的配置是使用 HTTP GET 请求进行检查:
livenessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 30
readinessProbe:
httpGet:
path: /
port: 8080
Service 配置示例:
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
selector:
app: frontend # 选择所有具有`app: frontend`标签的 Pod
ports:
- protocol: TCP
port: 80 # Service 对集群内暴露的端口
targetPort: 80 # 请求转发到 Pod 的容器端口
type: ClusterIP # 默认类型,仅在集群内部可访问
对于 Service 资源,apiVersion 值固定为 v1,kind 值固定为 Service。
apiVersion: v1
kind: Service
资源的元数据,用于标识和描述 Service 本身
🎈 name: 即服务名称。
🎈 namespace: 命名空间。若不指定,默认为 default。
🎈 labels: 添加的标签,用于组织和选择资源。
🎈 annotations: 注解,用于存储非标识性的配置信息,例如云服务商的特定配置(如 service.beta.kubernetes.io/aws-load-balancer-type: nlb)
metadata:
name: frontend-service
namespace: develop
labels: # 可选,用于识别和选择
app: my-fe-app
env: develop
annotations: # 可选,用于记录附加信息
description: "This is my web service."
标签选择器,用于确定 Service 要代理的后端 Pod,通过键值对匹配 Pod 的 labels。
例如 app: my-app 会选择所有带有 app=my-app 标签的 Pod。这是 Service 与 Pod 动态关联的基础。
spec:
selector:
app: frontend # 选择所有具有`app: frontend`标签的 Pod
Service 类型,决定了服务的暴露方式:
🎈 ClusterIP: 默认类型,为 Service 分配一个仅在 Kubernetes 集群内部可访问的虚拟 IP 地址,即 ClusterIP。
apiVersion: v1
kind: Service
metadata:
name: backend
spec:
type: ClusterIP # 默认值,可省略
selector:
app: backend
ports:
- port: 80 # Service 端口
targetPort: 8080 # Pod 容器端口
🎈 NodePort: 在 ClusterIP 基础上,在每个工作节点(Node)的 IP 地址上静态开放一个相同的端口(默认范围 30000–32767)。外部客户端可以通过访问任意节点的 <NodeIP>:<NodePort> 来访问服务。
用途: 允许集群外部的客户端(如浏览器、其他程序)直接访问服务。
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: NodePort
selector:
app: web
ports:
- port: 80 # Service 端口
targetPort: 80 # Pod 容器端口
nodePort: 30080 # 可选,不指定则由系统在范围内自动分配
🎈 LoadBalancer: 在 NodePort 基础上,如果运行在支持的云平台上,Kubernetes 会自动创建一个外部负载均衡器,并将流量导向该 Service。负载均衡器会提供一个外部 IP 供访问。
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 80
应用后,使用 kubectl get svc 命令可以看到该 Service 被分配了 EXTERNAL-IP
🎈 ExternalName: 一种特殊类型,将 Service 映射到一个外部域名(如 mydb.example.com)。它没有选择器 (selector) 和端口 (ports),主要用于让集群内部服务方便地访问外部服务。
apiVersion: v1
kind: Service
metadata:
name: my-database
spec:
type: ExternalName
externalName: mydb.example.com # 外部域名,不能是 IP 地址
端口列表,定义流量转发规则。每个端口项可配置:
🎈 name: 端口名称,可选,但在多端口配置中建议使用以便区分。
🎈 protocol: 协议,如 TCP、UDP 或 SCTP,默认为 TCP。
🎈 port: Service 自身暴露的端口,即客户端访问 Service 时使用的端口。
🎈 targetPort: Pod 上容器监听的端口,流量最终会被转发到此端口。它可以指定为数字或 Pod 中定义的容器端口名称。
🎈 nodePort: 当 type 为 NodePort 或 LoadBalancer 时,可指定节点上开放的端口号。若不指定,系统会在默认范围内自动分配。
spec:
ports:
- protocol: TCP
port: 80
targetPort: 8080
Ingress 配置示例:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: frontend-ingress
spec:
rules:
- host: frontend.local # 配置一个本地测试用的域名
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: frontend-service # 将流量指向我们创建的 Service
port:
number: 80
定义路由规则的核心字段。每个规则可以包含:
🎈 host:匹配请求的域名(如 app.example.com)。不指定则匹配所有主机名的请求。
🎈 http.paths:路径规则列表。每个路径包含:
使用 curl -H "Host: app.example.com" 测试路由。
# 查看 Pod 列表
$ kubectl get pods
# 查看 Pod 列表详细信息
$ kubectl get pods -o wide
# 查看指定 Pod 信息
$ kubectl describe pod <pod_name>
# 进入 Pod 内容器
$ kubectl exec -it <pod_name> -- /bin/bash
# 查看 Deployment 列表
$ kubectl get deployment
$ kubectl get deployments
# 将 Deployment 部署到 K8s 集群中
$ kubectl apply -f new-deployment.yaml
# 将目标 Deployment 的完整定义导出到本地文件
$ kubectl get deployment <原Deployment> -o yaml > new-deployment.yaml
# 删除 Deployment
$ kubectl delete deployment <deployment-name>
$ kubectl delete -f deployment.yaml
# 查看 Service 列表
$ kubectl get services
$ kubectl get service
$ kubectl get svc
# 应用服务
$ kubectl apply -f new-service.yaml
# 删除 Service
$ kubectl delete service <service-name>
$ kubectl delete -f service.yaml
# 查看 Ingress 列表
$ kubectl get ingress
# 查看 Ingress 状态
$ kubectl describe ingress <name>
# 删除 Ingress
$ kubectl delete ingress <ingress-name>
$ kubectl delete -f ingress.yaml
# 检查集群状态是否正常
$ kubectl cluster-info
# 启动 minikube 集群
$ minikube start
# 查看 minikube 集群状态
$ minikube status
# 停止 minikube 集群
$ minikube stop
# 查看集群 IP
$ minikube ip
# 让本地计算机能够直接访问 K8s 集群中类型为 LoadBalancer 的服务
$ minikube tunnel
# 绕过 Ingress 直接访问 Service,用返回的 URL 在浏览器中访问
# 如果成功,则问题集中在 Ingress 配置或 DNS;如果也失败,则问题在 Pod 或 Service。
$ minikube service <service-name> --url
↶ 返回首页 ↶