Kubernetes(K8s)的 YAML 文件是定义和配置集群中各种资源的关键。在这个实战指南中,我们将介绍常见的 Kubernetes 资源对象,并提供相应的 YAML 示例。
1. Pod
Pod 是 Kubernetes 最基本的部署单元,通常包含一个或多个容器。以下是一个简单的 Pod YAML 示例:
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mycontainer
image: nginx:latest
ports:
- containerPort: 80
- 创建 Pod:
kubectl apply -f pod.yaml
2. Deployment
Deployment 用于管理 Pod 的部署和更新。以下是一个 Deployment YAML 示例:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mydeployment
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: mycontainer
image: nginx:latest
ports:
- containerPort: 80
- 创建 Deployment:
kubectl apply -f deployment.yaml
3. Service
Service 用于将流量引导到一组 Pod。以下是一个 Service YAML 示例:
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
- 创建 Service:
kubectl apply -f service.yaml
4. ConfigMap
ConfigMap 用于存储配置数据,供 Pod 使用。以下是一个 ConfigMap YAML 示例:
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfigmap
data:
APP_NAME: "myapp"
LOG_LEVEL: "debug"
- 创建 ConfigMap:
kubectl apply -f configmap.yaml
5. Secret
Secret 用于存储敏感信息。以下是一个 Secret YAML 示例:
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: base64encodedusername
password: base64encodedpassword
- 创建 Secret:
kubectl apply -f secret.yaml
6. PersistentVolume (PV) 和 PersistentVolumeClaim (PVC)
PV 和 PVC 用于持久化存储。以下是一个 PV 和 PVC YAML 示例:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
- 创建 PV 和 PVC:
kubectl apply -f pv.yaml
7. StatefulSet
StatefulSet 用于管理有状态应用。以下是一个 StatefulSet YAML 示例:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mystatefulset
spec:
serviceName: "web"
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: mycontainer
image: nginx:latest
ports:
- containerPort: 80
- 创建 StatefulSet:
kubectl apply -f statefulset.yaml
8. Ingress
Ingress 用于管理外部访问到集群内服务的路由。以下是一个 Ingress YAML 示例:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myingress
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myservice
port:
number: 80
- 创建 Ingress:
kubectl apply -f ingress.yaml
9. HorizontalPodAutoscaler
HorizontalPodAutoscaler 用于自动调整 Pod 的数量以应对流量变化。以下是一个 HorizontalPodAutoscaler YAML 示例:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: myhpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: mydeployment
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
- 创建 HorizontalPodAutoscaler:
kubectl apply -f hpa.yaml
10. Job
Job 用于运行一次性任务或定期任务。以下是一个简单的 Job YAML 示例:
apiVersion: batch/v1
kind: Job
metadata:
name: myjob
spec:
template:
spec:
containers:
- name: mycontainer
image: busybox:latest
command: ["echo", "Hello from my job"]
restartPolicy: Never
- 创建 Job:
kubectl apply -f job.yaml
这些 YAML 示例提供了一套基本的 Kubernetes 部署和配置方案。在实际应用中,根据需求可能需要更多的资源对象以及更复杂的配置。通过深入学习 Kubernetes 官方文档并将这些概念应用到实际场景中,你将能更好地利用 Kubernetes 强大的功能。希望这个实战指南对你在 Kubernetes 中的工作和学习有所帮助。