【问题标题】:Does kubernetes kubectl run with image creates deployment yaml filekubernetes kubectl 是否与镜像一起运行创建部署 yaml 文件
【发布时间】:2020-04-15 15:29:50
【问题描述】:

我正在尝试使用 Minikube 和 Docker 来理解 Kubernetes 架构的概念。

我用 Dockerfile 创建了一个 Spring Boot 应用程序,创建了标签并推送到 Dockerhub。

为了在 K8s 集群中部署镜像,我发出了以下命令,

# deployed the image
$ kubectl run <deployment-name> --image=<username/imagename>:<version> --port=<port the app runs>

# exposed the port as nodeport
$ kubectl expose deployment <deployment-name> --type=NodePort

一切正常,我可以看到 1 个 pod 正在运行 kubectl get pods

我推送到 Dockerhub 的 Docker 镜像没有任何部署 YAML 文件。

下面的命令产生了一个 yaml 输出

kubectl 命令是否会创建开箱即用的部署 Yaml 文件?

 $ kubectl get deployments --output yaml 
apiVersion: v1
items:
- apiVersion: apps/v1
  kind: Deployment
  metadata:
    annotations:
      deployment.kubernetes.io/revision: "1"
    creationTimestamp: "2019-12-24T14:59:14Z"
    generation: 1
    labels:
      run: hello-service
    name: hello-service
    namespace: default
    resourceVersion: "76195"
    selfLink: /apis/apps/v1/namespaces/default/deployments/hello-service
    uid: 90950172-1c0b-4b9f-a339-b47569366f4e
  spec:
    progressDeadlineSeconds: 600
    replicas: 1
    revisionHistoryLimit: 10
    selector:
      matchLabels:
        run: hello-service
    strategy:
      rollingUpdate:
        maxSurge: 25%
        maxUnavailable: 25%
      type: RollingUpdate
    template:
      metadata:
        creationTimestamp: null
        labels:
          run: hello-service
      spec:
        containers:
        - image: thirumurthi/hello-service:0.0.1
          imagePullPolicy: IfNotPresent
          name: hello-service
          ports:
          - containerPort: 8800
            protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
        dnsPolicy: ClusterFirst
        restartPolicy: Always
        schedulerName: default-scheduler
        securityContext: {}
        terminationGracePeriodSeconds: 30
  status:
    availableReplicas: 1
    conditions:
    - lastTransitionTime: "2019-12-24T14:59:19Z"
      lastUpdateTime: "2019-12-24T14:59:19Z"
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: "True"
      type: Available
    - lastTransitionTime: "2019-12-24T14:59:14Z"
      lastUpdateTime: "2019-12-24T14:59:19Z"
      message: ReplicaSet "hello-service-75d67cc857" has successfully progressed.
      reason: NewReplicaSetAvailable
      status: "True"
      type: Progressing
    observedGeneration: 1
    readyReplicas: 1
    replicas: 1
    updatedReplicas: 1
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

【问题讨论】:

    标签: docker kubernetes kubectl


    【解决方案1】:

    是的,kubectl run 创建了一个部署。如果您查看标签字段,您可以看到run: hello-service。此标签稍后在选择器中使用。

    【讨论】:

      【解决方案2】:

      我认为使用命令式命令创建 kubernetes 资源时最简单的方法是了解底层发生的事情(与通过编写和应用 yaml 定义文件的声明式方法相比)是运行一个带有 2 个附加标志的简单示例:

      --dry-run
      

      --output yaml
      

      这些标志的名称是不言自明的,所以我认为没有必要进一步解释它们的作用。你可以简单地试试下面的例子,你就会看到效果:

      kubectl run nginx-example --image=nginx:latest --port=80 --dry-run --output yaml
      

      如您所见,它会生成适当的 yaml 清单,而无需应用它并创建实际部署:

      apiVersion: apps/v1beta1
      kind: Deployment
      metadata:
        creationTimestamp: null
        labels:
          run: nginx-example
        name: nginx-example
      spec:
        replicas: 1
        selector:
          matchLabels:
            run: nginx-example
        strategy: {}
        template:
          metadata:
            creationTimestamp: null
            labels:
              run: nginx-example
          spec:
            containers:
            - image: nginx:latest
              name: nginx-example
              ports:
              - containerPort: 80
              resources: {}
      status: {}
      

      expose 命令相同:

      kubectl expose deployment nginx-example --type=NodePort --dry-run --output yaml
      

      产生以下输出:

      apiVersion: v1
      kind: Service
      metadata:
        creationTimestamp: null
        labels:
          run: nginx-example
        name: nginx-example
      spec:
        ports:
        - port: 80
          protocol: TCP
          targetPort: 80
        selector:
          run: nginx-example
        type: NodePort
      status:
        loadBalancer: {}
      

      现在是最酷的部分。您可以使用简单的输出重定向:

      kubectl run nginx-example --image=nginx:latest --port=80 --dry-run --output yaml > nginx-example-deployment.yaml
      
      kubectl expose deployment nginx-example --type=NodePort --dry-run --output yaml > nginx-example-nodeport-service.yaml
      

      保存生成的DeploymentNodePort Service 定义,以便您可以根据需要进一步修改它们并使用kubectl apply -f filename.yamlkubectl create -f filename.yaml 应用。

      顺便说一句。 kubectl runkubectl expose 是基于生成器的命令,正如您在创建部署时可能已经注意到的那样(您可能收到消息:kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.)它们使用 --generator 标志。如果您没有明确指定它,它会获取kubectl run 的默认值--generator=deployment/apps.v1beta1,因此默认情况下它会创建一个Deployment。但是您可以通过提供--generator=run-pod/v1 nginx-example 来修改它,而不是Deployment,它将创建一个Pod。当我们回到之前的示例时,它可能看起来像这样:

      kubectl run --generator=run-pod/v1 nginx-example --image=nginx:latest --port=80 --dry-run --output yaml
      

      我希望这能回答你的问题,并稍微澄清一下使用命令式命令创建 kubernetes 资源的机制。

      【讨论】:

        猜你喜欢
        • 2018-06-09
        • 2020-03-20
        • 2020-09-15
        • 2019-12-14
        • 2020-08-01
        • 2019-11-26
        • 1970-01-01
        • 2020-11-24
        • 1970-01-01
        相关资源
        最近更新 更多