preface
We must build up the environment, see is not to solve the problem, must be actual combat. This article introduces the use of Kubectl.
Kubernetes series:
- Kubernetes is introduced
- Kubernetes environment setup
Kubectl core function is through the API Server operation Kubernetes various resource objects, including the creation of resource objects, delete, view, modify, configure, run and so on, learning Kubectl related commands is a means to understand Kubernetes cluster management.
Kubectl grammar
kubectl [command] [TYPE] [NAME] [flags]
Copy the code
- Command: Specifies the operation to be performed on one or more resources. For example: create, get, describe, delete, apply;
- TYPE: specifies the resource TYPE. Resource types are case sensitive, you can specify singular, plural, or abbreviated;
- Name: Specifies the Name of the resource. The name is case sensitive. If you omit the name, the details of all resources are displayed;
Type name1 name2
kubectl get pod example-pod1 example-pod2
Type /name type2/name2
kubectl get pod/example1 replicationcontroller/example2
# Apply multiple YAML files simultaneously, represented by multiple -f flie parameters
kubectl get pod -f pod1.yaml -f pod2.yaml
Copy the code
- Specify optional tags. For example, you can specify the address and port of the Kubernetes API server using the -s or –server identifiers; -n Specifies the namespace.
The output format
The kubectl command can format the results in a variety of formats. The output format is specified with the -o argument:
kubectl [command] [TYPE] [NAME] -o <output_format>
Copy the code
Common output formats are as follows:
- Custom column name display;
kubectl get pod
#value is the POD definition field
kubectl get pods -o custom-columns=NAME:.metadata.name,UID:.metadata.uid,imageName:.spec.containers[0].image
Copy the code
- Custom column name configuration file name;
# Customizing requires display of column name files
vim custom-col.conf
# Enter the following
NAME UID imageName containerPort
metadata.name metadata.uid spec.containers[0].image spec.containers[0].ports[0].containerPort
# specify file output
kubectl get pod -o custom-columns-file=custom-col.conf
Copy the code
- Output in JSON format;
kubectl get pod -o json
Copy the code
- Output jsonPath file contents;
kubectl get pods -o jsonpath='{.items[0].metadata.name},{.items[0].spec.containers[0].image}'
Copy the code
- Output custom content through a custom JsonPath file;
# Customize JSONPath file
vim custom-json.conf
# Enter custom content
{.items[0].metadata.name},{.items[0].spec.containers[0].image},{.items[0].spec.containers[0].ports[0].containerPort}
Output to format
kubectl get pod -o jsonpath-file=custom-json.conf
Copy the code
- Only the name of the resource object is printed;
kubectl get pod -o name
Copy the code
- Show more Pod information;
kubectl get pod -o wide
Copy the code
- Display the results in YAML format;
kubectl get pod -o yaml
Copy the code
Kubectl command many, we can refer to the official documentation, here will not introduce each command in detail, as long as you master the common command, the following length is to introduce you commonly used commands;
Kubectl common command
- Kubectl create/replace and Kubectl apply deploy or replace resources. Kubectl Replace uses API objects in YAML files to replace existing API objects. Kubectl apply is a patch operation on the original API object.
Create a Deployment resource object using the YAML file from the previous section
kubectl create -f nginx-deployment.yaml
You can also modify the nginx-deployment.yaml file by using kubectl edit.
# after using Kubectl replace to trigger the update we changed the limited memory to 256, this way we call the imperative configuration file operation
kubectl replace -f nginx-deployment.yaml
We can also use Kubectl apply to create object resources
kubectl apply -f nginx-deployment.yaml
Yaml file and execute kubectl apply to complete the resource modification
kubectl apply -f nginx-deployment.yaml
Copy the code
- Kubectl get lists one or more resource objects of the same type;
# list all pods
kubectl get pods
List all pods on a nodeKubectl get pods --field-selector=spec.nodeName= nodeNameFilter pods labeled testing=true
kubectl get pod -l testing=true
Copy the code
- Kubectl describe returns node information, node time, and so on. Kubectl get users view one or more resource objects of the same type. Kubectl get users view one or more resource objects of the same type.
View the pod details
kubectl describe pod/nginx-deployment-776b4d7488-jfcjq
# check node details
kubectl describe nodes/demo-work-1
Copy the code
- Kubectl delete deletes the specified resource object entered. You can also delete the specified resource range by label selector, name selector, or resource selector.
Delete pods of the specified type
kubectl delete -f pod.yaml
# delete all label-key:label-value information
kubectl delete pods -l label-key=label-value
Copy the code
- Kubectl exec runs commands in the pod container;
The /bin/bash interface is displayed
kubectl exec -it nginx-deployment-776b4d7488-jfcjq -- /bin/bash
Copy the code
- Kubectl logs Print the container logs in Pod.
# print log
kubectl logs nginx-deployment-776b4d7488-jfcjq
Monitor pod logs continuously
kubectl logs -f nginx-deployment-776b4d7488-jfcjq
Copy the code
- Kubectl Edit online editing of resource objects;
Edit deployment resources online
kubectl edit deploy nginx-deployment
Copy the code
- Map Pod port to host port;
Map Pod 80 port to host 8888Kubectl port-forward --address 172.21.122.230,localhost pod/nginx-deployment-776b4d7488-jfcjq 8888:80Open another interface accessThe curl http://172.21.122.230:8888Copy the code
- Copy files between container and host;
kubectl cp nginx-deployment-776b4d7488-jfcjq:/etc/fastab /tmp
Copy the code
- Set the resource object label.
# set pod to "testing=true"
kubectl label pod nginx-deployment-776b4d7488-jfcjq testing=true
Filter pods labeled testing=true
kubectl get pod -l testing=true
Copy the code
- The most important command, the help command;
# list the types of commands
kubectl
# list the usage document of a certain command
kubectl command --help
Copy the code
The end of the
Welcome everyone little attention, little praise!