kubernetes-kubectl operation example

Posted by khayll on Sat, 22 Jan 2022 06:27:48 +0100

1. Create resource objects

Create a Service and RC at once from a YAML configuration file:

kubectl create -f my-service.yaml -f my-rc.yaml

According to all under the directory. yaml,. yml,. The definition of the json file is created:

kubectl create -f <directory>

2. View Resource Objects

View all Pod lists:

kubectl get pods

View RC and Service lists:

kubectl get rc,service

3. Describe the resource object

Show details of Node

kubectl describe nodes <node-name>

Show Pod details

kubectl describe pods/<pod-name>

Display information about Pod s managed by RC

kubectl describe pods <rc-name>

4. Delete resource objects

Based on pod.yaml Defined Name Delete Pod

kubectl delete -f pod.yaml

Delete all PD and Service that contain a Label

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

Delete all Pod s

kubctl delete pods --all

5. Execute Container Commands

Execute Pod's date command, using the first container in the Pod by default

kubectl exec <pod-name> date

Specify a container in Pod to execute the date command:

kubectl exec <pod-name> -c <container-name> date

Getting TTY from a container in a Pod through bash is equivalent to login container:

kubctl exec -it <pod-name> -c <container-name> /bin/bash

6. View container logs

View the log of container output to stdout

kubectl logs <pod-name>

Tracking and viewing the container's logs is equivalent to the result of the tail-f command:

kubectl logs -f <pod-name> -c <container-name>

7. Create or update resource objects

Usage is similar to kubectl create, with slightly different logic: if the target resource object does not exist, it is created; Otherwise update, for example:

kubectl apply -f app.yaml

8. Edit running resource objects Online

You can use the kubectl edit command to edit a running resource object, such as editing a Deployment in a run using the following command:

kubectl edit deploy nginx

After the command executes, the definition and state of the object are displayed in YAML format, and the user can edit and save the code to complete the direct modification of online resources.

9. Map Pod's open port to local

Map the 80 ports of Pod on the cluster to the local 8888 port in the browser

http://127.0.0.1:8888 Medium to access the services provided by the container:

kubectl port-forward --address 0.0.0.0 pod/nginx-6ddbbc-sfdcv 8888:80

10. Copy files between Pod and local

Copy/etc/fstab on Pod to local/tmp directory:

kubectl cp nginx-sdfdsdf-sfdcv:/etc/fstab /tmp

11. Label settings for resource objects

Set the test=true tag for default namespace:

kubectl label namespaces default testing=true

12. Check the list of available API resource types

This command is often used to check if a particular type of resource has been defined, listing all resource object types:

kubctl api-resources

13. Use command line plug-ins

To extend the functionality of kubectl, Kubernetes introduced a plug-in mechanism from version 1.8, reaching a stable version at version 1.14.

The executable name of the user-defined plug-in needs to start with "kubectl-" and be copied to

A directory in $PATH (for example, /usr/local/bin) can then be accessed via kubectl

Run the custom plug-in. For example, implement a plug-in called Hello that outputs the string "hello world" on the screen:

Create a new executable script file named kubectl-hello with the following contents

echo "hello word"

Copy the kubectl-hello file to the /usr/local/bin/directory and you are done installing the plug-in.

Then you can use the plug-in with the name of the plug-in after the kubectl command:

# kuectl hello
hello word

Use the kubectl plugin list command to view a list of plugins installed on the current system:

kubectl plugin list

More complete plug-in development examples are available: Plug-in plugin
https://github.com/kubernetes/sample- cli-plugin

Topics: Kubernetes Cloud Native kubectl