天天看點

KUBERNETES-1-4-控制器應用一

1.kubectl explain pods.spec.containers可以檢視容器可以定義的相關參數。image    <string>用來定義所引用的鏡像。imagePullPolicy    <string>用來定義鏡像拉去的政策,預設是always,即系統每次都會去抓取鏡像,但需要特變注意的是當我們的鏡像标簽為latest的時候,按照正常時肯定會抓取以确定是latest,有時候我們可能不想讓系統去抓取,可以選擇IfNotPresent。ports 主要用來選擇Pod 要暴露的端口。 args  用來指定向容器傳的參數。command  用來指定容器運作的程式,這裡的command可能更類似于dockerl裡面的ENTRYPOINT。附有一個dockerfile與kubernetes的對照說明。

[[email protected] ~]# kubectl explain pods.spec.containers

   image    <string>

     Docker image name. More info:

     https://kubernetes.io/docs/concepts/containers/images This field is

     optional to allow higher level config management to default or override

     container images in workload controllers like Deployments and StatefulSets.

   imagePullPolicy    <string>

     Image pull policy. One of Always, Never, IfNotPresent. Defaults to Always

     if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated.

     More info:

     https://kubernetes.io/docs/concepts/containers/images#updating-images

   ports    <[]Object>

     List of ports to expose from the container. Exposing a port here gives the

     system additional information about the network connections a container

     uses, but is primarily informational. Not specifying a port here DOES NOT

     prevent that port from being exposed. Any port which is listening on the

     default "0.0.0.0" address inside a container will be accessible from the

     network. Cannot be updated.

   args    <[]string>

     Arguments to the entrypoint. The docker image's CMD is used if this is not

     provided. Variable references $(VAR_NAME) are expanded using the

     container's environment. If a variable cannot be resolved, the reference in

     the input string will be unchanged. The $(VAR_NAME) syntax can be escaped

     with a double $$, ie: $$(VAR_NAME). Escaped references will never be

     expanded, regardless of whether the variable exists or not. Cannot be

     updated. More info:

     https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell

   command    <[]string>

     Entrypoint array. Not executed within a shell. The docker image's

     ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME)

     are expanded using the container's environment. If a variable cannot be

     resolved, the reference in the input string will be unchanged. The

     $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME).

     Escaped references will never be expanded, regardless of whether the

     variable exists or not. Cannot be updated. More info:

     https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell

KUBERNETES-1-4-控制器應用一

2.我們可以通過kubectl get pods --show-labels來檢視pod的标簽資訊。kubectl get pods -l 可以檢視含有标簽值含有某字段的相關資訊。kubectl get pods -L可以檢視含有标簽名含有某字段的相關資訊。

[[email protected] ~]# kubectl get pods

NAME                          READY     STATUS      RESTARTS   AGE

client                        0/1       Completed   0          2d

myapp-848b5b879b-7h254        1/1       Running     2          2d

myapp-848b5b879b-d7rjs        1/1       Running     2          2d

myapp-848b5b879b-wv5cz        1/1       Running     2          2d

nginx-deploy-5b595999-tj8ms   1/1       Running     2          2d

[[email protected] ~]# cd manifests/

[[email protected] manifests]# vim pod-demo.yaml 

[[email protected] manifests]# cat pod-demo.yaml

apiVersion: v1

kind: Pod

metadata:

  name: pod-demo

  namespace: default

  labels:

    app: myapp

    tier: frontend

spec:

  containers:

  - name: myapp

    image: ikubernetes/myapp:v1

    ports:

    - name: http

      containerPort: 80

    - name: https

      containerPort: 443

  - name: busybox

    image: busybox:latest

    imagePullPolicy: IfNotPresent

    command: 

    - "/bin/sh"

    - "-c"

    - "sleep 3600"

[[email protected] manifests]# kubectl create -f pod-demo.yaml 

pod/pod-demo created

[[email protected] manifests]# kubectl get pods --show-labels

NAME                          READY     STATUS      RESTARTS   AGE       LABELS

client                        0/1       Completed   0          2d        run=client

myapp-848b5b879b-7h254        1/1       Running     2          2d        pod-template-hash=4046164356,run=myapp

myapp-848b5b879b-d7rjs        1/1       Running     2          2d        pod-template-hash=4046164356,run=myapp

myapp-848b5b879b-wv5cz        1/1       Running     2          2d        pod-template-hash=4046164356,run=myapp

nginx-deploy-5b595999-tj8ms   1/1       Running     2          2d        pod-template-hash=16151555,run=nginx-deploy

pod-demo                      2/2       Running     0          51s       app=myapp,tier=frontend

[[email protected] manifests]# kubectl get pods -l app --show-labels

NAME       READY     STATUS    RESTARTS   AGE       LABELS

pod-demo   2/2       Running   0          4m        app=myapp,tier=frontend

[[email protected] manifests]# kubectl get pods -L app,run --show-labels

NAME                          READY     STATUS      RESTARTS   AGE       APP       RUN            LABELS

client                        0/1       Completed   0          2d                  client         run=client

myapp-848b5b879b-7h254        1/1       Running     2          2d                  myapp          pod-template-hash=4046164356,run=myapp

myapp-848b5b879b-d7rjs        1/1       Running     2          2d                  myapp          pod-template-hash=4046164356,run=myapp

myapp-848b5b879b-wv5cz        1/1       Running     2          2d                  myapp          pod-template-hash=4046164356,run=myapp

nginx-deploy-5b595999-tj8ms   1/1       Running     2          2d                  nginx-deploy   pod-template-hash=16151555,run=nginx-deploy

pod-demo                      2/2       Running     0          4m        myapp                    app=myapp,tier=frontend

3.kubectl label pods可以為pod增加标簽值。對同一個标簽再次指派時會報錯,需要使用--overwrite參數。

[[email protected] manifests]# kubectl label pods pod-demo release=canary

pod/pod-demo labeled

[[email protected] manifests]# kubectl get pods -l app --show-labels

NAME       READY     STATUS    RESTARTS   AGE       LABELS

pod-demo   2/2       Running   0          6m        app=myapp,release=canary,tier=frontend

[[email protected] manifests]# kubectl label pods pod-demo release=stable

error: 'release' already has a value (canary), and --overwrite is false

[[email protected] manifests]# kubectl label pods pod-demo release=stable --overwrite

pod/pod-demo labeled

[[email protected] manifests]# kubectl get pods -l app --show-labels

NAME       READY     STATUS    RESTARTS   AGE       LABELS

pod-demo   2/2       Running   0          11m       app=myapp,release=stable,tier=frontend

4.标簽選擇器中等值關系選擇器的使用。kubectl get pods -l 後面附加等值篩選。或者後面加不等值篩選也可以實作。

[[email protected] manifests]# kubectl get pods -l release=stable --show-labels

NAME       READY     STATUS    RESTARTS   AGE       LABELS

pod-demo   2/2       Running   0          15m       app=myapp,release=stable,tier=frontend

[[email protected] manifests]# kubectl get pods -l release!=stable --show-labels

NAME                          READY     STATUS      RESTARTS   AGE       LABELS

client                        0/1       Completed   0          2d        run=client

myapp-848b5b879b-7h254        1/1       Running     2          2d        pod-template-hash=4046164356,run=myapp

myapp-848b5b879b-d7rjs        1/1       Running     2          2d        pod-template-hash=4046164356,run=myapp

myapp-848b5b879b-wv5cz        1/1       Running     2          2d        pod-template-hash=4046164356,run=myapp

nginx-deploy-5b595999-tj8ms   1/1       Running     2          2d        pod-template-hash=16151555,run=nginx-deploy

5.标簽選擇器中集合關系選擇器的使用。kubectl get pods -l後面通過in來篩選标簽屬于某一集合。或者通過notin來篩選标簽屬于某一集合。對于字元串中間可能存在的空格,可以使用雙引号。

[[email protected] manifests]# kubectl get pods -l "release in (stable,alpha,beta)"

NAME       READY     STATUS    RESTARTS   AGE

pod-demo   2/2       Running   0          25m

[[email protected] manifests]# kubectl get pods -l "release notin (stable,alpha,beta)"

NAME                          READY     STATUS      RESTARTS   AGE

client                        0/1       Completed   0          2d

myapp-848b5b879b-7h254        1/1       Running     2          2d

myapp-848b5b879b-d7rjs        1/1       Running     2          2d

myapp-848b5b879b-wv5cz        1/1       Running     2          2d

nginx-deploy-5b595999-tj8ms   1/1       Running     2          2d

6.标簽選擇器除了可以給Pod打标簽,也可以用來給node等各種資源打标簽。

[[email protected] manifests]# kubectl get nodes

NAME                 STATUS    ROLES     AGE       VERSION

master.example.com   Ready     master    3d        v1.11.1

node1.example.com    Ready     <none>    3d        v1.11.1

node2.example.com    Ready     <none>    2d        v1.11.1

[[email protected] manifests]# kubectl get nodes --show-labels

NAME                 STATUS    ROLES     AGE       VERSION   LABELS

master.example.com   Ready     master    3d        v1.11.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master.example.com,node-role.kubernetes.io/master=

node1.example.com    Ready     <none>    3d        v1.11.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node1.example.com

node2.example.com    Ready     <none>    2d        v1.11.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node2.example.com

[[email protected] manifests]# kubectl label nodes node1.example.com disktype=ssd

node/node1.example.com labeled

[[email protected] manifests]# kubectl get nodes --show-labels

NAME                 STATUS    ROLES     AGE       VERSION   LABELS

master.example.com   Ready     master    3d        v1.11.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master.example.com,node-role.kubernetes.io/master=

node1.example.com    Ready     <none>    3d        v1.11.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=node1.example.com

node2.example.com    Ready     <none>    2d        v1.11.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node2.example.com

7.nodeSelector可以作為節點标簽選擇器,來對pod運作的節點進行篩選。例如在pod-demo.yaml中增加disktype: ssd的參數,則在建立資源的時候nodeSelector會自動篩選符合的節點。

[[email protected] manifests]# kubectl delete -f pod-demo.yaml 

pod "pod-demo" deleted

[[email protected] manifests]# vim pod-demo.yaml 

[[email protected] manifests]# cat pod-demo.yaml 

apiVersion: v1

kind: Pod

metadata:

  name: pod-demo

  namespace: default

  labels:

    app: myapp

    tier: frontend

spec:

  containers:

  - name: myapp

    image: ikubernetes/myapp:v1

    ports:

    - name: http

      containerPort: 80

    - name: https

      containerPort: 443

  - name: busybox

    image: busybox:latest

    imagePullPolicy: IfNotPresent

    command: 

    - "/bin/sh"

    - "-c"

    - "sleep 3600"

  nodeSelector:

    disktype: ssd

[[email protected] manifests]# kubectl create -f pod-demo.yaml

pod/pod-demo created

[[email protected] manifests]# kubectl describe pods pod-demo

Events:

  Type    Reason     Age   From                        Message

  ----    ------     ----  ----                        -------

  Normal  Scheduled  1m    default-scheduler           Successfully assigned default/pod-demo to node1.example.com

  Normal  Pulled     1m    kubelet, node1.example.com  Container image "ikubernetes/myapp:v1" already present on machine

  Normal  Created    1m    kubelet, node1.example.com  Created container

  Normal  Started    1m    kubelet, node1.example.com  Started container

  Normal  Pulled     1m    kubelet, node1.example.com  Container image "busybox:latest" already present on machine

  Normal  Created    1m    kubelet, node1.example.com  Created container

  Normal  Started    1m    kubelet, node1.example.com  Started container

8.資源注解。pod-demo.yaml腳本中通過annotations:來定義注解。kubectl describe pods檢視innotation。

[[email protected] manifests]# kubectl delete -f pod-demo.yaml 

pod "pod-demo" deleted

[[email protected] manifests]# vim pod-demo.yaml 

[[email protected] manifests]# cat pod-demo.yaml 

apiVersion: v1

kind: Pod

metadata:

  name: pod-demo

  namespace: default

  labels:

    app: myapp

    tier: frontend

  annotations:

    example.com/created-by: "cluster admin"

spec:

  containers:

  - name: myapp

    image: ikubernetes/myapp:v1

    ports:

    - name: http

      containerPort: 80

    - name: https

      containerPort: 443

  - name: busybox

    image: busybox:latest

    imagePullPolicy: IfNotPresent

    command: 

    - "/bin/sh"

    - "-c"

    - "sleep 3600"

  nodeSelector:

    disktype: ssd

[[email protected] manifests]# kubectl create -f pod-demo.yaml

pod/pod-demo created

[[email protected] manifests]# kubectl describe pods pod-demo

Name:               pod-demo

Namespace:          default

Priority:           0

PriorityClassName:  <none>

Node:               node1.example.com/172.20.0.129

Start Time:         Mon, 10 Dec 2018 00:48:43 -0500

Labels:             app=myapp

                    tier=frontend

Annotations:        example.com/created-by=cluster admin

繼續閱讀