Kubernetes常用命令

一 Kubernetes常用命令

1 获取命令

kubectl --help

Basic Commands (Beginner):
  create         Create a resource from a file or from stdin.
  expose         Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
  run            Run a particular image on the cluster
  set            Set specific features on objects

Basic Commands (Intermediate):
  explain        Documentation of resources
  get            Display one or many resources
  edit           Edit a resource on the server
  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout        Manage the rollout of a resource
  scale          Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job
  autoscale      Auto-scale a Deployment, ReplicaSet, or ReplicationController

Cluster Management Commands:
  certificate    Modify certificate resources.
  cluster-info   Display cluster info
  top            Display Resource (CPU/Memory/Storage) usage.
  cordon         Mark node as unschedulable
  uncordon       Mark node as schedulable
  drain          Drain node in preparation for maintenance
  taint          Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe       Show details of a specific resource or group of resources
  logs           Print the logs for a container in a pod
  attach         Attach to a running container
  exec           Execute a command in a container
  port-forward   Forward one or more local ports to a pod
  proxy          Run a proxy to the Kubernetes API server
  cp             Copy files and directories to and from containers.
  auth           Inspect authorization

Advanced Commands:
  apply          Apply a configuration to a resource by filename or stdin
  patch          Update field(s) of a resource using strategic merge patch
  replace        Replace a resource by filename or stdin
  wait           Experimental: Wait for a specific condition on one or many resources.
  convert        Convert config files between different API versions

Settings Commands:
  label          Update the labels on a resource
  annotate       Update the annotations on a resource
  completion     Output shell completion code for the specified shell (bash or zsh)

Other Commands:
  alpha          Commands for features in alpha
  api-resources  Print the supported API resources on the server
  api-versions   Print the supported API versions on the server, in the form of "group/version"
  config         Modify kubeconfig files
  plugin         Provides utilities for interacting with plugins.
  version        Print the client and server version information

1.1 获取子命令

kubectl <command> --help
kubectl create --help

2 常用命令介绍

2.1 run

创建容器镜像
	1:启动nginx实例
		kubectl run nginx --image=nginx
	2:启动nginx实例,暴露容器端口80,设置副本数2
		kubectl run nginx --image=nginx --port=80 --replicas=2

2.2 expose

为Pod资源创建一个service
	1:为nginx创建service,并通过Service的80端口转发至容器的80端口上
		kubectl expose deployment nginx --name=nginx --port=80 --target-port==80

2.3 create

创建一个集群资源对象(Pod,Service,job,configMap,namespace...)
	1:基于命令创建资源对象
		kubectl create deployment my-dep --image=busybox
		kubectl create service clusterip my-cs --tcp=5678:8080
	2:基于文件创建资源对象
		kubectl create -f pod.yaml
		kubectl create -f service.yaml

2.4 delete

删除资源对象(Pod,Service,job,configMap,namespace...)
	1:基于命令删除资源对象
		kubectl delete deployment my-dep
		kubectl delete service my-cs
	2:基于文件删除资源对象
		kubectl delete -f pod.yaml
		kubectl delete -f service.yaml

2.5 apply

创建一个集群资源对象(Pod,Service,job,configMap,namespace...)
	1:基于文件创建资源对象
		kubectl apply -f pod.yaml
		kubectl apply -f service.yaml
	2:apply与create区别
		kubectl apply -f FILENAME 可多次执行,每次修改资源清单后,使用apply可直接部署
		kubectl create -f FILENAME 不可多次执行,每次修改资源清单后,需要先删除资源后才能部署
		推荐使用apply部署资源对象,delete删除资源对象

2.6 get

获取资源信息(Pod,Service,job,configMap,namespace...)
	1:列出所有运行的Pod信息,列出kube-system命令空间中运行的Pod
		kubectl get pods
		kubectl get pods -n kube-system
	2:列出Pod以及运行Pod节点信息
		kubectl get pods -o wide
	3:以JSON格式输出一个pod信息
		kubectl get pod nginx -o json
	4:列出所有replication controllers,deployment和service信息
		kubectl get rc
		kubectl get deployment
		kubectl get services
	5:列出所有不同的资源对象
		kubectl get all
	6:列出集群组件运行状态信息
		kubectl get cs

2.7 explain

列出支持的资源的字段,用于资源清单文件编写资料查看<.yaml>(Pod,Service,job,configMap,namespace...)
	1:获取pods/service支持的资源字段
		kubectl explain pods
		kubectl explain service
	2:获取pods资源下spec资源支持的字段
		kubectl explain pods。spec

2.8 describe

显示特定资源的信息
	1:显示pods nginx的详细信息
		kubectl describe pods nginx
	2:显示service nginx的详细信息
		kubectl describe svc nginx
	3:显示节点1的详细信息
		kubectl describe node k8s-node1

2.9 scale

扩缩Pod数量(扩容或缩容 Deployment、ReplicaSet、Replication Controller或 Job 中Pod数量)
	1:将名为nginx的pod副本数设置为3
		kubectl scale --replicas=3 deployment nginx
	2:将名为nginx的pod副本数设置为1
		kubectl scale --replicas=1 deployment nginx

2.10 set

配置应用资源(升级、回滚等)
	1:设置名为nginx的pod运行的镜像为:nginx:1.14-alpine
		kubectl set image deployment nginx nginx=nginx:1.14-alpine

2.11 rollout

对资源进行管理
	1:查看历史版本
		kubectl rollout history deployment nginx
	2:暂停资源 ()只要deployment在暂停中,使用deployment更新将不会生效)
		kubectl rollout pause deployment nginx
	3:恢复暂停资源
		kubectl rollout resume deployment nginx
	4:查看资源状态
		kubectl rollout status deployment nginx
	5:回滚版本
		kubectl rollout undo deployment nginx

2.12 exec

在容器中执行命令
	1:连接Pod终端
		kubectl exec -it PODNAME -- sh
	2:连接kube-system命名空间中的Pod终端
		kubectl exec -it -n kube-system PODNAME -- sh

2.13 logs

打印Pod日志:
	1:查看Pod日志
		kubectl logs PODNAME

2.14 cp

拷贝文件
	1: 从msster节点拷贝文件到Pod
		kubectl cp /etc/fstab PODNAME:/tmp
	2:从Pod节点拷贝文件到msster
		kubectl cp PODNAME:/tmp /etc/fstab

3 参考文档

Kubernetes 官方文档
Kubernetes 中文文档

原文地址:https://www.cnblogs.com/evescn/p/12260015.html