kubernets-基本使用
作者:雪庆华
原创作品,严禁转载!
1.资源清单的结构
apiVersion:资源的API版本号。固定的,但定义时必须声明。
kind:资源的类型。固定的,但定义时必须声明。
metadata:元数据资源,用于描述资源的信息。包括但不限于: name,labels,namespace,annotations。
spec:期望资源的运行状态。
status:资源的实际状态,由K8S组件自行维护。书写时可忽略
2.资源清单使用案例
2.1.创建工作目录
[root@master231 ~]# mkdir -pv /huazai/manifests/pods
[root@master231 ~]# cd /huazai/manifests/pods
2.2.编写资源清单
[root@master231 pods]# cat 01-pods-xiuxian.yaml
# 声明资源的版本号
apiVersion: v1
# 声明资源的类型
kind: Pod
# 声明资源的元数据信息
metadata:
# 声明资源的名称
name: huazai-v1-xiuxian
# 定义资源的期望状态
spec:
# 定义容器的相关配置
containers:
# 定义容器的名称
- name: c1
# 定义镜像的名称
image: registry.cn-hangzhou.aliyuncs.com/huazai007-k8s/apps:v1
#这又有个坑,说明文档没有写必填镜像,但是如果不填就会报错
2.3.创建资源
方式一
[root@master231 pods]# kubectl create -f 01-pods-xiuxian.yaml
pod/huazai-v1-xiuxian created
[root@master231 pods]#
[root@master231 pods]# kubectl create -f 01-pods-xiuxian.yaml # 非"幂等性"
Error from server (AlreadyExists): error when creating "01-pods-xiuxian.yaml": pods "huazai-v1-xiuxian" already exists
方式二(推荐)
[root@master231 pods]# kubectl apply -f 01-pods-xiuxian.yaml
# "幂等性",如果资源存在则尝试更新,如果不存在则创建。
pod/huazai-v1-xiuxian unchanged
[root@master231 pods]#
[root@master231 pods]# kubectl apply -f 01-pods-xiuxian.yaml
pod/huazai-v1-xiuxian unchanged
2.4.查看资源
1 查看Pod资源列表
[root@master231 pods]# kubectl get pods
NAME READY STATUS RESTARTS AGE
huazai-v1-xiuxian 1/1 Running 0 5m27s
2 以长格式显示资源列表
[root@master231 pods]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
huazai-v1-xiuxian 1/1 Running 0 5m34s 10.100.2.5 worker233 <none> <none>
相关资源说明:
NAME
资源的名称。
READY:
以"/"为分隔符,右侧的数字表示Pod有多少个容器。左侧的数字表示Pod内有多少个容器处于运行状态。
STATUS:
资源是否处于就绪状态,目前认为"Running"就表示资源处于正常运行状态。
RESTARTS:
Pod内容器的重启次数总和。
AGE:
表示该资源创建的时间统计。
IP:
表示Pod的IP地址。
NODE:
表示Pod调度到哪个worker node节点。
2.5.删除资源
[root@master231 pods]# kubectl delete -f 01-pods-xiuxian.yaml
pod "huazai-v1-xiuxian" deleted
[root@master231 pods]# kubectl get pods -o wide
No resources found in default namespace.
3. 基于标签的增删改查
1.资源没有标签
[root@master231 pods]# cat 02-pods-xiuxian-labels.yaml
apiVersion: v1
kind: Pod
metadata:
name: huazai-v1-xiuxian-labels
# 给资源打标签,标签的key和value都可以自定义
#labels:
# school: huazai
# class: v1
spec:
containers:
- name: c1
image: registry.cn-hangzhou.aliyuncs.com/huazai007-k8s/apps:v1
[root@master231 pods]#
[root@master231 pods]# kubectl apply -f 02-pods-xiuxian-labels.yaml
pod/huazai-v1-xiuxian-labels created
[root@master231 pods]#
[root@master231 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
huazai-v1-xiuxian-labels 1/1 Running 0 12s <none>
[root@master231 pods]#
2.编写资源清单
[root@master231 pods]# cat 02-pods-xiuxian-labels.yaml
apiVersion: v1
kind: Pod
metadata:
name: huazai-v1-xiuxian-labels
# 给资源打标签,标签的key和value都可以自定义
labels:
school: huazai
class: v1
spec:
containers:
- name: c1
image: registry.cn-hangzhou.aliyuncs.com/huazai007-k8s/apps:v1
[root@master231 pods]#
3.给资源打标签
[root@master231 pods]# kubectl apply -f 02-pods-xiuxian-labels.yaml
pod/huazai-v1-xiuxian-labels configured
[root@master231 pods]#
[root@master231 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
huazai-v1-xiuxian-labels 1/1 Running 0 73s class=v1,school=huazai
[root@master231 pods]#
4.基于标签匹配Pod,从而删除Pod
[root@master231 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
huazai-v1-xiuxian-labels 1/1 Running 0 4m13s class=v1,school=huazai
[root@master231 pods]#
[root@master231 pods]# kubectl delete pods -l class=v1
pod "huazai-v1-xiuxian-labels" deleted
[root@master231 pods]#
[root@master231 pods]# kubectl get pods --show-labels
No resources found in default namespace.
[root@master231 pods]#
5.基于名字删除
[root@master231 pods]# kubectl apply -f 02-pods-xiuxian-labels.yaml
pod/huazai-v1-xiuxian-labels created
[root@master231 pods]#
[root@master231 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
huazai-v1-xiuxian-labels 1/1 Running 0 5s class=v1,school=huazai
[root@master231 pods]#
[root@master231 pods]# cat 02-pods-xiuxian-labels.yaml
apiVersion: v1
kind: Pod
metadata:
name: huazai-v1-xiuxian-labels
# 给资源打标签,标签的key和value都可以自定义
#labels:
# school: huazai
# class: v1
spec:
containers:
- name: c1
image: registry.cn-hangzhou.aliyuncs.com/huazai007-k8s/apps:v1
[root@master231 pods]#
[root@master231 pods]# kubectl apply -f 02-pods-xiuxian-labels.yaml
pod/huazai-v1-xiuxian-labels configured
[root@master231 pods]#
[root@master231 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
huazai-v1-xiuxian-labels 1/1 Running 0 71s <none>
[root@master231 pods]#
6.基于资源的名称删除Pod
[root@master231 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
huazai-v1-xiuxian-labels 1/1 Running 0 2m31s <none>
[root@master231 pods]#
[root@master231 pods]# kubectl delete pod huazai-v1-xiuxian-labels
pod "huazai-v1-xiuxian-labels" deleted
[root@master231 pods]#
[root@master231 pods]# kubectl get pods --show-labels
No resources found in default namespace.
[root@master231 pods]#
4.响应式管理Pod资源
1.创建Pod资源
[root@master231 pods]# kubectl run xiuxian --image=registry.cn-hangzhou.aliyuncs.com/huazai007-k8s/apps:v1
pod/xiuxian created
2.查看Pod资源
[root@master231 pods]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
xiuxian 1/1 Running 0 3s 10.100.2.8 worker233 <none> <none>
[root@master231 pods]#
[root@master231 pods]# curl 10.100.2.8
3.给资源打标签
[root@master231 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 2m25s run=xiuxian
[root@master231 pods]#
[root@master231 pods]# kubectl label pod xiuxian class=v1 school=huazai
pod/xiuxian labeled
[root@master231 pods]#
[root@master231 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 2m35s class=v1,run=xiuxian,school=huazai
4.修改资源的标签
[root@master231 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 3m16s class=v1,run=xiuxian,school=huazai
[root@master231 pods]#
[root@master231 pods]#
[root@master231 pods]# kubectl label pod xiuxian school=laonanhai --overwrite
pod/xiuxian labeled
[root@master231 pods]#
[root@master231 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 4m43s class=v1,run=xiuxian,school=laonanhai
[root@master231 pods]#
5.删除Pod资源
[root@master231 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 5m32s class=v1,run=xiuxian,school=laonanhai
xiuxian2 1/1 Running 0 2s run=xiuxian2
[root@master231 pods]#
[root@master231 pods]#
[root@master231 pods]# kubectl delete pod --all
pod "xiuxian" deleted
pod "xiuxian2" deleted
[root@master231 pods]#
[root@master231 pods]# kubectl get pods --show-labels
No resources found in default namespace.
[root@master231 pods]#
5.将多个资源合并为一个资源清单
[root@master231 pods]# cat 05-pods-all-in-one-file.yaml
apiVersion: v1
kind: Pod
metadata:
name: xiuxian-v1
labels:
apps: v1
spec:
hostNetwork: true
nodeName: worker232
containers:
- image: registry.cn-hangzhou.aliyuncs.com/huazai007-k8s/apps:v1
name: xiuxian
# 此处的"---"表示一个文件的结束
---
apiVersion: v1
kind: Pod
metadata:
name: xiuxian-v2
labels:
apps: v2
spec:
hostNetwork: true
nodeName: worker233
containers:
- image: registry.cn-hangzhou.aliyuncs.com/huazai007-k8s/apps:v2
name: xiuxian
[root@master231 pods]#
[root@master231 pods]#
[root@master231 pods]# kubectl apply -f 05-pods-all-in-one-file.yaml
pod/xiuxian-v1 created
pod/xiuxian-v2 created
[root@master231 pods]#
[root@master231 pods]# kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
xiuxian-v1 1/1 Running 0 6s 10.0.0.232 worker232 <none> <none> apps=v1
xiuxian-v2 1/1 Running 0 6s 10.0.0.233 worker233 <none> <none> apps=v2
[root@master231 pods]#
[root@master231 pods]# kubectl delete pods -l apps
pod "xiuxian-v1" deleted
pod "xiuxian-v2" deleted
[root@master231 pods]#
[root@master231 pods]# kubectl get pods -o wide --show-labels
No resources found in default namespace.
[root@master231 pods]#
标签:
kubernetes
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)