当前位置:网站首页>Kubectl latest common command --v1.24 version

Kubectl latest common command --v1.24 version

2022-06-09 18:22:00 Quiet and thoughtful

Kubectl Automatic completion

BASH

source <(kubectl completion bash) #  stay  bash  Set current in  shell  Automatic completion of , Install first.  bash-completion  package .
echo "source <(kubectl completion bash)" >> ~/.bashrc #  In your  bash shell  Add automatic completion permanently in 

It can also be kubectl  Use a shorthand alias , This alias can also be used with completion Use it together :

alias k=kubectl
complete -F __start_kubectl k

Kubectl Context and configuration

Set up  kubectl  With which Kubernetes Communicate with the cluster and modify the configuration information . see Use kubeconfig Authorize access across clusters   Document get profile details .

kubectl config view #  Show merged  kubeconfig  To configure .

#  Use multiple... At the same time  kubeconfig  File and view the merged configuration 
KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 kubectl config view

#  obtain  e2e  User's password 
kubectl config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}'

kubectl config view -o jsonpath='{.users[].name}'    #  Show the first user 
kubectl config view -o jsonpath='{.users[*].name}'   #  Get the list of users 
kubectl config get-contexts                          #  Show context list 
kubectl config current-context                       #  Show the current context 
kubectl config use-context my-cluster-name           #  Set the default context to  my-cluster-name

#  Add a new user profile to  kubeconf  in , Use  basic auth  Authentication 
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword

#  Persist the namespace in the specified context , For all subsequent  kubectl  Command to use 
kubectl config set-context --current --namespace=ggckad-s2

#  Set the context with a specific user name and namespace 
kubectl config set-context gce --user=cluster-admin --namespace=foo \
  && kubectl config use-context gce

kubectl config unset users.foo                       #  Delete user  foo

#  Set or display  context / namespace  Short alias for 
# ( Only applicable to  bash  and  bash  Compatible  shell, In the use of  kn  Before setting the namespace, you must first set  current-context)
alias kx='f() { [ "$1" ] && kubectl config use-context $1 || kubectl config current-context ; } ; f'
alias kn='f() { [ "$1" ] && kubectl config set-context --current --namespace $1 || kubectl config view --minify | grep namespace | cut -d" " -f6 ; } ; f'

Kubectl apply

apply  By defining Kubernetes Resource files to manage applications . It works by running  kubectl apply  Create and update resources in the cluster . This is management in production Kubernetes Recommended method of application

# -f  Specify the path 
kubectl apply -f ./my-manifest.yaml           #  Create resources 
kubectl apply -f ./my1.yaml -f ./my2.yaml     #  Use multiple files to create 
kubectl apply -f ./dir                        #  Create resources based on all manifest files in the directory 
kubectl apply -f https://git.io/vPieo         #  from  URL  Create resources in 

#  Create multiple... From standard input  YAML  object 
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep
spec:
  containers:
  - name: busybox
    image: busybox:1.28
    args:
    - sleep
    - "1000000"
---
apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep-less
spec:
  containers:
  - name: busybox
    image: busybox:1.28
    args:
    - sleep
    - "1000"
EOF

Kubectl get

get see pod And find service controller resources

# get  The basic output of the command 
kubectl get services                          #  Lists all the in the current namespace  services
kubectl get pods --all-namespaces             #  List all the... Under all namespaces  Pods
kubectl get pods -o wide                      #  List all the in the current namespace  Pods, And display more detailed information 
kubectl get deployment my-dep                 #  List a specific  Deployment
kubectl get pods                              #  List all the in the current namespace  Pods
kubectl get pod my-pod -o yaml                #  Get one  pod  Of  YAML
#  List all... In the current namespace  Services, Sort by name 
kubectl get services --sort-by=.metadata.name
#  List all  PV  Persistent volume , Sort by capacity 
kubectl get pv --sort-by=.spec.capacity.storage
#  List all  Pods  The container that initializes the container in  ID(containerID)
#  Can be used to avoid deleting initialization containers when cleaning up stopped containers 
kubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}' | cut -d/ -f3

#  List Events (Events), Sort by timestamp 
kubectl get events --sort-by=.metadata.creationTimestamp

#  Compare the current cluster state with the cluster state after assuming that a list is applied 
kubectl diff -f ./my-manifest.yaml

#  Generate a period separated tree , It contains all the keys returned for the node 
#  In complex nesting JSON Very useful when positioning keys in structures 
kubectl get nodes -o json | jq -c 'paths|join(".")'

#  Generate a period separated tree , It contains pod Wait for all keys returned 
kubectl get pods -o json | jq -c 'paths|join(".")'

 kubectl rollout

rollout Update resources , edition Back off

kubectl rollout history deployment/frontend                      #  Check  Deployment  Historical record , Including version 
kubectl rollout undo deployment/frontend                         #  Roll back to the last deployed version 
kubectl rollout undo deployment/frontend --to-revision=2         #  Rollback to a specific deployment version 
kubectl rollout status -w deployment/frontend                    #  monitor  "frontend" Deployment  Scroll through the upgrade status until complete 
kubectl rollout restart deployment/frontend                      #  Alternate restart  "frontend" Deployment

 kubectl patch

Update some resources

#  Partially update a node 
kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'

#  Update the image of the container ;spec.containers[*].name  Is a must . Because it is a consolidated primary key .
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'

#  Use an array with positions  JSON patch  Update the image of the container 
kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'

#  Use an array with positions  JSON patch  Disable a  Deployment  Of  livenessProbe
kubectl patch deployment valid-deployment  --type json   -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'

#  Add an element to the array with position 
kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1", "value": {"name": "whatever" } }]'

 kubectl edit

Edit resources

kubectl edit svc/docker-registry                      #  The editor's name is  docker-registry  Service for 
KUBE_EDITOR="nano" kubectl edit svc/docker-registry   #  Use other editors 

 kubectl scale

Expand and shrink resources

kubectl scale --replicas=3 rs/foo                                 #  Name it  'foo'  The replica set of scales to  3  copy 
kubectl scale --replicas=3 -f foo.yaml                            #  Will be in  "foo.yaml"  Specific resources in scale to  3  Copies 
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql  #  If it's called  mysql  Of  Deployment  The copy of is currently  2, Then stretch it to  3
kubectl scale --replicas=5 rc/foo rc/bar rc/baz                   #  Scaling multiple replica controllers 

 kubectl delete

Delete resources

kubectl delete -f ./pod.json                                              #  Delete on  pod.json  Of the type and name specified in  Pod
kubectl delete pod,service baz foo                                        #  Delete the name as  "baz"  and  "foo"  Of  Pod  And the service 
kubectl delete pods,services -l name=myLabel                              #  Delete include  name=myLabel  Labeled  pods  And the service 
kubectl -n my-ns delete pod,svc --all                                     #  Delete on  my-ns  All in the namespace  Pods  And the service 
#  Delete all and  pattern1  or  pattern2 awk  Pattern matching  Pods
kubectl get pods  -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs  kubectl delete -n mynamespace pod

kubectl log

Log view

kubectl logs my-pod                                 #  obtain  pod  journal ( standard output )
kubectl logs -l name=myLabel                        #  Get with  name=myLabel  Labeled  Pods  Log ( standard output )
kubectl logs my-pod --previous                      #  Gets the name of the last container instance  pod  journal ( standard output )
kubectl logs my-pod -c my-container                 #  obtain  Pod  Container log ( standard output ,  Multi container scenario )
kubectl logs -l name=myLabel -c my-container        #  Get with  name=myLabel  Labeled  Pod  Container log ( standard output ,  Multi container scenario )
kubectl logs my-pod -c my-container --previous      #  obtain  Pod  Log of the last instance of a container in ( standard output ,  Multi container scenario )
kubectl logs -f my-pod                              #  Streaming output  Pod  Log ( standard output )
kubectl logs -f my-pod -c my-container              #  Streaming output  Pod  Container log ( standard output ,  Multi container scenario )
kubectl logs -f -l name=myLabel --all-containers    #  Streaming output includes  name=myLabel  Labeled  Pod  All the logs of ( standard output )

 

kubectl cp

Copy files and directories from the container

kubectl cp /tmp/foo_dir my-pod:/tmp/bar_dir            #  take  /tmp/foo_dir  The local directory is copied to the remote current namespace  Pod  Medium  /tmp/bar_dir
kubectl cp /tmp/foo my-pod:/tmp/bar -c my-container    #  take  /tmp/foo  Copy local files to remote  Pod  Of a particular container in  /tmp/bar  Next 
kubectl cp /tmp/foo my-namespace/my-pod:/tmp/bar       #  take  /tmp/foo  Copy local files to remote  “my-namespace”  Namespace  Pod  Medium  /tmp/bar
kubectl cp my-namespace/my-pod:/tmp/foo /tmp/bar       #  take  /tmp/foo  From remote  Pod  Copy to local  /tmp/bar
tar cf - /tmp/foo | kubectl exec -i -n my-namespace my-pod -- tar xf - -C /tmp/bar  #  take  /tmp/foo  Copy local files to remote  “my-namespace”  In the namespace  pod  Medium  /tmp/bar
kubectl exec -n my-namespace my-pod -- tar cf - /tmp/foo | tar xf - -C /tmp/bar    #  take  /tmp/foo  From remote  pod  Copy to local  /tmp/bar
<!--
## Interacting with Deployments and Services
-->
##  And  Deployments  and  Services  Interact 

<!--

 

Interact with nodes and clusters

kubectl cordon my-node                                                #  Mark  my-node  The node is not schedulable 
kubectl drain my-node                                                 #  Yes  my-node  Empty the node , Prepare for node maintenance 
kubectl uncordon my-node                                              #  Mark  my-node  Nodes are schedulable 
kubectl top node my-node                                              #  Displays the measurements for a given node 
kubectl cluster-info                                                  #  Displays the address of the master node and the service 
kubectl cluster-info dump                                             #  Dump the current cluster state to standard output 
kubectl cluster-info dump --output-directory=/path/to/cluster-state   #  Output the current cluster state to  /path/to/cluster-state

#  If there is already a stain with the specified key and effect , Then replace its value with the specified value .
kubectl taint nodes foo dedicated=special-user:NoSchedule

The resource type

List all supported resource types and their abbreviations 、API Group , Whether it is a namespace scope   and  Kind.

kubectl api-resources

To explore API Other operations of resources :

kubectl api-resources --namespaced=true      #  All namespace scoped resources 
kubectl api-resources --namespaced=false     #  All non namespace scoped resources 
kubectl api-resources -o name                #  List all resources in a simple format ( Show only resource names )
kubectl api-resources -o wide                #  List all resources in extended format ( also called  "wide"  Format )
kubectl api-resources --verbs=list,get       #  Support  "list"  and  "get"  Request all the resources of the verb 
kubectl api-resources --api-group=extensions # "extensions" API  All resources in the group 

Format output

To output details in a specific format to the terminal window , take  -o( perhaps  --output) Parameter added to supported  kubectl  In command .

Output format describe
-o=custom-columns=<spec> Use comma separated custom columns to print the table
-o=custom-columns-file=<filename> Use  <filename>  The custom column template in the file prints the table
-o=json Output JSON Format API object
-o=jsonpath=<template> Print  jsonpath  Fields defined in expressions
-o=jsonpath-file=<filename> Printed on  <filename>  Defined in the file  jsonpath  The field specified by the expression .
-o=name Print only the resource name and nothing else
-o=wide Output additional information in plain text format , about Pod Come on , The node name is included in the output
-o=yaml Output YAML Format API object

Use  -o=custom-columns  An example of :

#  All mirrors running in the cluster 
kubectl get pods -A -o=custom-columns='DATA:spec.containers[*].image'

#  list  default  All images running in the namespace , Press  Pod  grouping 
kubectl get pods --namespace default --output=custom-columns="NAME:.metadata.name,IMAGE:.spec.containers[*].image"

#  except  "k8s.gcr.io/coredns:1.6.2"  All mirrors except 
kubectl get pods -A -o=custom-columns='DATA:spec.containers[?(@.image!="k8s.gcr.io/coredns:1.6.2")].image'

#  Output  metadata  All fields below , No matter what  Pod  What's the name 
kubectl get pods -A -o=custom-columns='DATA:metadata.*'
原网站

版权声明
本文为[Quiet and thoughtful]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091808068624.html