美文网首页
YAML格式和K8S的设置, 2023-11-05

YAML格式和K8S的设置, 2023-11-05

作者: Mc杰夫 | 来源:发表于2023-11-04 13:27 被阅读0次

    (2023.11.05 Sun @KLN)

    YAML格式

    YAML全称Yet Another Makeup Language。经常见到的.yml格式文件可以当做是json文件的超集(superset),也就是说json文件可以转换为.yml文件。

    在Kubernetes中,一个.yml文件作为K8S manifest用来创建pod。

    YAML格式简单易学,其中只有两种数据结构,

    • maps
    • lists
      下面分别介绍。

    YAML Maps

    maps是一个key-value型结构,类似于Python中的dict。

    ---
    apiVersion: v1
    kind: Pod
    

    首行的---是分隔符,在.yml中是可选项,除非需要同单一文件中定义多个结构。在该案例中有两个值v1Pod,对应的key是apiVersionkind

    该案例相当于如下的JSON文件

    {
       "apiVersion": "v1",
       "kind": "Pod"
    }
    

    需要注意的是,在YAML格式中,引号"可以省略。

    maps结构可以更加复杂。

    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: rss-site
      labels: 
        api: web
    

    这个案例中多了一个key metadata,该key是嵌入(nested)式的key,其中还包含了labels和api这样的key

    YAML的处理器可以根据空格数(indentation level)来判定,空格数可两个可多个,由使用者来定,但一定要保持一致性(consistency),比如name和labels是同一级key,所以前面都是两个空格,而api是labels的nested key,所以空四格。

    注意,不要在YAML文件中使用Tab键空格。

    上面案例对应的JSON文件如下

    {
    
      "apiVersion": "v1",
    
      "kind": "Pod",
    
      "metadata": {
    
                   "name": "rss-site",
    
                   "labels": {
    
                              "app": "web"
    
                             }
    
                  }
    
    }
    

    YAML Lists

    一个包含多个元素的数据结构。每个元素前面空格,由-开始,在该符号之后在空一格。一个-符号对应了一个元素。

    案例如

    args:
    
      - sleep
    
      - "1000"
    
      - message
    
      - "Bring back Firefly!"
    

    该list对应的JSON表达为

    {
       "args": ["sleep", "1000", "message", "Bring back Firefly!"]
    }
    

    lists中的元素可以是maps

    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: rss-site
      labels:
        app: web
    spec:
      containers:
        - name: front-end
          image: nginx
          ports:
            - containerPort: 80
        - name: rss-reader
          image: nickchase/rss-php-nginx:v1
          ports:
            - containerPort: 88
    

    该案例对应的JSON文件如

    {
      "apiVersion": "v1",
      "kind": "Pod",
      "metadata": {
                    "name": "rss-site",
                    "labels": {
                                "app": "web" 
                              }
                  },
       "spec": {
                  "containers": [ 
                                  { 
                                    "name": "front-end",
                                    "image": "nginx",
                                    "ports": [
                                               {
                                                  "containerPort": "80"
                                               }
                                             ]
                                  }, 
                                  {
                                    "name": "rss-reader",
                                    "image": "nickchase/rss-php-nginx:v1",
                                    "ports": [
                                               {
                                                  "containerPort": "88"
                                               }
                                             ]
                                  }
                                ]
               }
    }
    

    (2023.11.12 Sun @KLN)

    K8S中YAML设置

    K8S中可以使用设置Pod和Deployment。

    创建pod

    首先配置一个Pod,名pod.yml

    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: rss-site
      labels:
        app: web
    spec:
      containers:
        - name: front-end
          image: nginx
          ports:
            - containerPort: 80
        - name: rss-reader
          image: nickchase/rss-php-nginx:v1
          ports:
            - containerPort: 88
    

    逐个参数分析。apiVersion顾名思义,注意该案例为Pod,当kind为Deployment时,需要指定另一个版本因Deployment不存在于v1中。

    kind参数用来指定YAML创建的类型,除了稳重设置的Pod,还可设置DeploymentJobService以及其他。

    metadata参数,其中包括该Pod的名字,已经该pod在K8S中的识别标签label

    spec参数用来指定该pod的实体。spec的属性包括容器,内存需求,存储容量,网络设置,容器失败时是否重启,以及其他。

    通过kubectl create -f pod.yml命令即可根据pod.yml中指定的设置生成一个pod。

    > kubectl create -f pod.yaml
    pod "rss-site" created
    

    查看当前pod的情况,使用kubectl get pods命令

    > kubectl get pods
     NAME       READY     STATUS              RESTARTS   AGE
     rss-site   0/2       ContainerCreating   0          6s
    

    注意该pod的STATUSContainerCreating,稍事片刻重新执行kubectl get pods命令会看到变为running

    > kubectl get pods
    NAME       READY     STATUS    RESTARTS   AGE
    rss-site   2/2       Running   0          14s
    

    删除该pod,用kubectl delete rss-site

    > kubectl delete pod rss-site
    pod "rss-site" deleted
    

    当pod状态不正常,比如ErrImagePull,时,用kubectl describe pod rss-site查看pod状态

    > kubectl describe pod rss-site
    Name:           rss-site
    Namespace:      default
    Node:           10.0.10.7/10.0.10.7
    Start Time:     Sun, 08 Jan 2017 08:36:47 +0000
    Labels:         app=web
    Status:         Pending
    IP:             10.200.18.2
    Controllers:    
    Containers:
      front-end:
        Container ID:               docker://a42edaa6dfbfdf161f3df5bc6af05e740b97fd9ac3d35317a6dcda77b0310759
        Image:                      nginx
        Image ID:                   docker://sha256:01f818af747d88b4ebca7cdabd0c581e406e0e790be72678d257735fad84a15f
        Port:                       80/TCP
        State:                      Running
          Started:                  Sun, 08 Jan 2017 08:36:49 +0000
        Ready:                      True
        Restart Count:              0
        Environment Variables:      
      rss-reader:
        Container ID:
        Image:                      nickchase/rss-php-nginx
        Image ID:
        Port:                       88/TCP
        State:                      Waiting
          Reason:                   ErrImagePull
        Ready:                      False
        Restart Count:              0
        Environment Variables:      
    Conditions:
      Type          Status
      Initialized   True
      Ready         False
      PodScheduled  True
    No volumes.
    QoS Tier:       BestEffort
    Events:
      FirstSeen     LastSeen        Count   From                    SubobjectPath  Type             Reason                  Message
      ---------     --------        -----   ----                    -------------  -------- ------                  -------
      45s           45s             1       {default-scheduler }                   Normal           Scheduled               Successfully assigned rss-site to 10.0.10.7
      44s           44s             1       {kubelet 10.0.10.7}     spec.containers{front-end}      Normal          Pulling                 pulling image "nginx"
      45s           43s             2       {kubelet 10.0.10.7}                    Warning          MissingClusterDNS       kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
      43s           43s             1       {kubelet 10.0.10.7}     spec.containers{front-end}      Normal          Pulled                  Successfully pulled image "nginx"
      43s           43s             1       {kubelet 10.0.10.7}     spec.containers{front-end}      Normal          Created                 Created container with docker id a42edaa6dfbf
      43s           43s             1       {kubelet 10.0.10.7}     spec.containers{front-end}      Normal          Started                 Started container with docker id a42edaa6dfbf
      43s           29s             2       {kubelet 10.0.10.7}     spec.containers{rss-reader}     Normal          Pulling                 pulling image "nickchase/rss-php-nginx"
      42s           26s             2       {kubelet 10.0.10.7}     spec.containers{rss-reader}     Warning         Failed                  Failed to pull image "nickchase/rss-php-nginx": Tag latest not found in repository docker.io/nickchase/rss-php-nginx
      42s           26s             2       {kubelet 10.0.10.7}                    Warning          FailedSync              Error syncing pod, skipping: failed to "StartContainer" for "rss-reader" with ErrImagePull: "Tag latest not found in repository docker.io/nickchase/rss-php-nginx"
      41s   12s     2       {kubelet 10.0.10.7}     spec.containers{rss-reader}    Normal   BackOff         Back-off pulling image "nickchase/rss-php-nginx"
      41s   12s     2       {kubelet 10.0.10.7}                                    Warning  FailedSync      Error syncing pod, skipping: failed to "StartContainer" for "rss-reader" with ImagePullBackOff: "Back-off pulling image \"nickchase/rss-php-nginx\""
    

    创建deployment

    (2023.11.18 Sat @KLN)
    K8S的deployment应用包括:

    • workload的持续可用:deployment指定特定的保持运行的workload的拷贝数量,一旦某个workload挂掉,K8S自动重启该workload,确保高可用
    • 扩展workloads:K8S使得修改deployment的replicas数量变得容易
    • 管理应用状态:deployment可以暂停、编辑、回滚(rollback)
    • 易于将workload暴露于cluster外部

    案例:

    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: rss-site
      labels:
        app: web
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: web
      template:
        metadata:
          labels:
            app: web
        spec:
          containers:
            - name: front-end
              image: nginx
              ports:
                - containerPort: 80
            - name: rss-reader
              image: nickchase/rss-php-nginx:v1
              ports:
                - containerPort: 88
    

    和pod的模版有很多相似,差别之一是kind指定为Deployment。该文件保存为deployment.yaml,创建deployment命令kubectl create -f deployment.yaml

    > kubectl create -f deployment.yaml
    deployment "rss-site" created
    

    检测deployment列表,命令kubectl get deployment

    > kubectl get deployments
    NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    rss-site   2         2         2            1           7s
    

    查看某deployment的详细信息,命令同pod,kubectl describe deployment rss-site

    更新某个deployment,最简单的方式是更新创建该deployment的YAML文件,此时需要使用apply命令而非create命令。

    kubectl apply -f deployment.yaml
    

    此时更新已经进入YAML文件,再次运行kubectl apply则生效。

    另一种方法是用kubectl edit命令编辑某个特定的对象

    kubectl edit deployment.v1.apps/rss-site
    

    接着会跳转到一个编辑器编辑YAML文件。保存变更时也将自动应用于在线对象。

    扩展deployment的另一个方法是用scale

    kubectl scale deployment.v1.apps/rss-site --replicas=5
    

    甚至可以自动扩展,比如设置pod使用不超过60%的CPU

    kubectl autoscale deployment.v1.apps/rss-site --min=3 --max=20 --cpu-percent=60
    

    K8S仍然有其他的方式管理扩展。

    相关文章

      网友评论

          本文标题:YAML格式和K8S的设置, 2023-11-05

          本文链接:https://www.haomeiwen.com/subject/avjaidtx.html