k8s中教你快速写一条yaml文件

一条yaml中有很多字段,如果去背这些字段,其实也能背过,但是去写一条yaml,也往往浪费很多的时间,也会出错,其实我们可以用一条命令就能快速来写一段自定义的yaml,工作中去修改相应的yaml也得心应手,效率也会很高,接下来我们来看一下

[root@k8s-master ~]# kubectl create deployment nginx --image=nginx -o yaml --dry-run > my-deployment.yaml

-o yaml指定我们的yaml文件

--dry-run 不在k8s中执行

>重定向到我们的文件中
[root@k8s-master ~]# vim my-deployment.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: {}

这是新创建一条yaml文件,我们也可以根据我们的原有的pod来生成yaml,或者修改

[root@k8s-master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7bb7cd8db5-jrzgg   1/1     Running   0          93m
[root@k8s-master ~]# kubectl get deploy
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           93m
[root@k8s-master ~]# kubectl get deploy nginx -o yaml --export > my-deploy2.yaml

-o yaml:指定yaml文件

--export:输出

Flag --export has been deprecated, This flag is deprecated and will be removed in future.
[root@k8s-master ~]# vim my-deploy2.yaml
[root@k8s-master ~]# cat my-deploy2.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: null
  generation: 1
  labels:
    run: nginx
  name: nginx
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      run: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: 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: {}
这样的yaml效率很高,也不易出错,希望可以帮到你

转载于:https://www.cnblogs.com/zc1741845455/p/11314209.html

你可能感兴趣的:(k8s中教你快速写一条yaml文件)