快速编写yaml

快速编写YAML

kubectl run方式

kubectl run的方式只针对快速编排Pod管用

[root@localhost ~]# kubectl run mynginx --image=nginx --port=80 -o yaml --dry-run > nginx.yaml 
W0725 10:27:41.891682    1446 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client.

"查看"
[root@localhost ~]# cat  nginx.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: mynginx
  name: mynginx
spec:
  containers:
  - image: nginx
    name: mynginx
    ports:
    - containerPort: 80
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

 

kubectl create方式

kubectl create的方式可以快速编排大部分

[root@localhost ~]# kubectl create namespace  test -o yaml --dry-run  > namespace.yaml 
W0725 10:28:30.308434    2235 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client.

"查看"
[root@localhost ~]# cat  namespace.yaml 
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: null
  name: test
spec: {}
status: {}

 执行yaml文件

[root@localhost ~]# kubectl  apply -f namespace.yaml 
namespace/test created
[root@localhost ~]# kubectl get namespace
NAME                   STATUS   AGE
default                Active   12m
kube-node-lease        Active   12m
kube-public            Active   12m
kube-system            Active   12m
kubernetes-dashboard   Active   11m
test                   Active   10s

 

kubectk get

kubectl get的方式是导出来yaml文件

[root@localhost ~]# kubectl get  namespace test -o yaml > my.yaml

apiVersion: v1
items:
- apiVersion: v1
  kind: Namespace
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"creationTimestamp":null,"name":"test"},"spec":{},"status":{}}
    creationTimestamp: "2021-07-25T02:30:02Z"
    managedFields:
    - apiVersion: v1
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:annotations:
            .: {}
            f:kubectl.kubernetes.io/last-applied-configuration: {}
        f:status:
          f:phase: {}
      manager: kubectl
      operation: Update
      time: "2021-07-25T02:30:02Z"
    name: test
    resourceVersion: "2102"
    selfLink: /api/v1/namespaces/test
    uid: fc9d2ae6-f55a-4904-a333-d63f58b1bd31
  spec:
    finalizers:
    - kubernetes
  status:
    phase: Active
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
Error from server (NotFound): namespaces "my.yaml" not found

 

 

手册

kubectl explain pod

[root@localhost ~]# kubectl explain pod
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

FIELDS:
   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata     <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec <Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   status       <Object>
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
posted @ 2021-07-25 10:37  isicman  阅读(210)  评论(0编辑  收藏  举报