k8s快速生成yaml的两种方式

时间:2022-08-23 01:19:59

第一. kubectl create命令

[root@k8s-master ~]# kubectl create deployment nginx --image=nginx -o yaml --dry-run #不创建pod,打印出来
W0106 16:21:43.891679   17615 helpers.go:663] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
[root@k8s-master ~]# 
ot@k8s-master ~]# kubectl create deployment nginx --image=nginx -o yaml --dry-run=client  > nginx.yaml #不创建pod,导出到文件中
[root@k8s-master ~]# ls
nginx.yaml
[root@k8s-master ~]# cat nginx.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
[root@k8s-master ~]#

第二. kubectl get(导出)

[root@k8s-master ~]# kubectl get deploy nginx -o yaml  > nginx.yaml #直接导出到文档中,没有创建pod
[root@k8s-master ~]# ls
nginx.yaml
[root@k8s-master ~]# cat nginx.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2023-01-06T08:25:22Z"
  generation: 1
  labels:
    app: nginx
  name: nginx
  namespace: default
  resourceVersion: "342363"
  uid: 680d72b2-9904-43ad-9833-62b05fad8300
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: nginx
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2023-01-06T08:25:24Z"
    lastUpdateTime: "2023-01-06T08:25:24Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2023-01-06T08:25:22Z"
    lastUpdateTime: "2023-01-06T08:25:24Z"
    message: ReplicaSet "nginx-748c667d99" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1
[root@k8s-master ~]#

第三. 用命令创建pod

[root@k8s-master ~]# kubectl create deployment nginx --image=nginx  #直接创建pod
deployment.apps/nginx created
[root@k8s-master ~]# kubectl get pods,deployments.apps 
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-748c667d99-nnnpz   1/1     Running   0          19s
pod/web-7b6955f56f-t8cb9     1/1     Running   0          2d7h

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1/1     1            1           19s
deployment.apps/web     1/1     1            1           2d7h
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl expose deployment nginx --port=80 --type=NodePort
service/nginx exposed  #直接nginx的service服务
[root@k8s-master ~]# kubectl get pods,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-748c667d99-nnnpz   1/1     Running   0          15m
pod/web-7b6955f56f-t8cb9     1/1     Running   0          2d7h

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        2d23h
service/nginx        NodePort    10.109.239.113   <none>        80:21397/TCP   10s
service/web          NodePort    10.103.63.38     <none>        80:31960/TCP   2d7h
[root@k8s-master ~]#

第四. 验证nginx的pod是否正常

k8s快速生成yaml的两种方式

可以正常访问,就是OK