1. Overview of Kubectl
Kubectl is the command line tool of Kubernetes cluster, through Kubectl can manage the cluster itself, and can install and deploy containerized applications on the cluster. The syntax for running kubectl looks like this:
$ kubectl [command] [TYPE] [NAME] [flags]Copy the code
Here the command, TYPE, NAME and flags are:
- Comand: Specifies the operations to be performed on the resource, such as create, GET, Describe, and DELETE
- TYPE: Specifies the resource TYPE. The resource TYPE is sensitive and can be singular, plural, or abbreviated by the developer. Such as:
$ kubectl get pod pod1
$ kubectl get pods pod1
$ kubectl get po pod1Copy the code
- NAME: Specifies the NAME of the resource. The NAME is also case-sensitive. If the name is omitted, all resources are displayed, for example:
$ kubectl get podsCopy the code
- Flags: Specifies optional parameters. For example, you can specify the address and port of the Kubernetes API Server using either -s or -server arguments.
In addition, you can get more information by running the kubectl help command.
1.1 Kubectl operation
As a command line tool of Kubernetes, Kubectl’s main responsibility is to operate the resource objects in the cluster, including the creation, deletion and view of the resource objects. The following table shows all of the operations supported by Kubectl, along with their syntax and description information:
operation | grammar | describe |
---|---|---|
annotate | Kubectl annotate (-f FILENAME \ | TYPE NAME \ | TYPE/NAME) KEY_1 = VAL_1… KEY_N=VAL_N [-overwrite] [-all] [-resource -version=version] [flags] | Adds or updates comments for one or more resources |
api-versions | kubectl api-versions [flags] | Lists the API versions available |
apply | kubectl apply -f FILENAME [flags] | Apply configuration changes from files or STDIN to primary objects. |
attach | kubectl attach POD -c CONTAINER [-i] [-t] [flags] | Connect to a running container to view the output stream or interact with the container (STDIN). |
autoscale | Kubectl autoscale (-f FILENAME \ | TYPE NAME \ | TYPE/NAME) [- min = MINPODS] – Max = MAXPODS [– -percent = CPU CPU] [flags] | Automatic expansion of Pod managed by replica controller. |
cluster-info | kubectl cluster-info [flags] | Displays endpoint information for primary nodes and services in the cluster. |
config | kubectl config SUBCOMMAND [flags] | Modify kubeconfig file. |
create | kubectl create -f FILENAME [flags] | Create one or more resource objects from a file or stdin. |
delete | Kubectl delete (-f FILENAME \ | TYPE [NAME \ | label/NAME \ | – l \ | -all]) (flags) | Delete a resource object. |
describe | kubectl describe (-f FILENAME \| TYPE [NAME_PREFIX \| /NAME \| -l label]) [flags] | Displays the details of one or more resource objects. |
edit | kubectl edit (-f FILENAME \| TYPE NAME \| TYPE/NAME) [flags] | Edit and update one or more resource objects on the server through the default editor. |
exec | Kubectl exec POD [-c CONTAINER] [-i] [-t] [flags] [– COMMAND [args…]] | Execute a command in the Pod container. |
explain | Kubectl explain [– include-extended-apis=true] [– recursive=false] [flags] | Get documentation for resource objects such as pods, Nodes, and services. |
expose | Kubectl expose (-f FILENAME \ | TYPE NAME \ | TYPE/NAME) [- port = port] [- TCP \ | UDP protocol =] [-target – port = number – or – NAME] [- name = name] [- external IP = external – IP – of – service] [- type = type] [flags] | Expose a new service for replica controllers, services, or PODS, etc. |
get | Kubectl get (-f FILENAME \ | TYPE [NAME \ | / label NAME \ | – l]) [- watch] [- sort – by = FIELD] [[-o \ | – output] = OUTPUT_FORMAT] [flags] | List one or more resources.
|
label | Kubectl label (-f FILENAME \ | TYPE NAME \ | TYPE/NAME) KEY_1 = VAL_1… KEY_N=VAL_N [-overwrite] [-all] [-resource -version=version] [flags] | Adds or updates labels for one or more resource objects. |
logs | Kubectl logs POD [-c CONTAINER] [– follow] [flags] | Displays logs for a container in Pod. |
patch | Kubectl patch (-f FILENAME \ | TYPE NAME \ | TYPE/NAME) – patch patch (flags) | Update one or more fields in a resource object using the policy merge patch procedure. |
port-forward | Kubectl port-forward POD [LOCAL_PORT:]REMOTE_PORT [… [LOCAL_PORT_N:]REMOTE_PORT_N] [flags] | Forward one or more local ports to Pod. |
proxy | Kubectl proxy [– port= port] [– WWW =static-dir] [– www-prefix=prefix] [– api-prefix=prefix] [flags] | Run a proxy for the Kubernetes API server. |
replace | kubectl replace -f FILENAME | Replace a resource object from a file or stdin. |
rolling-update | Kubectl rolling – update OLD_CONTROLLER_NAME ([NEW_CONTROLLER_NAME] – image = NEW_CONTAINER_IMAGE \ | – f NEW_CONTROLLER_SPEC) [flags] | Rolling updates are performed by progressively replacing the specified replica controller and Pod. |
run | Kubectl run NAME — image=image [– env= “key=value”] [– port=port] [– replicas=replicas] [– dry-run=bool] [- overrides = the inline – json] [flags] | Runs a specified image on the cluster. |
scale | Kubectl scale (-f FILENAME \ | TYPE NAME \ | TYPE/NAME) – replicas = COUNT [- resource – version = version] [- current – replicas = count] [flags] | Expand the number of replica sets of accommodation. |
version | Kubectl version – [client] [flags] | Displays the versions of Kubernetes running on both the client and server sides. |
1.2 Resource Object Types
In Kubernetes, there are a number of resource objects that developers and operations personnel can use to orchestrate containers. In the following table, are the resource object types supported by Kubectl, along with their abbreviated aliases:
Resource object type | The thumbnail alias |
---|---|
apiservices | |
certificatesigningrequests | csr |
clusters | |
clusterrolebindings | |
clusterroles | |
componentstatuses | cs |
configmaps | cm |
controllerrevisions | |
cronjobs | |
customresourcedefinition | crd |
daemonsets | ds |
deployments | deploy |
endpoints | ep |
events | ev |
horizontalpodautoscalers | hpa |
ingresses | ing |
jobs | |
limitranges | limits |
namespaces | ns |
networkpolicies | netpol |
nodes | no |
persistentvolumeclaims | pvc |
persistentvolumes | pv |
poddisruptionbudget | pdb |
podpreset | |
pods | po |
podsecuritypolicies | psp |
podtemplates | |
replicasets | rs |
replicationcontrollers | rc |
resourcequotas | quota |
rolebindings | |
roles | |
secrets | |
serviceaccounts | sa |
services | svc |
statefulsets | |
storageclasses |
1.3 Output Options
The default output format of Kubectl is plain text. You can specify the output format of the command through the -o or -output field.
$ kubectl [command] [TYPE] [NAME] -o=<output_format>Copy the code
The output format | describe |
---|---|
-o=custom-columns=<spec> | Print the table using custom columns separated by commas. |
-o=custom-columns-file=<filename> | Print the table using custom columns from the file. |
-o=json | Outputs an API object in JSON format |
-o=jsonpath=<template> | Print fields defined in the JSONPath expression |
-o=jsonpath-file=<filename> | Print the fields defined as jSONPATH expressions in the file |
-o=name | Only the name of the resource object is printed. |
-o=wide | Output plain text with additional information. For the Pod object, the Node name will be included. |
-o=yaml | Output YAML API objects |
2. Kubectl installation and deployment
2.1 The installationkubectl
1) Download Kubectl
Here is under the Windows installation, so download kubectl. Exe, download address: storage.googleapis.com/kubernetes-… .
Add the address of kubectl.exe to the Path of Windows environment variable.
Please refer to: the other operating systems, the installation kubectl kubernetes. IO/docs/tasks /…
2) Configure the Kubeconfig file
RKE deploys a local file in the same directory as the configuration file that contains kube configuration information to connect to the newly generated cluster.
By default, the kube configuration file is called.kube_config_cluster.yml. Copy this file to your local ~/.kube/config and you can use kubectl locally.
Note that the deployed local KUBE configuration name is associated with the cluster configuration file. For example, if you use a configuration file named mycluster.yml, the local kube configuration will be named.kube_config_mycluster.yml.
2.2 validation
Execute kubectl to obtain information about nodes:
$ kubectl get nodesCopy the code
3, Kubectl common command examples
Examples of common commands are provided in this section to help you quickly get to know and try Kubectl.
1) kubectl create command
This command creates a resource object from a file or stdin. Assuming there is a YAML configuration file for nginx deployment, you can create the deployment object by executing the following command.
$ kubectl create -f nginx-deployment.yamlCopy the code
ApiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: nginx spec: replicas: 10 selector: matchLabels: app: nginx revisionHistoryLimit: 2 template: metadata: labels: app: nginx spec: ImagePullSecrets: - name: dC-hSPFD containers: # imagePullPolicy: nginx imagePullPolicy: Name: nginx80 # volumeMounts: - mountPath: /usr/share/nginx/html name: nginx-data-mountpath: /etc/nginx name: nginx-conf volumes: # Host directory Nginx-data NFS: path: /k8s-nfs/nginx server: 192.168.8.150 - name: nginx-conf NFS: path: /k8s-nfs/nginx/conf server: 192.168.8.150Copy the code
2) kubectl get
Use this command to list one or more resource objects, where all deployments under the default namespace are obtained using the kubectl get command.
3) Kubectl describe command
This command is used to display the details of one or more resource objects, in this case by obtaining the above nginx deployment information.
$ kubectl describe deployments/nginxCopy the code
4) kubectl exec command
This command is used to execute a command on a Pod container, where the /bin/bash command is executed on an nginx container.
$ kubectl exec -it nginx-5847748bf9-49k5k /bin/bashCopy the code
Kubectl logs
This command is used to obtain the log information of a Pod container.
$ kubectl logs nginx-5847748bf9-49k5kCopy the code
6) kubectl delete command
This command is used to delete an existing resource object from a cluster by specifying a name, label selector, or resource selector. Delete the nginx deployment you created earlier here.
$ kubectl delete deployments/nginxCopy the code
The resources
1. The kubectl Usage Conventions address: https://kubernetes.io/docs/reference/kubectl/conventions/
2. The kubectl Commands address: https://kubernetes.io/docs/reference/kubectl/kubectl-cmds/
3. “the Overview of kubectl address: https://kubernetes.io/docs/reference/kubectl/overview/
About the author: Ji Xiangyuan, product manager of Beijing Shenzhou Aerospace Software Technology Co., LTD. The copyright of this article belongs to the author.