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

Kubernetes Command Line tool-- kubectl Management

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

Share

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

Kubectl Overview 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 the kubeconfig file in another location by setting the KUBECONFIG environment variable or setting the command parameter-- kubeconfig. Kubectl syntax

1. Run the command from the terminal window using the following syntax kubectl:

Kubectl [command] [TYPE] [NAME] [flags]

two。 Where command,TYPE,NAME and flags are:

Command specifies the operation to be performed on one or more resources, such as create,get,describe,delete. For example, "kubectl get cs". TYPE specifies the resource type. Resource types are case-insensitive and can be specified in singular, plural, or abbreviated forms.

Example:

The following command produces the same output kubectl get pod pod1kubectl get pods pod1kubectl get po pod1NAME that specifies 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 performing operations on multiple resources, we can specify each resource by type and name, or specify one or more files:

1. To specify resources by type and name:

If the resource type is the same, the resources are grouped: TYPE1 name1 name2 name.

Example:

Kubectl get pod example-pod1 example-pod2 specifies multiple resource types: TYPE1/name1 TYPE1/name2 TYPE2/name3 TYPE/name.

Example:

Kubectl get pod/example-pod1 replicationcontroller/example-rc1

two。 To specify a resource using one or more files:

-f file1-f file2-f file# pay attention to using YAML instead of JSON, because YAML tends to be more user-friendly, especially for configuration files.

Example:

Kubectl get pod-f. / pod.yamlflags: optional flag specified, but it is worth noting that using the command line to specify parameters overrides the default value and the associated environment variables. For example, we can use the-s or-- server flag to specify the address and port of the Kubernetes API server. Kubectl Management Command Type Command description basic command create creates a resource expose by file name or standard input exposing a resource as a new Servicerun running a specific image set in a cluster setting specific functions on objects get displaying one or more resource explain documentation references edit using the default editor to edit a resource delete through file name, standard input, Resource name or tag selector to delete resource deployment commands rollout manage the release of resources rolling-update rolls update the number of scale capacity expansion or reduction Pod for a given replication controller Deployment, ReplicaSet, RC or Jobautoscale create an automatic selection of capacity expansion or reduction and set the number of Pod cluster management commands certificate modify certificate resources cluster-info display cluster information top display resources (CPU/Memory/Storage). Need Heapster to run cordon marking node unschedulable uncordon marking node can schedule applications on drain expelling node, prepare to go offline to maintain taint modify node taint tag debug command describe displays details of a specific resource or resource group logs prints a container log in a Pod. If Pod has only one container Container name is optional attach attached to a running container exec execute command to forward one or more local ports to a container port-forward run a proxy to Kubernetes API servercp copy file or directory to the container auth check authorization advanced command apply use patch modification to resource application configuration patch through file name or standard input, Field replace to update a resource replace a resource by file name or standard input convert configuration file settings between different API versions of convert commands label update tags on resources update comments on resources completion is used to implement kubectl tools automatic completion other commands api-versions print supported API versions config modify kubeconfig files (for access to API For example, configure authentication information) help all commands help plugin run a command line plug-in the similarities and differences between create and apply commands establish kubectl create-f FILENAME [options] through the YAML file upgrade deploymentkubectl apply-f FILENAME through the YAML file if the Kind value in the yaml file is deployment, then both of the above commands can create a deployment and generate the corresponding number of pod differences create commands: delete all existing things first Re-generate a new one from the yaml file. So the configuration in the yaml file must be a complete apply command: upgrade the existing one according to what is listed in the configuration file. Therefore, the contents of the yaml file can only be written to demonstrate the properties that need to be upgraded: project lifecycle creation-> release-> update-> rollback-> delete.

1. Create nginx

# Common syntax: kubectl run NAME-- image=image [--env= "key=value" parameter] [--port=port port] [--replicas=replicas replica set] [--dry-run=bool status] [--overrides=inline-json] [--command command]-- [COMMAND] [args...] [options] `kubectl run nginx-deployment-- image=nginx-- port=80-- replicas=3# View pod Node kubectl get pods

two。 Release nginx service to provide load balancing function

# Common syntax: kubectl expose (- f FILENAME | TYPE NAME) [--Port for internal communication between port=port clusters] [--protocol=TCP | UDP | SCTP] [--target-port exposed port = number-or-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 release Here the abbreviation of svc bit service service component kubectl get pods,svc# view backend associated node kubectl get endpoints# view network status details kubectl get pods-o wide# view service port kubectl get svc#kubernetes kube-proxy supports three modes Before v1.8, we used iptables and userspace modes. After kubernetes 1.8, we introduced ipvs mode # node node installation ipvsadmin tool to view the service port yum install ipvsadm-yipvsadm-L-n # using the tool to view, it can be seen that the scheduling algorithm is rr polling mode # Masterside view access log kubectl get pods

3. Update nginx to version 1.14

Browser reload refresh page to view nginx version information View nginx version steps: F12 Open 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. Roll back nginx

# View historical version kubectl rollout history deployment/nginx-deployment# perform rollback to the previous version kubectl rollout undo deployment/nginx-deployment# check rollback status kubectl rollout status deployment/nginx-deployment

5. Delete nginx

# Delete deploymentkubectl delete deployment/nginx-deployment#, delete service SVCkubectl get svc

6. Other commands

View the 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 objects abbreviated kubectl api-resources enter the corresponding pod kubectl exec POD [- c CONTAINER]-- COMMAND [args...] [options] View the generated 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 (similar to sh-n in shell script) kubectl run nginx-- image=nginx-- port=80-- replicas=2-- dry-run thank you for reading!

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