kubectl基本命令
文章目录
- kubectl基本命令
- 一、get 查看资源
- 二、创建资源 apply create
- 三、删除资源
- 四、执行 pod 的 date 命令
- 五、通过bash获得 pod 中某个容器的TTY,相当于登录容器
- 六、查看容器的日志 log
- 七、查看注释explain
- 八、重启 pod
- 九、修改网络类型
- 十、进入pod
- 十一、查看pod资源yaml文件并导出
一、get 查看资源
- 查看帮助
kubectl --help
- 查看版本
kubelet --version
- 查看资源 get
//查看所有pods
kubelet get pods 可缩写 po
//查看所有nodes
kubelet get nodes
//查看pod对应的节点服务器
[root@master01 dashboard]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NO
nginx-dbddb74b8-qpw7t 1/1 Running 0 7m53s 172.17.95.4 192.168.75.201 <none>
nginx-dbddb74b8-vrhw4 1/1 Running 0 13m 172.17.71.3 192.168.75.144 <none>
nginx-dbddb74b8-zmhcl 1/1 Running 0 9m52s 172.17.71.2 192.168.75.144 <none>
- 查看所有nodes 详情pod资源对应的后端node 节点情况
[root@master01 dashboard] kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
192.168.75.144 Ready <none> 16h v1.12.3 192.168.75.144 <none> CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 docker://19.3.13
192.168.75.201 Ready <none> 16h v1.12.3 192.168.75.201 <none> CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 docker://19.3.13
- 查看集群状态
[root@master01 dashboard]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
192.168.75.144 Ready <none> 16h v1.12.3
192.168.75.201 Ready <none> 16h v1.12.3
- 查看所有区域的pod
[root@master01 dashboard]# kubectl get po --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx-dbddb74b8-qpw7t 1/1 Running 0 13m
default nginx-dbddb74b8-vrhw4 1/1 Running 0 18m
default nginx-dbddb74b8-zmhcl 1/1 Running 0 15m
kube-system kubernetes-dashboard-65f974f565-v5zt8 1/1 Running 0 132m
- 查看资源并导出
3种方式查看一个pod的详细信息和参数:
//以yaml文件形式显示一个pod的详细信息
[root@master01 dashboard]# kubectl get po nginx-dbddb74b8-qpw7t -o yaml // nginx-dbddb74b8-qpw7t podname
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: 2020-10-09T04:12:40Z
generateName: nginx-dbddb74b8-
labels:
pod-template-hash: dbddb74b8
run: nginx
name: nginx-dbddb74b8-qpw7t
namespace: default
ownerReferences:
省略。。。。。。。。。。。。。。。。。。。
//正在运行的pod
kubectl get po nginx-dbddb74b8-qpw7t -o yaml > /opt/nginx.yaml
//以jison格式输出pod的详细信息。
kubectl get po <podname> -o json
kubectl get po <podname> -o json >/opt/nginx.yaml
- 查看pod所有信息(sever 、 name 、pod)
kubectl get all
- 查看pod资源创建过程
kubectl get pods -w
10.查看关联的后端节点(内部)
kubectl get ep (endpoints)
- 查看所有 pod 列表, -n 后跟 namespace, 查看指定的命名空间
kubectl get pod
kubectl get pod -n kube
kubectl get pod -o wide
- 查看 RC 和 service 列表, -o wide 查看详细信息
kubectl get rc,svc
kubectl get pod,svc -o wide
kubectl get pod <pod-name> -o yaml
- 显示指定 Node 的详细信息
kubectl describe node 192.168.0.212(node ip地址)
- 显示 Pod 的详细信息, 特别是查看 pod 无法创建的时候的日志
kubectl describe pod <pod-name> //使用kubectl get pods 查看pod name
eg:
kubectl describe pod redis-master-tqds9
- 查看节点 labels
kubectl get node --show-labels
16.查看pod labels
kubectl get pods --show-labels
- 查看资源对象简写
[root@localhost bin]# kubectl api-resources
二、创建资源 apply create
- 根据 yaml 创建资源, apply 可以重复执行,create 不行
kubectl create -f pod.yaml
kubectl apply -f pod.yaml
- 基于镜像创建pod资源
格式 : kubectl run --image=镜像名 --port=端口 --replicas=3 (设置副本集,这里的副本集设置之后,K8s有自我修复能力,如果pod发生故障将会自动修复一直保持3个副本)
[root@localhost bin]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx created
[root@localhost bin]# kubectl get pods,deployment
NAME READY STATUS RESTARTS AGE
pod/nginx-7697996758-jbln5 1/1 Running 0 47s
pod/nginx-7697996758-xgxzd 1/1 Running 0 47s
pod/nginx-7697996758-xjdlz 1/1 Running 0 47s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx 3 3 3 3 47s
三、删除资源
- 基于 pod.yaml 定义的名称删除 pod
kubectl delete -f pod.yaml
- 基于podname 删除
[root@localhost bin]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5477945587-b8r6m 1/1 Running 0 12m
nginx-deployment-5477945587-dz8hb 1/1 Running 0 12m
nginx-deployment-5477945587-wd82l 1/1 Running 0 12m
[root@localhost bin]# kubectl delete deploy/nginx-deployment //控制器deployment之后再删server //有延迟
[root@localhost bin]# kubectl get pods
No resources found.
- 删除所有包含某个 label 的pod 和 service
kubectl delete pod,svc -l name=<label-name>
- 删除所有 Pod
kubectl delete pod --all
- 删除当前所有资源 - 高危命令
kubectl delete -f .
- 强制删除命令
//问题处理
//如果遇到处于Terminating状态的无法删除的资源如何处理
[root@localhost demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-tomcat-57667b9d9-nklvj 1/1 Terminating 0 10h
my-tomcat-57667b9d9-wllnp 1/1 Terminating 0 10h
//这种情况下可以使用强制删除命令:
kubectl delete pod [pod name] --force --grace-period=0 -n [namespace]
[root@localhost demo]# kubectl delete pod my-tomcat-57667b9d9-nklvj --force --grace-period=0 -n default
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "my-tomcat-57667b9d9-nklvj" force deleted
[root@localhost demo]# kubectl delete pod my-tomcat-57667b9d9-wllnp --force --grace-period=0 -n default
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "my-tomcat-57667b9d9-wllnp" force deleted
四、执行 pod 的 date 命令
kubectl exec <pod-name> -- date
kubectl exec <pod-name> -- bash
kubectl exec <pod-name> -- ping 10.24.51.9
五、通过bash获得 pod 中某个容器的TTY,相当于登录容器
kubectl exec -it <pod-name> -c <container-name> -- bash
eg:
kubectl exec -it redis-master-cln81 -- bash
六、查看容器的日志 log
kubectl logs <pod-name>
kubectl logs -f <pod-name> # 实时查看日志
kubectl log <pod-name> -c <container_name> # 若 pod 只有一个容器,可以不加 -c
kubectl logs -l app=frontend # 返回所有标记为 app=frontend 的 pod 的合并日志。
七、查看注释explain
kubectl explain pod
kubectl explain pod.apiVersion
八、重启 pod
kubectl get pod <POD名称> -n <NAMESPACE名称> -o yaml | kubectl replace --force -f -
九、修改网络类型
kubectl patch service istio-ingressgateway -n istio-system -p '{"spec":{"type":"NodePort"}}'
十、进入pod
//进入pod
[root@localhost bin]# kubectl exec -it nginx-7697996758-75shs bash
root@nginx-7697996758-75shs:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
root@nginx-7697996758-75shs:/#
十一、查看pod资源yaml文件并导出
//自动测试命令的正确性,并不执行创建
[root@localhost demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx-deployment created (dry run)
//查看生成yaml格式
[root@localhost demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml
//查看生成json格式
[root@localhost demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o json
[root@localhost demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml > my-deployment.yaml
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
[root@localhost demo]# ls
my-deployment.yaml nginx-deployment.yaml nginx-service.yaml
//将现有的资源生成模板导出
[root@localhost demo]# kubectl get deploy/nginx --export -o yaml
//保存到文件中
[root@localhost demo]# kubectl get deploy/nginx --export -o yaml > my-deploy.yaml
//查看字段帮助信息
[root@localhost demo]# kubectl explain pods.spec.containers