YAML文件(资源编排)

作者: 风 哥 分类: Kubernetes 发布时间: 2019-01-28 16:52

YAML 是一种简洁的非标记语言。

语法格式:

• 缩进表示层级关系

• 不支持制表符“tab”缩进,使用空格缩进

• 通常开头缩进 2 个空格

• 字符后缩进 1 个空格,如冒号、逗号等

• “—” 表示YAML格式,一个文件的开始

• “#”注释

 

创建nginx pod对象

< 130  master01 - [root]: ~/yaml-test > # vim nginx-web.yaml

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: default

spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx

    spec:
      containers:
      - name: nginx
        image: nginx:1.15
        ports:
        - containerPort: 80

 

< 131  master01 - [root]: ~/yaml-test > # kubectl create -f nginx-web.yaml
deployment.apps/nginx-deployment created
< 134  master01 - [root]: ~/yaml-test > # kubectl get pods -l  app=nginx
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5d445489fb-jmpm7   1/1     Running   0          26s
nginx-deployment-5d445489fb-jsfg7   1/1     Running   0          26s
nginx-deployment-5d445489fb-q2xgh   1/1     Running   0          26s

 

创建nginx service服务

< 146  master01 - [root]: ~/yaml-test > # vim nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-web-service
  labels:
    app: nginx

spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80

  selector:
    app: nginx
< 144  master01 - [root]: ~/yaml-test > # kubectl create -f nginx-service.yaml
service/nginx-web-service created
< 145  master01 - [root]: ~/yaml-test > # kubectl get services
NAME                TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes          ClusterIP   10.0.0.1     <none>        443/TCP        3d
nginx-service       NodePort    10.0.0.210   <none>        80:42451/TCP   116m
nginx-web-service   NodePort    10.0.0.29    <none>        80:47489/TCP   7s

 

 

 

用run命令生成

< 147  master01 - [root]: ~/yaml-test > # kubectl run --image=nginx my-deploy -o yaml --dry-run > my-deploy.yaml
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.

用get命令导出

get命令可以导出已经运行的资源然后将状态转换成yaml文件保存

< 150  master01 - [root]: ~/yaml-test > # kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-7b67cfbf9f-fbgmr              1/1     Running   0          124m
nginx-7b67cfbf9f-hbvg2              1/1     Running   0          124m
nginx-7b67cfbf9f-qwhf6              1/1     Running   0          124m
nginx-deployment-5d445489fb-jmpm7   1/1     Running   0          14m
nginx-deployment-5d445489fb-jsfg7   1/1     Running   0          14m
nginx-deployment-5d445489fb-q2xgh   1/1     Running   0          14m
< 151  master01 - [root]: ~/yaml-test > #
< 151  master01 - [root]: ~/yaml-test > #
< 151  master01 - [root]: ~/yaml-test > #
< 151  master01 - [root]: ~/yaml-test > #
< 151  master01 - [root]: ~/yaml-test > #
< 151  master01 - [root]: ~/yaml-test > #
< 151  master01 - [root]: ~/yaml-test > # kubectl get my-deploy/nginx -o=yaml --export  > my-deploy.yaml

查看部署容器所涉及的字段

kubectl explain pods.spec.containers

发表评论

电子邮件地址不会被公开。 必填项已用*标注