K8s命令
查看所有命名空间
[root@master ~]# kubectl get ns
NAME STATUS AGE
cy Active 22h
default Active 41h
kube-node-lease Active 41h
kube-public Active 41h
kube-system Active 41h
xy Active 13h
查看指定ns
[root@master ~]# kubectl get ns xy
NAME STATUS AGE
xy Active 13h
查看ns的yaml格式
[root@master ~]# kubectl get ns xy -o yaml
apiVersion: v1
kind: Namespace
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"name":"xy"}}
creationTimestamp: "2025-10-29T11:45:27Z"
labels:
kubernetes.io/metadata.name: xy
name: xy
resourceVersion: "30668"
uid: f70ca315-1aec-44c3-a530-f4a3635df45f
spec:
finalizers:
- kubernetes
status:
phase: Active
详细查看ns
[root@master ~]# kubectl describe ns xy
Name: xy
Labels: kubernetes.io/metadata.name=xy
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
创建ns
[root@master ~]# kubectl create ns test1
namespace/test1 created
[root@master ~]# kubectl get ns
NAME STATUS AGE
cy Active 22h
default Active 41h
kube-node-lease Active 41h
kube-public Active 41h
kube-system Active 41h
test1 Active 6s
xy Active 13h
删除ns
[root@master ~]# kubectl delete ns test1
namespace "test1" deleted
[root@master ~]# kubectl get ns
NAME STATUS AGE
cy Active 22h
default Active 41h
kube-node-lease Active 41h
kube-public Active 41h
kube-system Active 41h
xy Active 13h
pod
查看集群的各个组件
[root@master ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-9d57d8f49-mndtc 1/1 Running 5 (21m ago) 41h
calico-node-n954f 1/1 Running 6 (21m ago) 41h
calico-node-s4mtv 1/1 Running 6 (21m ago) 41h
calico-node-sk2bk 1/1 Running 6 (21m ago) 41h
coredns-6554b8b87f-mnw6s 1/1 Running 5 (21m ago) 41h
coredns-6554b8b87f-zj9hd 1/1 Running 5 (21m ago) 41h
etcd-master 1/1 Running 6 (21m ago) 41h
kube-apiserver-master 1/1 Running 6 (21m ago) 41h
kube-controller-manager-master 1/1 Running 6 (21m ago) 41h
kube-proxy-76llz 1/1 Running 6 (21m ago) 41h
kube-proxy-mkf5f 1/1 Running 6 (21m ago) 41h
kube-proxy-ztbz2 1/1 Running 6 (21m ago) 41h
kube-scheduler-master 1/1 Running 6 (21m ago) 41h
kubernetes没有提供单独运行Pod的命令,都是通过Pod控制器来实现的
#创建并运行pod
[root@master ~]# kubectl run nginx --image=nginx:latest --port=80 --namespace xy
pod/nginx created
查看pod信息
[root@master ~]# kubectl get pods -n xy
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 48s
查看详细信息
[root@master ~]# kubectl describe pod nginx -n xy
Name: nginx
Namespace: xy
Priority: 0
Service Account: default
Node: node1/192.168.100.10
Start Time: Thu, 30 Oct 2025 15:12:23 +0800
Labels: run=nginx
.....
访问pod
#通过 -o wide能够看到pod的ip
[root@master ~]# kubectl get pods nginx -n xy -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 2m48s 172.16.166.135 node1 <none> <none>
[root@node1 ~]# curl http://172.16.166.135
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
删除pod
[root@master ~]# kubectl delete pod nginx -n xy
pod "nginx" deleted
[root@master ~]# kubectl get pods -n xy
No resources found in xy namespace.
通过pod控制器创建pod
[root@master ~]# kubectl create deployment nginx --image=nginx:latest --port 80 -n xy
deployment.apps/nginx created
#查看pod
[root@master ~]# kubectl get pod -n xy
NAME READY STATUS RESTARTS AGE
nginx-7c79c4bf97-4xgfj 1/1 Running 0 30s
#删除pod
[root@master ~]# kubectl delete pod nginx-7c79c4bf97-4xgfj -n xy
pod "nginx-7c79c4bf97-4xgfj" deleted
#可以看到已经删除了,再次查看pod
[root@master ~]# kubectl get pod -n xy
NAME READY STATUS RESTARTS AGE
nginx-7c79c4bf97-89xzz 1/1 Running 0 28s
#还是有pod,这是因为pod是由pod控制器创建,当前pod故障或死亡时,pod控制器会重建pod,想要删除pod,就要删除pod控制器
#删除pod控制器
[root@master ~]# kubectl delete deploy nginx -n xy
deployment.apps "nginx" deleted
#再次查看pod
[root@master ~]# kubectl get pod -n xy
No resources found in xy namespace.
创建一个test.yaml文件,如果不知道格式可以去官网查
官网:https://kubernetes.io/docs/home/
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
[root@master ~]# kubectl apply -f test.yaml
pod/nginx created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 8s
#没有指定命名空间的pod会放在默认空间里面
Deployment
Deployment是pod控制器的一种
格式:kubectl create deployment 名称 [参数]
--image 指定pod的镜像
--port 指定端口
--replicas 指定创建pod数量
--namespace 指定namespace
[root@master ~]# kubectl create deploy nginx --image=nginx:latest --port=80 --replicas=3 -n xy
deployment.apps/nginx created
查看pod
[root@master ~]# kubectl get pods -n xy
NAME READY STATUS RESTARTS AGE
nginx-7c79c4bf97-2v69g 1/1 Running 0 21s
nginx-7c79c4bf97-5kzn6 1/1 Running 0 21s
nginx-7c79c4bf97-nmbwq 1/1 Running 0 21s
查看deploy的信息
[root@master ~]# kubectl get deploy -n xy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 3/3 3 3 49s
# UP-TO-DATE:成功升级的副本数量
# AVAILABLE:可用副本的数量
[root@master ~]# kubectl get deploy -n xy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 3/3 3 3 94s nginx nginx:latest app=nginx
查看deploy的详细信息
[root@master ~]# kubectl describe deploy nginx -n xy
Name: nginx
Namespace: xy
CreationTimestamp: Thu, 30 Oct 2025 16:10:39 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
.......
删除
[root@master ~]# kubectl delete deploy nginx -n xy
deployment.apps "nginx" deleted
创建一个deploy-nginx.yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx:latest
name: nginx
ports:
- containerPort: 80
protocol: TCP
Label
Label 的特点
| 以 key/value 键值对的形式附加到各种对象上(如 Node、Pod、Service 等) |
|---|
| 一个资源对象可以定义任意数量的 Label |
| 同一个 Label 可以被添加到任意数量的资源对象上 |
| Label 通常在资源对象定义时确定,也可以在对象创建后动态添加或删除 |
| 通过 Label 实现资源的多维度分组,便于资源分配、调度、配置、部署等管理工作 |
Label 的常用示例
| 标签类型 | 示例 |
|---|---|
| 版本标签 | "version":"release", "version":"stable" |
| 环境标签 | "environment":"dev", "environment":"test", "environment":"pro" |
| 架构标签 | "tier":"frontend", "tier":"backend" |
当前有两种Label Selector:
基于等式的Label Selector
name = slave: 选择所有包含Label中key="name"且value="slave"的对象
env != production: 选择所有包括Label中的key="env"且value不等于"production"的对象
基于集合的Label Selector
name in (master, slave): 选择所有包含Label中的key="name"且value="master"或"slave"的对象
name not in (frontend): 选择所有包含Label中的key="name"且value不等于"frontend"的对象
标签的选择条件可以使用多个,此时将多个Label Selector进行组合,使用逗号","进行分隔即可。例如:
name=slave,env!=production
name not in (frontend),env!=production
为pod打标签
[root@master ~]# kubectl label pod nginx t1=10 -n xy
pod/nginx labeled
#查看
[root@master ~]# kubectl get pod nginx -n xy --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 60s t1=10
#更新标签
[root@master ~]# kubectl label pod nginx t1=20 -n xy --overwrite
pod/nginx labeled
#查看
[root@master ~]# kubectl get pod nginx -n xy --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 11m t1=20
#筛选标签
[root@master ~]# kubectl label pod nginx t2=20 -n xy
pod/nginx labeled
[root@master ~]# kubectl get pod -n xy -l t1=20 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 18m t1=20,t2=20
[root@master ~]# kubectl get pod -n xy -l t1!=20 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginxpod 1/1 Running 0 19m <none>
#删除标签
[root@master ~]# kubectl label pod nginx t1- -n xy
pod/nginx unlabeled
#在标签后面加上-