kubernetes pod资源清单之spec

spec目标状态

spec.containers
[root@k8s01 ~]# kubectl explain pod.spec.containers
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:
   image	
     Docker image name. More info: http://kubernetes.io/docs/user-guide/images

   readinessProbe	
     Periodic probe of container service readiness. Container will be removed
     from service endpoints if the probe fails. Cannot be updated.

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

   livenessProbe	
     Periodic probe of container liveness. Container will be restarted if the
     probe fails. Cannot be updated.

   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	
     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. Defaults to /dev/termination-log. Cannot be updated.

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

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

   resources	
     Compute Resources required by this container. Cannot be updated. 

   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.

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

   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. 

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

   imagePullPolicy	
     Image pull policy. One of Always, Never, IfNotPresent. Defaults to Always
     if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated.

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

   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. 

   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.

   securityContext	
     Security options the pod should run with. 
 
  
spec.containers常用参数
容器的名称
- name  
dorker镜像的名称和版本
  image 
拉取策略
  imagePullPolicy   
容器中公开的端口列表
  ports <[]Object>
入口点的数组
  command <[]string>
入口点的参数
  args <[]string>

kubernetes pod资源清单之spec_第1张图片
kubernetes pod资源清单之spec_第2张图片
如果command和args没有运行,则运行image中的entrtpoint和cmd
如果有command没有args,则只运行command,而image中的entrtpoint和cmd忽略
如果没有command有args,则使用image中entrtpoint,把args当参数传递给entrtpoint
如果command和args运行,则就是完全自定义,image全忽略

存活检测
  livenessProbe 
就绪检测
  readinessProbe 
 
  
生命周期事件的操作
  lifecycle 
 
  
spec.containers.ports常用参数
[root@k8s01 ~]# kubectl explain pod.spec.containers.ports

FIELDS:
   hostIP	
     What host IP to bind the external port to.

   hostPort	
     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	
     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	
     Protocol for port. Must be UDP or TCP. Defaults to "TCP".

   containerPort	 -required-
     Number of port to expose on the pod's IP address. This must be a valid port number, 0 < x < 65536.
spec.containers.探测
[root@k8s01 ~]# kubectl explain pod.spec.containers.livenessProbe(readinessProbe)

FIELDS:
   exec	 
     One and only one of the following should be specified. Exec specifies the action to take.
   httpGet	
     HTTPGet specifies the http request to perform.
   tcpSocket	
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet supported
   initialDelaySeconds	
     Number of seconds after the container has started before liveness probes are initiated.
 
  

例子1:

[root@k8s01 ]# cat liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-pod
  namespace: default
spec:
  containers:
  - name: liveness-exec-container
    image: 10.0.0.11:5000/nginx:1.13
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","touch /tmp/healthy; sleep 30; rm -f /tmp/healthy;sleep 3600"]
    livenessProbe:
      exec:
        command: ["test","-e","/tmp/healthy"]
      initialDelaySeconds: 1
      periodSeconds: 3

例子二:

[root@k8s01 ]# cat liveness-httpGet.yaml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
  - name: liveness-httpget-container
    image: 10.0.0.11:5000/nginx:1.13
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    livenessProbe:
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3

例子三:

[root@k8s01 ]# cat readiness-httpGet.yaml
apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-pod
  namespace: default
spec:
  containers:
  - name: readiness-httpget-container
    image: ikubernetes/myapp:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    readinessProbe:
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
spec.containers.lifecycle

定义容器启动后和终止前立即执行的动作(生命周期钩子函数)

[root@k8s01 ~]# kubectl explain pods.spec.containers.lifecycle
RESOURCE: lifecycle 

FIELDS:
   preStop	
     PreStop is called immediately before a container is terminated. The
     container is terminated after the handler completes. The reason for
     termination is passed to the handler. Regardless of the outcome of the
     handler, the container is eventually terminated. Other management of the
     container blocks until the hook completes. 
     
   postStart	
     PostStart is called immediately after a container is created. If the
     handler fails, the container is terminated and restarted according to its
     restart policy. Other management of the container blocks until the hook
     completes.

 
  

例子:

[root@k8s01 ]# cat poststart-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: poststart-pod
  namespace: default
spec:
  containers:
  - name: busybox-httpd
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh/","-c","echo Home_Page > /tmp/index.html"]
    command: ["/bin/sh","-c","sleep 3600"]

你可能感兴趣的:(kubernetes)