zoukankan      html  css  js  c++  java
  • Deployment.spec.selector.matchLables实验解释

    原文:https://cloud.tencent.com/developer/article/1394657

    Deployment.spec.selector.matchLables实验解释

    作者: 张首富
    时间: 2019-02-23
    个人博客: www.zhangshoufu.com
    QQ群: 895291458

    正确的Deployment,让matchLabels 和template.metadata.lables完全比配不报错

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

    pod创建成功

    [root@rke test_yaml]# kubectl get pods
    NAME                      READY   STATUS    RESTARTS   AGE
    my-nginx-9b44d8f5-d6n8z   1/1     Running   0          3s
    my-nginx-9b44d8f5-zzv52   1/1     Running   0          3s

    直接不写spec.mathlabels创建直接报错缺少缺少必要字段selector

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-nginx
    spec:
      replicas: 2
      template:
        metadata:
          labels:
            run: my-nginx
        spec:
          containers:
          - name: my-nginx
            image: nginx
            ports:
            - containerPort: 80

    运行报错结果如下:

    [root@rke test_yaml]# kubectl create -f test_pod_svc.yaml
    error: error validating "test_pod_svc.yaml": error validating data: ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false

    #当把matchLables匹配的和下面pod模板不相对应,也会直接报错,选择的和模板标签不匹配

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-nginx
    spec:
      selector:
        matchLabels:
          app: my-nginx-add
      replicas: 2
      template:
        metadata:
          labels:
            app: my-nginx
        spec:
          containers:
          - name: my-nginx
            image: nginx:1.14
            ports:
            - containerPort: 80

    运行报错结果如下:

    The Deployment "my-nginx" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"my-nginx"}: `selector` does not match template `labels`

    查看帮助手册

     kubectl explain Deployment.spec
    
        selector  <Object>
         Label selector for pods. Existing ReplicaSets whose pods are selected by
         this will be the ones affected by this deployment.
    
         pod的标签选择器。 由此选择其pod的现有ReplicaSet(副本集)将受此部署影响的副本。

    #总结: 1,在Deployment中必须写matchLables, 2,在定义模板的时候必须定义labels,因为Deployment.spec.selector是必须字段,而他又必须和template.labels对应, 3,template里面定义的内容会应用到下面所有的副本集里面,在template.spec.containers里面不能定义labels标签.

  • 相关阅读:
    多层结构中,事务的运用。
    A private conversation
    Sql Server 日志清理 (数据库压缩方法)
    Basic of Ajax
    Persin Buttons
    不知为什么无缘无故加到了一个“邯郸.net俱乐部”,想退出,找不到入口.....
    Wokflow designer not working when openning workflow in nonworkflow VS 2005 project
    GridView中如何取得隐藏列的值?
    Error: cannot obtain value
    Too late
  • 原文地址:https://www.cnblogs.com/robinunix/p/11155860.html
Copyright © 2011-2022 走看看