文章目录
配置kubeconfig
vi ~/.kube/config
mv ~/Downloads/kubectl /usr/local/bin
sudo mv ~/Downloads/kubectl /usr/local/bin
mkdir ~/.kube
mv ~/Downloads/config ~/.kube
帮助信息命令
查看kubectl的帮助信息
kubectl -h
或者
kubectl --help
查看具体某一个命令的帮助信息
kubectl <command> --help
或者
kubectl <command> -h
列出全局的选项参数(适用所有的命令)
kubectl options
常用的选项:
-n, –namespace=‘’: If present, the namespace scope for this CLI request
显示合并的 kubeconfig 配置或一个指定的 kubeconfig 文件
kubectl config view
基本命令
罗列所支持的完整资源清单
kubectl api-resources
查看资源的文档
kubectl explain -h
案例:获取资源的文档和它的所有字段
kubectl explain pods
案例:获取资源的特定字段的文档
kubectl explain pods.spec.containers
显示一个或更多 resources
资源清单,可以通过 kubectl api-resources
查看
kubectl get -h
常用的选项:
-A, –all-namespaces=false: If present, list the requested object(s) across all namespaces. Namespace in current
context is ignored even if specified with –namespace.
-o, –output=‘’: Output format. One of:
json|yaml|wide|name|custom-columns=…|custom-columns-file=…|go-template=…|go-template-file=…|jsonpath=…|jsonpath-file=…
See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template
[http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template
[http://kubernetes.io/docs/user-guide/jsonpath].
案例:列出所有的命名空间
kubectl get ns
案例:列出所有的pod
kubectl get pods
案例:列出所有的pod,同时输出更多的信息(例如,节点名)
kubectl get pods -o wide
案例:查看pod的信息,yaml格式
kubectl get pods <pod name> -o yaml
案例:查看pod中容器名
此命令不包括 init 容器
kubectl get pods <pod name> -o jsonpath="{.spec.containers[*].name}" -n <namespace>
查看 init 容器
kubectl get pods <pod name> -o jsonpath="{.spec.initContainers[*].name}" -n <namespace>
查看所有容器
kubectl get pod <pod name> -o jsonpath="{.spec['containers','initContainers'][*].name}" -n <namespace>
重启pod
kubectl delete pods -n <namespace> <pod name> --grace-period=0 --force
查找问题和调试命令
在一个 container 中执行一个命令
进入pod
kubectl exec -it <pod name> --namespace <namespace> --container <container name> -- bash
等价于
kubectl exec -it <pod name> -n <namespace> -c <container name> -- bash
转发一个或者多个本地端口到一个pod中
kubectl port-forward -h
例如:
kubectl port-forward service/<svc name> <locally port>:<pod port>
复制 files 和 directories 到 containers 和从容器中复制 files 和 directories
kubectl cp -h
案例:复制本地文件到远程
kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar
案例:复制远程到本地
kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/155595.html