Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

First acquaintance of Kubernetes (K8s): detailed use of the kubectl command

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

I. Kubectl Usage Overview

kubectl command line syntax is as follows:

kubectl [command] [type] [name] [flags] Parameter description: command: Subcommand, command used to manage and operate Kubernetes cluster resource objects. For example: create, delete, describe, get, apply, etc. type: The type of resource object, case-sensitive, can be expressed in singular, plural, or abbreviated form. kubectl get pod pod1 #singular kubectl get pods pod1 #plural form kubectl get po pod1 #Abbreviated form name: The name of the resource object, case-sensitive, if no name is specified, the system returns a list of all objects belonging to type. flags: Optional parameters to the kubectl subcommand, such as using "-s" to specify the apiserver URL address instead of the default.

Table 1: Resource object types that kubectl can manipulate

Name abbreviation of resource object clustersgroupentstatusescsconfigurmapscmdaemonsetsdsdeploymentsdeploymentpointsepeventsevizontalpodautoscalershpaingressesingJobslimitrangeslimitsnodesnonamespacesnsnetworkpoliciesstatefulsetspersistentvolumeclaimspvcpersistentvolumespopodsecuritypoliciessppodtemplatesreplicasetsrsreplicationcontrolersrccronjobsecretsserviceaccountsservicesvcstorageclassesscthirdpartyresources II, kubectl subcommand

Kuberctl subcommands are very rich, including resource object creation, deletion, view, modify, configure, run and so on.

Table 2: Kubectl Subcommand Syntax

III. Kubectl parameter

Table 3: Kubectl Command Line Common Startup Parameters

Each subcommand (e.g. create, delete, get, etc.) also has a specific flags parameter, which can be viewed with kubectl [command] --help.

IV. Kubectl output format

The kubectl command can display results in a variety of formats, the output format specified by the-o parameter:

kubectl [command] [TYPE] [NAME] -o=

Table 4: Output format of kubectl command

Examples of common output formats are as follows:

(1) Display more information about the Pod

kubectl get pod -o wide

(2) Display Pod details in yaml format

kubectl get pod -o yaml

(3) Display Pod information with custom column names

kubectl get pod -o=custom-columns=NAME:.metadata.name,RSRC:.metadata.resourceVersion

(4) File-based custom column name output

kubectl get pods -o=custom-columns-file= template.txttemplate.txt The contents of the file are: NAME RSRCmetadata.name metadata.resourceVersion Output: NAME RSRCPod-name 52305

Alternatively, you can sort the output by a field, specified as a jsonpath expression with the--sort-by parameter:

kubectl [command] [TYPE] [NAME] --sort-by=

For example, sort by name

kubectl get pods --sort-by=.metadata.name V. kubectl operation example

1. Create service and rc at one time according to yaml configuration file

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

2. Create according to the definitions of all.yaml,.yml and.json files in the directory

kubectl create -f

3. View a list of all pods

kubectl get pods

4. View rc and service list

kubectl get rc,service

5. Display node details

kubectl describe nodes

6. Display Pod Details

kubectl describe pods/

7. Display Pod information managed by RC

kubectl describe pods

Delete pods defined based on the pod.yaml file

kubectl delete -f pod.yaml

Delete all Pods and Services that contain a label

kubectl delete pods,services -l name=

Delete all pods

kubectl delete pods --all

11. Execute the date command in the pod container. By default, the first container in the Pod is used.

kubectl exec date

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

kubectl exec -c date

13. Log in to a container in Pod as bash

kubectl exec -it -c /bin/bash

14. View the log of container output to stdout

kubectl logs

15. Track and view the log of the container, equivalent to the result of the tail -f command

kubectl logs -f -c

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report