Files
k8s-note/k8s知识笔记/一、k8s 资源清单/2、资源清单的五大字段.conf
2025-10-09 23:37:29 +08:00

161 lines
4.6 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

一、资源清单-结构
五大对象字段:
1. apiVersion [接口组/版本]
2. kind [资源类型]
3. metadata [元数据]
4. spec [规格/期望]
5. status [状态]
1.1 [接口组/版本] group/apiVersion
* 例子: apiVersion: v1 # pod
apiVersion: apps/v1 # deployment
apiVersion: batch/v1 # job
apiVersion: networking.k8s.io/v1 # ingress
apiVersion: rbac.authorization.k8s.io/v1 # rbac
常见命令:
1、查看集群api版本命令: kubectl api-versions
root@k8s-master:~# kubectl api-versions
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1
authentication.k8s.io/v1
authorization.k8s.io/v1
autoscaling/v1
autoscaling/v2
batch/v1
certificates.k8s.io/v1
coordination.k8s.io/v1
crd.projectcalico.org/v1
discovery.k8s.io/v1
events.k8s.io/v1
flowcontrol.apiserver.k8s.io/v1
networking.k8s.io/v1
node.k8s.io/v1
policy/v1
rbac.authorization.k8s.io/v1
resource.k8s.io/v1
scheduling.k8s.io/v1
storage.k8s.io/v1
v1
[注意事项]:
1. group为空时, 直接使用v1表示
2. group和version之间用/分隔
3. v1表示默认的版本, 即 core/v1
2、查看某个资源的api解释命令: kubectl explain <资源类型> [Deployment、ReplicaSet、DaemonSet、Job、CronJob]
例子: kubectl explain Pod
1.2 [资源类型] kind: List
* 例子: kind: Pod
kind: Deployment
kind: Service
kind: Ingress
kind: ConfigMap
kind: Secret
kind: PersistentVolumeClaim
kind: Namespace
kind: Node
kind: PersistentVolume
kind: ClusterRole
kind: ClusterRoleBinding
kind: CustomResourceDefinition
kind: HPA
kind: PodTemplate
kind: LimitRange
1.3 [元数据] metadata:
* 例子: metadata:
name: my-pod
namespace: default
labels:
app: my-app
annotations:
description: "This is my pod"
1.4 [规格/期望] spec:
* 例子:
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80
replicas: 3
selector:
matchLabels:
app: my-app
volumeMounts:
- name: my-volume
mountPath: /data
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: my-pvc
1.5 [状态] status: # 初次自定义时, 一般不写该字段, 由系统自动生成和维护
* 例子:
status:
coordinations:
leaseDurationSeconds: 15
renewTime: "2023-10-01T12:00:00Z"
# =======================================================================
二、pod 资源清单示例:
# 1. 创建一个名为 my-pod 的 Pod包含两个容器 第一个容器使用 nginx 镜像,监听 80 端口, 第二个容器使用 busybox 镜像, 执行一个简单的命令 Pod 运行在 default 命名空间:
vim my-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
namespace: default
labels:
app: my-app
annotations:
description: "This is my pod"
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80
- name: sidecar-container
image: busybox
command:
- "bash/sh"
- "-c"
- "sleep 3600"
replicas: 3
selector:
matchLabels:
app: my-app
volumeMounts:
- name: my-volume
mountPath: /data
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: my-pvc
2、pod 资源清单应用命令: (实例化资源对象)
kubectl apply -f my-pod.yaml
3、查看 pod 资源命令:
kubectl get pods -A # 查看所有命名空间的 pod
kubectl get pods -n default # 查看 default 命名空间的 pod
kubectl get pods -n kube-system # 查看 kube-system 命名空间的 pod
kubectl get pod my-pod -o yaml # 查看指定 pod 的详细信息
kubectl get pod my-pod -o wide # 查看指定 pod 的宽表信息(IP、节点等)