一、K8S中常见的资源
Kubernetes中把资源实例化以后称之为对象,这里先介绍K8S中常见的核心资源有哪些:
- 工作负载型资源(workload):Pod、ReplicaSet、Deployment、StatefulSet、DaemonSet、Job、CronJob。(ReplicationController在v1.11版本被废弃)
- 服务发现及负载均衡型资源(ServiceDiscovery、LoadBalance) : Service 、Ingress, ...
- 配置与存储型资源: Volume(存储卷)、CSI(容器存储接口,可以扩展各种各样的第三方存储卷)
- 特殊类型的存储卷:ConfigMap(当配置中心来使用的资源类型)、Secret(保存敏感数据)、DownwardAPI(把外部环境中的信息输出给容器)
以上这些资源都是配置在名称空间级别。
- 集群级资源(都是配置在名): Namespace、Node、Role、ClusterRole、RoleBinding(角色绑定)、ClusterRoleBinding(集群角色绑定)、
- 元数据型资源:HPA、PodTemplate(Pod模板,用于让控制器创建Pod时使用的模板。)、LimitRange(用来定义硬件资源限制的)
下面是利用资源清单创建一个Pod的资源清单内容:
[root@s1 ~]# kubectl get pod myapp-7c468db58f-5nghn -o yaml
apiVersion: v1 # K8S API版本,应该由两部分组成:group/version,group省略表示默认为core
kind: Pod # 资源类别: Pod、Deployment、Service等等
metadata: # 资源元数据
creationTimestamp: "2019-12-16T07:45:17Z"
generateName: myapp-7c468db58f-
labels:
pod-template-hash: 7c468db58f
run: myapp
name: myapp-7c468db58f-5nghn
namespace: default
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: myapp-7c468db58f
uid: 7463ad31-bbfa-46a7-b301-5a70daba188b
resourceVersion: "40423"
selfLink: /api/v1/namespaces/default/pods/myapp-7c468db58f-5nghn
uid: a0f310d7-24f6-441e-a139-8cbe11e9a2ff
spec: # specifications, 资源规格。(定义资源对象期望的状态),这个是最重要的字段,用于规定接下来要创建的资源对象应该拥有的特性。然后依靠控制器确保这些特性能够被满足。
containers:
- image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
name: myapp
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-6hhk2
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: n1
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations: # 容忍度,能够容忍哪些污点
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-6hhk2
secret:
defaultMode: 420
secretName: default-token-6hhk2
status: # 用于显示这个资源对象当前的状态,这个字段是只读的。
conditions:
- lastProbeTime: null
lastTransitionTime: "2019-12-16T07:45:17Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2019-12-16T07:45:18Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2019-12-16T07:45:18Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2019-12-16T07:45:17Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://a5135c4064fc86edf913d5d053688ff188dc22a4723049d80089ca9a39c538b2
image: ikubernetes/myapp:v1
imageID: docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
lastState: {}
name: myapp
ready: true
restartCount: 0
started: true
state:
running:
startedAt: "2019-12-16T07:45:18Z"
hostIP: 192.168.100.50
phase: Running
podIP: 10.244.1.15
podIPs:
- ip: 10.244.1.15
qosClass: BestEffort
startTime: "2019-12-16T07:45:17Z"
命令:kubectl api-versions
可以查看所有API 群组/版本
[root@s1 ~]# 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/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
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
二、创建资源清单
2.1 创建资源的方法:
apiserver仅接收JSON格式的资源定义,利用yaml格式提供配置清单,apiserver可自动将其转为json格式,而后再提交并执行。
2.2 常用资源的配置清单
apiVersion: group/version
$ kubectl api-versions
kind: 资源类别
metadata: 元数据
name: 资源名称
namespace: 名称空间
labels: 标签,键值数据。数据大小有限制。
annotations: 注解,也是键值数据,但是它的数据没有大小限制。
spec: 期望的状态,disired state,由用户定义,最重要。每种资源支持的字段不一样。
status: 当前状态,current state, 本字段由K8S集群维护。
每个资源的引用PATH:
/api/GROUP/VERSION/namespace/NAMESPACE/TYPE/NAME
(大写单词替换为具体名称),可以通过这个PATH获取资源的信息。查看某个资源类型支持的字段:
命令:kubectl explain <resource_type>.<fieldName>[.<fieldName>]
[root@s1 ~]# kubectl explain Pod
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
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/sig-architecture/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec <Object>
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
status <Object>
Most recently observed status of the pod. This data may not be up to date.
Populated by the system. Read-only. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
每个字段的值都标记有对应的类型:
字段标记有-required-
表示必选字段。
值类型 | 简述 |
---|---|
<string> |
字符串 |
<[]string> |
字符串列表,所有的列表数据都可以放在[]中。 |
<integer> |
整数 |
<Object> |
对象,也就是可以嵌套二级或三级字段。。。 |
<[]Object> |
对象列表, |
<map[string]string> |
映射,多个k=v类型的json数组,也就是键值对,key=value,所有映射数据都可以直接放在{}中。 |
<boolean> |
布尔值,true或false |
2.3 利用资源清单创建Pod
先创建一个资源清单:
[root@s1 ~]# cat pod-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
- name: busybox
image: busybox:latest
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
执行命令以创建Pod:
[root@s1 ~]# kubectl create -f pod-demo.yaml
pod/pod-demo created
[root@s1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
client 0/1 Error 0 161m 10.244.2.6 n2 <none> <none>
myapp-7c468db58f-5nghn 1/1 Running 0 52m 10.244.1.15 n1 <none> <none>
myapp-7c468db58f-k2rgk 1/1 Running 0 52m 10.244.2.13 n2 <none> <none>
pod-demo 2/2 Running 0 17s 10.244.2.14 n2 <none> <none>
访问pod-demo pod中的myapp容器并查看其日志:
[root@s1 ~]# curl 10.244.2.14
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@s1 ~]# kubectl logs pod-demo myapp
10.244.0.0 - - [16/Dec/2019:08:38:11 +0000] "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
删除资源清单里面的资源:
[root@s1 ~]# kubectl delete -f pod-demo.yaml
pod "pod-demo" deleted
事实上使用kubectl
命令管理资源有三种用法:
- 命令式用法, K8S应用快速入门中讲的。
- 命令式资源清单用法,就是本章;
- 声明式资源清单。使用声明式资源清单,可以确保资源尽可能的向我们声明的状态改变,这样我们就可以随时改变声明,并随时应用。