6、Kubernetes Pod控制器应用进阶

定义pod时,在spec字段中常用的定义字段有哪些?

master ~]# kubectl explain pods.spec.containers

 

KIND:     Pod
VERSION:  v1

RESOURCE: containers <[]Object>

DESCRIPTION:
     List of containers belonging to the pod. Containers cannot currently be
     added or removed. There must be at least one container in a Pod. Cannot be
     updated.

     A single application container that you want to run within a pod.

FIELDS:
   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

   env    <[]Object>
     List of environment variables to set in the container. Cannot be updated.

   envFrom    <[]Object>
     List of sources to populate environment variables in the container. The
     keys defined within a source must be a C_IDENTIFIER. All invalid keys will
     be reported as an event when the container is starting. When a key exists
     in multiple sources, the value associated with the last source will take
     precedence. Values defined by an Env with a duplicate key will take
     precedence. Cannot be updated.

   image    
     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    
     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

   lifecycle    
     Actions that the management system should take in response to container
     lifecycle events. Cannot be updated.

   livenessProbe    
     Periodic probe of container liveness. Container will be restarted if the
     probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

   name    <string> -required-
     Name of the container specified as a DNS_LABEL. Each container in a pod
     must have a unique name (DNS_LABEL). Cannot be updated.

   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.

   readinessProbe    
     Periodic probe of container service readiness. Container will be removed
     from service endpoints if the probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

   resources    
     Compute Resources required by this container. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/

   securityContext    
     Security options the pod should run with. More info:
     https://kubernetes.io/docs/concepts/policy/security-context/ More info:
     https://kubernetes.io/docs/tasks/configure-pod-container/security-context/

   stdin    
     Whether this container should allocate a buffer for stdin in the container
     runtime. If this is not set, reads from stdin in the container will always
     result in EOF. Default is false.

   stdinOnce    
     Whether the container runtime should close the stdin channel after it has
     been opened by a single attach. When stdin is true the stdin stream will
     remain open across multiple attach sessions. If stdinOnce is set to true,
     stdin is opened on container start, is empty until the first client
     attaches to stdin, and then remains open and accepts data until the client
     disconnects, at which time stdin is closed and remains closed until the
     container is restarted. If this flag is false, a container processes that
     reads from stdin will never receive an EOF. Default is false

   terminationMessagePath    <string>
     Optional: Path at which the file to which the container's termination
     message will be written is mounted into the container's filesystem. Message
     written is intended to be brief final status, such as an assertion failure
     message. Will be truncated by the node if greater than 4096 bytes. The
     total message length across all containers will be limited to 12kb.
     Defaults to /dev/termination-log. Cannot be updated.

   terminationMessagePolicy    <string>
     Indicate how the termination message should be populated. File will use the
     contents of terminationMessagePath to populate the container status message
     on both success and failure. FallbackToLogsOnError will use the last chunk
     of container log output if the termination message file is empty and the
     container exited with an error. The log output is limited to 2048 bytes or
     80 lines, whichever is smaller. Defaults to File. Cannot be updated.

   tty    
     Whether this container should allocate a TTY for itself, also requires
     'stdin' to be true. Default is false.

   volumeDevices    <[]Object>
     volumeDevices is the list of block devices to be used by the container.
     This is a beta feature.

   volumeMounts    <[]Object>
     Pod volumes to mount into the container's filesystem. Cannot be updated.

   workingDir    <string>
     Container's working directory. If not specified, the container runtime's
     default will be used, which might be configured in the container image.
     Cannot be updated. 
     
    

 

修改镜像中的默认应用:为容器设置启动时要执行的命令及其入参

https://kubernetes.io/zh/docs/tasks/inject-data-application/define-command-argument-container/

6、Kubernetes Pod控制器应用进阶_第1张图片

Docker 与 Kubernetes中对应的字段名称

6、Kubernetes Pod控制器应用进阶_第2张图片

6、Kubernetes Pod控制器应用进阶_第3张图片

标签

一个资源可以拥有多个标签,同一个标签可以被添加至多个资源对象;

标签可以在资源创建时指定,

标签:

key=value

key:字母、数字、_、-、.

value:可以为空,只能字母或数字开头及结尾,中间可使用字母、数字、_、-、.

 

查看标签

master manifests]# kubectl create -f pod-demo.yaml
pod/pod-demo created

master ~]# kubectl get pods --show-labels

NAME                           READY   STATUS             RESTARTS   AGE     LABELS
client                         0/1     Error              0          12d     run=client
client1                        0/1     Completed          0          11d     run=client1
client2                        0/1     Error              0          2d7h    run=client2
client3                        0/1     Error              0          2d6h    run=client3
myapp-5bc569c47d-5cdpw         1/1     Running            1          2d3h    pod-template-hash=5bc569c47d,run=myapp
myapp-5bc569c47d-c4gr2         1/1     Running            1          2d3h    pod-template-hash=5bc569c47d,run=myapp
myapp-5bc569c47d-njr5w         1/1     Running            1          2d3h    pod-template-hash=5bc569c47d,run=myapp
nginx-deploy-55d8d67cf-hlj9v   1/1     Running            4          12d     pod-template-hash=55d8d67cf,run=nginx-deploy
pod-demo                       1/2     ImagePullBackOff   0          8m23s   app=myapp,tier=frontend

# kubectl get pods -L app  //-L选项用于显示指定资源对象类别下的所有资源的对应的标签的值

# kubectl get pods -l app  //做标签过滤

NAME       READY   STATUS             RESTARTS   AGE
pod-demo   1/2     ImagePullBackOff   0          12m

# kubectl get pods -l app --show-labels

NAME       READY   STATUS             RESTARTS   AGE   LABELS
pod-demo   1/2     ImagePullBackOff   0          13m   app=myapp,tier=frontend

如何打标签

# kubectl label --help   //打标签的用法

Usage:
kubectl label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]

# kubectl label pods pod-demo release=canary  //打标签
pod/pod-demo labeled
# kubectl get pods -l app --show-labels
NAME       READY   STATUS    RESTARTS   AGE    LABELS
pod-demo   2/2     Running   2          165m   app=myapp,release=canary,tier=frontend
# kubectl label pods pod-demo release=stable  //强行打标会报错
error: 'release' already has a value (canary), and --overwrite is false
# kubectl label pods pod-demo release=stable --overwrite  //此处需要将替换的标签进行覆盖
pod/pod-demo labeled
# kubectl get pods -l app --show-labels
NAME       READY   STATUS    RESTARTS   AGE    LABELS
pod-demo   2/2     Running   2          167m   app=myapp,release=stable,tier=frontend

 

标签选择器:

等值关系:=,==,!=

集合关系:

KEY in(VALUE1,VALUE2,...)

KEY notin(VALUE1,VALUE2,...)

KEY  存在此键

!KEY 不存在此键

等值关系示例:
# kubectl get pods -l release=stable --show-labels
NAME       READY   STATUS    RESTARTS   AGE    LABELS
pod-demo   2/2     Running   2          172m   app=myapp,release=stable,tier=frontend
集合关系示例:
# kubectl get pods -l "release in (stable,beta,alpha)" --show-labels NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 2 3h5m app=myapp,release=stable,tier=frontend # kubectl get pods -l "release notin (stable,beta,alpha)" --show-labels NAME READY STATUS RESTARTS AGE LABELS client 0/1 Error 0 12d run=client client1 0/1 Completed 0 12d run=client1 client2 0/1 Error 0 2d10h run=client2 client3 0/1 Error 0 2d9h run=client3 myapp-5bc569c47d-5cdpw 1/1 Running 1 2d6h pod-template-hash=5bc569c47d,run=myapp myapp-5bc569c47d-c4gr2 1/1 Running 1 2d6h pod-template-hash=5bc569c47d,run=myapp myapp-5bc569c47d-njr5w 1/1 Running 1 2d6h pod-template-hash=5bc569c47d,run=myapp nginx-deploy-55d8d67cf-hlj9v 1/1 Running 4 12d pod-template-hash=55d8d67cf,run=nginx-deploy

 

许多资源支持内嵌字段定义其使用标签选择器

matchLabels:直接给定键值

matchExpressions:基于给定的表达式来定义使用的标签选择器,{key:"KEY",operator:"OPERATOR",values:[VAL1,VAL2,...]},表示意思是:将KEY和VAL1、VAL2通过操作符OPERATOR进行比较;

操作符:

In,Notin:其values字段必须为非空列表;

Exists,NotExists:其values字段必须为空列表。

不但可以为pod打标签,各种对象都可以打标签

# kubectl get nodes --show-labels
NAME     STATUS   ROLES    AGE   VERSION   LABELS
master   Ready    master   15d   v1.14.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master,kubernetes.io/os=linux,node-role.kubernetes.io/master=
node01   Ready       13d   v1.14.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux
node02   Ready       15d   v1.14.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node02,kubernetes.io/os=linux
node03   Ready       13d   v1.14.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node03,kubernetes.io/os=linux
# kubectl label nodes node01 disktype=ssd  //为node01节点打标签
node/node01 labeled
# kubectl get nodes --show-labels
NAME     STATUS   ROLES    AGE   VERSION   LABELS
master   Ready    master   15d   v1.14.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master,kubernetes.io/os=linux,node-role.kubernetes.io/master=
node01   Ready       13d   v1.14.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux
node02   Ready       15d   v1.14.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node02,kubernetes.io/os=linux
node03   Ready       13d   v1.14.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node03,kubernetes.io/os=linux

点带有标签的好处在于在添加资源时,可以让资源对带有某标签的节点具有倾向性

# kubectl explain pods.spec

 nodeSelector    映射值  //节点选择器,可以限定pod运行在哪个节点上
     NodeSelector is a selector which must be true for the pod to fit on a node.
     Selector which must match a node's labels for the pod to be scheduled on
     that node. More info:
     https://kubernetes.io/docs/concepts/configuration/assign-pod-node/

 

   nodeName      //节点名称
     NodeName is a request to schedule this pod onto a specific node. If it is
     non-empty, the scheduler simply schedules this pod onto that node, assuming
     that it fits resource requirements.

 

 

 

示例:

pod-demo运行在node02上,如果想让pod-demo运行在node01上,可以在yaml文件中添加标签选择器

# kubectl get pods -o wide
NAME                           READY   STATUS      RESTARTS   AGE     IP            NODE     NOMINATED NODE   READINESS GATES
...........
pod-demo                       2/2     Running     3          3h57m   10.244.2.10   node02              

master manifests]# vim pod-demo.yaml

  1 apiVersion: v1
  2 kind: Pod
  3 metadata:
  4   name: pod-demo
  5   namespace: default
  6   labels:
  7     app: myapp
  8     tier: frontend
  9 spec:
 10   containers:
 11   - name: myapp
 12     image: ikubernetes/myapp:v1
 13   - name: busybox
 14     image: busybox:latest
 15     command:
 16     - "/bin/sh"
 17     - "-c"
 18     - "sleep 3600"
 19   nodeSelector:   //此处要和上面的container对齐,因为这里不是容器的属性,而是pod的属性
 20     disktype: ssd

# kubectl create -f pod-demo.yaml   //重新创建pod

# kubectl describe pods pod-demo

Name:               pod-demo
Namespace:          default
Priority:           0
PriorityClassName:  
Node:               node01/192.168.184.142
........
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  2m1s               default-scheduler  Successfully assigned default/pod-demo to node01

 

annotations:资源也可以使用注解;

与label不同的地方在于,annotations不能用于挑选资源对象,仅用于为对象提供"元数据"

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/hanshanxiaoheshang/p/11032759.html

你可能感兴趣的:(6、Kubernetes Pod控制器应用进阶)