Kubernetes之(六)资源清单定义

释放双眼,带上耳机,听听看~!

目录

  • Kubernetes之(六)资源清单定义

  • 常用资源

    • 利用配置清单定义自主式Pod资源

Kubernetes之(六)资源清单定义

常用资源

服务发现及均衡
Sevice,Ingress,…
配置与存储
Volume,CSI,ConfigMap,Secret,DownwardAPI
集群级资源
Namespace,Node,Role,ClusterRole,RoleBinding,ClusterRoleBinding
元数据型资源
HPA,PodTemplate,LimitRange

Kubernetes不只是使用命令行进行配置,常用使用yaml文件来创建配置清单
Pod的资源清单
apiserver仅接收JSON格式的资源定义;

  • 当我们使用kubectl run直接创建资源的时候会被自动转换为JSON格式传给apiserver;

  • 使用yaml格式提供配置清单,apiserver可自动将其转换为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
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
1[root@master ~]# kubectl get pods myapp-9b4987d5-djdr9 -o yaml
2apiVersion: v1
3kind: Pod
4metadata:
5  creationTimestamp: "2019-03-28T06:42:04Z"
6  generateName: myapp-9b4987d5-
7  labels:
8    pod-template-hash: 9b4987d5
9    run: myapp
10  name: myapp-9b4987d5-djdr9
11  namespace: default
12  ownerReferences:
13  - apiVersion: apps/v1
14    blockOwnerDeletion: true
15    controller: true
16    kind: ReplicaSet
17    name: myapp-9b4987d5
18    uid: bc03afbd-5120-11e9-80a7-000c295ec349
19  resourceVersion: "38679"
20  selfLink: /api/v1/namespaces/default/pods/myapp-9b4987d5-djdr9
21  uid: 995067e0-5124-11e9-80a7-000c295ec349
22spec:
23  containers:
24  - image: ikubernetes/myapp:v1
25    imagePullPolicy: IfNotPresent
26    name: myapp
27    resources: {}
28    terminationMessagePath: /dev/termination-log
29    terminationMessagePolicy: File
30    volumeMounts:
31    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
32      name: default-token-dqd2f
33      readOnly: true
34  dnsPolicy: ClusterFirst
35  enableServiceLinks: true
36  nodeName: node02
37  priority: 0
38  restartPolicy: Always
39  schedulerName: default-scheduler
40  securityContext: {}
41  serviceAccount: default
42  serviceAccountName: default
43  terminationGracePeriodSeconds: 30
44  tolerations:
45  - effect: NoExecute
46    key: node.kubernetes.io/not-ready
47    operator: Exists
48    tolerationSeconds: 300
49  - effect: NoExecute
50    key: node.kubernetes.io/unreachable
51    operator: Exists
52    tolerationSeconds: 300
53  volumes:
54  - name: default-token-dqd2f
55    secret:
56      defaultMode: 420
57      secretName: default-token-dqd2f
58status:
59  conditions:
60  - lastProbeTime: null
61    lastTransitionTime: "2019-03-28T06:42:04Z"
62    status: "True"
63    type: Initialized
64  - lastProbeTime: null
65    lastTransitionTime: "2019-03-28T06:42:05Z"
66    status: "True"
67    type: Ready
68  - lastProbeTime: null
69    lastTransitionTime: "2019-03-28T06:42:05Z"
70    status: "True"
71    type: ContainersReady
72  - lastProbeTime: null
73    lastTransitionTime: "2019-03-28T06:42:04Z"
74    status: "True"
75    type: PodScheduled
76  containerStatuses:
77  - containerID: docker://69b4cab1eb139c8e9c23e79792782db739fae21bedbc9199e1ab75b10729b038
78    image: ikubernetes/myapp:v1
79    imageID: docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
80    lastState: {}
81    name: myapp
82    ready: true
83    restartCount: 0
84    state:
85      running:
86        startedAt: "2019-03-28T06:42:05Z"
87  hostIP: 10.0.0.12
88  phase: Running
89  podIP: 10.244.2.13
90  qosClass: BestEffort
91  startTime: "2019-03-28T06:42:04Z"
92

大部分资源清单有以下五个字段组成:

  • apiVersion: group/version # 指明api资源所属的群组及版本,使用kubectl api-version可查看,同一组子资源可以有多个版本

  • kind: 资源类别,Pod,ReplicaSet,Deployment,StatefulSet,DaemonSet,Job,Cronjob 。注意大小写

  • metadata: 元数据

  • name:同一类别要求名字唯一

    • namespace:对应的对象属于哪个名称空间,默认default
    • labels: 标签,搜友资源都可以有标签,K/V类型
    • annotations:资源注解

每个资源的引用PATH
/api/GROUP/VERSION/namespaces/NAMESPACE/TYPE/NAME
小写是固定字符,大写是根据实际情况修改

  • **spec:**最重要字段,定义目标的期望状态,desired state,不同类型资源内部可能有所不同

  • **status:**当前状态(只读)本字段由kubernetes进行维护

以上可以使用kubectl explain 进行查看相应字段


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
1[root@master ~]# kubectl explain pods
2KIND:     Pod
3VERSION:  v1
4
5DESCRIPTION:
6     Pod is a collection of containers that can run on a host. This resource is
7     created by clients and scheduled onto hosts.
8
9FIELDS:
10   apiVersion   <string>
11     APIVersion defines the versioned schema of this representation of an
12     object. Servers should convert recognized schemas to the latest internal
13     value, and may reject unrecognized values. More info:
14     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
15
16   kind <string>
17     Kind is a string value representing the REST resource this object
18     represents. Servers may infer this from the endpoint the client submits
19     requests to. Cannot be updated. In CamelCase. More info:
20     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
21
22   metadata     <Object>
23     Standard object's metadata. More info:
24     https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
25
26   spec <Object>
27     Specification of the desired behavior of the pod. More info:
28     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
29
30   status       <Object>
31     Most recently observed status of the pod. This data may not be up to date.
32     Populated by the system. Read-only. More info:
33     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
34

查看下一集字段,例如pods下的metadata,使用kubectl explain pods.metadata,以此类推.

二级字段下,每一种字段都有对应的键值类型,常用类型大致如下:

  • <[ ]string>:表示是一个字串列表,也就是字串类型的数组

  • <Object>:表示是可以嵌套的字段

  • <map[string]string>:表示是一个由键值组成映射

  • <[ ]Object>:表示是一个对象列表

  • <[ ]Object> -required-:required表示该字段是一个必选的字段

利用配置清单定义自主式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
1[root@master ~]# mkdir manifests
2[root@master ~]# cd manifests/
3[root@master manifests]# vim pod-demo.yaml
4  labels:
5apiVersion: v1
6kind: Pod
7metadata:
8  name: pod-demo
9  namespace: default
10  #labels: {&quot;app&quot;: &quot;myapp&quot;,&quot;tier&quot;: &quot;frontend&quot;} 和下面效果一样,建议使用下面格式
11  labels:
12    app: myapp
13    tier: frontend
14spec:
15  containers:
16  - name: myapp
17    image: ikubernetes/myapp:v1
18  - name: busybox
19    image: busybox
20    command:
21    - &quot;/bin/sh&quot;
22    - &quot;-c&quot;
23    - &quot;echo $(date)&gt;&gt;/usr/share/nginx/html/index.html;sleep 3600&quot;
24

使用**kubectl create -f **.yaml**创建资源


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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
1[root@master manifests]# kubectl create -f pod-demo.yaml
2pod/pod-demo created
3[root@master manifests]# kubectl describe pods pod-demo
4Name:               pod-demo
5Namespace:          default
6Priority:           0
7PriorityClassName:  &lt;none&gt;
8Node:               node02/10.0.0.12
9Start Time:         Thu, 28 Mar 2019 17:27:35 +0800
10Labels:             app=myapp
11                    tier=frontend
12Annotations:        &lt;none&gt;
13Status:             Running
14IP:                 10.244.2.15
15Containers:
16  myapp:
17    Container ID:   docker://81fcdf25bac4f9691aaa80ccf1acd0fe565575ea894d07ea1c382e0366bcbfba
18    Image:          ikubernetes/myapp:v1
19    Image ID:       docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
20    Port:           &lt;none&gt;
21    Host Port:      &lt;none&gt;
22    State:          Running
23      Started:      Thu, 28 Mar 2019 17:27:35 +0800
24    Ready:          True
25    Restart Count:  0
26    Environment:    &lt;none&gt;
27    Mounts:
28      /var/run/secrets/kubernetes.io/serviceaccount from default-token-dqd2f (ro)
29  busybox:
30    Container ID:  docker://af0d0f76b0f6ba9eeaea18178d1d9cf3a052176e219471896a56d727622c9a36
31    Image:         busybox
32    Image ID:      docker-pullable://busybox@sha256:061ca9704a714ee3e8b80523ec720c64f6209ad3f97c0ff7cb9ec7d19f15149f
33    Port:          &lt;none&gt;
34    Host Port:     &lt;none&gt;
35    Command:
36      /bin/sh
37      -c
38      sleep 3600
39    State:          Running
40      Started:      Thu, 28 Mar 2019 17:27:37 +0800
41    Ready:          True
42    Restart Count:  0
43    Environment:    &lt;none&gt;
44    Mounts:
45      /var/run/secrets/kubernetes.io/serviceaccount from default-token-dqd2f (ro)
46Conditions:
47  Type              Status
48  Initialized       True
49  Ready             True
50  ContainersReady   True
51  PodScheduled      True
52Volumes:
53  default-token-dqd2f:
54    Type:        Secret (a volume populated by a Secret)
55    SecretName:  default-token-dqd2f
56    Optional:    false
57QoS Class:       BestEffort
58Node-Selectors:  &lt;none&gt;
59Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
60                 node.kubernetes.io/unreachable:NoExecute for 300s
61Events:
62  Type    Reason     Age   From               Message
63  ----    ------     ----  ----               -------
64  Normal  Scheduled  13s   default-scheduler  Successfully assigned default/pod-demo to node02
65  Normal  Pulled     13s   kubelet, node02    Container image &quot;ikubernetes/myapp:v1&quot; already present on machine
66  Normal  Created    13s   kubelet, node02    Created container
67  Normal  Started    13s   kubelet, node02    Started container
68  Normal  Pulling    13s   kubelet, node02    pulling image &quot;busybox&quot;
69  Normal  Pulled     11s   kubelet, node02    Successfully pulled image &quot;busybox&quot;
70  Normal  Created    11s   kubelet, node02    Created container
71  Normal  Started    11s   kubelet, node02    Started container
72

使用kubectl delete -f .yaml删除资源
使用
kubectl logs POD_NAME -c CONTAINER_NAME 查看指定Pod内的指定容器的日志
使用
kubectl exec -it POD_NAME-c CONTAINER_NAME — /bin/sh ** 交互式进入指定Pod内的指定容器内部

给TA打赏
共{{data.count}}人
人已打赏
安全运维

故障复盘的简洁框架-黄金三问

2021-9-30 19:18:23

安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索