k8spod Introduction

Introduction yaml

apiVersion: v1 APIserver version 
kind: Pod resource type 
metadata: metadata definition 
        name: pod-demo metadata resource name 
        labels: define the label 
             app: myapp two tabs 
             Tier: frontend 
spec: defines the desired state of the vessel 
        containers: pod defined 
        - name : myapp pod name 
          image: ikubernetes / myapp: versions and path v1 mirror 
        - name: busyboxa pod name 
          image: busybox: latest image name 
          imagePullPolicy: IfNotPresent # obtain a mirror image of 
          ports: port exposure defined 
          - name: http pod container name 
          - containerPort: 80 container ports 
          command: modify the default run command 
          - "/ bin / sh" 
          - "-c"
          -  "sleep 36000"

  pod custom help

[root@master manifests]# kubectl explain pods.spec.container
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, at The value Associated with at The Last Source by Will the Take 
     precedence Values defined by AN Env with A Duplicate Key by Will the Take. 
     precedence of Can not BE Updated.. 

   Image <String> Mirror warehouse mirror 
     Docker Image name More info:. 
     HTTPS: / This Field iS /kubernetes.io/docs/concepts/containers/images 
     optional to the allow iN AREAS oF COMMUNICAITIONS Level Management config to the override or default 
     Container Images in the Deployments Workload Controllers and StatefulSets like. 

   imagePullPolicy <String> tag acquired mirror image is the latest method of acquiring the default always means always a way to download, Never have local representation to use, no local nor to the warehouse download, IfNotPresent have to use local local local warehouse does not go download 
     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.

  pod port operator exposure; Shengming information

[root@master manifests]# kubectl explain pods.spec.containers.ports
KIND:     Pod
VERSION:  v1

RESOURCE: ports <[]Object>

DESCRIPTION:
     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.

     ContainerPort represents a network port in a single container.

FIELDS:
   containerPort	<integer> -required-  pod容器端口
     Number of port to expose on the pod's IP address. This must be a valid port
     number, 0 < x < 65536.

   hostIP	<string>  节点IP,建议写成0.0.0.0
     What host IP to bind the external port to.

   hostPort	<integer>  节点上的那个端口
     Number of port to expose on the host. If specified, this must be a valid
     port number, 0 < x < 65536. If HostNetwork is specified, this must match
     ContainerPort. Most containers do not need this.

   name	<string>   端口名称,server中可以引用名称
     If specified, this must be an IANA_SVC_NAME and unique within the pod. Each
     named port in a pod must have a unique name. Name for the port that can be
     referred to by services.

   protocol	<string>  协议默认TCP
     Protocol for port. Must be UDP, TCP, or SCTP. Defaults to "TCP".

  Modify the default pod were ordered to run

[root@master manifests]# 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>#作为参数传递给comment,如果没给就把容器镜像制作时的CMD指定的做为参数传递给comment
     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 the If A variable CAN not BE resolved, at The Reference in. 
     at The the INPUT String by Will BE Unchanged at The $ (VAR_NAME) syntax CAN BE ESCAPED. 
     with A Double $$, IE:. $$ (VAR_NAME) Escaped the References by Will Never BE 
     Expanded, . The variable regardless of Whether or Not EXISTS Can Not BE 
     . Updated More info: 
     https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in shell--a 

   the command <[] String> # default command here does not run shll in, if you want to run in shll must specify; if not specified, then it runs the specified time docker mirroring EntryPoint 
     Entrypoint Array not. the WITHIN A shell the Executed. Image at The Docker's 
     EntryPoint Used IS IF the this IS not Provided. Variable the 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

 The official document reference address: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/ 

Description Docker field name Kubernetes field name
The command run by the container Entrypoint command
The arguments passed to the command Cmd args

 

 

 

When you override the default Entrypoint and Cmd, these rules apply:

  • If you do not provide Container command or argsuse the Container, the default values defined in Docker mirror is used.

  • If the offer Containercommand but not argsprovided, the only offer  commandof. Cmd default default EntryPoint and Docker mirroring defined will be ignored.

  • If only argsContainer provided, the default entry point Docker mirroring defined and argsprovide you with the run together .

  • If provided command, and argsdefault Cmd and the default entry point defined Docker image are ignored. Youcommand and run with you args

The image input point Cmd image Container command Container args Run command
[/ep-1] [foo bar] <Not set> <Not set> [ep-1 foo bar]
[/ep-1] [foo bar] [/ep-2] <Not set> [ep-2]
[/ep-1] [foo bar] <Not set> [zoo boo] [ep-1 zoo boo]
[/ep-1] [foo bar] [/ep-2] [zoo boo] [ep-2 zoo boo]

 

Help define the metadata tags

[root@master manifests]# kubectl explain pods.metadata.labels
KIND:     Pod
VERSION:  v1

FIELD:    labels <map[string]string>

DESCRIPTION:
     Map of string keys and values that can be used to organize and categorize
     (scope and select) objects. May match selectors of replication controllers
     and services. More info: http://kubernetes.io/docs/user-guide/labels

  example

apiVersion: v1
kind: Pod
metadata:
  name: label-demo
  labels:
    environment: production
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9
    ports:
    - containerPort: 80
    

  Delete created using the pod yaml

[root@master manifests]# kubectl delete -f pod.demo.yaml 
pod "pod-demo" deleted

  View

[root@master manifests]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
myapp-84cd4b7f95-g6ldp   1/1     Running   3          9d
nginx-5896f46c8-zblcs    1/1     Running   3          9d

   Creation

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

  Check the label pods

[root@master manifests]# kubectl get pods --show-labels
NAME                     READY   STATUS    RESTARTS   AGE     LABELS
myapp-84cd4b7f95-g6ldp   1/1     Running   3          9d      pod-template-hash=84cd4b7f95,run=myapp
nginx-5896f46c8-zblcs    1/1     Running   3          9d      pod-template-hash=5896f46c8,run=nginx
pod-demo                 2/2     Running   0          3m58s   app=myapp,tier=frontend

    See filtered label pods, pod for display tag values ​​app

[root@master manifests]# kubectl get pods -L app
NAME                     READY   STATUS    RESTARTS   AGE     APP
myapp-84cd4b7f95-g6ldp   1/1     Running   3          9d      
nginx-5896f46c8-zblcs    1/1     Running   3          9d      
pod-demo                 2/2     Running   0          6m23s   myapp

  Check the filter specified tag pods 

[root@master manifests]# kubectl get pods -l app
dNAME       READY   STATUS    RESTARTS   AGE
pod-demo   2/2     Running   0          9m30s

  Check carefully the type of

[root@master manifests]# kubectl get pods -l app --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
pod-demo   2/2     Running   0          11m   app=myapp,tier=frontend

  Added Resources tab

[root@master manifests]# kubectl label pods pod-demo chenxi=cx
pod/pod-demo labeled
[root@master manifests]# kubectl get pods -l chenxi,app --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
pod-demo   2/2     Running   0          14m   app=myapp,chenxi=cx,tier=frontend

  Modify the resource label

[root@master manifests]# kubectl label pods pod-demo chenxi=hgf --overwrite
pod/pod-demo labeled
[root@master manifests]# kubectl get pods -l chenxi,app --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
pod-demo   2/2     Running   0          17m   app=myapp,chenxi=hgf,tier=frontend

  Use tag selector; equivalence classes using

[root @ master manifests] # kubectl get pods -l app = myapp --show-labels # denotes a display tag is equal to app MyApp   
NAME LABELS of AGE RESTARTS the STATUS READY 
POD-Demo] 2/2 0 20m Running app = MyApp, Chenxi = HGF, = frontend Tier 
[Master manifests the root @] -l PODS GET # kubectl app = myapp, Chenxi! = cx --show-display app labels and label equal myapp Chenxi tag value is not equal to cx POD 
NAME lABELS of AGE RESTARTS the STATUS READY 
POD- demo 2/2 Running 0 20m app = myapp , chenxi = hgf, tier = frontend

  Selecting a set of relations, pods cx is equal to or hgf

[root@master manifests]# kubectl label pods nginx-5896f46c8-zblcs chenxi=hgf 
pod/nginx-5896f46c8-zblcs labeled
[root@master manifests]# kubectl get pods -l "chenxi,app"
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   2/2     Running   0          27m
[root@master manifests]# kubectl label pods nginx-5896f46c8-zblcs chenxi=cx --overwrite
pod/nginx-5896f46c8-zblcs labeled
[root@master manifests]# kubectl get pods -l "chenxi in (cx,hgf)"
NAME                    READY   STATUS    RESTARTS   AGE
nginx-5896f46c8-zblcs   1/1     Running   3          9d
pod-demo                2/2     Running   0          29m

  Value is not equal to or hgf of pods cx

[root@master manifests]# kubectl get pods -l "chenxi notin (cx,hgf)"
NAME                     READY   STATUS    RESTARTS   AGE
myapp-84cd4b7f95-g6ldp   1/1     Running   3          9d

  Many resources to support embedded field definitions it uses the tag selector

matchLabels: direct setpoint

matchExpressions: expression to define a given tag selector based on {key: "KEY", operator: "Expression", values: [VAL1, VAL2, ...]}

    Expression operator: In, NotIN: the value of the field values ​​must be non-empty list; Exists, NotExist: the value of the field values ​​must be empty list

 View node label

[root@master manifests]# kubectl get nodes --show-labels
NAME     STATUS   ROLES    AGE   VERSION   LABELS
master   Ready    master   9d    v1.15.1   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    <none>   9d    v1.15.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux
node02   Ready    <none>   9d    v1.15.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node02,kubernetes.io/os=linux

  To play tag node

[root@master manifests]# kubectl label node node01 chenxi=cx 
node/node01 labeled
[root@master manifests]# kubectl get nodes -l chenxi --show-labels
NAME     STATUS   ROLES    AGE   VERSION   LABELS
node01   Ready    <none>   9d    v1.15.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,chenxi=cx,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux

  Modify the node label

[root@master manifests]# kubectl label node node01 chenxi=gfd --overwrite
node/node01 labeled
[root@master manifests]# kubectl get nodes -l chenxi=gfd  --show-labels
NAME     STATUS   ROLES    AGE   VERSION   LABELS
node01   Ready    <none>   9d    v1.15.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,chenxi=gfd,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux

  Node tag selector, so that pods runs only on the kind of node

  nodeSelector	<map[string]string>
     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/

  Use node label selector yaml

[Master manifests the root @] # Vim pod.demo.yaml 

apiVersion: V1 
kind: Pod 
Metadata: 
        name: POD-Demo 
        Labels: 
             App: MyApp 
             Tier: frontend 
spec: 
        Containers: 
        - name: MyApp 
          Image: ikubernetes / MyApp: V1 
          the ports : 
           - containerPort: 80 
        - name: busyboxa 
          Image: busybox: Latest 
          imagePullPolicy: IfNotPresent 
          Command: 
          - "/ bin / SH" 
          - "-C" 
          - "SLEEP 36000" 
        nodeSelector: 
          Chenxi: DF runs and has a value equal to the tag gfd Chenxi the node

  View pod node running

[root@master manifests]# kubectl get pods pod-demo -o wide
NAME       READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
pod-demo   2/2     Running   0          60m   10.244.1.21   node01   <none>           <none>

  Adding a node label on node02

[root@master manifests]# kubectl label node node02 chenxi=df 
node/node02 labeled
[root@master manifests]# kubectl get nodes -l chenxi=df  --show-labels
NAME     STATUS   ROLES    AGE   VERSION   LABELS
node02   Ready    <none>   9d    v1.15.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,chenxi=df,kubernetes.io/arch=amd64,kubernetes.io/hostname=node02,kubernetes.io/os=linux

  Re-create the deleted pod

[root@master manifests]# kubectl create -f pod.demo.yaml
pod/pod-demo created
[root@master manifests]# kubectl get pods pod-demo -o wide
NAME       READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
pod-demo   2/2     Running   0          6s    10.244.2.13   node02   <none>           <none>
[root@master manifests]# kubectl get pods pod-demo -o wide
NAME       READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
pod-demo   2/2     Running   0          7s    10.244.2.13   node02   <none>           <none>
[root@master manifests]# 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/rdchenxi/p/11297156.html