名称

podman-kube-generate - 根据容器、Pod 或卷生成 Kubernetes YAML

简介

podman kube generate [选项] 容器… | Pod… | 卷…

描述

podman kube generate 从 Podman 容器、Pod 或卷生成 Kubernetes YAML(v1 规范)。无论输入是容器还是 Pod,Podman 默认都将规范生成为 Pod。输入可以是多个容器、Pod 或卷的名称或 ID。

Podman 容器 Pod

卷在生成的 YAML 中根据两种不同的卷类型出现。绑定挂载的卷成为 hostPath 卷类型,命名卷成为 persistentVolumeClaim 卷类型。生成的 hostPath 卷类型有三种子类型,具体取决于主机路径的状态:当主机上不存在文件或目录时为 DirectoryOrCreate,当主机路径是目录时为 Directory,当主机路径是文件时为 FilepersistentVolumeClaimclaimName 值是 Podman 中注册的命名卷的名称。

通过对每种卷类型使用标准命名方案,可以避免卷之间潜在的名称冲突。hostPath 卷类型根据主机上的路径命名,将斜杠替换为连字符,并去除前导和尾随斜杠。文件系统根目录的特殊情况 /,转换为名称 root。此外,名称后缀为 -host 以避免与 persistentVolumeClaim 卷的命名冲突。每个 persistentVolumeClaim 卷类型都使用其关联的命名卷的名称,后缀为 -pvc

请注意,如果使用 once 类型创建了 init 容器且 Pod 已启动,则它不会出现在生成的 kube YAML 中,因为 once 类型的 init 容器在运行后会被删除。如果 Pod 仅被创建但未启动,则它会出现在生成的 kube YAML 中。使用 always 类型创建的 init 容器始终会在 kube YAML 中生成,因为它们永不删除,即使运行完成。

注意:在使用卷并为启用了 SELinux 的系统上的非特权和无根 podman 容器生成 Kubernetes YAML 时,必须完成以下选项之一

  • 将“privileged: true”选项添加到 Pod 规范中

  • 在 Pod 规范的 securityContext seLinuxOptions 下添加 type: spc_t

  • 通过 CLI 命令 chcon -t container_file_t -R <directory> 重新标记卷

完成后,当在 Kubernetes 集群中创建 Pod/容器时,将具有正确的权限来访问卷。

请注意,生成的 Kubernetes YAML 文件可用于通过 podman-play-kube(1) 重新运行部署。

请注意,如果生成的 Pod 是使用 --infra-name 标志创建的,则生成的 kube yaml 将设置 io.podman.annotations.infra.name,其值是用户设置的基础设施容器的名称。

请注意,Deployment 和 DaemonSet 只能将 restartPolicy 设置为 Always

请注意,Job 只能将 restartPolicy 设置为 OnFailureNever。默认情况下,podman 在使用 kube generate 生成 kube yaml 时将其设置为 Never

选项

--filename, -f=文件名

输出到指定文件而不是 STDOUT。如果文件已存在,kube generate 会拒绝替换它并返回错误。

--podman-only

在生成的 YAML 文件中添加仅限 podman 的保留注解(Kubernetes 无法使用)

--replicas, -r=副本数

生成 Deployment 类型时要设置 replicas 的值。注意:这只能与 --type=deployment 选项一起设置。

--service, -s

除了 Pods 之外,还生成一个 Kubernetes 服务对象。用于为相应的 Pod 输出生成服务规范。特别是,如果对象具有端口映射绑定,则服务规范包含 NodePort 声明以公开服务。Podman 在规范中分配一个随机端口。

--type, -t=pod | deployment | daemonset | job

要在 YAML 文件中生成的 Kubernetes 类型。目前,唯一支持的 Kubernetes 规范是 PodDeploymentJobDaemonSet。默认情况下,生成 Pod 规范。

示例

为指定的容器创建 Kubernetes Pod YAML。

$ podman kube generate some-mariadb
# Save the output of this file and use kubectl create -f to import
# it into Kubernetes.
#
# Created with podman-4.8.2

# NOTE: If you generated this yaml from an unprivileged and rootless podman container on an SELinux
# enabled system, check the podman generate kube man page for steps to follow to ensure that your pod/container
# has the right permissions to access the volumes added.
---
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2024-01-09T02:24:55Z"
  labels:
    app: some-mariadb-pod
  name: some-mariadb-pod
spec:
  containers:
  - args:
    - mariadbd
    env:
    - name: MARIADB_ROOT_PASSWORD
      value: x
    image: docker.io/library/mariadb:10.11
    name: some-mariadb
    ports:
    - containerPort: 3306
      hostPort: 34891
    volumeMounts:
    - mountPath: /var/lib/mysql
      name: mariadb_data-pvc
  volumes:
  - name: mariadb_data-pvc
    persistentVolumeClaim:
      claimName: mariadb_data

为指定的容器创建具有 3 个副本的 Kubernetes Deployment YAML。

$ podman kube generate --type deployment --replicas 3 dep-ct
r
# Save the output of this file and use kubectl create -f to import
# it into Kubernetes.
#
# Created with podman-4.5.0-dev
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: "2023-03-27T20:45:08Z"
  labels:
    app: dep-ctr-pod
  name: dep-ctr-pod-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: dep-ctr-pod
  template:
    metadata:
      annotations:
        io.podman.annotations.ulimit: nofile=524288:524288,nproc=127332:127332
      creationTimestamp: "2023-03-27T20:45:08Z"
      labels:
        app: dep-ctr-pod
      name: dep-ctr-pod
    spec:
      containers:
      - command:
        - top
        image: docker.io/library/alpine:latest
        name: dep-ctr

为指定的容器创建 Kubernetes Pod YAML,并将主机目录 /home/user/my-data 绑定挂载到容器路径 /volume

$ podman kube generate my-container-with-bind-mounted-data
# Save the output of this file and use kubectl create -f to import
# it into Kubernetes.
#
# Created with podman-3.1.0-dev
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-03-18T16:26:08Z"
  labels:
    app: my-container-with-bind-mounted-data
  name: my-container-with-bind-mounted-data
spec:
  containers:
  - command:
    - /bin/sh
    image: docker.io/library/alpine:latest
    name: test-bind-mount
    volumeMounts:
    - mountPath: /volume
      name: home-user-my-data-host
  restartPolicy: Never
  volumes:
  - hostPath:
      path: /home/user/my-data
      type: Directory
    name: home-user-my-data-host

为指定的容器创建 Kubernetes Pod YAML,并将命名卷 priceless-data 挂载到容器路径 /volume

$ podman kube generate my-container-using-priceless-data
# Save the output of this file and use kubectl create -f to import
# it into Kubernetes.
#
# Created with podman-3.1.0-dev
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-03-18T16:26:08Z"
  labels:
    app: my-container-using-priceless-data
  name: my-container-using-priceless-data
spec:
  containers:
  - command:
    - /bin/sh
    image: docker.io/library/alpine:latest
    name: test-bind-mount
    volumeMounts:
    - mountPath: /volume
      name: priceless-data-pvc
  restartPolicy: Never
  volumes:
  - name: priceless-data-pvc
    persistentVolumeClaim:
      claimName: priceless-data

为指定的 Pod 创建 Kubernetes Pod YAML,并包含一个服务。

$ sudo podman kube generate -s demoweb
# Save the output of this file and use kubectl create -f to import
# it into Kubernetes.
#
# Created with podman-0.12.2-dev
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: 2018-12-18T15:16:06Z
  labels:
    app: demoweb
  name: demoweb-libpod
spec:
  containers:
  - command:
    - python3
    - /root/code/graph.py
    image: quay.io/baude/demoweb:latest
    name: practicalarchimedes
    tty: true
    workingDir: /root/code
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: 2018-12-18T15:16:06Z
  labels:
    app: demoweb
  name: demoweb-libpod
spec:
  ports:
  - name: "8050"
    nodePort: 31269
    port: 8050
    targetPort: 0
  selector:
    app: demoweb
  type: NodePort
status:
  loadBalancer: {}

另请参阅

podman(1)podman-container(1)podman-pod(1)podman-kube-play(1)podman-kube-down(1)

历史

2018 年 12 月,最初由 Brent Baude (bbaude at redhat dot com) 编写