定义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. imageDocker 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
修改镜像中的默认应用:为容器设置启动时要执行的命令及其入参
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字段必须为空列表。
不但可以为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 Ready13d 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 Ready13d 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 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不能用于挑选资源对象,仅用于为对象提供"元数据"