Detailed explanation of kubernetes-5 yaml file

Overview

The resource management and resource object arrangement and deployment in the k8s cluster can be solved through the life style (YAML) file, that is, the need to operate on the resource object can be edited into the YAML format file. We call this file the resource list file. The kubectl command directly uses the resource manifest file to arrange and deploy a large number of resource objects. Generally, when we develop, we deploy the cluster by configuring the yaml file.
YAML file: resource list file, used for resource arrangement

YAML file introduction

YAML overview

YAML: is a markup language. In order to emphasize that this language is data-centric rather than markup language.
YAML is a highly readable format used to express data sequences.

YAML basic syntax

Use spaces as indentation
. The number of indented spaces is not important, as long as the elements of the same level are aligned to the left.
Tab key is not allowed in lower version indentation. Only spaces are allowed.
Use # to mark comments, from this character to the end of the line. Will be ignored by the interpreter.
Use—indicating the start of a new yaml file

Data structure supported by YAML

Object

A collection of key-value pairs, also known as mapping/hash/dictionary
#Object type: A set of key-value pairs of an object, using a colon structure to represent
name: Ben
age: 18

#yaml also allows another way to write all key-value pairs as an inline object
hash: {name: Ben, age: 18}

Array

#Array type: A group of lines starting with a conjunction line to form an array
People
-Tom
-Jack

#Array can also use inline notation
People: [Tom, Jack]

YAML file components

Mainly divided into two parts, one is the definition of the controller, the other is the object to be controlled

Definition of controller

api version, controller type

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: default
spec:
  replicas: 3
  selector: 
    matchLables:
      app: nginx

Controlled object

Contains some images, versions, ports, etc.

  template:
    metadata:
      lables:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports: 
        - containerPort: 80

Property description

In the controller definition of the YAML file, there are many attribute names

Attribute name Introduction
apiVersion API version
kind Resource Type
metadata Resource metadata
spec Resource specifications (expected)
replicas Number of copies
selector Label selector
template Pod template
spec Pod expectations
containers Container arrangement

A quick way to write YAML files

Generally speaking, we rarely write YAML files by ourselves, because there are a lot of content involved, and we usually create them with the help of tools.

Use kubectl create command

This method is generally used when resources are not deployed, we can directly create a YAML configuration file

#尝试运行,并不会真正的创建镜像
kubectl create deployment web --image=nginx -o yaml --dry-run=client

Or it can be redirected to a file

kubectl create deployment web --image=nginx -o yaml --dry-run=client > web.yaml

Then you can modify it through this template

Use the kubectl get command to export the yaml file

Here you need a deployed image to export the configuration file

kubectl get deployment web -o yaml > web.yaml

Modify the YAML file you want

Thanks for browsing! ! !

Guess you like

Origin blog.csdn.net/qq_51574197/article/details/115192215