k8s的资源管理
Kubernetes 将所有功能实体都抽象为"资源",其管理本质就是对各种资源进行定义、部署和协调。要掌握 Kubernetes,核心在于理解并操作以下几类关键资源:
- 工作负载(Workloads):Pod 与控制器
 Kubernetes 的最小管理单元是 Pod ,它作为一个或多个容器的逻辑"主机",是部署在集群中的具体实例。然而,Pod 本身是易逝的,通常不直接创建,而是通过 Pod 控制器(如 Deployment、StatefulSet)来管理。控制器负责维护 Pod 的期望状态,实现故障自愈、滚动更新与弹性伸缩。
- 网络与访问(Networking):Service
 当 Pod 能够提供服务后,需要一个稳定的端点以供访问。由于 Pod 的 IP 地址会随着重启或调度而变化,Service 资源应运而生。它作为一个稳定的抽象层,通过定义访问策略与服务发现,为一组 Pod 提供统一的网络入口。
- 存储(Storage):持久化卷
 为了满足应用数据持久化的需求,Kubernetes 提供了 存储资源,如 Persistent Volume (PV) 和 Persistent Volume Claim (PVC)。它们将存储抽象出来,使应用在调度和重启时能够独立于底层存储系统,实现数据的持久化与迁移
k8s的资源管理方式有3种
1.命令式对象管理:直接用命令操作k8s资源
2.命令式对象配置:通过命令和配置文件操作k8s资源
3.声明式对象配置:通过apply和配置文件操作k8s资源
常用的管理方式是声明式对象配置,
命令式对象管理
通过kubectl命令来对集群本身进行管理,并能够在集群上进行容器化应用的安装部署
语法:kubectl [command] [type] [name] [flags]
Command: 指定要对资源执行的操作,比如:create、get、delete
Type:指定资源类型,比如deployment、pod、service
Name:指定资源的名称,名称大小写敏感
Flags:指定额外的可选参数
#查看所有pod
[root@master ~]# kubectl get pods
No resources found in default namespace.  #没有指定命名空间会查看默认空间里的pod
#查看单个pod
[root@master ~]# kubectl get pod nginxpod -n xy
NAME       READY   STATUS    RESTARTS   AGE
nginxpod   1/1     Running   0          10m
#查看pod以yaml格式
[root@master ~]# kubectl get pod nginxpod -n xy -o yaml
apiVersion: v1
kind: Pod
metadata:
  annotations:
    cni.projectcalico.org/containerID: c5d9c6e6c85f4eab076214e6ad7d0ef6ab1de56c2fda5d49758950ddb3448c24
    cni.projectcalico.org/podIP: 172.16.166.130/32
    cni.projectcalico.org/podIPs: 172.16.166.130/32
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"nginxpod","namespace":"xy"},"spec":{"containers":[{"image":"nginx:latest","name":"nginx-containers"}]}}
  creationTimestamp: "2025-10-29T11:17:16Z"
  name: nginxpod
  namespace: xy
  resourceVersion: "28073"
  uid: f2802be9-43e6-468a-8966-f1ac0afb8796
spec:
  containers:
  - image: nginx:latest
    imagePullPolicy: Always
    name: nginx-containers
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-qhp5z
      readOnly: true
......命令式对象配置
命令式对象配置就是使用命令配合配置文件一起来操作k8s资源
#创建yaml文件
[root@master ~]# vim xy.yaml
apiVersion: v1
kind: Namespace
metadata: 
  name: xy
---
apiVersion: v1
kind: Pod
metadata:
  name: nginxpod
  namespace: xy
spec:
  containers:
  - name: nginx-containers
    image: nginx:latest
    
#用create创建
[root@master ~]# kubectl create -f xy.yaml
[root@master ~]# kubectl get -f xy.yaml 
NAME           STATUS   AGE
namespace/xy   Active   14s
NAME           READY   STATUS    RESTARTS   AGE
pod/nginxpod   1/1     Running   0          14s
#这里显示的两个一个是namespace一个是pod
#删除文件
[root@master ~]# kubectl delete -f xy.yaml 
namespace "xy" deleted
pod "nginxpod" deleted
[root@master ~]# kubectl get -f xy.yaml 
Error from server (NotFound): namespaces "xy" not found
Error from server (NotFound): namespaces "xy" not found
两个都被删除了声明式对象配置
声明式对象配置跟命令式对象配置很相似,但是它只有一个命令apply
[root@master ~]# kubectl apply -f xy.yaml 
namespace/xy created
pod/nginxpod created
[root@master ~]# kubectl apply -f xy.yaml 
namespace/xy unchanged
pod/nginxpod unchanged
#声明式对象配置就是使用apply描述一个资源最终的状态(在yaml中定义状态)
使用apply操作资源:
如果资源不存在,就创建,相当于kubectl  create
如果资源已存在,就更新,就相当于kubectl  patch创建/更新资源,使用声明式对象配置 kubectl apply -f
删除资源,使用命令式对象配置 kubectl delete -f
查询资源 使用命令式对象管理 kubectl get
创建一个命名空间
[root@master ~]# kubectl create namespace test1
namespace/test1 created
#获取命名空间
[root@master ~]# kubectl get ns
NAME              STATUS   AGE
cy                Active   8h
default           Active   28h
kube-node-lease   Active   28h
kube-public       Active   28h
kube-system       Active   28h
test1             Active   24s
xy                Active   2m28s
#default:所有未指定的Namespace的对象都会被分配在default命名空间。
 kube-node-lease:集群节点之间的心跳维护,v1.13开始引入
 kube-public:此命名空间的资源可以被所有人访问(包括未认证用户)。
 kube-system:所有由kubernetes系统创建的资源都处于这个命名空间。在test1的命名空间下创建并运行一个nginx的pod
[root@master ~]# kubectl run pod1 --image=nginx:latest -n test1
pod/pod1 created
[root@master ~]# kubectl get pods -n test1
NAME   READY   STATUS    RESTARTS   AGE
pod1   1/1     Running   0          111s
#删除pod
[root@master ~]# kubectl delete pod pod1 -n test1
pod "pod1" deleted
[root@master ~]# kubectl get pods -n test1
No resources found in test1 namespace.
#删除test1命名空间
[root@master ~]# kubectl delete ns test1
namespace "test1" deleted
[root@master ~]# kubectl get ns
NAME              STATUS   AGE
cy                Active   8h
default           Active   28h
kube-node-lease   Active   28h
kube-public       Active   28h
kube-system       Active   28h
xy                Active   7m16s| 类型 | 操作对象 | 适用环境 | 优点 | 缺点 | 
|---|---|---|---|---|
| 命令式对象管理 | 对象 | 测试 | 简单 | 只能操作活动对象,无法审计、跟踪 | 
| 命令式对象配置 | 文件 | 开发 | 可以审计、跟踪 | 项目大时,配置文件多,操作麻烦 | 
| 声明式对象配置 | 目录 | 开发 | 支持目录操作 | 意外情况下难以调试 | 
如果不知道命令,还可以用kubectl --help
[root@master ~]# kubectl --help
kubectl controls the Kubernetes cluster manager.
 Find more information at: https://kubernetes.io/docs/reference/kubectl/
Basic Commands (Beginner):
  create          Create a resource from a file or from stdin
  expose          Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
  run             Run a particular image on the cluster
  set             Set specific features on objects
Basic Commands (Intermediate):
  explain         Get documentation for a resource
  get             Display one or many resources
  edit            Edit a resource on the server
  delete          Delete resources by file names, stdin, resources and names, or by resources and label selector
Deploy Commands:
  rollout         Manage the rollout of a resource
  scale           Set a new size for a deployment, replica set, or replication controller
  autoscale       Auto-scale a deployment, replica set, stateful set, or replication controller
Cluster Management Commands:
  certificate     Modify certificate resources
  cluster-info    Display cluster information
  top             Display resource (CPU/memory) usage
  cordon          Mark node as unschedulable
  uncordon        Mark node as schedulable
  drain           Drain node in preparation for maintenance
  taint           Update the taints on one or more nodes常用资源
| 资源分类 | 资源名称 | 缩写 | 资源作用 | 
|---|---|---|---|
| 集群资源 | nodes | no | 集群组成部分 | 
| Namespaces | namespace | ns | 隔离pod | 
| Pod资源 | pods | po | 装载容器 | 
| Pod资源控制器 | replicationcontrollers | rc | 控制pod资源 | 
| Pod资源控制器 | replicasets | rs | 控制pod资源 | 
| Pod资源控制器 | deployment | deploy | 控制pod资源 | 
| Pod资源控制器 | daemonsets | ds | 控制pod资源 | 
| Pod资源控制器 | jobs | 控制pod资源 | |
| Pod资源控制器 | cronjobs | cj | 控制pod资源 | 
| Pod资源控制器 | horizontalpodautoscalers | hpa | 控制pod资源 | 
| Pod资源控制器 | statefulsets | sts | 控制pod资源 | 
| 服务发现资源 | services | svc | 统一pod对外接口 | 
| 服务发现资源 | ingress | ing | 统一pod对外接口 | 
| 存储资源 | volumeattachements | 存储 | |
| 存储资源 | persistentvolumes | pv | 存储 | 
| 存储资源 | persistentvolumeclaims | pvc | 存储 | 
| 配置资源 | configmaps | cm | 配置 | 
| 配置资源 | secrets | 配置 |