3. Kubernetes资源-概述
3.1 资源概念
Kubernetes中所有内容都抽象为资源,资源实例化之后叫做对象。Kubernetes的资源根据不同的维度可以分为:
- 工作负载型:Pod、ReplicaSet、Deployment、DaemonSet、Job/CronJob、StatefulSet
- 服务发现型:Service、Ingress
- 配置与存储:ConfigMap、Secret、Volume、PV-PVC
- 集群级别:Namespace、Role、ClusterRole、RoleBinding、ClusterRoleBinding
- 元数据型:HPA、PodTemplate、LimitRangs
3.2 资源清单
Kubernetes中一般使用YAML格式的文件来创建符合预期期望的Pod,这样的YAML文件称为资源清单。可以使用kubectl explain RESOURCE [options]查看不同资源支持的YAML标签。例如:查看Pod资源所支持的标签
[root@k8smaster43-11 CSDN]# 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
[root@k8smaster43-11 CSDN]#
3.2.1 YAML
- YAML文件使用#标识注释,只支持空格缩进,并且相同层级的元素左对齐;
- YAML数据支持对象(键值对)、数组和纯量(字符串、布尔、整数、浮点数、空值、时间、日期)。
3.2.2 创建资源(自主式Pod为例)
- 编写YAML文件:
apiVersion: v1 # API版本
kind: Pod # 资源类型
metadata:
name: tomcat-pod # Pod资源名称
namespace: default # Pod所在名称空间
labels:
app: tomcat # Pod资源标签
spec:
containers:
- name: tomcat # Pod内容器名称
image: tomcat:8.0 # Pod内容器镜像
imagePullPolicy: Never
ports:
- containerPort: 8080 # Pod内容器服务端口
hostPort: 8080 # 主机映射端口
protocol: TCP # 端口服务类型
- 创建Pod资源:
[root@k8smaster43-11 CSDN]# kubectl create -f pod.yaml
pod/tomcat-pod created
[root@k8smaster43-11 CSDN]#
- 查看Pod资源:
[root@k8smaster43-11 CSDN]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat-pod 1/1 Running 0 2m58s 10.244.1.53 k8sworker43-21 <none> <none>
- 查看资源详情:
[root@k8smaster43-11 CSDN]# kubectl describe pod/tomcat-pod
Name: tomcat-pod
Namespace: default
Priority: 0
Node: k8sworker43-21/192.168.43.21
Start Time: Sat, 23 Jul 2022 08:08:49 +0800
Labels: app=tomcat
Annotations: <none>
Status: Running
IP: 10.244.1.53
IPs:
IP: 10.244.1.53
Containers:
tomcat:
Container ID: docker://81e61ed10151b679bc987f388422372d4bff66eae7ba4246ed064d0035d6ac22
Image: tomcat:8.0
Image ID: docker-pullable://tomcat@sha256:8ecb10948deb32c34aeadf7bf95d12a93fbd3527911fa629c1a3e7823b89ce6f
Port: 8080/TCP
Host Port: 8080/TCP
State: Running
Started: Sat, 23 Jul 2022 08:08:50 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-sbvk4 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-sbvk4:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-sbvk4
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 5m49s default-scheduler Successfully assigned default/tomcat-pod to k8sworker43-21
Normal Pulled 5m45s kubelet Container image "tomcat:8.0" already present on machine
Normal Created 5m45s kubelet Created container tomcat
Normal Started 5m45s kubelet Started container tomcat
[root@k8smaster43-11 CSDN]#
- 删除Pod资源:
[root@k8smaster43-11 CSDN]# kubectl delete -f pod.yaml
pod "tomcat-pod" deleted
[root@k8smaster43-11 CSDN]#