1、语法结构
kubectl command type name flag
元素 | 说明 |
---|---|
command | 子命令,操作资源对象,比如create、delete、apply、get、describe等 |
type | 资源对象类型,大小写敏感,但可以单复数或简写表示 |
name | 资源对象名称,大小写敏感 |
flag | kubectl参数,不同参数输出结果截然不同 |
2、常见资源对象类型缩写
资源对象类型 | 缩写 |
---|---|
componentstatuses | cs |
configmaps | cm |
daemonsets | ds |
deployments | deploy |
endpoints | ep |
events | ev |
horizontalpodautoscalers | hpa |
ingress | ing |
limitanges | limits |
nodes | no |
namespaces | ns |
pods | po |
persistentvolumeclaims | pvc |
persistentvolumes | pv |
replicasets | rs |
podsecuritypolicies | psp |
replicationcontrollers | rc |
services | svc |
storageclasses | sc |
3、常见子命令
命令 | 说明 |
---|---|
create | 从stdin中创建资源对象 |
apply | 从stdin中更新资源对象 |
delete | 根据资源对象名称、labe selector、配置文件或stdin删除指定资源对象 |
scale | 扩容、缩容一个Deployment、ReplicaSet、RC或Job中的Pod的数量 |
4、输出格式
kubectl get pods podname -o wide
显示pod更多信息
5、常见命令
kubectl create -f yamlfile
可以接多个yaml
kubectl create -f yaml1 -f yaml2
kubectl get pods
查看pod
kubectl describe nodes nodename
描述node的详细信息
kubectl describe pods podname
描述pod的详细信息
kubectl describe pods rcname
显示由rc管理所有pod
kubectl delete -f yamlfile
基于yaml文件删除定义的pod
kubectl delete pods -l name=<labelname>
删除包含某个label的pod
kubectl delete pods --all
删除所有pod
kubectl exec podname date
在pod中第一个容器中执行date命令
kubectl exec podname -c containername date
指定pod中的某个容器执行data命令
kubectl exec -it podname -c containername bash
登录podname中的指定容器
kubectl logs containername
查看指定podname日志
kubectl logs -f podname -c containername
跟踪指定容器日志
kubectl apply -f app.yml
创建或更新资源
kubectl edit deploy nginx
编辑控制器nginx
kubectl port-forward --address 0.0.0.0 pod/xxxxxx 8888:80
将pod内部端口80映射出来
kubectl cp podname:/etc/fstab /tmp
将pod内的文件/etc/fstab复制到本地的/tmp下
kubectl label namespace default testing=true
为namespace default设置标签testing=true
kubectl api-resources
检查可用的资源对象列表
自定义插件
echo "echo "hello world"" > kubectl-hello
chmod +x kubectl-hello
cp kubectl-hello /usr/local/bin
kubectl-hello返回
hello world
ps:自定义的插件必须以kubectl开头
kubectl plugin list
查看可用插件