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

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

   livenessProbe    <Object>
     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    <Object>
     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    <Object>
     Compute Resources required by this container. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/

   securityContext    <Object>
     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    <boolean>
     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    <boolean>
     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    <boolean>
     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/

Docker 与 Kubernetes中对应的字段名称

标签

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

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

标签:

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字段必须为空列表。

猜你喜欢

转载自www.cnblogs.com/hanshanxiaoheshang/p/11032759.html