Main points:
1. Overview of kubectl
2. kubectl syntax
3. kubectl Management Commands
4. Similarities and differences between create and apply commands
5. Example demonstration
1. Overview of kubectl
Kubectl is a command line interface for running commands against the Kubernetes cluster.The configuration file for Kubectl is in the $HOME/.kube directory.We can specify a kubeconfig file in another location by setting the KUBECONFIG environment variable or the command parameter, kubeconfig.
2. kubectl Syntax
1. Run the command from the terminal window using the following syntax, kubectl:
kubectl [command] [TYPE] [NAME] [flags]
2. where command, TYPE, NAME, and flags are:
- command
Specify the operations to be performed on one or more resources, such as create, get, describe, delete.For example, "kubectl get cs".
- TYPE
Specify the resource type.Resource types are case-insensitive and can be singular, plural, or abbreviated.
Example:
#The following commands produce the same output kubectl get pod pod1 kubectl get pods pod1 kubectl get po pod1
- NAME
Specify the name of the resource.Names are case sensitive.If the name is omitted, details of all resources, such as "kubectl get pods", are displayed. When working with multiple resources, we can specify each resource by type and name, or one or more files:
1. To specify resources by type and name:
- If the resource types are the same, group the resources:
TYPE1 name1 name2 name<#>.
Example:
kubectl get pod example-pod1 example-pod2
- Specify multiple resource types:
TYPE1/name1 TYPE1/name2 TYPE2/name3 TYPE<#>/name<#>.
Example:
kubectl get pod/example-pod1 replicationcontroller/example-rc1
2. To specify resources using one or more files:
-f file1 -f file2 -f file<#> #Be aware of using YAML instead of JSON, as YAML is often more user-friendly, especially for configuration files.
Example:
kubectl get pod -f ./pod.yaml
- flags:
The optional flag specified, but it is worth noting that specifying parameters using the command line overrides the default values and associated environment variables. For example, we can use the -s or--server flag to specify the address and port of the Kubernetes API server.
3. kubectl Management Commands
type | command | describe |
---|---|---|
Basic Commands | create | Create resources by filename or standard input |
expose | Exposing a resource as a new Service | |
run | Run a specific image in a cluster | |
set | Setting specific functions on objects | |
get | Show one or more resources | |
explain | Document Reference | |
edit | Edit a resource using the default editor | |
delete | Delete resources by file name, standard input, resource name, or label selector | |
Deployment Command | rollout | Manage Resource Publishing |
rolling-update | Rolling update for a given replication controller | |
scale | Expand or shrink the number of Pod s, Deployment, ReplicaSet, RC, or Job | |
autoscale | Create an automatic selection to expand or shrink and set the number of Pod s | |
Cluster Management Commands | certificate | Modify Certificate Resource |
cluster-info | Show cluster information | |
top | Display resource usage (CPU/Memory/Storage).Require Heapster to run | |
cordon | Marker node is not dispatchable | |
uncordon | Tag node dispatchable | |
drain | Expulsion node application, ready for offline maintenance | |
taint | Modify node taint tag | |
Debugging Commands | describe | Show details of a particular resource or resource group |
logs | Print a container log in a Pod.If the Pod has only one container, the container name is optional | |
attach | Attach to a running container | |
exec | Execute command to container | |
port-forward | Forward one or more local ports to a pod | |
proxy | Run a proxy to the Kubernetes API server | |
cp | Copy a file or directory into a container | |
auth | Check Authorization | |
Advanced Commands | apply | Apply configuration to resources by filename or standard input |
patch | Use patches to modify and update fields of resources | |
replace | Replace a resource with a file name or standard input | |
convert | Converting profiles between different API versions | |
set command | label | Update labels on resources |
annotate | Update comments on resources | |
completion | Used for automatic completion of kubectl tools | |
Other Commands | api-versions | Print supported API versions |
config | Modify the kubeconfig file (used to access API s, such as configuring authentication information) | |
help | All Command Help | |
plugin | Run a command line plugin |
4. Similarities and differences between create and apply commands
- Create from YAML file
kubectl create -f FILENAME [options]
- Upgrade deployment through YAML file
kubectl apply -f FILENAME
- Same point
If the kind value in the yaml file is deployment, both commands above can create a deployment and generate the appropriate number of pod s
- Difference
create command: delete all existing things first, and regenerate new ones from the yaml file.So the configuration in the yaml file must be complete apply command: Upgrade an existing one according to what is listed in the configuration file.So the contents of the yaml file can only write attributes that need to be upgraded
5. Example demonstration:
- Life cycle of project
Create->Publish->Update->Rollback->Delete
1. Create nginx
#Common syntax: kubectl run NAME --image=image [--env="key=value" parameter] [--port=port port] [--replicas=replicas replicas replicas set] [--dry-run=bool state] [--overrides=inline-json] [--command command] -- [COMMAND] [args...] [options]` kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 #View pod nodes kubectl get pods
2. Publish nginx service to provide load balancing
#Common syntax: kubectl expose (-f FILENAME | TYPE NAME) [--port=port f or internal communication between port clusters] [--protocol=TCP|UDP|SCTP] [--target-port exposed port=number-or-name] [--name=name specified name] [--name=name specified name] [--external-ip=external-ip-of-service] [--type=type specified type] [options] kubectl expose deployment nginx-deployment --port=80 --target-port=80 --name=nginx-deployment-service --type=NodePort #View service publication, abbreviation of svc bit service component here kubectl get pods,svc #View Backend Associated Nodes kubectl get endpoints #View network status details kubectl get pods -o wide #View ports for services kubectl get svc #kube-proxy supports three modes in kubernetes. Before v1.8 we used iptables and userspace, and after kubernetes 1.8 we introduced ipvs #Node node installs ipvsadmin tool to view service ports yum install ipvsadm -y ipvsadm -L -n #Looking at it with the tool, you can see that the scheduling algorithm is rr polling mode
#View access log on master side kubectl get pods
3. Update nginx version 1.14
- Google Browser reloads the refresh page to view nginx version information
View nginx version steps: F12 opens developer options - > F5 refresh access - > Find Network - > Click name - > Find headers header information
kubectl set image deployment/nginx-deployment nginx-deployment=nginx:1.14
4. Rollback nginx
#View historical versions kubectl rollout history deployment/nginx-deployment #Execute rollback to previous version kubectl rollout undo deployment/nginx-deployment #Check rollback status kubectl rollout status deployment/nginx-deployment
5. Delete nginx
#Delete deployment kubectl delete deployment/nginx-deployment #Delete Service SVC kubectl get svc
6. Other Commands
- View details of specific resources
kubectl describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME) [options]
- View deployment resources
kubectl describe deployment/nginx-deployment
- View resource object abbreviations
kubectl api-resources
- Enter the corresponding pod
kubectl exec POD [-c CONTAINER] -- COMMAND [args...] [options]
- View the resulting YAML format file
kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml
- View the generated JSON format file
kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o json
- Generate YAML, JSON file export
kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o json(yaml) > test.json(yaml)
- Export existing resources
kubectl get svc/nginx-svc --export -o yaml > nginx-svc.yaml
- Test command correctness, do not execute (like sh-n in shell scripts)
kubectl run nginx --image=nginx --port=80 --replicas=2 --dry-run