1、pod调度
[root@k8s-master ~]# kubectl get pod -o wide
#-o 是查看更多信息
[root@k8s-master ~]# kubectl get node --show-labels
# 查看各个节点标签
[root@k8s-master ~]# kubectl get pod --show-labels #查看标签信息
NAME READY STATUS RESTARTS AGE LABELS
xiaomi 2/2 Running 5 (<invalid> ago) 2d23h run=xiaomi
zhao-64898f87fb-596s9 1/1 Running 1 (<invalid> ago) 2d1h app=zhao,pod-template-hash=64898f87fb
[root@k8s-master ~]# kubectl get pod -l app=zhao
!!!!!!语法:kubectl get pod -l <key>=<value>
NAME READY STATUS RESTARTS AGE
zhao-64898f87fb-596s9 1/1 Running 1 (<invalid> ago) 2d1h
[root@k8s-master ~]# kubectl describe pod xiaomi #查看某个pod的详细信息
!!!!!!语法:kubectl describe pod <podName>
[root@k8s-master ~]# kubectl api-resources #查看资源缩写
NAME SHORTNAMES APIVERSION NAMESPACED KIND
services svc v1 true Service
deployments deploy apps/v1 true Deployment
componentstatuses cs v1 false ComponentStatus
[root@k8s-master ~]# kubectl get pod,svc,deploy #查看pod,service,deployment信息。
[root@k8s-master ~]# kubectl delete pod <podName> #删除某个具体的pod
[root@k8s-master ~]# kubectl run zhou --image=nginx --dry-run=client -o yaml > zhou.yaml #创建pod的yaml
2、namespace命令
[root@k8s-master ~]# kubectl get namespace #查看命名空间
[root@k8s-master ~]# kubectl create namespace test #创建一个名为test的命名空间
[root@k8s-master ~]# kubectl get pod -n default #查看default命名空间下的所有资源。如果该命名空间下没有资源会给出提示
[root@k8s-master ~]# kubectl delete namespace test #删除名为test的命名空间
[root@k8s-master ~]# kubectl get ns test -o yaml #将创建的test以yaml输出在控制行中
[root@k8s-master ~]# kubectl explain (资源类型service,namespace,pod等) #查看资源类型语法使用
注意:删除一个命名空间,会将该命名空间下的资源全部删除,该命令慎重操作
3、yaml命令
[root@k8s-master ~]# kubectl apply -f xxx.yaml #创建yaml文件
[root@k8s-master ~]# kubectl delete -f xxx.yaml #删除ymal文件
[root@k8s-master ~]# kubectl exec -it <pod名字> -c <yaml中定义的服务名字> -- sh
# -c 是当pod中有多个副本,需要用-c指定使用哪个副本,如果只有一个就不用指定
[root@k8s-master ~]# kubectl create deployment test --image=nginx -n shop --dry-run=client -o yaml > deployment.yaml #用create命令生成yaml不执行,并将yaml导出
# test 自定义名字
# --dry-run=client 模拟创建pod,但不会真的执行
# -o 导出yaml
# -n 指定标签,标签信息必须存在
[root@k8s-master ~]# kubectl expose deployment test --port=80 --target-port=80 --type=NodePort --dry-run=client -o yaml > server.yaml #创建service端,将80端口暴露出去
4、查看日志信息
[root@k8s-master ~]# kubectl logs <podName> -f #查看某个pod的日志信息
-f 实时监控不退出
[root@k8s-master ~]# kubectl logs -f <podName> -c test# -c 是当pod中有多个副本,需要用-c指定使用哪个副本,如果只有一个就不用指定
5、查看node资源
[root@k8s-master ~]# kubectl describe node k8s-node2 查看node2节点的资源详细信息(CPU、momory)
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 450m (11%) 0 (0%)
memory 140Mi (1%) 340Mi (4%)
ephemeral-storage 0 (0%) 0 (0%)
hugepages-1Gi 0 (0%) 0 (0%)
hugepages-2Mi 0 (0%) 0 (0%)
Events: <none>
# Requests(预分配,node2节点分配了450m,11%)
6、job命令
[root@k8s-master ~]# kubectl get job # 查看job任务
[root@k8s-master ~]# kubectl delete job <name> # job执行完成后,不需要保留记录,可以执行删除job
[root@k8s-master ~]# kubectl delete cronjob # 查看crontab任务
[root@k8s-master ~]# kubectl delete cronjob <name> # 删除cronjob