K8s command chapter - kubernetes working practical command collection number

High rise zee 2020-11-17 21:04:03
k8s command chapter kubernetes working

1.kubectl Command Overview

kubectl As a client CLI Tools , Can let the user through the command line to Kubernetes Set Group operation . In practical work, skillfully use these commands to locate K8s When it comes to cluster problems, it's a good partner we can't put down , Come and get to know each other .

2.kubectl Usage overview

2.1.kubectl Command line syntax

kubectl [command] [Type] [NAME] [flas]
  1. command: Sons command , Used to operate Kubernetes The life of cluster resource objects Make , for example create、delete、describe、get、apply etc. .
  2. TYPE: The type of resource object , Case sensitive , Can be singular 、 Plural or Short form means .
  3. NAME: The name of the resource object , Case sensitive . If you do not specify a name , The system will return to belong to TYPE A list of all the objects of , for example $ kubectl get pods Will return Back to all Pod A list of .
  4. flags:kubectl Optional parameters to the subcommand , For example, using “-s” Appoint API Server Of URL Address without default value .

2.2.kubectl Operational resource objects and their abbreviations

Resource object type



















































kubectl Working with multiple objects in one command

# Get multiple Pod Information about
kubectl get pods pod1 pod2
# Get multiple object type information
kubectl get pod/pod1 rc/rc1
# Apply more than one at the same time YAML file , With many -f file parameter
kubectl get pod -f pod1.yaml -f pod2.yaml
kubectl create -f pod1.yaml -f pod2.yaml

2.3.kubectl The subcommand explains

kubectl The subcommands are very rich , Covering the right to Kubernetes The main operation of the cluster , Including the creation of resource objects 、 Delete 、 see 、 modify 、 To configure 、 Operation, etc

Sons command




kubectl api-versions [flags]

List the current system supports API Version list , The format is group/version


kubectl apply -f FileName [flags]

From the configuration file or stdin Update the configuration of resource objects in


kubectl attach POD -c container [flags]

Go into a running container


kubectl auth [flags] [options]

testing RBAC permissions


kubectl autoscale (-f FileName|TYPE NAME)[--min=minPods]--max=maxPods [--cpu-percent=CPU][flags]

Yes Deployment、RelicaSet or ReplicationController Set the horizontal automatic expansion or reduction


kubectl cluster-info [flags]

Show clusters Master And information about the built-in Services


kubectl completion SHELL[flags]

Output shell Command execution result code


kubectl config SubCommand[flags]

modify kubeconfig file


kubectl convert -f FileName[flags]

Convert configuration files to different API edition


kubectl cordon Node[flags]

take Node Marked as unschedulable, That is to isolate the cluster scheduling range .


kubectl create -f FileName[flags]

Create a resource object from the configuration file


kubectl delete (-f FileName|TYPE NAME|-l label|--all)[flags]

According to the configuration file 、 Resource object name or label selector Delete resource object


kubectl diff -f FileName[options]

View the difference between the configuration file and the running resource object in the current system


kubectl drain Node[flags]

First of all, will Node Set to unschedulabel, Then delete in the node All of the Pod, But it won't be deleted APIServer Managed Pod


kubectl edit (-f FileName|TYPE NAME)[flags]

Edit the properties of the resource object , Online updating


kubectl exec Pod [-c container] [-i][-t][flags][--COMMAND][args...]

Execute a command in a container


kubectl explain [--include-extended-apis=true][--recursive=flags][flags]

A detailed description of resource object properties


kubectl expose(-f FileName|TYPE NAME)[--port=port][--protocol=TCP|UDP][--target-port=number-or-name][--name=name] [----extemal-ip=extemal-ip-of-service] [--type=type] [flags]

There will be one that already exists RC、Service、Deployment or Pod Exposed as a new Service


kubectl get (-f FileName|TYPE NAME|-l label)[--watch] [--sort-by=FIELD][[-o|--output]=OUTPUT_FORMAT] [flags]

Displays a summary of one or more resource objects


kubectl label (-f FileName|TYPE NAME)Key_1=Val_1 [--overwrite] [--all][resource-version=version][flags]

Set or update the resource object labels


kubectl logs Pod[-c container] [--follow] [flags]

Print a container log on the screen


kubectl patch (-f FileName|TYPE NAME)--patch PATCH [flags]

With merge Form to modify the values of some fields of the resource object


kubectl plugin [flags][options]

stay kubectl The command line uses user-defined plug-ins


kubectl port-forward Pod [LOCAL_PORT:]REMOTE_PORT

Map a port of this machine to Pod The port of , Usually used to test


kubectl replace -f FileName [flags]

Replace resource object from configuration file


kubectl rolling-update Old_controller_name ([new_controller_name]--image=new_contrainer_image|-f new_controller_spec)[flags]

Yes RC Rolling upgrade


kubectl rollout SUBCOMMAND [flags]

Yes Deployment Conduct management , Available operations include :history、pause、resume、undo、status


kubectl run name --image=image[-env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [flags]

Based on a mirror image in Kubernetes Start one on the cluster Deployment


kubectl scale (-f FileNale|TYPE NAME) --replicas=count [resource-version=version] [--current-replicas=count] [flags]

Capacity expansion 、 Let's shrink to one Deployment、ReplicaSet、RC or Job in Pod The number of


kubectl set SUBCOMMAND [flags]

Set a specific information of the resource object , At present, only support to modify the image of the container


kubectl taint NodeName key_1=val_1:taint_effect_1 [flags]

Set up Node Of taint Information , Used to put specific Pod Schedule to a specific Node The operation of .


kubectl top node |kubectl top pod

see Node or Pod Resource usage of , Need to run in a cluster Metrics Service


kubectl uncordon Node [flags]

take Node Set to sechedulable


kubectl version [--client] [flags]

Print system version information

2.4.kubectl parameter list

kubectl The common startup parameters of the command line are arranged in the following table

Parameter name and value example


- -alsologtostderr=false

Set to true Means that the log is output to a file at the same time stderr

-- as=

Set the user name for this operation


be used for CA Authorized by the cert File path


be used for TLS The client certificate file path of


be used for TLS The client of key File path


Set the... To use kubeconfig Medium cluster name


Set the kubeconfig Medium context The name of


Set to true To skip TLS Security authentication mode , Will make HTTPS Links are not secure


kubeconfig Profile path , Include in the configuration file Master Address information and necessary authentication information


The log didn't arrive file: Line number Print once stack trace


Log file path


Set up flush Log file interval


Set up true Output log to stderr, No output to log file


Set to true Indicates that the version number of the client must be consistent with that of the server


Set the location of this operation Namespace


Set up APIServer Of basic authentication Password


Set up API Server Of URL Address , The default value is localhost:8080


Set access APIServer The safety of the token


Specify user name ( Should be in kubeconfig The configuration file has set )


Set up APIServer Of basic authentication Username


glog The level of logging


glog Detailed log level based on Module

2.5.kubectl Output format

kubectl The command can display the results in a variety of formats , The output is formatted by -o ginseng Number assignment :

kubectl [command] [type] [name] -o=<output_format>

Output format



Output according to the custom column name , Separated by commas


Get the custom column name from the file and output


With json Format display results


Output resource object name only


Output extra information , about pod、 Will output where Node name


With YAML Format display results

  • Examples of common output formats
# Show Pod For more information
kubectl get pod <pod-name> -o wide
# With YAML Format display Pod Details of
kubectl get pod <pod-name> -o yaml
# Display with custom column names Pod Information about
kubectl get pod <pod-name> -o =custom-columns=name:.metadata.name,RSRC:.metadata.resourceVersion
# Custom column name output based on file
kubectl get pods <pod-name> -o=custom-file=template.txt
# Sort the output by a field
kubectl get pods --sort-by=.metadata.name

3.kubectl The operation sample

3.1. Create resource object

# according to YAML Configuration file created at one time Service and RC
kubectl create -f my-service.yaml -f my-rc.yaml

3.2. View resource objects

# View all Pod list
kubectl get pods
# see RC and Service list
kubectl get rc,service

3.3. Describe resource objects

# Show Node Details of
kubectl describe nodes <node-name>
# Show Pod Details of
kubectl get describe pods <pod-name>
# Show by RC Managed Pod Information about
kubectl describe pods <rc-name>

3.4. Delete resource object

# be based on pod.yaml Defined name delete Pod
kubectl delete -f pod.yaml
# Delete all containing a Label Of Pod and Service
kubectl delete pods,services -l name=<label-name>
# Delete all Pod
kubectl delete pods --all

3.5. Execute the command of the container

# perform Pod Of date command , By default Pod No 1 Containers execute
kubectl exec <pod=name> data
# Appoint Pod A container in the date command
kubectl exec <pod-name> -c <container-name> data
# adopt bash get Pod Of a container in TTY, Equivalent to login container
kubectl exec -it <pod-name> -c <container-name> /bin/bash

3.6. View the container's log

# See the container output to stdout Log
kubectl logs <pod-name>
# Track and view container logs , amount to tail -f Results of command
kubectl logs -f <pod-name> -c <container-name>

3.7. Create or update resource objects

# Usage and kubectl create similar , The logic is slightly different : If the target resource object does not exist stay , Then create ; Otherwise update it
kubectl apply -f app.yaml

3.8. Edit running resource objects online

After command execution , Will pass YAML Format shows the definition and state of the object , Users can edit and save the code , In order to complete the direct modification of online resources .

# have access to kubectl edit Command to edit the running resource object , For example, use the following Command to edit one of the running Deployment
kubectl edit deploy nginx

3.9. take Pod Open port mapping to local

# Will cluster on Pod Of 80 Port mapping to local 8888 port , In the browser. You can access the services provided by the container in
kubectl port-forward --address pod/nginx-6ddbbc47-sfdcv 8888:80

3.10. stay Pod Copy files between and local

# hold Pod Upper /etc/fstab Copy to local /tmp Catalog
kubectl cp nginx-6ddbbc47-sfdcv:/etc/fstab /tmp

3.11. Label settings for resource objects

# by default namespace Set up testing=true label
kubectl label namespaces default testing=true

3.12. Check what is available API List of resource types

# This command is often used to check whether a specific type of resource has been defined , List all the resources object type
kubectl api-resources

3.13. Use the command line plug-in

The executable file name of the user-defined plug-in needs to use “kubectl-” start , Copied to the $PATH A directory in ( Such as /usr/local/bin), Then you can go through kubectl Running custom plug-ins . for example , Implement a program called hello Plug in for , Its The function is to output string on the screen “hello world”

New name is kubectl-hello The executable script file of , Its content is

echo "hello world"

Copy kubectl-hello File to /usr/local/bin/ Under the table of contents , The plug-in installation is completed Work . And then in kubectl After the command, you can use the plug-in with the plug-in name :

kubectl hello
hello world

Use kubectl plugin list The command can view the list of plug-ins installed in the current system surface :

kubectl plugin list

More complete plug-in development examples can be found from https://github.com/kubernetes/sample- cli-plugin find .

This article is from WeChat official account. - 7DGroup(Zee_7DGroup) , author :Bruce

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2020-11-16

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

本文为[High rise zee]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云