Kubernetes

kubectl命令行

roya · 7月7日 · 2020年

Basic Commands

create

创建一个集群资源对象
Usage:kubectl create -f FILENAME [options]

Options:

  • -f, --filename, 指定配置文件

expose

将已经存在的资源RC、RS、Deployment、pod暴露为一个新的service
Usage:
kubectl expose (-f FILENAME | TYPE NAME) [–port=port] [–protocol=TCP|UDP|SCTP] [–target-port=number-or-name]
[–name=name] [–external-ip=external-ip-of-service] [–type=type] [options]

Options:

  • -f, –filename=[]: 指定文件
  • -l, –labels=” 设置服务标签
  • –name=” 设置服务名称
  • –port=” 服务端口
  • –protocol=” 协议,默认为TCP
  • –target-port=” 容器暴露的端口

Examples: kubectl expose rc nginx --port=80 --target-port=8000

run

创建并运行指定镜像
Usage:
kubectl run NAME –image=image [–env=”key=value”] [–port=port] [–replicas=replicas] [–dry-run=bool] [–overrides=inline-json] [–command] — [COMMAND] [args…] [options]

Options:

  • –command=false: 指定容器命令
  • –env=[]: 设置容器环境变量
  • –expose=false: 指定容器暴露端口
  • –hostport=-1: 容器端口映射的主机端口,适用于单机
  • –image=”: 指定镜像
  • -l, –labels=”: 设置pod标签
  • –port=”: 容器暴露的端口
  • -r, –replicas=1: 容器运行个数,默认为1.

Examples:

    # 启动一个 Nginx 实例
kubectl run nginx –image=nginx
# 启动一个 hazelcast 单个实例,并开放容器的5701端口。
kubectl run hazelcast –image=hazelcast –port=5701

Basic Commands

get

列出一个或多个资源信息
Usage:

kubectl get [(-o|–output=)json|yaml|wide|custom-columns=…|custom-columns-file=…|go-template=…|go-template-file=…|jsonpath=…|jsonpath-file=…] (TYPE[.VERSION][.GROUP] [NAME | -l label] | TYPE[.VERSION][.GROUP]/NAME …) [flags] [options]

Examples:

    # 获取所有pods
kubectl get pods
# 获取所有pods,列出更多信息
kubectl get pods -o wide
# 获取RC web
kubectl get replicationcontroller web

delete

通过配置文件名、stdin、资源名称或label选择器来删除资源
Usage:

    kubectl delete ([-f FILENAME] | TYPE [(NAME | -l label | –all)]) [options]

Examples:

    # 通过pod.json删除对应的pod
kubectl delete -f ./pod.json  
# 删除带有标签name=myLabel的pod和service
kubectl delete pods,services -l name=myLabel
# 删除所有pod
kubectl delete pods –all

Options:

    –all=false 删除所有资源
-f, –filename=[]: 配置文件
–force=false: 强制删除

Deploy Commands:

scale

设置资源对象的个数.

Usage:

    kubectl scale [–resource-version=version] [–current-replicas=count] –replicas=COUNT (-f FILENAME | TYPE NAME) [options]

Examples:

    # Scale a replicaset named ‘foo’ to 3.
kubectl scale –replicas=3 rs foo
# If the deployment named mysql’s current size is 2, scale mysql to 3.
 kubectl scale –current-replicas=2 –replicas=3 deployment/mysql
 # Scale multiple replication controllers.
 kubectl scale –replicas=5 rc/foo rc/bar rc/baz
# Scale statefulset named ‘web’ to 3.
kubectl scale –replicas=3 statefulset/web

Options:

    –all=false: 选择所有资源类型
–current-replicas=-1: 当前资源个数,需要匹配
–replicas=0: 新的资源个数 Required.
–resource-version=”: 资源版本,需要与当前资源版本匹配

autoscale

对Deployment、RS、RC进行水平自动扩容和缩容设置

Usage:

    kubectl autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [–min=MINPODS] –max=MAXPODS [–cpu-percent=CPU] [options]

Examples:

    # 自动缩放deployment foo,数量介于2-10之间
kubectl autoscale deployment foo –min=2 –max=10

Options:

    –cpu-percent=-1: 目标平均CPU利用率(表示为请求CPU的百分比)pod。如果未指定或为负数,则将使用默认的自动缩放策略
–max=-1: pods最大数
–min=-1: pods最小数

Cluster Management Commands:

cordon

将node标记为unschedulable,即隔离出集群调度范围

Usage:

    kubectl cordon NODE [options]

Examples:

    # Mark node “foo” as unschedulable.
kubectl cordon foo

Options:

    -l, –selector=’ ‘ : 使用选择器通过标签进行筛选

uncordon

将node标记为schedulable

Usage:

    kubectl uncordon NODE [options]

Examples:

    # Mark node “foo” as schedulable.
kubectl uncordon foo

Options:

    -l, –selector=”: Selector (label query) to filter on

drain

首先将Node设置为unschedulable ,然后删除该node上运行的所有pod

Usage:

    kubectl drain NODE [options]

Examples:

    # 驱散节点foo
kubectl drain foo –force

Options:

    –force=false: 强制执行

taint

Usage:

kubectl taint NODE NAME KEY_1=VAL_1:TAINT_EFFECT_1 … KEY_N=VAL_N:TAINT_EFFECT_N

Troubleshooting and Debugging Commands:

describe

描述资源对象的详细信息

Usage:

    kubectl describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME) [options]

Examples:

    # 显示某个node的详细信息
kubectl describe nodes (node-name)
# 显示pod nginx的详细信息
kubectl describe pods nginx

Options:

    –all-namespaces=false: 显示所有空间的对象
-f, –filename=[]: 指定文件

logs

显示容器的日志,如果pod有多个容器,默认显示第一个

Usage:

    kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER] [options]

Examples:

    # Return snapshot logs from pod nginx with only one container
kubectl logs nginx

Options:

    –all-containers=false: Get all containers’s logs in the pod(s).
-c, –container=”:指定容器
-f, –follow=false: 跟随输出

exec

向容器下发命令

Usage:

    kubectl exec POD [-c CONTAINER] — COMMAND [args…] [options]

Examples:

    # 向pod 123456-7890的第一个容器下发date命令
kubectl exec 123456-7890 date

Options:

    -c, –container=”: 容器名称,如果不填,则默认为pod里面的第一个容器
-i, –stdin=false
-t, –tty=false

port-forward

设置pod端口

Usage:

    kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT […[LOCAL_PORT_N:]REMOTE_PORT_N]

Examples:

    # 本地5000和6000端口对应pod5000和6000端口
kubectl port-forward pod/mypod 5000 6000

cp

在宿主机和容器之间拷贝文件或目录

Usage:

    kubectl cp <file-spec-src> <file-spec-dest> [options]

Examples:

    # Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>

Options:

    -c, –container=”: 指定容器

Advanced Commands:

apply

通过文件名或控制台对资源进行管理

Usage:

    kubectl apply -f FILENAME [options]

Examples:

    # 通过json文件部署pod
kubectl apply -f ./pod.json
# 通过stdin部署pod
cat pod.json | kubectl apply -f –

Options:

    -f, –filename=[]: 指定配置文件

Settings Commands:

label

更新资源标签

Usage:

    kubectl label [–overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 … KEY_N=VAL_N [–resource-version=version] [options]

Examples:

    # Update pod ‘foo’ with the label ‘unhealthy’ and the value ‘true’.
kubectl label pods foo unhealthy=true
# Update pod ‘foo’ with the label ‘status’ and the value ‘unhealthy’, overwriting any existing value.
kubectl label –overwrite pods foo status=unhealthy
# Update pod ‘foo’ only if the resource is unchanged from version 1.
kubectl label pods foo status=unhealthy –resource-version=1
# Update pod ‘foo’ by removing a label named ‘bar’ if it exists.
# Does not require the –overwrite flag.
kubectl label pods foo bar-

Options:

    –all=false: 选取一个空间内的所有资源
–overwrite=false: If true, 更新已存在的标签
–resource-version=”: If non-empty, the labels update will only succeed if this is the current resource-versionfor the object. Only valid when specifying a single resource.
-l, –selector=”:

annotate

更新一个或多个资源的注解

Usage:

    kubectl annotate [–overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 … KEY_N=VAL_N [–resource-version=version] [options]

Examples:

    # 更新pod “foo”,设置其注解description的值为my frontend。
# 如果同一个注解被赋值了多次,只保存最后一次设置的值。
kubectl annotate pods foo description=’my frontend’
# 更新pod “foo”,设置其注解description的值为my frontend running nginx,已有的值将被覆盖。
kubectl annotate –overwrite pods foo description=’my frontend running nginx’
# 更新同一namespace下所有的pod。
kubectl annotate pods –all description=’my frontend running nginx’
# 仅当pod “foo”当前版本为1时,更新其注解
kubectl annotate pods foo description=’my frontend running nginx’ –resource-version=1
# 更新pod “foo”,删除其注解description。
# 不需要–override选项。
kubectl annotate pods foo description-

Options:

    -all=false: 同一空间所有类型资源
–overwrite=false: 更新以存在的注解
–resource-version=”: 更新对应版本的资源

Other Commands:

api-resources

输出当前版本支持的API资源

api-versions

输出当前版本支持的API接口

version

输出当前客户端和服务端的版本信息

0 条回应