Kubernetes第七曲 Kubernetes支持YAML和JSON格式创建资源对象

Kubernetes第七曲 Kubernetes支持YAML和JSON格式创建资源对象

一、YAML和JSON

1、概述

Kubernetes支持YAML和JSON格式创建资源对象
JSON格式用于接口之间消息的传递
YAML格式用于配置和管理
YAML是一种简洁的非标记性语言

2、语法格式:

缩进标识层级关系
不支持制表符缩进,使用空格缩进
通常开头缩进两个空格
字符后缩进一个空格,如冒号,逗号,短横杆等
“—”表示YAML格式,一个文件的开始
“#”表示注释

[root@localhost demo]# kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
apps/v1beta1
apps/v1beta2
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1

二、演示yaml

1、编写yaml文件

[root@localhost ~]# mkdir demo     #创建一个专门的目录
[root@localhost ~]# cd demo/
[root@localhost demo]# vim nginx-deployment.yaml    #编写yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
       image: nginx:1.15.4
       ports:
       - containerPort: 80

2、创建资源,kind种类为deployment

[root@localhost demo]# kubectl create --help
Usage:
  kubectl create -f FILENAME [options]

[root@localhost demo]# kubectl create -f nginx-deployment.yaml  #创建资源
deployment.apps/nginx-deployment created

[root@localhost demo]# kubectl get pods     #查看创建的资源
NAME                              READY   STATUS    RESTARTS   AGE
nginx-7697996758-75shs            1/1     Running   0          21h
nginx-7697996758-b7tjw            1/1     Running   0          21h
nginx-7697996758-jddc5            1/1     Running   0          21h
nginx-deployment-d55b94fd-4px2w   1/1     Running   0          2m13s
nginx-deployment-d55b94fd-899hz   1/1     Running   0          2m13s
nginx-deployment-d55b94fd-d7fqn   1/1     Running   0          2m13s

3、编写service的yaml

[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

[root@localhost demo]# kubectl create -f nginx-service.yaml 
service/nginx-service created

[root@localhost demo]# kubectl get svc  
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        13d
nginx-service   NodePort    10.0.0.225   <none>        80:47722/TCP   23s

三、详解k8s中的port

port
port是k8s集群内部访问service的端口,即通过clusterIP: port可以访问到某个service

nodePort
nodePort是外部访问k8s集群中service的端口,通过nodeIP: nodePort可以从外部访问到某个service。

targetPort
targetPort是pod的端口,从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上,最后进入容器。

containerPort
containerPort是pod内部容器的端口,targetPort映射到containerPort。

Kubernetes第七曲 Kubernetes支持YAML和JSON格式创建资源对象

四、自动测试命令的正确性,并不执行创建(–dry-run)

[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

#也可以> 导出到一个新文件
[root@localhost demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml > my-deployment.yaml
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
[root@localhost demo]# ls
my-deployment.yaml  nginx-deployment.yaml  nginx-service.yaml

七、将现有的资源生成模板导出

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

八、保存到文件中

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

九、查看字段帮助信息

[root@localhost demo]# kubectl explain pods.spec.containers
上一篇:Kubernetes Pod 中的 ConfigMap 配置更新


下一篇:第1章 网络编程基础(2)——Socket编程原理