In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about the necessary kubectl commands for Kubernetes administrators. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Kubernetes is the dominant technology in today's infrastructure, which means that system administrators need to be familiar with its management. Over the years, the author has been managing Kubernetes clusters on a daily basis and summed up some tips to help others simplify their management.
The author shares 9 commands about kubectl here, mainly explaining the commands I use to manage the normal operation of the Kubernetes cluster every day. The author divides them into several parts to help readers determine whether they should be used for certain tasks. The author also includes some logos in trombone and abbreviated forms to help readers use them more quickly.
1. Use Kubectl to get (get), create (create), edit (edit), and delete (delete) resources
Starting with the command-line utility, the safest place is to ask questions (read operations) rather than issuing commands (write operations). The useful get command allows you to scroll.
Command 1:Kubectl get
Using get, you can get a list of resources currently owned in the cluster. The types of resources you can obtain include:
Namespace
Pod
Node
Deployment
Service
ReplicaSets
Each option provides detailed information about the resources available in the cluster. For example, this is the output of the get nodes command, which provides the usage and status version of Kubernetes.
$kubectl get nodesNAME STATUS ROLES AGE VERSIONminikube Ready master 9d v1.18.0
Most of these commands are scaled back. To get the namespace, run kubectl get namespaces or kubectl get ns:
$kubectl get nsNAME STATUS AGEcharts Active8ddefaultActive9dkube-node-lease Active9dkube-public Active9dkube-system Active9d
Each get command can use the-namespace or-n flags to focus on a given namespace. When you want to view the Pod in kube-system, the author provides special help, which is the service needed to run Kubernetes itself.
$kubectl get pods-n kube-systemNAME READY STATUS RESTARTS AGEcoredns-66bff467f8-mjptx1/1Running29dcoredns-66bff467f8-t2xcz1/1Running29detcd-minikube1/1Running19dkube-apiserver-minikube1/1Running19dkube-controller-manager-minikube1/1Running29dkube-proxy-rpc9d1/1Running19dkube-scheduler-minikube1/1Running29dstorage-provisioner1/1Running19d
Command 2:Kubectl create
With kubectl, you can create almost any type of resource in a cluster. Some of these resources do require configuration files and namespaces to set the resource and its name. The resources you can create include:
service
cronjob
deployment
job
namespace (ns)
Therefore, creating a create namespace requires another parameter to name the namespace.
$kubectl create ns hello-therenamespace/hello-there created
We can also use the cron command to continuously create and run jobs, as many Linux friends will be familiar with. Here, we use the cronjob command to echo "hello" every five seconds.
$kubectl create cronjob my-cron-image=busybox-schedule= "* / 5 *"-echohellocronjob.batch/my-namespaced-cron created
You can also use a simplified version of the command cj instead of cronjob.
$kubectl create cj my-existing-cron-image=busybox-schedule= "* / 15 *"-echohellocronjob.batch/my-existing-cron created
Command 3:Kubectl edit
So what happens when authors create something and want to update it? That's what kubectl edit does.
When running this command, you can edit any resource in the cluster. It opens your default text editor. So, we will edit the existing cronjob, and we can run:
$kubectl edit cronjob/my-existing-cron
This shows the configuration we want to edit.
# Please edit the object below. Lines beginning with a'# 'will be ignored,# and an empty file will abort the edit. If an error occurs while saving this file will be# reopened with the relevant failures.#apiVersion: batch/v1beta1kind: CronJobmetadata:creationTimestamp: "2020-04-19T16:06:06Z" managedFields:-apiVersion: batch/v1beta1 fieldsType: FieldsV1 fieldsV1: f:concurrencyPolicy: {} f:failedJobsHistoryLimit: {} f:jobTemplate: f:metadata: f:name: {} f:spec: f:template: f:spec: f:containers:k: {"name": "my-new-cron"}:. : {} f:command: {} f:image: {} f:imagePullPolicy: {}
This plan is set to every 15 seconds:
We changed it to write to the resource every 25 seconds:
After the writing is complete, we can see that it has changed.
$kubectl edit cronjob/my-existing-croncronjob.batch/my-existing-cron edited
If you want to use another editor, you can use this KUBE_EDITOR syntax addition to override it.
$KUBE_EDITOR= "nano" kubectl edit cronjob/my-existing-cron
Command 4:Kubectl delete
So far, we've done everything except delete it completely, and that's what we're going to do next. The cronjob we just edited is one of two cronjobs, so now we will delete the entire resource.
$kubectldeletecronjobmy-existing-croncronjob.batch "my-existing-cron" deleted
As a warning, do not delete all relevant information that you do not know. Once the resource is deleted, it cannot be restored. You will have to recreate it, so think twice before running this command.
Command 5:Kubectl apply
Previously, the author mentioned that some commands will require configuration files. This apply command allows you to configure resources through file applications within the cluster. This can also be done through the command line standard in (STDIN), but it is recommended that you always do this by file.
The author thinks this command is a bit advanced because you need to know how to use the cluster and which configuration file to apply. For this example, the author used role-based access control (RBAC) configuration from Helm for a service account.
$kubectl apply-f commands.yamlserviceaccount/tiller createdclusterrolebinding.rbac.authorization.k8s.io/tiller created
You can apply almost any configuration you need, but you always need to determine which configuration to apply, or you may see unexpected results.
two。 Use Kubectl to troubleshoot Kubernetes
Command 6:Kubectl describe
Describe displays the details of the resource you are viewing. The most common use case is to describe a pod or node to check for errors in the event or if resources are too limited to be used.
Resources that you can describe include:
Nodes
Pods
Services
Deployments
Replica sets
Cronjobs
In this example, we can describe the current cronjob in the cluster from the previous example.
$kubectldescribecronjob my-cron fragment: Name: my-cronNamespace:defaultLabels: Annotations: Schedule: * / 5 * * ConcurrencyPolicy:AllowSuspend:FalseSuccessful Job HistoryLimit:3FailedJob HistoryLimit:1StartingDeadlineSeconds: Selector: Parallelism: Completions: PodTemplate:Labels: Containers: my-cron: Image: busyboxPort: Host Port:
Command 7:Kubectl logs
Although the describe command provides events that occur within the application within pod, logs provides details of events that occur within the Kubernetes associated with pod. Understanding this difference can help you solve problems within your application and within Kubernetes, because they are the same problems that are not allowed to occur.
$kubectl logs cherry-chart-88d49478c-dmcfv-n charts
Clips:
172.17.0.1-[19/Apr/2020:16:01:15+0000] "GET / HTTP/1.1" 200612 "-" kube-probe/1.18 "-" 172.17.0.1-- [19/Apr/2020:16:01:20+0000] "GET / HTTP/1.1" 200612 "-" kube-probe/1.18 "-" 172.17.0.1-- [19/Apr/2020:16:01: 252.0000] "GET / HTTP/1.1" 200612 "-" kube-probe/1.18 "-" 172.17.0.1-- [19/Apr/2020:16:01:30+0000] "GET / HTTP/1.1" 200612 "-" kube-probe/1.18 "-" 172.17.0.1-- [19/Apr/2020:16:01:35+0000] "GET / HTTP/1.1" 200612 "-"kube-probe/1. 18 ""-"172.17.0.1-- [19/Apr/2020:16:01:40+0000]" GET / HTTP/1.1 "200612"-"kube-probe/1.18"-"172.17.0.1-- [19/Apr/2020:16:01:45+0000]" GET / HTTP/1.1 "200612"-"kube-probe/1.18"-"172.17.0.1-- [19/Apr/2020" "GET / HTTP/1.1" 200612 "-" kube-probe/1.18 "-" 172.17.0.1-- [19/Apr/2020:16:01:55+0000] "GET / HTTP/1.1" 200612 "-"kube-probe/1.18"-"
You can also use the grep command to eliminate extra noise or look for other events. The kube-probe may be noisy, so let's filter it out with the grep command.
$kubectl logs cherry-chart-88d49478c-dmcfv-n charts | grep-vie kube-probe127.0.0.1--[10/Apr / 2020 GET / HTTP/1.1 "200612"-"Mozilla/5.0 (X11; Ubuntu; Linux x8634; rv:75.0) Gecko/20100101 Firefox/75.0"-"
Because some deployments have multiple containers in a pod, you can also use-c for logs to find logs in only one specific container.
Command 8:Kubectl exec
Much like the docker exec command, you can also execute into the container to troubleshoot the application directly. This feature is useful when the logs in Pod do not provide you with answers to questions you might want to debug. When using the exec command, the shell you use within pod must always be provided at the end of the line.
$kubectl exec-it cherry-chart-88d49478c-dmcfv-n charts-/ bin/bashroot@cherry-chart-88d49478c-dmcfv:/#
Command 9:Kubectl cp
This command is used to copy files and directories between containers, just like the Linux cp command. It's not something you use every day, but it's my personal favorite and is used to extract or restore backups in an emergency if automation fails.
This is an example of copying local files to a container. The syntax follows the kubectl cp format:
$kubectl cp commands_copy.txt charts/cherry-chart-88d49478c-dmcfv:commands.txt$ kubectl exec-it cherry-chart-88d49478c-dmcfv-n charts-/ bin/bashroot@cherry-chart-88d49478c-dmcfv:/# lsbin boot commands.txt dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
This is another example, but this time the file is extracted from the container on our local computer. The syntax is in kubectl cp format:
$kubectl cp charts/cherry-chart-88d49478c-dmcfv:commands.txt commands_copy.txt$lscommands_copy.txt
Thank you for reading! This is the end of this article on "what are the necessary kubectl commands for Kubernetes administrators?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.