zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【K8S系列】第八讲:Kubernetes 之kubectl 常用命令汇总

k8sKubernetes 系列 汇总 常用命令 kubectl 第八
2023-09-14 09:15:25 时间

目录

一、kubetcl简单介绍

 二、命令介绍

详细介绍:

三、部分命令详细介绍

3.1 create

3.2 get

3.3 describe

3.4 rolling-update

3.5 exec

3.6 log


kubectl 是 Kubernetes 自带的客户端,可以用它来直接操作 Kubernetes 集群。

日常在使用 Kubernetes 的过程中,kubectl 工具应该是最常用的工具了。需要了解下如何高效的使用它。

官方介绍:

Command line tool (kubectl) | Kubernetes

本文总结了一些常用命令,供学习使用

一、kubetcl简单介绍

在控制台输入

kubectl --help

会出现以下界面,大概稍微列出来一些常用命令,如下图:

 二、命令介绍

详细介绍:

  1. get:显示一个或多个资源
  2. describe:显示资源详情:
  3. create:从文件或标准输入创建资源:create    
  4. update:从文件或标准输入更新资源:   
  5. delete:通过文件名、标准输入、资源名或者 label 删除资源:   
  6. log:输出 pod 中一个容器的日志:       
  7. rolling-update:对指定的 RC 执行滚动升级
  8. exec:在容器内部执行命令
  9. port-forward:将本地端口转发到 Pod
  10. proxy:为 Kubernetes API server 启动代理服务器  
  11. run:在集群中使用指定镜像启动容器     
  12. expose:将 SVC 或 pod 暴露为新的 kubernetes service 
  13. label :更新资源的 label    
  14. config:修改 kubernetes 配置文件
  15. cluster-info:显示集群信息
  16. api-versions :以”组/版本”的格式输出服务端支持的 API 版本
  17. version:输出服务端和客户端的版本信息
  18. help:显示各个命令的帮助信息         
  19. ingress-nginx:管理 ingress 服务的插件(官方安装和使用方式)  

三、部分命令详细介绍

3.1 create

# yaml
kubectl create -f test-rc.yaml
kubectl create -f test-service.yaml

# json
kubectl create -f ./pod.json
cat pod.json | kubectl create -f -

# yaml2json
kubectl create -f docker-registry.yaml --edit -o json

 1.使用url 创建

kubectl create -f https://git.io/vPieo

2. 一次性创建

kubectl create -f test-service.yaml -f test.yaml

3.根据目录创建

kubectl create -f <dir>

3.2 get

1.查看所有 Node 或 Namespace 

kubectl get nodes
kubectl get namespace

2.查看所有 Pod 对象

# 查看子命令帮助信息
kubectl get --help

# 列出默认namespace中的所有pod
kubectl get pods

# 列出指定namespace中的所有pod
kubectl get pods --namespace=test

# 列出所有namespace中的所有pod
kubectl get pods --all-namespaces

# 列出所有pod并显示详细信息
kubectl get pods -o wide
kubectl get replicationcontroller web
kubectl get -k dir/
kubectl get -f pod.yaml -o json
kubectl get rc/web service/frontend pods/web-pod-13je7
kubectl get pods/app-prod-78998bf7c6-ttp9g --namespace=test -o wide
kubectl get -o template pod/web-pod-13je7 --template={{.status.phase}}

# 列出该namespace中的所有pod包括未初始化的
kubectl get pods,rc,services --include-uninitialized

3.查看service

kubectl get svc
kubectl get service

4.查看deployment

# 查看全部deployment
kubectl get deployment

# 列出指定deployment
kubectl get deployment my-app

3.3 describe

1.显示 Pod 详细信息

kubectl describe pods/nginx
kubectl describe pods nginx
kubectl describe -f nginx.json

2.查看 Node 详细信息

kubectl describe nodes nginx

3.RC 关联的 Pod 信息

kubectl describe pods <rc-name>

3.4 rolling-update

1.滚动更新

# 滚动更新 pod test-pod
kubectl rolling-update test-pod -f test-pod2.json

# 更新资源名称并更新镜像
kubectl rolling-update test-pod test-pod2 --image=image:v2

# 更新 test-pod pod 中的镜像
kubectl rolling-update test-pod --image=image:v2

# 退出已存在的进行中的滚动更新
kubectl rolling-update test-pod test-pod2 --rollback

# 强制替换; 删除后重新创建资源; 服务会中断
kubectl replace --force -f ./test.json

# 添加标签
kubectl label pods test-pod new-label=awesome

# 添加注解
kubectl annotate pods test-pod icon-url=http://*****

3.5 exec

1.进入某个容器:eg:nginx

kubectl exec test-pod -c nginx-container -it -- bash

2.指定容器执行命令

可以在寄主机上,不进入容器直接执行命令

kubectl exec test-pod -c nginx-container -- date

3.6 log

1.查看日志并实时刷新

kubectl logs -f test-pod -c nginx-container

2.查看日志不刷新

kubectl logs test-pod --namespace=test