$ ls ~yifei/notes/

kubectl 备忘录

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

资源名称缩写

ResourceShort Name
deploymentsdeploy
podspo
replicasetsrs
cronjobscj
endpointsep
ingressesing
certificiaterequestscr, crs
certificatescert, certs
componentstatusescs
configmapscm
servicessvc
namespacesns
nodesno
statefulsetssts
daemonsetsds
resourcequotasquota
eventsev
limitrangeslimits
replicationcontrollersrc
serviceaccountssa
customresourcedefinitionscrd, crds
replicasetsrs
horizontalpodautoscalershpa
certificatesigningrequestscsr
networkpoliciesnetpol
podsecuritypoliciespsp
scheduledscalersss
priorityclassespc
storageclassessc
persistentvolumeclaimspvc
persistentvolumespv

kubectl create & apply

用来创建资源。一般情况下都是使用 -f 参数来制定配置文件。实际使用中,几乎不会用到 create

kubectl apply -f file.yml

kubectl delete

用来删除节点上的 pod, deployment 等信息

kubectl run

类似于 docker run, 但是由 kubernetes 接管,直接运行在集群上。比如运行 hello world

kubectl logs

类似于 docker logs, 用来显示打印到 stdout 的日志

复制文件

可以复制文件到 Pod 中。

kubectl cp -h

port-forward 临时访问集群中的服务

例如:

k port-forward svc/elasticsearch 9200:9200

把集群中 elasticsearch 服务映射到本地 9200 端口,非常方便测试。

列出当前集群的 API version

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:"

切换集群

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

参考

  1. https://akhilsharma.work/kubectl-get-resource-short-names/
  2. https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/
  3. https://blog.pipetail.io/posts/2020-05-04-most-common-mistakes-k8s/

© 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.