k8s------资源管理
一、资源管理介绍
在kubernetes中,所有的内容都抽象为资源,用户需要通过操作资源来管理kubernetes。
(1)kubernetes的本质上就是一个集群系统,用户可以在集群中部署各种服务,所谓的部署服务,其实就是在kubernetes集群中运行一个个容器,并将指定的程序跑在容器中。
(2)Kubernetes的最小管理单元是pod,而不是容器,所以只能将容器放在pod中,而kubernetes一般也不会直接管理pod,而是通过pod控制器来管理pod。
(3)Pod可以提供服务之后,就要考虑如何访问pod中服务,kubernetes提供了service资源实现这个功能。
(4)如果pod中程序的数据需要持久化,kubernetes还提供了各种存储系统。

学习kubernetes的核心就是学习如何对集群中pod、pod控制器、service、存储等各种资源进行操作。
二、资源管理方式
(1)命令式对象管理:直接使用命令去操作kubernetes资源
kubectl run nginx-pod --image=nginx:1.17.1 --port=80
(2)命令式对象配置:通过命令配置和配置文件去操作kubernetes资源
Kubectl create/path -f nginx-pod.yml
(3)声明式对象配置:通过apply和配置文件去操作kubernetes资源
Kubectl apply -f nginx-pod.yml
| 类型 | 操作对象 | 适用环境 | 优点 | 缺点 |
|---|---|---|---|---|
| 命令式对象管理 | 对象 | 测试 | 简单 | 只能操作活动对象,无法审计、跟踪 |
| 命令式对象配置 | 文件 | 开发 | 可以审计、跟踪 | 项目大时,配置文件多,操作麻烦 |
| 声明式对象配置 | 目录 | 开发 | 支持目录操作 | 意外情况下难以调试 |
1、命令式对象管理
Kubectl命令
Kubectl是kubernetes集群的命令行工具,通过它能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署。Kubectl命令的语法如下:
Kubectl [command] [type] [name] [flags]
Command: 指定要对资源执行的操作,比如:create、get、delete
Type:指定资源类型,比如deployment、pod、service
Name:指定资源的名称,名称大小写敏感
Flags:指定额外的可选参数
(1)查看node节点
powershell
[root@master ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
master Ready control-plane 17h v1.28.15
node1 Ready <none> 17h v1.28.15
node2 Ready <none> 17h v1.28.15
(2)查看命名空间
powershell
[root@master ~]# kubectl get namespace
NAME STATUS AGE
default Active 17h
kube-node-lease Active 17h
kube-public Active 17h
kube-system Active 17h
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 17h
kube-node-lease Active 17h
kube-public Active 17h
kube-system Active 17h
(3)查看所有pod(查看的是default中的pod)
powershell
[root@master ~]# kubectl get pods
No resources found in default namespace.
(4)创建一个nginx-pod(没有单独指定默认创建到default中)
powershell
[root@master ~]# kubectl run nginx-pod --image=nginx:latest --port=80
pod/nginx-pod created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-pod 0/1 ContainerCreating 0 3s
#查看nginx-pod的详细信息,因为在default命名空间中,不用指定namespace
[root@master ~]# kubectl describe pod nginx-pod
(5)查看某个namespace中的所有pod
powershell
[root@master ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-9d57d8f49-5zfvc 1/1 Running 1 (29m ago) 16h
calico-node-nr2ml 1/1 Running 1 (28m ago) 16h
calico-node-rbpwd 1/1 Running 1 (29m ago) 16h
calico-node-wfrtx 1/1 Running 1 (29m ago) 16h
coredns-6554b8b87f-6q2k6 1/1 Running 1 (29m ago) 17h
coredns-6554b8b87f-8sd7d 1/1 Running 1 (29m ago) 17h
etcd-master 1/1 Running 1 (29m ago) 17h
kube-apiserver-master 1/1 Running 1 (29m ago) 17h
kube-controller-manager-master 1/1 Running 1 (29m ago) 17h
kube-proxy-7j4jz 1/1 Running 1 (29m ago) 17h
kube-proxy-95t2g 1/1 Running 1 (29m ago) 17h
kube-proxy-pcfqn 1/1 Running 1 (28m ago) 17h
kube-scheduler-master 1/1 Running 1 (29m ago) 17h
(6)查看某个pod的详细信息
powershell
[root@master ~]# kubectl describe pods calico-node-nr2ml -n kube-system
Name: calico-node-nr2ml
Namespace: kube-system
Priority: 2000001000
Priority Class Name: system-node-critical
Service Account: calico-node
Node: node1/192.168.100.110
Start Time: Tue, 28 Oct 2025 17:49:57 +0800
......
......
(7)查看某个pod(不加namespace,只能查看default中的pod)
kubectl get pod pod_name
powershell
[root@master ~]# kubectl get pod nginx-pod
NAME READY STATUS RESTARTS AGE
nginx-pod 1/1 Running 0 5m1s
[root@master ~]# kubectl get pod calico-node-wfrtx -n kube-system
NAME READY STATUS RESTARTS AGE
calico-node-wfrtx 1/1 Running 1 (48m ago) 17h
(8)查看某个pod,以yaml格式展示结果
kubectl get pod pod_name -o yaml
powershell
[root@master ~]# kubectl get pod nginx-pod -o yaml
apiVersion: v1
kind: Pod
metadata:
annotations:
......
资源类型:
Kubernetes中所有的内容都抽象为资源,可以通过下面的命令进行查看:
kubectl api-resources
powershell
[root@master ~]# kubectl api-resources
NAME SHORTNAMES APIVERSION NAMESPACED KIND
bindings v1 true Binding
componentstatuses cs v1 false ComponentStatus
configmaps cm v1 tr
......
经常使用的资源有下面这些:
| 资源分类 | 资源名称 | 缩写 | 资源作用 |
|---|---|---|---|
| 集群资源 | nodes | no | 集群组成部分 |
| Namespaces | namespace | ns | 隔离pod |
| Pod资源 | pods | po | 装载容器 |
| Pod资源控制器 | replicationcontrollers | rc | 控制pod资源 |
| replicasets | rs | 控制pod资源 | |
| deployment | deploy | 控制pod资源 | |
| daemonsets | ds | 控制pod资源 | |
| jobs | 控制pod资源 | ||
| cronjobs | cj | 控制pod资源 | |
| horizontalpodautoscalers | hpa | 控制pod资源 | |
| statefulsets | sts | 控制pod资源 | |
| 服务发现资源 | services | svc | 统一pod对外接口 |
| ingress | ing | 统一pod对外接口 | |
| 存储资源 | volumeattachements | 存储 | |
| persistentvolumes | pv | 存储 | |
| persistentvolumeclaims | pvc | 存储 | |
| 配置资源 | configmaps | cm | 配置 |
| secrets | 配置 |
操作:
Kubernetes允许对资源进行多种操作,可以通过--help查看详细的操作命令
kubectl --help
powershell
[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
......
......
经常使用的操作有下面这些:
| 命令分类 | 命令 | 翻译 | 命令作用 |
|---|---|---|---|
| 基本命令 | create | 创建 | 创建一个资源 |
| edit | 编辑 | 编辑一个资源 | |
| get | 获取 | 获取一个资源 | |
| patch | 更新 | 更新一个资源 | |
| delete | 删除 | 删除一个资源 | |
| explain | 解释 | 展示资源文档 | |
| run | 运行 | 在集群中运行一个指定的镜像 | |
| expose | 暴露 | 暴露资源为service | |
| describe | 描述 | 显示资源内部信息 | |
| logs | 日志输出容器在pod中的日志 | 输出容器在pod中的日志 | |
| attach | 缠绕进入运行中的容器 | 进入运行中的容器 | |
| exec | 执行容器中的一个命令 | 执行容器中的一个命令 | |
| cp | 复制 | 在pod内外复制文件 | |
| rollout | 首次展示 | 管理资源的发布 | |
| scale | 规模 | 扩(缩)容pod的数量 | |
| autoscale | 自动调整 | 自动调整pod的数量 | |
| 高级命令 | apply | Rc | 通过文件对资源进行配置 |
| label | 标签 | 通过文件对资源进行配置 | |
| 其他命令 | cluster-info | 集群信息 | 显示集群信息 |
| version | 版本 | 显示当前server和client版本 |
以一个namespace / pod的创建和删除简单演示:
(1)创建一个namespace
powershell
[root@master ~]# kubectl create namespace stw
namespace/stw created
(2)查看namespace
powershell
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 17h
kube-node-lease Active 17h
kube-public Active 17h
kube-system Active 17h
stw Active 10s
default:所有未指定的Namespace的对象都会被分配在default命名空间
kube-node-lease:集群节点之间的心跳维护,v1.13开始引入
kube-public:此命名空间的资源可以被所有人访问(包括未认证用户)
kube-system:所有由kubernetes系统创建的资源都处于这个命名空间
(3)在此namespace(stw)下创建并运行一个nginx的pod(pod1)
powershell
[root@master ~]# kubectl run pod1 --image=nginx -n stw
pod/pod1 created
(4)查看新创建的pod
powershell
[root@master ~]# kubectl get pods -n stw
NAME READY STATUS RESTARTS AGE
pod1 0/1 ContainerCreating 0 54s
#等待一会儿之后变为running状态
[root@master ~]# kubectl get pods -n stw
NAME READY STATUS RESTARTS AGE
pod1 1/1 Running 0 2m6s
#查看此pod的详细信息
[root@master ~]# kubectl describe pod pod1 -n stw
(5)编辑指定pod
powershell
[root@master ~]# kubectl edit pod pod1 -n stw
Edit cancelled, no changes made.
(7)删除指定pod
powershell
[root@master ~]# kubectl delete pod pod1 -n stw
pod "pod1" deleted
[root@master ~]# kubectl get pod pod1 -n stw
Error from server (NotFound): pods "pod1" not found
[root@master ~]# kubectl get pod -n stw
No resources found in stw namespace.
(6)删除指定的namespace
powershell
[root@master ~]# kubectl delete ns stw
namespace "stw" deleted
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 17h
kube-node-lease Active 17h
kube-public Active 17h
kube-system Active 17h
kubectl子命令Tab补齐
powershell
[root@master ~]# source /usr/share/bash-completion/bash_completion
[root@master ~]# source <(kubectl completion bash)
[root@master ~]# echo "source <(kubectl completion bash)" >> ~/.bashrc
2、命令式对象配置
命令式对象配置就是使用命令配合配置文件一起来操作kubernetes资源
搜索官方文档:https://kubernetes.io/doc (建议Google搜索)


点开pod资源

使用pods的模版,复制此模板

(1)创建一个nginxpod.yaml
powershell
[root@master ~]# vim nginxpod.yaml
[root@master ~]# cat nginxpod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
(2)执行create命令,创建资源
powershell
[root@master ~]# kubectl create -f nginxpod.yaml
pod/nginx created
(3)执行get命令,查看资源
powershell
[root@master ~]# kubectl get -f nginxpod.yaml
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 16s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 27s
nginx-pod 1/1 Running 0 41m
(4)执行delete命令,删除资源
powershell
[root@master ~]# kubectl delete -f nginxpod.yaml
pod "nginx" deleted
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-pod 1/1 Running 0 41m
[root@master ~]# kubectl get -f nginxpod.yaml
Error from server (NotFound): pods "nginx" not found
总结:命令式对象配置的方式操作资源,可以简单的认为:命令+yaml配置文件(里面是命令需要的各种参数)
3、声明式对象配置
声明式对象配置跟命令式对象配置很相似,但是它只有一个命令apply。
(1)首先执行一次kubectl apply -f yaml文件,发现创建了资源
powershell
[root@master ~]# kubectl apply -f nginxpod.yaml
pod/nginx created
[root@master ~]# kubectl get -f nginxpod.yaml
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 121m
(2)再执行一次kubectl apply -f yaml文件,发现说资源没有变动
powershell
[root@master ~]# kubectl apply -f nginxpod.yaml
pod/nginx unchanged
总结:
其实声明式对象配置就是使用apply描述一个资源最终的状态(在yaml中定义状态)
使用apply操作资源:
如果资源不存在,就创建,相当于kubectl create
如果资源已存在,就更新,就相当于kubectl patch
实现更新效果
(1)更改yaml文件内容
powershell
[root@master ~]# vim nginxpod.yaml
[root@master ~]# cat nginxpod.yaml
apiVersion: v1
kind: Namespace
metadata:
name: luoqi
---
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: luoqi
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
(2)再执行一次kubectl apply -f yaml文件(资源更新了)
powershell
[root@master ~]# kubectl apply -f nginxpod.yaml
namespace/luoqi created
pod/nginx created
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 20h
kube-node-lease Active 20h
kube-public Active 20h
kube-system Active 20h
luoqi Active 43s
[root@master ~]# kubectl get pods -n luoqi
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 64s
(3)执行delete命令,删除资源
powershell
[root@master ~]# kubectl delete -f nginxpod.yaml
namespace "luoqi" deleted
pod "nginx" deleted
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 20h
kube-node-lease Active 20h
kube-public Active 20h
kube-system Active 20h
扩展:
(1)查看全部pod
powershell
[root@master ~]# kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx 1/1 Running 0 132m
default nginx-pod 1/1 Running 0 3h
kube-system calico-kube-controllers-9d57d8f49-5zfvc 1/1 Running 1 (3h41m ago) 20h
......
(2)查看集群中所有 Pod 的详细信息,其中 -o wide 选项会显示更全面的输出
powershell
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 133m 172.16.166.131 node1 <none> <none>
nginx-pod 1/1 Running 0 3h1m 172.16.104.1 node2 <none> <none>