K8s创建Pod的两种方式

Kubernetes创建pod一般有两种方式:

  1. 使用命令行的方式创建pod;
  2. 使用yaml文件创建pod(资源清单)

命令行方式

kubectl run

# 创建一个最简单的pod
[root@k8s01 ~]# kubectl run nginx --image=nginx
pod/nginx created

# 查看pod,等STATUS为Running就表示pod创建成功
[root@k8s01 ~]# kubectl get pod  
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          11s
[root@k8s01 ~]# kubectl get pod 
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          2m15s
[root@k8s01 ~]# kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE    IP         NODE    NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          106s   10.2.1.7   k8s03   <none>           <none>
[root@k8s01 ~]# kubectl delete pod nginx 
pod "nginx" deleted
[root@k8s01 ~]# kubectl get pod 
No resources found in default namespace.


# 创建pod,指定镜像下载策略
镜像的下载策略:Always:每次都下载最新的镜像;Never:只使用本地镜像,从不下载,IfNotPresent:本地没有才下载镜像 默认值为:Always
kubectl run nginx --image=nginx --image-pull-policy=IfNotPresent

yaml文件方式(资源清单)

yaml概述

YAML是"YAML Ain’t a Markup Language"(YAML不是一种标记语言)的递归缩写。YAML的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。主要强调这种语言是以数据为中心,而不是以标记语言为重心,例如像xml语言就会使用大量的标记。

YAML是一个可读性高,易于理解,用来表达数据序列化的格式。它的语法和其他高级语言类似,并且可以简单表达清单(数组)、散列表,标量等数据形态。它使用空白符号缩进和大量依赖外观的特色,特别适合用来表达或编辑数据结构、各种配置文件等。YAML的配置文件后缀为 .yaml。

如何生成创建pod的yaml文件

–dry-run 模拟运行,并不会真的创建一个pod , --dry-run=client输出信息少 ,–dry-run=server输出信息多, -o yaml以yaml文件的格式输出

–dry-run=server输出信息如下:内容很多

[root@k8s01 ~]# kubectl run nginx --image=nginx --image-pull-policy=IfNotPresent --dry-run=server -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2024-02-10T12:54:57Z"
  labels:
    run: nginx
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:labels:
          .: {}
          f:run: {}
      f:spec:
        f:containers:
          k:{"name":"nginx"}:
            .: {}
            f:image: {}
            f:imagePullPolicy: {}
            f:name: {}
            f:resources: {}
            f:terminationMessagePath: {}
            f:terminationMessagePolicy: {}
        f:dnsPolicy: {}
        f:enableServiceLinks: {}
        f:restartPolicy: {}
        f:schedulerName: {}
        f:securityContext: {}
        f:terminationGracePeriodSeconds: {}
    manager: kubectl-run
    operation: Update
    time: "2024-02-10T12:54:57Z"
  name: nginx
  namespace: default
  selfLink: /api/v1/namespaces/default/pods/nginx
  uid: aacd1776-9abc-446d-bc25-9b9ee3c8da87
spec:
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: nginx
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-dnrc7
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-dnrc7
    secret:
      defaultMode: 420
      secretName: default-token-dnrc7
status:
  phase: Pending
  qosClass: BestEffort

–dry-run=client输出信息如下:输出内容恰到好处

[root@k8s01 ~]# kubectl run nginx --image=nginx --image-pull-policy=IfNotPresent --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

[root@k8s01 ~]# kubectl run nginx --image=nginx --image-pull-policy=IfNotPresent --dry-run=client -o yaml >nginx.yaml

`

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐