docker - 如何使用kubectl命令生成yaml模板?

标签 docker kubernetes yaml kubectl

是否可以使用kubernetes kubectl命令生成yaml?需要澄清的是-我不是在谈论从现有的部署(例如kubectl get XXXX -o yaml)生成yaml,而只是在第一次针对pod,服务,入口等生成yaml。

PS有一种从kubernetes.io网站(12)获取yaml文件的方法,但我正在寻找是否有一种仅用kubectl生成yamls模板的方法。

最佳答案

create中有一个kubectl命令可以解决上述问题,并取代了过去使用的run:让我们为要创建一个运行nginx:latest Docker镜像的Deployment镜像。

# kubectl create deployment my_deployment --image=busybox --dry-run=client --output=yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: my_deployment
  name: my_deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my_deployment
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my_deployment
    spec:
      containers:
      - image: busybox
        name: busybox
        resources: {}
status: {}
让我们分析每个参数:
  • my_deployment是您选择的部署名称
  • --image是您要部署的Docker镜像
  • --dry-run=client将不会执行资源创建,主要用于验证。对于较旧版本的Kubernetes,将'client'替换为'true'。
  • --output=yaml将部署资源的YAML定义输出到标准输出。

  • 显然,您只需很少的Kubernetes默认资源即可执行此选项:
    # kubectl create 
      clusterrole         Create a ClusterRole.
      clusterrolebinding  Create a ClusterRoleBinding for a particular ClusterRole
      configmap           Create a configmap from a local file, directory or literal value
      deployment          Create a deployment with the specified name.
      job                 Create a job with the specified name.
      namespace           Create a namespace with the specified name
      poddisruptionbudget Create a pod disruption budget with the specified name.
      priorityclass       Create a priorityclass with the specified name.
      quota               Create a quota with the specified name.
      role                Create a role with single rule.
      rolebinding         Create a RoleBinding for a particular Role or ClusterRole
      secret              Create a secret using specified subcommand
      service             Create a service using specified subcommand.
      serviceaccount      Create a service account with the specified name
    
    据此,您可以在无需部署资源的情况下呈现模板。

    关于docker - 如何使用kubectl命令生成yaml模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57696087/

    相关文章:

    azure - 版本 "2019-12-01"中的 Microsoft.ContainerInstance/containerGroups 类型没有匹配项

    logging - kube-controller-manager 未出现在 Google Kubernetes Engine 上

    kubernetes - 试图了解绒布vs绒布荚的用法

    ruby-on-rails - 在 ruby​​ on rails 中更新 yaml 文件的键值

    docker - Docker镜像缓存失效什么时候发生?

    docker - 如何使用 dockerfiles CMD 运行 shell 脚本

    java - 如何在 Kubernetes 上将应用程序配置与容器中的 API 分离?

    docker - Docker-在DNS缓存中未找到主机名

    python-3.x - PyYaml 将 '9:00' 解析为 int

    environment-variables - 从 serverless.yml 中的外部文件设置环境变量