![26be9f682bf6c696d961a3da31135719.png](https://img-blog.csdnimg.cn/img_convert/26be9f682bf6c696d961a3da31135719.png)
Kubectl 自动补全
bash
source > ~/.bashrc # 在您的 bash shell 中永久的添加自动补全
Kubectl 上下文和配置
kubectl config view # 显示合并的 kubeconfig 配置。# 同时使用多个 kubeconfig 文件并查看合并的配置KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 kubectl config view# 获取 e2e 用户的密码kubectl config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}'kubectl config view -o jsonpath='{.users[].name}' # 显示第一个用户kubectl config view -o jsonpath='{.users[*].name}' # 获取用户列表kubectl config get-contexts # 显示上下文列表kubectl config current-context # 展示当前所处的上下文kubectl config use-context my-cluster-name # 设置默认的上下文为 my-cluster-name# 添加新的集群配置到 kubeconf 中,使用 basic auth 进行身份认证kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword# 在指定上下文中持久性地保存名字空间,供所有后续 kubectl 命令使用kubectl config set-context --current --namespace=ggckad-s2# 使用特定的用户名和名字空间设置上下文kubectl config set-context gce --user=cluster-admin --namespace=foo && kubectl config use-context gcekubectl config unset users.foo # 删除用户 foo
创建对象
Kubernetes 配置可以用 YAML 或 JSON 定义。可以使用的文件扩展名有 .yaml、.yml 和 .json。
kubectl apply -f ./my-manifest.yaml # 创建资源kubectl apply -f ./my1.yaml -f ./my2.yaml # 使用多个文件创建kubectl apply -f ./dir # 基于目录下的所有清单文件创建资源kubectl apply -f https://git.io/vPieo # 从 URL 中创建资源kubectl create deployment nginx --image=nginx # 启动单实例 nginxkubectl explain pods,svc # 获取 pod 清单的文档说明# 从标准输入创建多个 YAML 对象cat <
查看和查找资源
# get 命令的基本输出kubectl get services # 列出当前命名空间下的所有 serviceskubectl get pods --all-namespaces # 列出所有命名空间下的全部的 Podskubectl get pods -o wide # 列出当前命名空间下的全部 Pods,并显示更详细的信息kubectl get deployment my-dep # 列出某个特定的 Deploymentkubectl get pods # 列出当前命名空间下的全部 Podskubectl get pod my-pod -o yaml # 获取一个 pod 的 YAML# describe 命令的详细输出kubectl describe nodes my-nodekubectl describe pods my-pod# 列出当前名字空间下所有 Services