Kubernetes 第五章 YAML

对于kubernetes 资源可以使用加载 yaml 标记语言的方式,进行自定义:

YAML(/ˈjæməl/,尾音类似camel骆驼)是一个可读性高,用来表达数据序列化的格式。YAML参考了其他多种语言,包括:C语言、Python、Perl,并从XML、电子邮件的数据格式(RFC 2822)中获得灵感。Clark Evans在2001年首次发表了这种语言[1],另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者[2]。当前已经有数种编程语言或脚本语言支持(或者说解析)这种语言。

YAML是"YAML Ain't a Markup Language"(YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)[3],但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。

使用YAML用于k8s的定义将给你一些好处,包括:

  • 便捷性:你将不再需要添加大量的参数到命令行中执行命令
  • 可维护性:YAML文件可以通过源头控制,可以跟踪每次的操作
  • 灵活性:通过YAML你将可以创建比命令行更加复杂的结构

YAML是一个JSON的超集,意味着任何有效JSON文件也都是一个有效的YAML文件。所以一方面,如果你知道JSON,你只是要去写自己的YAML(而不是阅读别人的)也就可以了。另一方面,不太可能,不幸的是,尽管你尝试去网上找到例子,但是他们通常都不是JSON,所以我们可能需要去习惯它。不过,有JSON的情况下可能会更方便,这样你将会很开心你懂得JSON。

幸运的是,YAML只有两种结构类型你需要知道:

  • Lists
  • YAML lists 是一个序列的对象
  • Maps
  • Maps让你将键值组合,你就可以更加方便的去设置配置信息

  

查看pod 资源清单

[root@kube ~]# kubectl get pod nginx-app-7756966bc9-qrksb -o yaml
    //这是一个通过 kubectl run 方式运行的 pod ,那么我们可以手动定义一个 和这个类似的 yaml 配置文件进行加载然后生成pod
apiVersion: v1 kind: Pod metadata: creationTimestamp: "2019-07-16T06:39:02Z" generateName: nginx-app-7756966bc9- labels: pod-template-hash: 7756966bc9 run: nginx-app name: nginx-app-7756966bc9-qrksb namespace: default ownerReferences: - apiVersion: apps/v1 blockOwnerDeletion: true controller: true kind: ReplicaSet name: nginx-app-7756966bc9 uid: 51997507-0ad5-4f71-86db-3ad6eff70171 resourceVersion: "603662" selfLink: /api/v1/namespaces/default/pods/nginx-app-7756966bc9-qrksb uid: 7035cfbd-a8f3-45a9-933c-4ce1cef45183 spec: containers: - image: nginx:alpine imagePullPolicy: IfNotPresent name: nginx-app ports: - containerPort: 80 protocol: TCP resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /var/run/secrets/kubernetes.io/serviceaccount name: default-token-bsthb readOnly: true dnsPolicy: ClusterFirst enableServiceLinks: true nodeName: kube.node1 priority: 0 restartPolicy: Always schedulerName: default-scheduler securityContext: {} serviceAccount: default serviceAccountName: default terminationGracePeriodSeconds: 30 tolerations: - effect: NoExecute key: node.kubernetes.io/not-ready operator: Exists tolerationSeconds: 300 - effect: NoExecute key: node.kubernetes.io/unreachable operator: Exists tolerationSeconds: 300 volumes: - name: default-token-bsthb secret: defaultMode: 420 secretName: default-token-bsthb status: conditions: - lastProbeTime: null lastTransitionTime: "2019-07-16T06:39:02Z" status: "True" type: Initialized - lastProbeTime: null lastTransitionTime: "2019-07-16T06:39:36Z" status: "True" type: Ready - lastProbeTime: null lastTransitionTime: "2019-07-16T06:39:36Z" status: "True" type: ContainersReady - lastProbeTime: null lastTransitionTime: "2019-07-16T06:39:02Z" status: "True" type: PodScheduled containerStatuses: - containerID: docker://6f50a8038d1f9b484c3af46fa21ef0bdce963b95c4f7f5980302bc70ca46eea6 image: nginx:alpine imageID: docker-pullable://nginx@sha256:17bd1698318e9c0f9ba2c5ed49f53d690684dab7fe3e8019b855c352528d57be lastState: {} name: nginx-app ready: true restartCount: 0 state: running: startedAt: "2019-07-16T06:39:36Z" hostIP: 10.2.61.22 phase: Running podIP: 10.244.2.7 qosClass: BestEffort startTime: "2019-07-16T06:39:02Z" [root@kube ~]#

  

针对yaml 格式我们对如下进行分析:

#maps 是key|value 的组合,list 是key[args,args ,多个项的组合]用破折号(-)开头
apiVersion: v1 //maps kind: Pod     //maps metadata:       name: rss-site  //maps labels: app: web    /maps spec: //maps containers: //list name - name: front-end  //list参数子项是maps image: nginx    //list 参数子项是maps ports: - containerPort: 80 //list 参数子项maps - name: rss-reader image: nickchase/rss-php-nginx:v1 ports: - containerPort: 88

  

创建一个简单的yaml 文件

root@kube test]# cat pod-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
   name: pod-test
   namespace: default
   labels:
     app: myapp
     tier: frontend
spec:
   containers:
     - name: mytest
       image: nginx:latest
            
[root@kube test]# 
[root@kube test]# kubectl create -f pod-demo.yaml
pod/pod-test created
[root@kube test]# kubectl get pods
NAME                         READY   STATUS              RESTARTS   AGE
busy1-78c9f4b47-pm2qx        0/1     CrashLoopBackOff    22         100m
busy2-7f9dbf96d6-2d778       1/1     Running             0          2d23h
busy3-9877c76bf-ts5rl        1/1     Running             0          2d23h
nginx-7bb7cd8db5-6sgvp       1/1     Running             0          3d
nginx-app-54c844949f-6zlmr   0/1     ContainerCreating   0          41h
nginx-app-54c844949f-n82d6   1/1     Running             0          2d
nginx-app-54c844949f-xmkb8   1/1     Running             0          2d
nginx-app-7756966bc9-qrksb   1/1     Running             0          2d2h
pod-test                     0/1     ContainerCreating   0          1s
[root@kube test]# 

  

[root@kube test]# kubectl describe pod pod-test
Name:         pod-test
Namespace:    default
Priority:     0
Node:         kube.node1/10.2.61.22
Start Time:   Thu, 18 Jul 2019 16:44:23 +0800
Labels:       app=myapp
              tier=frontend
Annotations:  <none>
Status:       Running
IP:           10.244.2.12
Containers:
  mytest:
    Container ID:   docker://4e40fc5b6cae440881cea707b79c2d17692c47d75df69a571fa0e7d57dff5e3a
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Thu, 18 Jul 2019 16:44:46 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-bsthb (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-bsthb:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-bsthb
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From                 Message
  ----    ------     ----  ----                 -------
  Normal  Scheduled  62s   default-scheduler    Successfully assigned default/pod-test to kube.node1
  Normal  Pulling    61s   kubelet, kube.node1  Pulling image "nginx:latest"
  Normal  Pulled     40s   kubelet, kube.node1  Successfully pulled image "nginx:latest"
  Normal  Created    40s   kubelet, kube.node1  Created container mytest
  Normal  Started    39s   kubelet, kube.node1  Started container mytest
[root@kube test]# 

  

 

[root@kube ~]# 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/v1
coordination.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
[root@kube ~]# kubectl explain pods
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds

   metadata	<Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata

   spec	<Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

   status	<Object>
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

[root@kube ~]# 

  

 

猜你喜欢

转载自www.cnblogs.com/zy09/p/11208338.html