4.kubernetes:podコントローラを深く理解する


1.自主pod資源
一級フィールド:
apiVersion(group/version)
kind
metadata
spec
status
 
2.podリソース
spec.containers
- name
image
imagePullPolicy Always(ローカルにあるかどうかにかかわらずダウンロードし、最もよく使われる)、Never(あれば使う、なければダウンロードしない)、IfNotPresent(ローカルに存在しないならダウンロード)
ミラー名ラベルがlatest、オーバーラップポリシーがIfNotPresentの場合alwaysに等しい
ports:[]object,デフォルトTCPプロトコル
-name:http(サービスで参照できます)
containerPort: 8080
- name: https
containerPort: 443
command[]string(実行するプログラム、[]stringは[]の形式で使用できます)
Args[]string(commandに渡されるパラメータ、変数置換は$(VAR_NAME)を使用)
 
 
3.dockerのcmdとentrypointとk 8 sのcommandとargsの関係
  • If you do not supply command or args for a Container, the defaults defined in the Docker image are used.
  • If you supply a command but no args for a Container, only the supplied command is used. The default EntryPoint and the default Cmd defined in the Docker image are ignored.
  • If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you supplied.
  • If you supply a command and args, the default Entrypoint and the default Cmd defined in the Docker image are ignored. Your command is run with your args.

  •  
     
     
    4.ラベルのアート
    機能ラベル:フロントエンド、バックエンド、データベース.キャッシュ
    バージョンラベル:安定版、開発版、bate版、alpha版.
    環境ラベル:env=qa,prd
    タブ選択:
    (1)等値
    kubectl get pods-l release=stable(このラベルがあり、値がstableの)
    kubectl get pods-l release(このラベルがある)
    kubectl get pods -l release=stable,app!=myapp(この2つのラベルkvは一致する)
    (2)集合関係
    key in (v1,v2)
    key not in (v1,v2)
    !key
    key
    kubectl get pods -l "release in (v1,v2)"
     
    (3)ラベルの関連付け
    matchLabels:keyを直接与える
    matchExpressions:指定された式に基づいてラベルセレクタの使用を定義する.{key:"KEY",operator:"OPERATER",values:[v1,v2]}
    オペレータ:in notin,exists,NotExists
     
    (4)操作
    key:アルファベット、数値下線、接続線、点
    value:空白にできます
    -L:appタグのタグ値を表示する
    kubectl get pods -L app
    -l:appタグを持つ:
    kubectl get pods -l app --show-labels
    リソースラベルの変更
    kubectl label pods pod-demo release=canary--overwrite(releaseラベルが既に存在する場合は上書きする必要があります)
     
    kubectl get nodes --show-labels
    ノードセレクタ:
    nodeSelector
    nodename:指定したnodeでのみ実行できます.
    annotations:labelとは異なる点は、リソースオブジェクトを選択できず、オブジェクトにメタデータを提供するためにのみ使用される点である.
     
    apiVersion: v1
    kind: Pod
    matedata:
        name: myapp
        namespace: default
        labels:(kv   )
            app: myapp
            tier: frontend   
    spec:
        containers:([]object        -)
        - name:myapp 
          image: ikube/app:v1
        - name: busybox
          image: busy:latest
          command: 
          - "bin/sh"
          - "-c"
          - "echo ${date} >> /usr/share/nginx/html; sleep 5"
         nodeSelector: ( containers      )
           diskType: ssd 
      

    5.podのライフサイクル
    ステータス:
    pending:保留、条件が満たされない、例えばnodeが条件を満たすことができない.掛けるしかない.
    Running
    Failed
    Successed
    Unknown
    podを作成するには:
    (1)初期化容器
    (2)容器探知:
    liveness
    readiness
    6.再起動ポリシーrestartPolicyy:
  • always;podiのcontaionerが切れたら再起動します.再起動ポリシーは絶えず遅延する.コンテナの再起動は、同じnodeで
  • を再起動します.
  • OnFailure:
  • Never
  • Defaultはalways
  • です
     
    ---------------------------------------------------------------------------------------------------------------------------------------
    二.podプローブ
    1.プローブ:
    (1)livenessProbe
    execプローブ:
     
    apiVersion: v1
    kind: pod
    metadata:
        name: liveness-exec-container
        namespace: default
    spec:
        containers: 
        - name: liveness-exec-container
          image: busybox:latest
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh","-c","touch /tmp/healthy;sleep 30;rm -rf /tmp/healthy ;sleep 3600"]
          livenessProbe:
              exec:
                  command: ["test","-e","/tmp/healthy"]
              initialDelaySeconds: 1
              periodSeconds: 3    

    httpプローブ:
     
    apiVersion: v1
    kind: pod
    metadata:
        name: liveness-http-container
        namespace: default
    spec:
        containers: 
        - name: liveness-http-container
          image: xxx/myapp:v1
          imagePullPolicy: IfNotPresent
          ports:
          - name: http
            containerPort: 80    
          livenessProbe:
              httpGet:
                  path: /index.html
                  port: http
              initialDelaySeconds: 1
              periodSeconds: 3    

    (2)readinessProbe:レディプローブ
    apiVersion: v1
    kind: pod
    metadata:
        name: readyness-http-container
        namespace: default
    spec:
        containers: 
        - name: readyness-http-container
          image: xxx/myapp:v1
          imagePullPolicy: IfNotPresent
          ports:
          - name: http
            containerPort: 80    
          readinessProbe:
              httpGet:
                  path: /index.html
                  port: http
              initialDelaySeconds: 1
              periodSeconds: 3  

    (3)lifeCycle
    postStart:起動後に実行する動作:コマンドの相互関係に注意し、containerのcommandはpoststartのexecのcommandと関係がない.
    apiVersion: v1
    kind: Pod
    metadata:
        name: postStart-pod
    spec:
        containers:
        - name: busybox-httpd
          image: busybox:latest
          imagePullPolicy: IfNotPresent
          lifecycle:
              postStart:
                  exec:
                      command: ["/bin/sh","-C","mkdir -p /data/web/html;echo aaa >> /data/web/html/index.html"]
          command: ["/bin/httpd"]
          args: ["-f","-h /data/web/html"]  

    presStop:終了前の動作
    1.spec:
     	container
    nodeSelector
    nodeName
    restartPolicy:
    Always,Never,OnFailure
    containers:
    name
    image
    imagePullPolicy:Always.Never,IfNotPresent
    ports:
    name
    containerPort
    livenessProbe
    readlinessProbe
    lifecycle

    2.podコントローラ
    自律型Pod:コントローラに管理されていないpod.
     
    3.ReplicaSet-代替RC
    コアリソースは次のとおりです.
    (1)コピー数
    (2)タブセレクタ
    (3)podテンプレート
    コマンドこまんど:kubectl explain rs
    KIND:     ReplicaSet
    VERSION:  extensions/v1beta1
    DESCRIPTION:
         DEPRECATED - This group version of ReplicaSet is deprecated by
         apps/v1beta2/ReplicaSet. See the release notes for more information.
         ReplicaSet ensures that a specified number of pod replicas are running at
         any given time.

    コマンド:kubectl explain rs.spec
    3つの主要なリソース:
      replicas	
         Replicas is the number of desired replicas. This is a pointer to
         distinguish between explicit zero and unspecified. Defaults to 1. More
         info:
         https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller
    
       selector	
         Selector is a label query over pods that should match the replica count. If
         the selector is empty, it is defaulted to the labels present on the pod
         template. Label keys and values that must match in order to be controlled
         by this replica set. More info:
         https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
    
       template	
         Template is the object that describes the pod that will be created if
         insufficient replicas are detected. More info:
         https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template
    

    kubectl explain rs.spec.template.specのspecはpodのspecです.
     
    replicasetを定義します.
    apiVersion: apps/v1
    kind: ReplicaSet
    metadata:
      name: myapp
      namespace: default
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: myapp
          release: canary
      templete:
        metadata:
          name: myapp-pod
          labels: #            matchLabels         ,           pod,      
            app: myapp
            release: canary
        spec: #   pod spec
          containers:
          - name: myapp-container
            image: ikubernetes/myapp:v1
            ports:
            - name: http
              containerPort: 80

    rsで定義されたpod記述を変更した場合、再構築または新規のpodのみが変更され、元のpodは変更されません.グレースケールパブリケーションに適用されます.
    金糸雀リリースvsグレースケールリリース
    17  ,        ,              。             ,         ;             ,           ,          。               ,                  “      ”,            。

    青緑のリリース:
    別のrsを作成し、異なるラベルセレクタを使用します.サービスにすべてのpodを関連付けることができます
     
    4.deployment
    replicasetの上で動作し、replicasetはpodを制御します.ステータスレスサービスのみを管理できます.
    deploymentは、複数のrsを同時に制御して、階調パブリケーションの効果を実現することができる.
    deployは最大10 rsを保持します.
    また、更新の粒度を制御することもできます.1つでも少なくても、2つでもかまいません.readinessプローブが重要です.
    リズムをコントロールすることで、階調、カナリア、青緑の配置を実現します.
     
    5.DaemonSet:ログ収集シーン
    各ノードに1つのagentがあり、ノードレベルで動作し、DaemonSetを使用して実現されます.システムレベルのデーモンプロセスを実現します.
     
    6.Job:ワンタイムタスク、再構築するかどうかは、タスクが正常に完了するかどうかによって決まります.
     
    7.cronJob:周期的なタスク.
     
    8.statefulSet:ステータスサービスあり.
    独自のリソースを持ち、新しく加入したpodはこれらのリソースを継承します.StatefulSetは、サービスを構成するために必要な操作手順が異なり、複雑なスクリプトを人為的に定義する必要があります.
     
    9.CDRリソース:ユーザー定義リソース1.8+
     
    10.Operator
    プルミシューズ、ETCDなど.
     
    11.Helm:conteosのyumに似ている
    -----------------------------------------------------------------------------------------------------------------------
    podコントローラ更新ポリシーの概要
    1.deploy
    ポリシーの更新:
    (1)kubectl explain deploy.spec.strategy.rollingUpdate
    maxSurge: The maximum number of pods that can be scheduled above the desired number
    of pods. Value can be an absolute number (ex: 5) or a percentage of desired
    pods (ex: 10%).
    maxUnavailable:The maximum number of pods that can be unavailable during the update. Value
    can be an absolute number (ex: 5) or a percentage of desired pods (ex:
    10%)
    2.deployを作成する
    apiVersion: apps/v1
    kind: Deployment
    metadata:
        name: myapp-deploy
    spec:
        replicas: 2
        selector:
            matchLabels:
                app: myapp
                release: canary
        templete:
            metadata:
                labels: 
                    app: myapp 
                    release: canary
            spec:
                containers:
                -   name:myapp
                    image: ikubernetes/myapp:v1
                    ports:
                    - name:http
                      containerPort: 80     

    kubectl apply -f deploy-demo.yaml:applyは、作成または更新操作に使用できます.
     
    3.kubectl path deploy myapp-deploy -p '{"spec":{"replicas":5}}'
    podを見ると5つになりました.
     
    4.kubectl path deploy myapp-deploy -p '{"spec":{"strategy":{“rollingUpdate”:{"maxSurge":1,"maxUnavailable":0}}}}'
     
    5.kubectl set image deploy myapp-deploy myapp=ikubernetes/myapp:v3 && kubectl rollout pause deployment myapp-deployment
     
    6.ロールバック:
    kubectl rollout undo前のバージョンにロールバック
    kubectl rollout history deployment myapp-deploy
    1,2,3バージョンがある場合
    kubectl rollout undo--to-revision=1第1版にロールバック
    スクロールして第1版がなくなり、2,3,4版だけになりました.
     
    7.redisとfilebeatの定義
    apiVersion: apps/v1
    kind: Deployment
    metadata:
        name: redis
    spec:
        replicas: 1
        selector:
          matchLabels:
              app: redis
             role: logstor
         templete:
             metadata:
                 labels:
                     app: redis
                     role: logstor
             spec:
                 containers:
                 -   name: redis
                     image: redis:4.0
                     ports:
                     - name: redis
                       containerPort: 6379
    ---
    apiVersion: apps/v1
    kind: DaemonSet
    metadata:
      name: myapp-ds
    spec:
      selector:
        matchLabels:
          app: filebeat
          release: stable
      templete:
        metadata:
          labels:
            app: filebeat
            release: stable
        spec:
          containers:
          - name: filebeat
            image: ikubernetes/filebeat:5.6.5-alpine
            env:
            - name: REDIS_HOST
              value: redis.default.svc.cluster.local
            - name: REDIS_LOG_LEVEL
              value: info