We already know the common terms and ideas of Kubernetes, and to do secondary development, or simply to run and run a small instance, we need to be familiar with the common operations of TA. Kubectl is a very quick way to get started, so let’s take a look at the common ways kubectl command line operations are used.

1. Kubectl

  1. Kubectl grammar

    kubectl [command] [Type] [NAME] [flags]

    Command: subcommands used to manipulate kubernetes cluster resource objects, such as create, delete, describe, get, apply, etc

TYPE: The TYPE of the resource object. It is case sensitive and can be singular, plural, or abbreviated. For example, TYPE is equivalent in the following 3.

kubectl get pod pod1
kubectl get pods pod1
kubectl get po pod1
Copy the code

NAME: Case sensitive NAME of the resource object. If you do not specify a name, the system returns a list of all objects belonging to TYPE. For example, kubectl get Pods returns a list of all pods

Flags: an optional argument to the kubectl subcommand, for example, using -s to specify the API server URL instead of the default.

Kubectl operable resource object types and abbreviations:

It is also possible to operate on multiple resource objects simultaneously in a command line, expressed as a combination of types and names, as shown in the following example: Get information about multiple pods: kubectl get Pods POd1 Pod2 Get information about multiple objects: Kubectl get pod/pod1 rc/rc1

kubectl get pod -f pod1.yaml -f pod2.yaml
kubectl create -f pod1.yaml -f rc1.yaml -f service1.yaml
Copy the code

2. Kubectl subcommand explanation

Kebectl subcommands are very rich, covering the main operations on kubernetes cluster, including the creation, deletion, viewing, modification, configuration, running of resource objects, etc. Table 2.10 shows the detailed subcommands:

3. Kubectl parameter list

The Kubectl command line public startup parameters are as follows:

Kubectl output format

The kubectl command can display the results in a variety of formats. The output format is specified with the -o argument:

5. Kubectl operation example

1. Create the service and rc at once from the YAML configuration file

kubectl create -f my-service.yaml -f my-rc.yaml
Copy the code

Kubectl create -f

kubectl get Pods kubectl get pods kubectl Kubectl describe nodes

6, kubectl describe pods/ Kubectl delete -f pod.yaml kubectl delete -f pod.yaml kubectl delete -f pod.yaml Delete all pods and services containing a label

kubectl delete pods,services -l name=<label-name>
Copy the code

Kubectl exec date kubectl exec date kubectl exec date

12. Specify a container in Pod to execute the date command

kubectl exec <pod-name> -c <container-name> date
Copy the code

Bash login to a Pod container

kubectl exec -it <pod-name> -c <container-name> /bin/bash
Copy the code

Kubectl logs kubectl logs kubectl logs kubectl logs

kubectl logs -f <pod-name> -c <container-name>
Copy the code