一、kubectl概述
kubectl 是 Kubernetes 集群的命令行工具,通过 kubectl 能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署。Kubectl的配置文件在$HOME/.kube目录。我们可以通过设置KUBECONFIG环境变量或设置命令参数--kubeconfig来指定其他位置的kubeconfig文件。
二、kubectl命令的语法
1.语法
kubectl [command] [TYPE] [NAME] [flags]
官网地址:https://kubernetes.io/docs/reference/kubectl/overview/
kubectl flags:https://kubernetes.io/docs/reference/kubectl/kubectl/
2.command
指定要在一个或多个资源进行的操作,例如create,get,describe,delete。例如“kubectl get cs”。
3.TYPE
指定资源类型,资源类型在YAML文件中是大小写敏感的,资源类型在命令行是不区分大小写的,开发者能够以单数、复数和缩略的形式。例如:
#以下命令产生相同的输出
kubectl get pod -n kubernetes-dashboard
kubectl get pods -n kubernetes-dashboard
kubectl get po -n kubernetes-dashboard
4.NAME
指定资源的名称,名称也大小写敏感的。如果省略名称空间,则显示默认名称空间资源的详细信息或者提示:No resources found in default namespace.。
# 示例:
[root@k8s-master-001 ~]# kubectl get pods
No resources found in default namespace.
[root@k8s-master-001 ~]# kubectl get pods --all-namespaces # 或者 kubectl get pods --A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-6955765f44-c9zfh 1/1 Running 8 6d7h
kube-system coredns-6955765f44-lrz5q 1/1 Running 8 6d7h
kube-system etcd-k8s-master-001 1/1 Running 9 6d7h
kube-system kube-apiserver-k8s-master-001 1/1 Running 9 6d7h
kube-system kube-controller-manager-k8s-master-001 1/1 Running 8 6d7h
kube-system kube-flannel-ds-amd64-dngrk 1/1 Running 13 6d7h
kube-system kube-flannel-ds-amd64-h4sn6 1/1 Running 13 6d6h
kube-system kube-flannel-ds-amd64-m92wp 1/1 Running 11 6d6h
kube-system kube-proxy-28dwj 1/1 Running 9 6d6h
kube-system kube-proxy-c875m 1/1 Running 8 6d7h
kube-system kube-proxy-stg6w 1/1 Running 10 6d6h
kube-system kube-scheduler-k8s-master-001 1/1 Running 9 6d7h
kubernetes-dashboard dashboard-metrics-scraper-7b8b58dc8b-nr5fz 1/1 Running 7 6d1h
kubernetes-dashboard kubernetes-dashboard-755dcb9575-9kg7p 1/1 Running 9 6d1h
[root@k8s-master-001 ~]# kubectl get service --all-namespaces # 或者 kubectl get service -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6d7h
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 6d7h
kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.104.12.221 <none> 8000/TCP 6d1h
kubernetes-dashboard kubernetes-dashboard NodePort 10.110.157.29 <none> 443:30001/TCP 6d1h
5.flags
指定可选的标记。例如,可以使用 -s 或 --server标识来指定Kubernetes API服务器的地址和端口;-n指定名称空间;等等。
注意:你从命令行指定的flags将覆盖默认值和任何相应的环境变量。优先级最高。
6.多资源操作
#1.按类型和名称指定资源:
1)查看一个资源类型中的多个资源
[root@k8s-master ~]# kubectl get pod -n kube-system coredns-6d99d5879f-2pvbr kube-proxy-28dwj
NAME READY STATUS RESTARTS AGE
coredns-6d99d5879f-2pvbr 1/1 Running 1 3d17h
kube-proxy-28dwj 1/1 Running 9 3d15h
2)查看多个资源类型
[root@k8s-master-001 ~]# kubectl get svc,node
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d19h
service/nginx NodePort 10.96.95.61 <none> 80:46847/TCP 3d16h
NAME STATUS ROLES AGE VERSION
node/k8s-master-001 Ready master 3d18h v1.18.8
node/k8s-master-002 Ready master 3d18h v1.18.8
node/k8s-master-003 Ready master 3d18h v1.18.8
node/k8s-node-001 Ready node 3d16h v1.18.8
node/k8s-node-002 Ready node 3d16h v1.18.8
#2.使用一个或多个文件指定资源:-f file1 -f file2 -f file<#>
使用YAML而不是JSON,因为YAML更容易使用,特别是对于配置文件。
[root@k8s-master-001 ~]# kubectl get pod -f pod.yaml
三、kubectl查看类命令
1.获取节点和服务版本信息
[root@k8s-master-001 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master-001 Ready master 3d18h v1.18.8
k8s-master-002 Ready master 3d18h v1.18.8
k8s-master-003 Ready master 3d18h v1.18.8
k8s-node-001 Ready node 3d16h v1.18.8
k8s-node-002 Ready node 3d16h v1.18.8
2.获取节点和服务版本信息,并查看附加信息
[root@k8s-master-001 ~]# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k8s-master-001 Ready master 3d18h v1.18.8 172.16.1.110 <none> CentOS Linux 7 (Core) 5.4.147-1.el7.elrepo.x86_64 docker://20.10.8
k8s-master-002 Ready master 3d18h v1.18.8 172.16.1.111 <none> CentOS Linux 7 (Core) 5.4.147-1.el7.elrepo.x86_64 docker://20.10.8
k8s-master-003 Ready master 3d18h v1.18.8 172.16.1.112 <none> CentOS Linux 7 (Core) 5.4.147-1.el7.elrepo.x86_64 docker://20.10.8
k8s-node-001 Ready node 3d16h v1.18.8 172.16.1.113 <none> CentOS Linux 7 (Core) 5.4.147-1.el7.elrepo.x86_64 docker://20.10.8
k8s-node-002 Ready node 3d16h v1.18.8 172.16.1.114 <none> CentOS Linux 7 (Core) 5.4.147-1.el7.elrepo.x86_64 docker://20.10.8
3.获取pod信息,默认是default名称空间
[root@k8s-master-001 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-sd6bq 1/1 Running 1 3d16h
4.获取pod信息
#1.获取pod信息,默认是default名称空间,并查看附加信息【如:pod的IP及在哪个节点运行】
[root@k8s-master-001 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-f89759699-sd6bq 1/1 Running 1 3d16h 10.241.184.2 k8s-node-002 <none> <none>
#2.获取指定名称空间的pod
[root@k8s-master-001 ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-6d99d5879f-2pvbr 1/1 Running 1 3d23h
#3.获取指定名称空间中的指定pod
[root@k8s-master-001 ~]# kubectl get pod -n kube-system coredns-6d99d5879f-2pvbr
NAME READY STATUS RESTARTS AGE
coredns-6d99d5879f-2pvbr 1/1 Running 1 3d23h
#4.获取所有名称空间的pod
[root@k8s-master-001 ~]# kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx-f89759699-sd6bq 1/1 Running 1 3d22h
kube-system coredns-6d99d5879f-2pvbr 1/1 Running 1 3d23h
#5.查看pod的详细信息,以yaml格式或json格式显示
[root@k8s-master-001 ~]# kubectl get pods -o yaml
[root@k8s-master-001 ~]# kubectl get pods -o json
#6.查看pod的标签信息
[root@k8s-master-001 ~]# kubectl get pod -A --show-labels
NAMESPACE NAME READY STATUS RESTARTS AGE LABELS
default nginx-f89759699-sd6bq 1/1 Running 1 3d22h app=nginx,pod-template-hash=f89759699
kube-system coredns-6d99d5879f-2pvbr 1/1 Running 1 3d23h k8s-app=kube-dns,pod-template-hash=6d99d5879f
#7.根据Selector(label query)来查询pod
[root@k8s-master-001 ~]# kubectl get pod -A --selector='k8s-app=kube-dns'
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-6d99d5879f-2pvbr 1/1 Running 1 4d
#8.查看运行pod的环境变量
[root@k8s-master-001 ~]# kubectl exec nginx-f89759699-sd6bq -- env
#9.查看指定pod的日志
[root@k8s-master-001 ~]# kubectl logs -f --tail 100 -n kube-system coredns-6d99d5879f-2pvbr
5.获取service信息
#1.查看所有名称空间的service信息
[root@k8s-master-001 ~]# kubectl get svc -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d2h
default nginx NodePort 10.96.95.61 <none> 80:46847/TCP 3d23h
kube-system kube-dns ClusterIP 10.96.0.2 <none> 53/UDP,53/TCP,9153/TCP 4d1h
#2.查看默认名称空间的service信息
[root@k8s-master-001 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d2h
nginx NodePort 10.96.95.61 <none> 80:46847/TCP 3d23h
#3.查看指定名称空间的service信息
[root@k8s-master-001 ~]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.2 <none> 53/UDP,53/TCP,9153/TCP 4d1h
6.查看状态信息
#1.查看component statuses信息(组件状态)
[root@k8s-master-001 ~]# kubectl get cs
#2.查看所有config maps信息(配置映射)
[root@k8s-master-001 ~]# kubectl get cm -A
#3.查看所有service accounts信息(服务账号)
[root@k8s-master-001 ~]# kubectl get sa -A
#4.查看所有daemonsets信息(守护程序)
[root@k8s-master-001 ~]# kubectl get ds -A
#5.查看所有deployments信息(部署应用)
[root@k8s-master-001 ~]# kubectl get deploy -A
#6.查看所有replicasets信息(复制集)
[root@k8s-master-001 ~]# kubectl get rs -A
#7.查看所有statefulsets信息
[root@k8s-master-001 ~]# kubectl get sts -A
#8.查看所有jobs信息
[root@k8s-master-001 ~]# kubectl get jobs -A
#9.查看所有ingresses信息
[root@k8s-master-001 ~]# kubectl get jobs -A
#10.查看有哪些名称空间
[root@k8s-master-001 ~]# kubectl get ns
7. 查看pod的描述信息
[root@k8s-master-001 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-sd6bq 1/1 Running 1 4d15h
#1.查看pod的描述信息
[root@k8s-master-001 ~]# kubectl describe pod nginx-f89759699-sd6bq
[root@k8s-master-001 ~]# kubectl describe pod -n kube-system coredns-6d99d5879f-2pvbr
#2.查看指定名称空间中指定deploy的描述信息
[root@k8s-master-001 ~]# kubectl describe deploy -n kube-system coredns
#3.查看node或pod的资源使用情况(需要heapster 或metrics-server支持)
[root@k8s-master-001 ~]# kubectl top node
[root@k8s-master-001 ~]# kubectl top pod
8.查看集群信息
#1.查看集群信息
[root@k8s-master-001 ~]# kubectl cluster-info
或者
[root@k8s-master-001 ~]# kubectl cluster-info dump
#2.查看各组件信息【172.16.1.110为master机器】
kubectl -s https://172.16.1.110:6443 get componentstatuses
四、kubectl操作类命令
# 创建资源
kubectl create -f xxx.yaml
# 应用资源
kubectl apply -f xxx.yaml
# 应用资源,该目录下的所有 .yaml, .yml, 或 .json 文件都会被使用
kubectl apply -f <directory>
# 创建test名称空间
kubectl create namespace test
# 删除资源
kubectl delete -f xxx.yaml
kubectl delete -f <directory>
# 删除指定的pod
kubectl delete pod podName
# 删除指定名称空间的指定pod
kubectl delete pod -n test podName
# 删除其他资源
kubectl delete svc svcName
kubectl delete deploy deployName
kubectl delete ns nsName
# 强制删除
kubectl delete pod podName -n nsName --grace-period=0 --force
kubectl delete pod podName -n nsName --grace-period=1
kubectl delete pod podName -n nsName --now
# 编辑资源
kubectl edit pod podName
五、kubectl进阶命令操作
1.进入pod启动的容器
# kubectl exec:进入pod启动的容器
kubectl exec -it podName -n nsName /bin/sh #进入容器
kubectl exec -it podName -n nsName /bin/bash #进入容器
2.添加label值
# kubectl label:添加label值
kubectl label nodes k8s-node01 zone=north #为指定节点添加标签
kubectl label nodes k8s-node01 zone- #为指定节点删除标签
kubectl label pod podName -n nsName role-name=test #为指定pod添加标签
kubectl label pod podName -n nsName role-name=dev --overwrite #修改lable标签值
kubectl label pod podName -n nsName role-name- #删除lable标签
3.滚动升级
[root@k8s-master-001 ~]# vi myapp-deployment-v1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
app: myapp
spec:
replicas: 10
# 重点关注该字段
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1
ports:
- containerPort: 80
[root@k8s-master-001 ~]# vi myapp-deployment-v2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
app: myapp
spec:
replicas: 10
# 重点关注该字段
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v2
ports:
- containerPort: 80
# kubectl滚动升级; 通过 kubectl apply -f myapp-deployment-v1.yaml 启动deploy
kubectl apply -f myapp-deployment-v2.yaml #通过配置文件滚动升级
kubectl set image deploy/myapp-deployment myapp="registry.cn-beijing.aliyuncs.com/google_registry/myapp:v3" #通过命令滚动升级
kubectl rollout undo deploy/myapp-deployment 或者 kubectl rollout undo deploy myapp-deployment #pod回滚到前一个版本
kubectl rollout undo deploy/myapp-deployment --to-revision=2 #回滚到指定历史版本
4.动态伸缩
# kubectl scale:动态伸缩
kubectl scale deploy myapp-deployment --replicas=5 # 动态伸缩
kubectl scale --replicas=8 -f myapp-deployment-v2.yaml #动态伸缩【根据资源类型和名称伸缩,其他配置「如:镜像版本不同」不生效】
六、kubectl help 获取更多信息
官网地址查看更多详情:
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
https://kubernetes.io/docs/reference/kubectl/overview/#operations
1.kubectl语法中的command操作
Operation |
Syntax |
Description |
create |
kubectl create -f FILENAME [flags] |
从文件或标准输入创建一个或多个资源★★★ |
expose |
kubectl expose (-f FILENAME |
TYPE NAME |
run |
kubectl run NAME --image=image [–env=“key=value”][–port=port] [–replicas=replicas][–dry-run=bool] [–overrides=inline-json][flags] |
在集群上运行指定的镜像★★★ |
explain |
kubectl explain [–recursive=false][flags] |
获取各种资源的文档。例如pods、nodes、services等。★★★★★ |
get |
kubectl get (-f FILENAME |
TYPE [NAME |
edit |
kubectl edit (-f FILENAME |
TYPE NAME |
delete |
kubectl delete (-f FILENAME |
TYPE [NAME |
rollout |
kubectl rollout SUBCOMMAND [options] |
对资源进行管理。有效的资源类型包括:deployments,daemonsets 和statefulsets |
scale |
kubectl scale (-f FILENAME |
TYPE NAME |
autoscale |
kubectl autoscale (-f FILENAME |
TYPE NAME |
cluster-info |
kubectl cluster-info [flags] |
显示集群信息,显示关于集群中的主机和服务的端点信息。★★★ |
top |
kubectl top node、kubectl top pod 需要heapster 或metrics-server支持 |
显示资源(CPU/内存/存储)使用情况★★★ |
cordon |
kubectl cordon NODE [options] |
将node标记为不可调度 |
uncordon |
kubectl uncordon NODE [options] |
将node标记为可调度 |
drain |
kubectl drain NODE [options] |
排除指定node节点,为维护做准备 |
taint |
kubectl taint NODE NAME KEY_1=VAL_1:TAINT_EFFECT_1 … KEY_N=VAL_N:TAINT_EFFECT_N [options] |
更新一个或多个节点上的污点★★★ |
describe |
kubectl describe (-f FILENAME |
TYPE [NAME_PREFIX |
logs |
kubectl logs POD [-c CONTAINER][–follow] [flags] |
打印pod中一个容器的日志★★★★★ |
exec |
kubectl exec POD [-c CONTAINER][-i] [-t][flags] [-- COMMAND [args…]] |
对pod中的容器执行命令或进入Pod容器★★★★★ |
proxy |
kubectl proxy [–port=PORT][–www=static-dir] [–www-prefix=prefix][–api-prefix=prefix] [flags] |
运行Kubernetes API服务的代理 |
cp |
kubectl cp [options] |
从宿主机复制文件和目录到一个容器;或则从容器中复制文件和目录到宿主机★★★ |
auth |
kubectl auth [flags][options] |
检查授权 |
apply |
kubectl apply -f FILENAME [flags] |
通过文件名中的内容或stdin将配置应用于资源★★★★★ |
patch |
kubectl patch (-f FILENAME |
TYPE NAME |
replace |
kubectl replace -f FILENAME |
通过文件或stdin替换资源 |
rolling-update |
kubectl rolling-update OLD_CONTROLLER_NAME ([NEW_CONTROLLER_NAME] –image=NEW_CONTAINER_IMAGE |
-f NEW_CONTROLLER_SPEC) [flags] |
label |
kubectl label (-f FILENAME |
TYPE NAME |
annotate |
kubectl annotate (-f FILENAME |
TYPE NAME |
api-resources |
kubectl api-resources [flags][options] |
打印支持的API资源★★★ |
api-versions |
kubectl api-versions [flags] |
列出可用的API版本★★★ |
config |
kubectl config SUBCOMMAND [flags] |
修改kubeconfig文件。有关详细信息,请参见各个子命令 |
plugin |
kubectl plugin [flags][options] |
提供与插件交互的实用工具 |
version |
kubectl version [–client][flags] |
显示在客户端和服务器上运行的Kubernetes版本★★★ |
2.kubectl语法中的TYPE资源
下表包含常用的资源类型及其缩写别名的列表。
Resource Name |
Short Names |
Namespaced |
Resource Kind |
bindings |
|
TRUE |
Binding |
componentstatuses |
cs |
FALSE |
ComponentStatus |
configmaps |
cm |
TRUE |
ConfigMap |
endpoints |
ep |
TRUE |
Endpoints |
events |
ev |
TRUE |
Event |
limitranges |
limits |
TRUE |
LimitRange |
namespaces |
ns |
FALSE |
Namespace |
nodes |
no |
FALSE |
Node |
persistentvolumeclaims |
pvc |
TRUE |
PersistentVolumeClaim |
persistentvolumes |
pv |
FALSE |
PersistentVolume |
pods |
po |
TRUE |
Pod |
podtemplates |
|
TRUE |
PodTemplate |
replicationcontrollers |
rc |
TRUE |
ReplicationController |
resourcequotas |
quota |
TRUE |
ResourceQuota |
secrets |
|
TRUE |
Secret |
serviceaccounts |
sa |
TRUE |
ServiceAccount |
services |
svc |
TRUE |
Service |
mutatingwebhookconfigurations |
|
FALSE |
MutatingWebhookConfiguration |
validatingwebhookconfigurations |
|
FALSE |
ValidatingWebhookConfiguration |
customresourcedefinitions |
crd, crds |
FALSE |
CustomResourceDefinition |
apiservices |
|
FALSE |
APIService |
controllerrevisions |
|
TRUE |
ControllerRevision |
daemonsets |
ds |
TRUE |
DaemonSet |
deployments |
deploy |
TRUE |
Deployment |
replicasets |
rs |
TRUE |
ReplicaSet |
statefulsets |
sts |
TRUE |
StatefulSet |
tokenreviews |
|
FALSE |
TokenReview |
localsubjectaccessreviews |
|
TRUE |
LocalSubjectAccessReview |
selfsubjectaccessreviews |
|
FALSE |
SelfSubjectAccessReview |
selfsubjectrulesreviews |
|
FALSE |
SelfSubjectRulesReview |
subjectaccessreviews |
|
FALSE |
SubjectAccessReview |
horizontalpodautoscalers |
hpa |
TRUE |
HorizontalPodAutoscaler |
cronjobs |
cj |
TRUE |
CronJob |
jobs |
|
TRUE |
Job |
certificatesigningrequests |
csr |
FALSE |
CertificateSigningRequest |
leases |
|
TRUE |
Lease |
endpointslices |
|
TRUE |
EndpointSlice |
events |
ev |
TRUE |
Event |
ingresses |
ing |
TRUE |
Ingress |
networkpolicies |
netpol |
TRUE |
NetworkPolicy |
runtimeclasses |
|
FALSE |
RuntimeClass |
poddisruptionbudgets |
pdb |
TRUE |
PodDisruptionBudget |
podsecuritypolicies |
psp |
FALSE |
PodSecurityPolicy |
clusterrolebindings |
|
FALSE |
ClusterRoleBinding |
clusterroles |
|
FALSE |
ClusterRole |
rolebindings |
|
TRUE |
RoleBinding |
roles |
|
TRUE |
Role |
priorityclasses |
pc |
FALSE |
PriorityClass |
csidrivers |
|
FALSE |
CSIDriver |
csinodes |
|
FALSE |
CSINode |
storageclasses |
sc |
FALSE |
StorageClass |
volumeattachments |
|
FALSE |
VolumeAttachment |
3.kubectl 子命令使用分类
#1.基础命令
#2.部署和集群管理命令
#3.故障和调试命令
#4.其他命令
七、kubectl输出选项
1.概述
所有kubectl命令的默认输出格式是人类可读的纯文本格式。
要将详细信息以特定的格式输出到终端窗口,可以将 -o 或 --output标识添加到受支持的kubectl命令中。
2.语法
kubectl [command] [TYPE] [NAME] -o <output_format>
根据kubectl操作,支持以下输出格式:
Output format |
Description |
-o custom-columns= |
使用逗号分隔的自定义列列表打印表 |
-o custom-columns-file= |
使用文件中的自定义列模板打印表 |
-o json |
输出一个JSON格式的API对象 |
-o jsonpath= |
打印jsonpath表达式中定义的字段 |
-o jsonpath-file= |
通过文件打印jsonpath表达式定义的字段 |
-o name |
只打印资源名,不打印其他任何内容 |
-o wide |
以纯文本格式输出,包含附加信息。对于pods,包含节点名 |
-o yaml |
输出一个YAML格式的API对象 |
3.示例
#1.wide示例
[root@k8s-master-001 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-sd6bq 1/1 Running 1 5d
[root@k8s-master-001 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-f89759699-sd6bq 1/1 Running 1 5d 10.241.184.2 k8s-node-002 <none> <none>
#2.yaml示例
[root@k8s-master-001 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-sd6bq 1/1 Running 1 5d
[root@k8s-master-001 ~]# kubectl get pod -o yaml
apiVersion: v1
items:
- apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2021-09-22T10:22:13Z"
generateName: nginx-f89759699-
labels:
app: nginx
pod-template-hash: f89759699
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
………………
#3.json示例
[root@k8s-master-001 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-sd6bq 1/1 Running 1 5d
[root@k8s-master ~]# kubectl get pod -o json
{
"apiVersion": "v1",
"items": [
{[root@k8s-master-001 ~]# kubectl get pod -o json
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"creationTimestamp": "2021-09-22T10:22:13Z",
"generateName": "nginx-f89759699-",
"labels": {
"app": "nginx",
………………
#4.name示例
[root@k8s-master-001 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-sd6bq 1/1 Running 1 5d
[root@k8s-master-001 ~]# kubectl get pod -o name
pod/nginx-f89759699-sd6bq
#5.custom-columns示例
[root@k8s-master-001 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-sd6bq 1/1 Running 1 5d
[root@k8s-master-001 ~]# kubectl get pods -o custom-columns=NAME:.metadata.name,UID:.metadata.uid,imageName:.spec.containers[0].image
NAME UID imageName
nginx-f89759699-sd6bq 069dc771-65f2-47d4-9ff9-866297a69f06 nginx
说明:custom-columns=key:value;其中key表示列明;value表示要显示信息,这个value信息可以通过-o json或-o yaml获取。
#6.custom-columns-file示例
[root@k8s-master-001 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-sd6bq 1/1 Running 1 5d
# 要显示的列明和数据来源
[root@k8s-master-001 ~]# cat custom-col.conf
NAME UID imageName containerPort
metadata.name metadata.uid spec.containers[0].image spec.containers[0].ports[0].containerPort
[root@k8s-master-001 ~]# kubectl get pod -o custom-columns-file=custom-col.conf
NAME UID imageName containerPort
nginx-f89759699-sd6bq 069dc771-65f2-47d4-9ff9-866297a69f06 registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 80
#7.jsonpath示例
[root@k8s-master-001 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-sd6bq 1/1 Running 1 5d
[root@k8s-master-001 ~]# kubectl get pods -o jsonpath='{.items[0].metadata.name},{.items[0].spec.containers[0].image}'
nginx-f89759699-sd6bq,nginx
#8.jsonpath-file示例
[root@k8s-master-001 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-sd6bq 1/1 Running 1 5d
# 要显示的数据来源
[root@k8s-master-001 ~]# cat custom-json.conf
{.items[0].metadata.name},{.items[0].spec.containers[0].image},{.items[0].spec.containers[0].ports[0].containerPort}
[root@k8s-master-001 ~]# kubectl get pod -o jsonpath-file=custom-json.conf
nginx-f89759699-sd6bq,registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17,80