In k8s, yaml files are generally used to create pod s that meet our expectations
Called resource list
Brief description
Is a highly readable format used to express data sequences. YAML actually means: it is still a markup language, but in order to emphasize this language, count
Data as the center, rather than focusing on markup language
Basic grammar
The Tab key is not allowed when indenting, only spaces are allowed
The number of indented spaces is not important, as long as the elements of the same level are aligned to the left
#The identifier comment, from this character to the end of the line, will be ignored by the interpreter
Data structures supported by YAML
Object: a collection of key value pairs, also known as mapping / hashes / dictionary
Array: a set of values arranged in order, also known as sequence / list
scalars: single, non separable value
Object type: a set of key value pairs of an object, represented by a colon structure
name: Steve age: 18
Yaml also allows another way to write all key value pairs as an inline object
hash: { name: Steve, age: 18 }
Array type: a group of lines beginning with a conjunction line to form an array
animal - Cat - Dog
Arrays can also use inline representation
animal: [Cat, Dog]
Composite structure: objects and arrays can be used together to form a composite structure
anguages: - Ruby - Perl - Python websites: YAML: yaml.org Ruby: ruby-lang.org Python: python.org Perl: use.perl.org
Scalar: scalar is the most basic and non separable value. The following data types are scalar
1 string Boolean integer floating point number Null
2 time and date
Values are expressed directly in literal form
number: 12.30
Boolean values are represented by true and false
isSet: true
null is represented by ~
parent: ~
The time is in ISO8601 format
iso8601: 2001-12-14t21:59:43.10-05:00
The date is represented by year, month and day in compound iso8601 format
date: 1976-07-31
YAML allows the use of two exclamation marks to cast data types
!!str 123 !!str true
character string
Strings are not quoted by default
str: This is a line of string
If the string contains spaces or special characters, you need to put them in quotation marks
str: 'Content: String'
Single quotation marks and double quotation marks can be used, and double quotation marks will not escape special characters
s1: 'content\n character string' s2: "content\n character string"
If there are still single quotation marks in single quotation marks, two single quotation marks must be used continuously to escape
str: 'labor''s day'
The string can be written in multiple lines. Starting from the second line, there must be a single space indentation. Line breaks are converted to spaces
str: This is a paragraph Multiline character string
For multiline strings, you can use | to preserve line breaks or > to collapse line breaks
this: | Foo Bar that: > Foo Bar
- Means to keep the line break at the end of the text block, - means to delete the line break at the end of the string
s1: | Foo s2: |+ Foo s3: |- Foo
Explanation of common fields in resource list
View detailed resource help information
[root@k8s-master01 ~]# kubectl explain pod [root@k8s-master01 ~]# kubectl explain pod.apiVersion KIND: Pod VERSION: v1 FIELD: apiVersion <string> DESCRIPTION: APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
Resource list format
apiVersion: group/apiversion # If no group name is given, the default is core. You can use kubectl api- versions # Get the version information of all apiversions on the current k8s Version (each version may be different) kind: #Resource category metadata: #Resource metadata name namespace lables #label annotations # The main purpose is to facilitate users to read and search spec: # Desired state status: # In the current status, this field is maintained by Kubernetes, and cannot be defined by users
Write a pod yaml
apiVersion: v1 kind: Pod metadata: name: nginx labels: app: nginx version: v1 spec: containers: - name: nginx image: at.harbor.com/library/nginx:v1
[root@k8s-master01 ~]# kubectl apply -f pod.yaml pod/nginx created
view log
kubectl log pod name - c container name
[root@k8s-master01 ~]# kubectl log nginx -c nginx log is DEPRECATED and will be removed in a future version. Use logs instead. /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Configuration complete; ready for start up
Start the error report. You can use this command to view the information of the container
[root@k8s-master01 ~]# kubectl describe pod nginx
Delete pod
[root@k8s-master01 ~]# kubectl delete pod nginx
New pod
[root@k8s-master01 ~]# kubectl create -f pod.yaml pod/nginx created
Common commands for resource lists
Get apivision version information
[root@k8s-master01 ~]# kubectl api-versions
Get apiVersion version information of the resource
[root@k8s-master01 ~]# kubectl explain pod KIND: Pod VERSION: v1
Get field settings help document
[root@k8s-master01 ~]# kubectl explain pod KIND: Pod VERSION: v1
Field configuration format
#Represents the string type
apiVersion <string>
#Indicates that multiple fields need to be nested
metadata <Object>
#Represents a mapping consisting of k:v
labels <map[string]string>
#Represents a list of strings
finalizers <[]string>
#Represents a list of objects
ownerReferences <[]Object>
#Boolean type
hostPID <boolean>
#Integer
priority <integer>
#If the type is followed by - required -, it is a required field
name <string> -required-
Create Pod by defining manifest file
apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: default labels: app: myapp spec: containers: - name: myapp-1 image: hub.atguigu.com/library/myapp:v1 - name: busybox-1 image: busybox:latest command: - "/bin/sh" - "-c" - "sleep 3600"
kubectl get pod xx.xx.xx -o yaml <!--use -o Parameter plus yaml,You can configure resources to yaml It can also be used json,Output as json grid