YAML
YAML 文件用于定义和管理Kubernetes
集群资源,如Deployment
、Service
、Pod
等。
其作用主要体现在以下几个方面:
1. 定义资源对象的配置与期望状态
YAML 文件通过声明式语法描述资源对象的属性,包括 API 版本(apiVersion
)、资源类型(kind
)、元数据(metadata
)和详细配置(spec
)等。
2. 支持声明式管理
YAML 文件采用声明式设计,用户只需描述"期望结果",而无需关注具体执行步骤。
3. 实现多环境配置复用
YAML 文件支持参数化配置,可通过变量或外部工具实现不同环境(开发、测试、生产)的差异化部署。
4. 管理复杂资源关系
单个 YAML 文件可定义多个关联资源(如同时声明 Deployment 和 Service),并通过标签(labels
)和选择器(selector
)建立资源间的逻辑联系。
5. 标准化与安全性
YAML 文件强制使用空格缩进和层级结构,避免了格式混乱。
6. 扩展性与自动化集成
YAML 文件可与 CI/CD 工具(如 Jenkins、GitLab CI)结合,实现自动化部署。
示例:部署应用并对外暴露
一、Deployment YAML文件
这段 YAML 文件定义了一个 Kubernetes Deployment ,用于部署一个名为 test-app
的应用程序。
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-app
namespace: dev
labels:
name: test-app
spec:
replicas: 2
selector:
matchLabels:
name: test-app
template:
metadata:
labels:
name: test-app
spec:
nodeSelector:
feature1: test
initContainers:
- image: harbor:5000/test-app
name: copyfile
command: ["cp","-rf","/root/test-app.jar","/root/executable/jar/"]
containers:
- name: test-app
image: harbor:5000/jdk17
imagePullPolicy: IfNotPresent
envFrom:
- configMapRef:
name: dev-configmap
env:
- name: instanceId
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: instanceIp
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: JAR_Name
value: "test-app.jar"
volumeMounts:
- name: logs
mountPath: /home/listen/Apps/logs
- name: localtime
mountPath: /etc/localtime
- name: timezone
mountPath: /etc/timezone
- name: jar-vol
mountPath: /var/app/
ports:
- containerPort: 8080
resources:
limits:
cpu: 2
memory: 4.0Gi
requests:
cpu: 1
memory: 1024Mi
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
initialDelaySeconds: 30
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 30
volumes:
- name: logs
hostPath:
path: /logs/
- name: localtime
hostPath:
path: /etc/localtime
- name: timezone
hostPath:
path: /etc/timezone
- name: jar-vol
emptyDir: {}
1. 基本信息
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-app
namespace: dev
labels:
name: test-app
- apiVersion : 使用的 Kubernetes API 版本是
apps/v1
,这是 Deployment 的标准 API 版本。 - kind : 资源类型是
Deployment
,用于定义 Pod 的部署策略。 - metadata :
name
: Deployment 的名称是test-app
。namespace
: 部署在dev
命名空间中。labels
: 为 Deployment 打上标签name: test-app
,用于标识和选择。
2. Deployment 规格
yaml
spec:
replicas: 2
selector:
matchLabels:
name: test-app
- replicas: 指定运行 2 个 Pod 副本。
- selector : 定义 Deployment 如何选择管理的 Pod。这里通过标签
name: test-app
来选择 Pod。
3. Pod 模板
yaml
template:
metadata:
labels:
name: test-app
spec:
nodeSelector:
feature1: test
- template : 定义 Pod 的模板。
- metadata : 为 Pod 打上标签
name: test-app
,与selector
匹配。 - spec : 定义 Pod 的规格。
- nodeSelector : 指定 Pod 只能调度到具有标签
feature1: test
的节点上。
- nodeSelector : 指定 Pod 只能调度到具有标签
- metadata : 为 Pod 打上标签
4. 初始化容器 (initContainers)
yaml
initContainers:
- image: harbor:5000/test-app
name: copyfile
command: ["cp", "-rf", "/root/test-app.jar", "/root/executable/jar/"]
- initContainers : 在主容器启动之前运行的初始化容器。
- image : 使用镜像
harbor:5000/test-app
。 - name : 初始化容器的名称是
copyfile
。 - command : 运行命令
cp -rf /root/test-app.jar /root/executable/jar/
,将文件复制到指定目录。
- image : 使用镜像
5. 主容器 (containers)
yaml
containers:
- name: test-app
image: harbor:5000/jdk17
imagePullPolicy: IfNotPresent
envFrom:
- configMapRef:
name: dev-configmap
env:
- name: instanceId
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: instanceIp
valueFrom:
fieldRef:
fieldPath: status.podIP
# 其他环境变量...
volumeMounts:
- name: logs
mountPath: /home/listen/Apps/logs
- name: localtime
mountPath: /etc/localtime
- name: timezone
mountPath: /etc/timezone
- name: jar-vol
mountPath: /var/app/
ports:
- containerPort: 8080
resources:
limits:
cpu: 2
memory: 4.0Gi
requests:
cpu: 1
memory: 1024Mi
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
initialDelaySeconds: 30
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 30
- containers : 定义主容器。
- name : 容器名称是
test-app
。 - image : 使用镜像
harbor:5000/jdk17
。 - imagePullPolicy : 镜像拉取策略是
IfNotPresent
,即如果本地已存在镜像,则不拉取。 - envFrom : 从名为
dev-configmap
的 ConfigMap 注入环境变量。 - env : 定义环境变量,例如:
instanceId
: 从 Pod 的元数据中获取 Pod 名称。instanceIp
: 从 Pod 的状态中获取 Pod IP。- 其他环境变量用于配置应用程序的行为。
- volumeMounts : 挂载多个卷到容器中,例如:
logs
: 挂载到/home/listen/Apps/logs
。localtime
和timezone
: 挂载主机的时间配置。jar-vol
: 挂载到/var/app/
。
- ports : 暴露容器端口
8080
。 - resources : 定义资源限制和请求:
limits
: 容器最多使用 2 个 CPU 和 4Gi 内存。requests
: 容器至少需要 1 个 CPU 和 1024Mi 内存。
- readinessProbe : 就绪探针,检查路径
/actuator/health/readiness
,初始延迟 30 秒。 - livenessProbe : 存活探针,检查路径
/actuator/health/liveness
,初始延迟 30 秒。
- name : 容器名称是
6. 卷 (volumes)
yaml
volumes:
- name: logs
hostPath:
path: /logs/
- name: localtime
hostPath:
path: /etc/localtime
- name: timezone
hostPath:
path: /etc/timezone
- name: jar-vol
emptyDir: {}
- volumes : 定义 Pod 使用的卷。
logs
: 使用主机路径/logs/
。localtime
和timezone
: 使用主机的时间配置文件。jar-vol
: 使用emptyDir
卷,生命周期与 Pod 绑定。
7. 总结
这段 YAML 文件定义了一个 Kubernetes Deployment,用于部署一个名为 test-app
的应用程序。它包括:
- 2 个 Pod 副本。
- 一个初始化容器,用于复制文件。
- 一个主容器,运行 Java 应用程序,并配置了环境变量、资源限制、探针和卷挂载。
- 使用
hostPath
和emptyDir
卷来存储数据和配置文件。
这个 Deployment 的目标是在具有特定标签的节点上运行应用程序,并通过探针确保应用程序的健康状态。
二、根据 Deployment 创建资源
- 应用这个YAML文件
使用kubectl
命令行工具将这个Deployment应用到Kubernetes集群中:
bash
kubectl apply -f test-app-deployment.yaml
- 验证部署状态
使用以下命令来查看Deployment和Pod的状态:
bash
kubectl get deployments
kubectl get pods
三、Service YAML文件
为了暴露这个test-app
应用,还需要创建一个Service。
这段 YAML 文件定义了一个 Kubernetes Service,用于暴露名为 test-app
的应用程序。
yaml
apiVersion: v1
kind: Service
metadata:
name: test-app-service
spec:
selector:
app: test-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
1. 基本信息
yaml
apiVersion: v1
kind: Service
metadata:
name: test-app-service
- apiVersion : 使用的 Kubernetes API 版本是
v1
,这是 Service 的标准 API 版本。 - kind : 资源类型是
Service
,用于定义如何访问 Pod。 - metadata :
name
: Service 的名称是test-app-service
。
2. Service 规格
yaml
spec:
selector:
app: test-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
2.1. Selector
yaml
selector:
app: test-app
- selector : 定义 Service 如何选择 Pod。
- 这里通过标签
app: test-app
来选择 Pod。 - 这意味着所有具有
app: test-app
标签的 Pod 都会被该 Service 管理。
- 这里通过标签
2.2. Ports
yaml
ports:
- protocol: TCP
port: 80
targetPort: 80
- ports : 定义 Service 的端口映射。
- protocol : 使用
TCP
协议。 - port : Service 对外暴露的端口是
80
。 - targetPort : 将流量转发到 Pod 的端口
80
。
- protocol : 使用
2.3. Type
yaml
type: LoadBalancer
- type : 定义 Service 的类型。
LoadBalancer
: 表示这是一个负载均衡类型的 Service。- Kubernetes 会自动为该 Service 创建一个外部负载均衡器,并将外部流量路由到后端 Pod。
3. 总结
这段 YAML 文件定义了一个 Kubernetes Service,用于暴露 test-app
应用程序。
它的主要功能包括:
- 选择 Pod :通过标签
app: test-app
选择后端 Pod。 - 端口映射 :将外部流量从 Service 的端口
80
转发到 Pod 的端口80
。 - 负载均衡 :使用
LoadBalancer
类型,自动创建外部负载均衡器,将外部流量分发到Pod。
4. 实际效果
- 当该 Service 被创建后:
- Kubernetes 会为该 Service 分配一个外部
IP
地址。 - 外部客户端可以通过该
IP
地址和端口80
访问test-app
应用程序。 - 流量会被负载均衡到所有具有
app: test-app
标签的 Pod。
- Kubernetes 会为该 Service 分配一个外部
5. 适用场景
- 适用于需要对外暴露服务的应用程序,例如 Web 服务或 API。
- 如果运行在云平台上,
LoadBalancer
类型会自动创建云负载均衡器。 - 如果运行在本地环境,可能需要额外的配置(如 MetalLB)来支持
LoadBalancer
类型。
四、根据 service 创建资源
- 应用这个YAML文件
使用kubectl
命令行工具将这个Service应用到Kubernetes集群中:
bash
kubectl apply -f test-app-service.yaml
- 获取Service信息
使用以下命令来获取Service的详细信息,包括外部IP地址:
bash
kubectl get services