A set of K8S commands for everyone, it’s here!

Hello, this is the network technology alliance station.

Last time, I introduced the common commands of Docker to you, and the response was good. Some friends in the comment area want a collection of K8S commands, so today Rui Ge will arrange it!

Before starting to list the K8S command set, let me briefly introduce the basic theory of K8S.

What is K8S?

K8S is actually Kubernetes. Since Kubernetes reads like K8S , over time, everyone calls Kubernetes K8S for short.

Originally developed by Google, K8S is actually a container or microservice platform for orchestrating computing, networking, and storage infrastructure workloads.

The so-called container is to provide a way to package code, runtime, system tools, system libraries and configuration, that is to say, with this container, no matter which server of the same system you put it on, it can be intact. Running programs, the best example is Docker.

Advantages of K8S

The advantage of K8S is why most enterprises and organizations choose K8S. In short, it generally has the following four advantages:

1. Improve work efficiency

K8S provides a PaaS platform that creates a hardware abstraction layer where users can request the resources they need quickly and efficiently, and if they need more resources to handle additional work, they can also scale quickly, just a click away configuration, and can leverage Kubernetes-developed tools to automate packaging, deployment, and testing.

2. Flexibility

Container engines are programs that run containers, and K8s supports many types of container runtimes and infrastructures, as long as they have some version of Linux or Windows, and the portability of K8s allows development teams to easily switch engines, servers, or environment configurations.

3. Cost saving

K8S can save not only labor cost, but also material cost. Through dynamic and intelligent container management, K8S can help enterprises save their ecosystem management and ensure scalability across multiple environments. Resource allocation is automatically adjusted based on actual application needs while reducing low-level manual operations on the infrastructure. With automation, IT teams no longer need to perform a large number of operational tasks related to system administration, and can spend a lot of energy on more meaningful things, which greatly saves the cost of the enterprise.

4. Open source

K8S is a fully open-source, community-led project overseen by the CNCF, it has several major corporate sponsors, and for many, this open-source strategy makes K8S superior to closed-source orchestrators.

K8S Command Memo

1. List resources

Generate a plain text list of all namespaces:

 kubectl get namespaces  

Display a plain text list of all pods:

 kubectl get pods  

Generate a detailed plain-text listing of all pods, with information such as node names:

 kubectl get pods -o wide  

Display a list of all pods running on a specific node server:

 kubectl get pods --field-selector=spec.nodeName=[server-name]  

List specific replication controllers in plain text:

 kubectl get replicationcontroller [replication-controller-name]  

Generate a plain text list of all replication controllers and services:

 kubectl get replicationcontroller,services  

Display a plain text listing of all daemon sets:

 kubectl get daemonset  

2. Deployment

Create a new deployment

 kubectl create deployment <deployment_name>  

List one or more deployments

 kubectl get deployment  

List the detailed status of one or more deployments

 kubectl describe deployment <deployment_name>  

delete deployment

 kubectl delete deployment<deployment_name>  

3. Namespace related

Create namespace by given name

 kubectl create namespace <namespace_name>  

List the current namespaces in the cluster

 kubectl get namespace  

Display detailed status of one or more namespaces

 kubectl describe namespace <namespace_name>  

delete namespace

 kubectl delete namespace <namespace_name>  

Edit and update namespace definitions

 kubectl edit namespace <namespace_name>  

4. Execute the command

Receive the output of the command run on the first container in the pod:

 kubectl exec [pod-name] -- [command]  

Get output from a command run on a specific container in a pod:

 kubectl exec [pod-name] -c [container-name] -- [command]  

Run /bin/bash from a specific pod. The output received is from the first container:

 kubectl exec -ti [pod-name] -- /bin/bash  

5. Daemon process

List one or more daemon sets

 kubectl get daemonset  

Edit and update the definitions of one or more daemon sets

 kubectl edit daemonset <daemonset_name>  

delete daemon set

 kubectl delete daemonset <daemonset_name>  

Create a new daemon

 kubectl create daemonset <daemonset_name>  

Manage the rollout of the daemon set

 kubectl rollout daemonset  

Displays detailed status of daemon sets in a namespace

 kubectl describe ds <daemonset_name> -n <namespace_name  

8. Print the container log

print pod logs

 kubectl logs <pod_name>  

Print the logs for the last hour of the pod

 kubectl logs --since=1h <pod_name>  

Get the last 20 lines of log

 kubectl logs --tail=20 <pod_name>  

Get logs from service and choose which container to choose

 kubectl logs -f <service_name> [-c <$container>]  

Print the pod’s logs and keep an eye on new logs

 kubectl logs -f <pod_name>  

Print the logs of the containers in the pod

 kubectl logs -c <container_name> <pod_name>  

Output the pod’s logs to a file named “pod.log”

 kubectl logs <pod_name> pod.log  

View logs of previously failed pods

 kubectl logs --previous <pod_name>  

Get logs for all pods named with pod_prefix

 kubetail <pod_prefix>  

Include logs from the last 5 minutes

 kubetail <pod_prefix> -s 5m  

11. Other commands

Documentation

Documentation for getting the pod manifest

 kubectl explain pod  

Documentation for getting service manifest

 kubectl explain service  

alias

create alias on linux

 alias k=kubectl  

Create an alias on Windows

 Set-Alias ​​-Name k -Value kubectl  

node

get node

 kubectl get nodes  

get a specific node

 kubectl get nodes <node>  

show node metrics

 kubectl top node <node>  

service account

List service accounts

 kubectl get serviceaccounts  

Get service account

 kubectl get serviceaccount <serviceaccount>  

Create a service account

 kubectl create serviceaccount <serviceaccount>  

Delete service account

 kubectl delete serviceaccount <serviceaccount>  

Describe the service account

 kubectl describe serviceaccount <serviceaccount>  

10. Formatted output

If you want to output detailed information to the terminal window in a specific format, you can add the -o parameter to the K8S command.

Print table with comma separated list of custom columns

 -o=custom-columns=<spec>  

Print table using custom column template from file

 -o=custom-columns-file=<filename><filename>  

Output API object in JSON format

 -o=json  

print the fields defined in the jsonpath expression

 -o=jsonpath=<template>  

Print the field <filename> defined by the jsonpath expression in the file

 -o=jsonpath-file=<filename>  

Print only the resource name, nothing else

 -o=name  

Output any additional information in plain text, for pods, including the node name

 -o=wide  

Output API object in YAML format

 -o=yaml  

The above is the command list of K8S. Below is a list of frequently used commands, I hope you can keep it in mind! ! !

Commands used by K8S high frequency

1. List all namespace services

View a list of all services in the current namespace:

 kubectl get services  

See a list of services in all namespaces:

 kubectl get pods --all-namespaces  

2. Retrieve detailed information on a node

View the overall status of the node:

 kubectl get nodes  

3. Create a new namespace with a unique name

Create a new namespace:

 kubectl create ns hello-there  

4. Use files to configure Kubernetes

Take advantage of configuration files and change resources:

 kubectl apply -f config.yaml  

5. List all running pods in the namespace

 kubectl get pods --field-selector=status.phase=Running  

6. Cluster service

Display endpoint information for the cluster master and services:

 kubectl cluster-info  

7. Service log ⭐

 kubectl logs -f <service_name>  

8. Key

Get a list of all Kubernetes keys:

 kubectl get secrets  

9. Events

See a list of all resource-based events:

 kubectl get events  

10. Create a new DaemonSet

 kubectl create daemonset <daemonset_name>  

Summarize

K8S has been called standard technology in the era of the prevalence of container technology. In this article, Ruige began to introduce the basic knowledge of K8S, then listed the command list of K8S, and finally extracted the frequently used commands. I hope this article can to help you.

Finally, thank you for reading. If you think the article is helpful to you, don’t forget to like ? and bookmark ⭐! If you have any questions, welcome to discuss with me in the comment section below! ! !

The text and pictures in this article are from InfoQ

loading.gif

This article is reprinted from https://www.techug.com/post/a-set-of-k8s-commands-for-everyone-it-s-coming036ee5e69b97f25fa0af/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment