当前位置:网站首页>[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl

[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl

2022-07-05 08:07:00 It's a bubble

This article has been featured 《 Learn from scratch k8s》 Included

 Insert picture description here

summary

kubectl yes Kubernetes Command line tools for clustering , adopt kubectl Be able to manage the cluster itself , And can install and deploy container applications on the cluster .

 Insert picture description here

Command format

The command format is as follows

kubectl [command] [type] [name] [flags]

Parameters

  • command: Specifies the action to be performed on the resource , for example create、get、describe、delete

  • type: Specify the resource type , Resource types are case sensitive , Developers can use the singular number , The plural , Abbreviated form specifies .

  • name: Specify the name of the resource , Names are also case sensitive , If you omit the name , All resources will be displayed .

  • flags: Specify additional optional parameters , For example, you can use -s perhaps -server Parameter assignment Kubernetes API server The address and port of

for example :

kubectl get pod pod1
kubectl get pods pod1
kubectl get po pod1
kubectl get pods

 Please add a picture description

Common command

kubectl help For more information

adopt help command , Can get help information

#  obtain kubectl The order of 
kubectl --help

#  Get the introduction and use of a command 
kubectl get --help

#  View the current status 
kubectl get cs

#  View all pod
kubectl get pod 

#  View a certain pod
kubectl get pod pod_name

#  View a certain pod, With yaml Format display results 
kubectl get pod pod_name -o yaml

Basic commands

Common basic commands

command Introduce
create Create resources by filename or standard input
expose Expose a resource as a new Service
run Run a specified image in the cluster
set Set specific functions on objects
get Show one or more resources
explain Show document references
edit Use the default editor to edit a resource
delete By file name , The standard input , Resource name or tag to delete the resource
apply Apply configuration to resources by filename or standard input
patch Patch modification 、 Update resources
replace Replace a resource with a filename or standard input
convert Different API Convert configuration files between versions
label Update the label on the resource
annotate Update comments on resources
completion Used to implement kubectl The tool automatically completes
api-versions Print supported API edition
config modify kubeconfig file ( Used to access the API, For example, configure authentication information )
help All command help
plugin Run a command line plug-in
version Print client and service version information

Deployment command

command Introduce
rollout Manage the release of resources
rolling-update Scroll through the updates for a given replication controller
scale Expand or shrink Pod Number ,Deployment、ReplicaSet、RC or Job
autoscale Create an automatic selection to expand or shrink and set Pod Number

Cluster management commands

command Introduce
certificate Modify certificate resources
cluster-info Display cluster information
top Show resources (CPU/M)
cordon The tag node is not schedulable
uncordon Marker nodes can be scheduled
drain Applications on eviction nodes , Prepare for offline maintenance
taint Modify node taint Mark

Fault and debug commands

command Introduce
describe Show details of a specific resource or resource group
logs In a Pod Print a container log , If Pod There is 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
proxy Run one proxy To Kubernetes API Server
cp Copy files or directories into containers
auth Check authorization

Currently commonly used commands

#  Create a nginx Mirror image 
kubectl create deployment nginx --image=nginx

#  External exposure port 
kubectl expose deployment nginx --port=80 --type=NodePort

#  See the resources 
kubectl get pod

#  View details ip, Port, etc .
kubectl get svc

demonstration

Create a namespace

[[email protected] ~]# kubectl create namespace paopao
namespace/paopao created

obtain namespace

[[email protected] ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   7d23h
kube-node-lease   Active   7d23h
kube-public       Active   7d23h
kube-system       Active   7d23h
paopao            Active   22s

Here it is namespace Create and run a nginx Of Pod

[[email protected] ~]# kubectl run pod --image=nginx:latest -n paopao
pod/pod created

See the newly created pod

[[email protected] ~]# kubectl get pod -n paopao
NAME   READY   STATUS    RESTARTS   AGE
pod    1/1     Running   0          3m14s

Delete specified namespace

[[email protected] ~]# kubectl delete ns dev
namespace "dev" deleted

establish / Update resources Configure with declarative objects kubectl apply -f XXX.yaml

Delete resources Use command object configuration kubectl delete -f XXX.yaml

Query resources Use imperative object management kubectl get(describe) Resource name

Resource Management

Imperative object management : Directly use commands to operate kubernetes resources , Direct action object , Suitable for testing , Relatively simple , The disadvantage is that you can only manipulate active objects , Unable to audit trail .

kubectl run nginx-pod --image=nginx --port=80

Imperative object configuration : Operate through command configuration and configuration file kubernetes resources , Direct operation of documents , Suitable for development , Can audit and track , But if the project is too big , There will be many configuration files , Operation trouble .

kubectl create -f nginx-pod.yaml
kubectl patch -f nginx-pod.yaml

Declarative object configuration : adopt apply Commands and configuration files to operate kubernetes resources , Direct operation directory , Suitable for development , Support directory operation , But it's not easy to debug .

kubectl apply -f nginx-pod.yaml

The resource type

kubernetes All the content in is abstracted as resources , You can check it with the following command :

kubectl api-resources

At the end

It's not easy to create , If you think the content will help you , Please pay attention to the third company and support me ! If there is an error , Please point out... In the comment area , I'll change it in time !
Series currently being updated : Learn from scratch k8s, Learn from scratch zabbix
Thank you for watching , The article is mixed with personal understanding , If there is any error, please contact me to point out ~
 Insert picture description here

原网站

版权声明
本文为[It's a bubble]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050803368408.html