使用 Kubernetes 部署 YAML 文件将 Docker 映像部署到 Google 云时未创建 pods

No pods created when deploying a Docker image to Google Cloud using a Kubernetes deployment YAML file

我在尝试使用 Kubernetes YAML 文件在 Google 云上创建部署时遇到问题。我看到使用 YAML 文件时没有创建 pods;但是,如果我使用 kubectl create deployment...,就会创建 pods。

我的 YAML 文件如下:

#Deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
  generation: 1
  labels:
    app: hello-world
  name: hello-world
  namespace: default
  resourceVersion: "3691124"
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: hello-world
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - image: myrepo/hello-world:0.0.4.RELEASE
        imagePullPolicy: IfNotPresent
        name: hello-world
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
#Service
apiVersion: v1
kind: Service
metadata:
  labels:
    app: hello-world
  name: hello-world
  namespace: default
  resourceVersion: "3691877"
spec:
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 32448
    port: 8080
    protocol: TCP
    targetPort: 8001
  selector:
    app: hello-world
  sessionAffinity: None
  type: LoadBalancer

这是我在 运行 kubectl get all :

时看到的
NAME                           TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)          AGE
service/hello-world            LoadBalancer   Some IP        Some IP 3     8001:32449/TCP    15m
service/kubernetes             ClusterIP      Some IP 2      <none>         444/TCP           12d

如您所见,启动的唯一资源是服务。我既没有看到任何部署,也没有看到正在创建任何 pods。

注意出于显而易见的原因,我已将上面的实际 IP 地址替换为 "Some IP"..

问题:为什么当我使用 YAML 时没有创建 pods 而当我使用 kubectl create deployment 而不是时创建 pods使用 YAML 配置文件?

  1. 当您在一个文件中有多个资源时,您应该在 YAML 中使用 ---
  2. 您必须在部署中设置 apiVersion: apps/v1
---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
  generation: 1
  labels:
    app: hello-world
  name: hello-world
  namespace: default
  resourceVersion: "3691124"
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: hello-world
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - image: myrepo/hello-world:0.0.4.RELEASE
        imagePullPolicy: IfNotPresent
        name: hello-world
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: hello-world
  name: hello-world
  namespace: default
  resourceVersion: "3691877"
spec:
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 32448
    port: 8080
    protocol: TCP
    targetPort: 8001
  selector:
    app: hello-world
  sessionAffinity: None
  type: LoadBalancer

输出

$ kubectl get deply,po,svc
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-world   0/3     3            0           5m34s

NAME                               READY   STATUS             RESTARTS   AGE

pod/hello-world-6bd8d58486-7lzh9   0/1     ImagePullBackOff   0          3m49s
pod/hello-world-6bd8d58486-m56rq   0/1     ImagePullBackOff   0          3m49s
pod/hello-world-6bd8d58486-z9xmz   0/1     ImagePullBackOff   0          3m49s

NAME                  TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)           AGE
service/hello-world   LoadBalancer   10.108.65.81     <pending>     8080:32448/TCP    3m49s
service/kubernetes    ClusterIP      10.96.0.1        <none>        443/TCP           9d

最佳做法是为不同的资源创建不同的 yaml 文件。如果需要将多个 kubernetes 资源打包到一个实体中,请使用 Helm。