1. Overview of YAML files

The management of resources and the deployment of resource objects in k8S cluster can be solved by declarative style (YAML) files, that is, the operation on resource objects can be edited into YAML format files, which are called resource manifest files. A large number of resource objects can be choreographed and deployed using the kubectl command directly from the resource manifest file.

2. Format of the YAML file

2.1 YAML is introduced

YAML is still a markup language. To emphasize the language’s data-centric focus, rather than its markup language focus. YAML is a readable format for expressing data sequences.

2.2 YAML Basic Syntax

  • Use Spaces for indentation
  • The number of Spaces indented does not matter, as long as elements of the same rank are aligned to the left
  • In earlier versions, the Tab key is not allowed for indentation. Only Spaces are allowed
  • Use # to identify comments that are ignored by the interpreter from this character to the end of the line

2.3 Data structures supported by YAML

  • object

    A collection of key-value pairs, also known as mapping/hashes/dictionary

    Object type: A set of key-value pairs of an object, represented by a colon structure
    name: To the north
    age: 18
    gender: male
    
    # Yaml also allows you to write all key-value pairs as an inline object
    hash: {name: To the south.age: 18.gender: female}
    Copy the code
  • Array:

A sequence of values, also called a sequence/list

Array type: a set of lines at the beginning of a conjunction line, forming an array
people
- Tom
- Jack

Arrays can also be expressed in inline notation
people: [Tom.Jack]
Copy the code
  • Scalar (scalars)

A single, non-divisible value

# Values are expressed directly as literals
number: 10.24

Boolean values are true and false
isAnble: true

# null is represented by ~
length: ~

The time is in ISO8601 format
iso8601: The 2020-12-26 t15: o. 10 to 05:00

# The date is represented by year, month and day in composite ISO8601 format
date: 2020-12-26
Copy the code

3. Resource list describes methods

(1) In K8S, it is common to use YAML files to create pods that meet our expectations. Such YAML files are called resource lists.

(2) Commonly used fields

Attributes that must exist

Parameter names The field type instructions
version String The version of the K8S API can be queried by running the kubectl api-sersion command
kind String This specifies the type of K8S resource, such as Pod
matedata Object Metadata object, fixed write value mateData
matedata-name String The name of the metadata object, such as the name that names pod
matedata-namespace String Namespace of the metadata object
Spec Object Define objects in detail and write specs for fixed values
spec.container[] list Container list definition of a spec object
spec.container[].name String The name of the container object
spec.container[].image String Define the name of the object to use

Spec Main object

spec.containers

Parameter names The field type instructions
spec.containers[].name String Define the name of the container
spec.containers[].image String Define the image name to use
spec.containers[].imagePullPolicy String Define a mirror pull policy. The options are Always, Never, and IfNotPresent. (1) Always: indicates that the mirror is pulled every time. If there is a local mirror, use the local mirror. If there is no online mirror, pull the online mirror. If none of the above values is set, the default is Always.
spec.containers[].command[] List Specify the container startup command, because arrays can be multiple, otherwise use the startup command used for image packaging.
spec.containers[].args[] List Specify container start command arguments, because arrays can be multiple.
spec.containers[].workingDir String Specifies the working directory for the container
spec.containers[].volumeMounts[] List Specifies the volume configuration inside the container
spec.containers[].volumeMounts[].name String Specifies the name of the storage volume that can be mounted by the container
spec.containers[].volumeMounts[].mountPath String Specifies the path of the storage volume that can be mounted by the container
spec.containers[].volumeMounts[].readOnly String Set the read/write mode (true or false) of the storage volume path. The default mode is read/write
spec.containers[].ports[] List Specifies the list of ports the container needs to use
spec.containers[].ports[].name String Specify port name
spec.containers[].ports[].containerPort String Specifies the port number that the container needs to listen on
spec.containers[].ports[].hostPort String Specifies the port number of the container host to listen on. The default port number is the same as that of containerPort. Note that the same host cannot start the same copy of the container.
spec.containers[].ports[].protocol String The port protocol can be TCP or UDP. The default value is TCP
spec.containers[].env[] List Specifies a list of environment variables to set before the container can run
spec.containers[].env[].name String Specify the environment variable name
spec.containers[].env[].value String Specify environment variable values
spec.containers[].resources Object Specify resource limits and resource request values (starting with setting the container’s resource limit)
spec.containers[].resources.limits Object Specifies the upper limit for the container’s runtime resources to run
spec.containers[].resources.limits.cpu String Specifies the CPU limit, in cores, to be used for the docker run –cpu-shares parameter.
spec.containers[].resources.limits.memory String Specifies the MEM memory limit. The unit is MIB or GiB
spec.containers[].resources.requests Object Specifies the limit Settings for container startup and scheduling
spec.containers[].resources.requests.cpu String CPU request, in cores, initialized available when the container is started
spec.containers[].resources.requests.memory String Memory request. The unit is MIB or GiB. The number of initializations available for container startup

spec.volumes

Parameter names The field type instructions
spec.volumes[].name String The name of the shared storage volume that defines Pod, spec.containers[].volumemounts [].name of the container definition section is the same as here.
spec.volumes[].emptyDir Object Specifies a temporary directory for Pod with an empty object: emptyDir:{}
spec.volumes[].hostPath Object Specifies the directory on which the Pod is to be mounted
spec.volumes[].hostPath.path String Specifies the host directory where the Pod is located, which will be used to mount the container
spec.volumes[].secret Object Specify the type of storage volume secret, secret means private, easy to understand, it stores some sensitive security files such as passwords, tokens or secret keys. Mount the cluster’s predefined Secret object inside the container.
spec.volumes[].configMap Object If you specify a configMap volume, you can mount predefined configMap objects of the cluster to the container.
spec.volumes[].livenessProbe Object Specifies the Settings for container health check in Pod so that the system will automatically restart the container when the probe is unresponsive several times. This can be set to exec, httpGet, and tcpSocket.
spec.volumes[].livenessProbe.exec Object Specify the Settings for container health check in Pod, make sure it is exec mode
spec.volumes[].livenessProbe.exec.command[] String After specifying the exec mode, you need to specify the command or script
spec.volumes[].livenessProbe.httpGet Object Specify Pod container health check Settings for httpGet
spec.volumes[].livenessProbe.tcpSocket Object Specify Pod container health check Settings to ensure tcpSocket mode
spec.volumes[].livenessProbe.initialDelaySeconds Number Set the manual detection time after the container is started, in seconds
spec.volumes[].livenessProbe.timeoutSeconds Number Timeout set for probe wait response for container health check, in seconds, default 1s. If the timeout period exceeds this value, the container is considered unhealthy and restarts.
spec.volumes[].livenessProbe.periodSeconds Number Set the periodic detection time for container health check (unit: S). The default detection time is 10 seconds.

Additional parameters

Parameter names The field type instructions
spec.restartPolicy String Define the Pod restart policy. The options are Always and OnFailure. The default value is Always. 1.Always: Once the Pod is terminated, kubelet service will restart the container regardless of how it was terminated. 2.OnFailure: Kubelet will restart the container only if the Pod terminates with a non-zero exit code. If the container ends normally (with an exit code of 0), Kubelet will not restart it. 3. Never: After Pod terminates, Kubelet reports the exit code to the Master and does not restart the Pod.
spec.nodeSelector Object Defines the Label of a Node. The Label is specified in key:value format
spec.imagePullSecrets Object Use the secret name when defining the pull image, specified in the name:secretkey format
spec.hostNetwork Boolean Defines whether to use host network mode. The default value is false. Set true to use the host network, not the Docker bridge, and you will not be able to start a second replica on the same host.

A simple example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginxdeployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    labels:
      app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

Copy the code

Create a Deployment resource from the above YAML file

View Deployment details

Check the podViewing POD Details