普拉多VX

人生一路,不问来时,不知归期

0%

Kubernetes Pod YAML模版文件格式

YAML

YAML 是专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便。它实质上是一种通用的数据串行化格式。

支持的三种数据结构:

  • 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
  • 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
  • 纯量(scalars):单个的、不可再分的值

YAML规则

  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进时不允许使用Tab键,只允许使用空格。
  • 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可

数据结构

对象

key:value的键值对,使用冒号表示

1
2
username: roddy
password: "ccccccc"

转成json

1
2
3
4
5

{
"username": "roddy",
"password": "ccccccc"
}

数组

以横线“-”开头的行

1
2
3
- Python
- Golang
- JAVA

转成json

1
2
3
4
5
[
"Python",
"Golang",
"JAVA"
]

复合结构

1
2
3
4
5
6
7
8
9
username: roddy
password: "passwd"
server_ip:
- 192.168.1.1
- 192.168.2.2
- 192.168.2.3
server_type:
web: "nginx"
db: "mysql"

转成json

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"username": "roddy",
"password": "passwd",
"server_ip": [
"192.168.1.1",
"192.168.2.2",
"192.168.2.3"
],
"server_type": {
"web": "nginx",
"db": "mysql"
}
}

参见yaml样例

推荐个在线yaml转json的网站:https://www.json2yaml.com/convert-yaml-to-json

YAML格式:

1
2
3
4
5
6
7
8
9
10
11
12
name:
- xiaomi
- huawei
yaml:
- slim and flexible
- better for configuration
object:
key: value
array:
- null_value:
- boolean: true
- integer: 1

转成JSON格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"name": [
"xiaomi",
"huawei"
],
"yaml": [
"slim and flexible",
"better for configuration"
],
"object": {
"key": "value",
"array": [
{
"null_value": null
},
{
"boolean": true
},
{
"integer": 1
}
]
}
}

YAML创建Pod

Kubernetes 资源是通过声明的方式创建的,因此可以使用 YAML 文件。Kubernetes 资源(比如 Pod、服务和部署)是使用 YAML 文件创建的。后期我们会经常编写yaml文件用于部署服务。

样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@k8s-master ~]# vim pod-busybox.yaml
apiVersion: v1 # api版本
kind: Pod # 资源类型
metadata:
name: myapp-pod #名字
labels:
app: myapp # 标签内容
spec:
containers:
- name: myapp-container # 容器名
image: busybox # 镜像名
command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']

[root@k8s-master ~]# kubectl create -f pod-busybox.yaml
pod/myapp-pod created
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 ContainerCreating 0 11s # 正在创建pod

[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 21s # 状态为Running
[root@k8s-master ~]#

配置文件详解

  • apiVersion: api版本,v1表示稳定版本,可以参考下面命令查看可用的版本
  • kind: 表示要创建的资源对象,关键字Pod ,可选关键字:Pod、ReplicaSet、ReplicationController、Deployment、StatefulSet、DaemonSet、Job、CronJob、HorizontalPodAutoscaling
  • metadata: 元数据,可以包含多个元数据
  • spec:表示资源对象的具体设置,containers表示容器的集合,可以定义多个容器

查看当前可用的API版本

Kubernetes 1.19.3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@k8s-master ~]# kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1

apiVersion该用哪一个,可以参考:https://www.jianshu.com/p/457cf0835f88

Pod yaml格式参考

使用 kubectl explain pods.spec.containers 命令获取参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[root@k8s-master ~]# 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>
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, the value associated with the last source will take
precedence. Values defined by an Env with a duplicate key will take
precedence. Cannot be updated.

image <string>
Docker image name. More info:
https://kubernetes.io/docs/concepts/containers/images This field is
optional to allow higher level config management to default or override
container images in workload controllers like Deployments and StatefulSets.
......

基本操作

查询所有正在运行的pod

1
2
3
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 21s

查询单个pod名,可以使用-w持续监听

1
2
3
4
5
6
7
[root@k8s-master ~]# kubectl get pod myapp-pod
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 21m

[root@k8s-master ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 24m

查询详情,可以查看调度到哪台主机上

kubectl get pod {pod 名} -o wide

1
2
3
4
[root@k8s-master ~]# kubectl get pod myapp-pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-pod 1/1 Running 0 21m 10.244.2.6 k8s-node2 <none> <none>
[root@k8s-master ~]#

查询pod输出的log

1
2
3
[root@k8s-master ~]# kubectl logs myapp-pod
Hello Kubernetes!
[root@k8s-master ~]#

查询更加详细的pod数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
[root@k8s-master ~]# kubectl describe pods myapp-pod
Name: myapp-pod
Namespace: default
Priority: 0
Node: k8s-node2/172.19.153.99
Start Time: Fri, 23 Oct 2020 14:45:32 +0800
Labels: app=myapp
Annotations: <none>
Status: Running
IP: 10.244.2.6
IPs:
IP: 10.244.2.6
Containers:
myapp-container:
Container ID: docker://3075475b43e69240d392eec14f4fa67bfed524fedbb0f505c56d257ff916080f
Image: busybox
Image ID: docker-pullable://busybox@sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d
Port: <none>
Host Port: <none>
Command:
sh
-c
echo Hello Kubernetes! && sleep 3600
State: Running
Started: Fri, 23 Oct 2020 14:45:48 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-4dfgr (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-4dfgr:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-4dfgr
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 27m default-scheduler Successfully assigned default/myapp-pod to k8s-node2
Normal Pulling 27m kubelet Pulling image "busybox"
Normal Pulled 27m kubelet Successfully pulled image "busybox" in 16.092616692s
Normal Created 27m kubelet Created container myapp-container
Normal Started 27m kubelet Started container myapp-container

get命令支持json或者yaml的格式化输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@k8s-master ~]# kubectl get pod myapp-pod --output yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2020-10-23T06:45:31Z"
labels:
app: myapp
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
..............................
startedAt: "2020-10-23T06:45:48Z"
hostIP: 172.19.153.99
phase: Running
podIP: 10.244.2.6
podIPs:
- ip: 10.244.2.6
qosClass: BestEffort
startTime: "2020-10-23T06:45:32Z"

参考