kubernetes支持yaml和json格式创建资源对象
json格式用于接口之间消息的传递
yaml格式用于配置和管理
yaml是一种简洁的非标记性语言
语法格式:
缩进标识层级关系
不支持制表符缩进,使用空格缩进
通出开头缩进两个空格
字符集缩进两个空格
字符集缩进一个空格,如冒号,逗号等
“—”表示yaml格式,一个文件的开始
“#”表示注释

//创建demo文件
[root@master1 ~]#mkdir demo
[root@master1 ~]#cd demo/
[root@master1 demo]# vim nginx-deployment.yaml
apiVersion: apps/v1      //版本类型
kind: Deployment         //指定创建资源的角色或类型,deployment表示控制台
metadata:               //定义元数据
  name: nginx-deployment    //资源名字
  labels:              ///定义标签
    app: nginx        
spec:               ///指定该资源的内容
  replicas: 3      ///指定副本数
  selector:         选择器
    matchLabels:     ///匹配标签
      app: nginx     ///匹配模板
  template:         ///分区岭上面指定是控制器下面是pod资源配置
    metadata:
      labels:
        app: nginx
    spec:            ///指定该资源的内容
      containers:    ///定义容器信息
      - name: nginx   ///定义容器名跟labels保持一致
        image: nginx:1.15.4    ///容器镜像版本
        ports:
        - containerPort: 80     ///容器对外接口

///创建pod
 [root@localhost demo]# kubectl create -f nginx-deployment.yaml 
  deployment.apps/nginx-deployment created
 ///查看pod
  [root@localhost demo]# kubectl get pods
   NAME                              READY   STATUS    RESTARTS   AGE
nginx-7697996758-5458k            1/1     Running   0          3h25m
nginx-7697996758-dz95v            1/1     Running   0          3h25m
nginx-7697996758-wqg6g            1/1     Running   0          3h25m
nginx-deployment-d55b94fd-dqsvz   1/1     Running   0          163m
nginx-deployment-d55b94fd-gtl8t   1/1     Running   0          163m
nginx-deployment-d55b94fd-t6q6r   1/1     Running   0          163m     

//编辑service 的yaml资源发布pod
 [root@localhost demo]# vim nginx-service.yaml
 apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
spec:
  type: NodePort    
  ports:
  - port: 80
    targetPort: 80  
  selector:
    app: nginx
 ///发布pod
 [root@localhost demo]# kubectl create -f nginx-service.yaml
 service/nginx-service created
 [root@master1 demo]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        3d3h
nginx-service   NodePort    10.0.0.163   <none>        80:36288/TCP   162m

自动测试命令的正确性,并不执行创建

[root@localhost demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx-deployment created (dry run)
//查看生成yaml格式
[root@localhost demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml
//查看生成json格式
[root@localhost demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o json
///将生成的文件导入到文件中(在把导出的文件当作模板直接修改成yaml文件)
[root@localhost demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml > my-deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: nginx-deployment
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      run: nginx-deployment
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: nginx-deployment
    spec:
      containers:
      - image: nginx
        name: nginx-deployment
        ports:
        - containerPort: 80
        resources: {}
status: {}

将现有的资源生成模板导出到文件中

[root@localhost demo]# kubectl get deploy/nginx --export -o yaml > my-deploy.yaml
Logo

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

更多推荐