Posted on:
Last modified:
获取各种资源列表:
# -o 指定范围格式,`-o wide` 用来显示更详细信息
kubectl get node/pod/deploy/node/svc -o wide/yaml/name
描述详细信息:
# -n 指定命名空间
kubectl describe node/pod/deploy/svc
其他一些操作
# 显示当前节点的一些信息
kubectl top node/pod
# 本地 5000 端口转发到 pod 6000
kubectl port-forward my-pod 5000:6000
# 编辑 kubernetes 的配置文件
kubectl edit deployment/my-nginx
Resource | Short Name |
---|---|
deployments | deploy |
pods | po |
replicasets | rs |
cronjobs | cj |
endpoints | ep |
ingresses | ing |
certificiaterequests | cr, crs |
certificates | cert, certs |
componentstatuses | cs |
configmaps | cm |
services | svc |
namespaces | ns |
nodes | no |
statefulsets | sts |
daemonsets | ds |
resourcequotas | quota |
events | ev |
limitranges | limits |
replicationcontrollers | rc |
serviceaccounts | sa |
customresourcedefinitions | crd, crds |
replicasets | rs |
horizontalpodautoscalers | hpa |
certificatesigningrequests | csr |
networkpolicies | netpol |
podsecuritypolicies | psp |
scheduledscalers | ss |
priorityclasses | pc |
storageclasses | sc |
persistentvolumeclaims | pvc |
persistentvolumes | pv |
用来创建或更新资源,使用 -f
参数来制定配置文件。实际使用中,几乎不会用到 create
。
kubectl apply -f file.yml
用来删除节点上的 pod, deployment 等信息
类似于 docker run, 但是由 kubernetes 接管,直接运行在集群上。比如运行 hello world
类似于 docker logs, 用来显示打印到 stdout 的日志
可以复制文件到 Pod 中。
kubectl cp -h
例如:
k port-forward svc/elasticsearch 9200:9200
把集群中 elasticsearch 服务映射到本地 9200 端口,非常方便测试。
kubectl api-resources # List the API resources that are available.
kubectl api-versions # List the API versions that are available.
for kind in `kubectl api-resources | tail +2 | awk '{ print $1 }'`; do
kubectl explain $kind;
done | grep -e "KIND:" -e "VERSION:"
kubectl rollout restart deployment <name>
kubectl exec -it POD-NAME -n NAMESPACE -- bash
使用代理可能会带来一些问题,一定要把集群内的地址设置为不用代理:
https://github.com/jetstack/cert-manager/issues/2640
kubernetes 支持在一个配置文件中存储多个集群的配置,并且可以使用 kubectl config 切换。
如果多个集群分散在不同文件中,首先我们需要合并一下这些文件:
# 把所有文件都加到 KUBECONFIG 路径中
export KUBECONFIG=/path/to/conf1:/path/to/conf2...
# 合并
kubectl config view --flatten > $HOME/.kube/all-in-one-kubeconfig.yaml
# 验证下是否可用
kubectl config get-contexts --kubeconfig=$HOME/.kube/all-in-one-kubeconfig.yaml
# 把合并后的文件放到默认位置
mv $HOME/.kube/all-in-one-kubeconfig.yaml $HOME/.kube/config
export KUBECONFIG=
列出当前的 contexts
$ k config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* docker-desktop docker-desktop docker-desktop
rancher-desktop rancher-desktop rancher-desktop
us-east-1a us-east-1a us-east-1a
切换 context
$ k config use-context rancher-desktop
Switched to context "rancher-desktop".
删除 context
$ k config delete-context rancher-desktop
warning: this removed your active context, use "kubectl config use-context" to select a different one
deleted context rancher-desktop from /Users/yifei/.kube/config
重命名 context
k config rename-context OLD NEW
© 2016-2022 Yifei Kong. Powered by ynotes
All contents are under the CC-BY-NC-SA license, if not otherwise specified.
Opinions expressed here are solely my own and do not express the views or opinions of my employer.
友情链接: MySQL 教程站