YAML字段的含义和作用
每个 Kubernetes YAML 文件都必须包含这四个顶级字段:
1 2 3 4 5 6 7 8
| apiVersion: v1 kind: Pod metadata: name: nginx-pod spec: containers: - name: nginx image: nginx:1.14.2
|
使用 ---
分隔符可以在一个文件中定义多个资源:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3
--- apiVersion: v1 kind: Service metadata: name: nginx-service spec:
|
一些常见的配置如下:
Deployment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| apiVersion: apps/v1 kind: Deployment metadata: name: my-app namespace: default labels: app: my-app environment: production annotations: description: "示例应用部署" spec: replicas: 3 selector: matchLabels: app: my-app strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 1 minReadySeconds: 5 revisionHistoryLimit: 10 template: metadata: labels: app: my-app spec: serviceAccountName: my-service-account terminationGracePeriodSeconds: 30 containers: - name: main-container image: nginx:1.14.2 imagePullPolicy: IfNotPresent ports: - name: http containerPort: 80 protocol: TCP env: - name: DB_HOST value: "mysql" - name: API_KEY valueFrom: secretKeyRef: name: api-secret key: api-key resources: requests: cpu: "100m" memory: "128Mi" limits: cpu: "200m" memory: "256Mi" livenessProbe: httpGet: path: /healthz port: http initialDelaySeconds: 15 periodSeconds: 10 readinessProbe: httpGet: path: /ready port: http initialDelaySeconds: 5 periodSeconds: 10 volumeMounts: - name: config-volume mountPath: /etc/config - name: data-volume mountPath: /data volumes: - name: config-volume configMap: name: app-config - name: data-volume persistentVolumeClaim: claimName: data-pvc affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/os operator: In values: - linux podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchExpressions: - key: app operator: In values: - my-app topologyKey: kubernetes.io/hostname tolerations: - key: "node-role.kubernetes.io/master" operator: "Exists" effect: "NoSchedule"
|
Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| apiVersion: v1 kind: Service metadata: name: my-service namespace: default labels: app: my-app spec: type: ClusterIP ports: - port: 80 targetPort: http protocol: TCP name: http selector: app: my-app
|
ConfigMap
1 2 3 4 5 6 7 8 9 10 11 12 13
| apiVersion: v1 kind: ConfigMap metadata: name: app-config namespace: default data: config.yml: | environment: production log_level: info features: feature1: true feature2: false
|
Secret
1 2 3 4 5 6 7 8
| apiVersion: v1 kind: Secret metadata: name: api-secret namespace: default type: Opaque data: api-key: BASE64_ENCODED_API_KEY
|
PersistentVolumeClaim(PVC)
1 2 3 4 5 6 7 8 9 10 11 12 13
| apiVersion: v1 kind: PersistentVolumeClaim metadata: name: data-pvc namespace: default spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi storageClassName: standard
|
HorizontalPodAutoscaler(HPA)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: my-app-hpa namespace: default spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: my-app minReplicas: 1 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 80
|
Ingress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-ingress namespace: default annotations: nginx.ingress.kubernetes.io/rewrite-target: / spec: ingressClassName: nginx rules: - host: myapp.example.com http: paths: - path: / pathType: Prefix backend: service: name: my-service port: name: http
|
NetworkPolicy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: my-network-policy namespace: default spec: podSelector: matchLabels: app: my-app policyTypes: - Ingress - Egress ingress: - from: - podSelector: matchLabels: app: allowed-app ports: - protocol: TCP port: 80 egress: - to: - podSelector: matchLabels: app: allowed-destination ports: - protocol: TCP port: 5432
|
ResourceQuota
1 2 3 4 5 6 7 8 9 10 11 12 13
| apiVersion: v1 kind: ResourceQuota metadata: name: my-quota namespace: default spec: hard: requests.cpu: "4" requests.memory: 4Gi limits.cpu: "8" limits.memory: 8Gi pods: "10"
|
LimitRange
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| apiVersion: v1 kind: LimitRange metadata: name: my-limit-range namespace: default spec: limits: - type: Container default: cpu: 200m memory: 256Mi defaultRequest: cpu: 100m memory: 128Mi max: cpu: 1 memory: 1Gi min: cpu: 50m memory: 64Mi
|