Шпаргалка по Kubernetes (Cheatsheet)

1019
Шпаргалка по Kubernetes (Cheatsheet)
Шпаргалка по Kubernetes (Cheatsheet)

Команды Kubernetes, которые должны знать разработчики и DevOps

Kubernetes — это портативная, расширяемая платформа с открытым исходным кодом для управления контейнеризованными рабочими нагрузками и сервисами, которая облегчает как декларативное конфигурирование, так и автоматизацию. Он имеет большую, быстро развивающуюся экосистему. Услуги, поддержка и инструменты Kubernetes широко доступны. Kubernetes предоставляет вам основу для отказоустойчивой работы распределенных систем. Он позаботится о масштабировании и отказоустойчивости вашего приложения, предоставит шаблоны развертывания, канареечные развертывания и многое другое. В этой статье я расскажу о командах Kubernetes, которые необходимы нам для большинства случаев использования.

Я перечислю команды kubectl в разделах ниже в качестве краткого руководства по работе с Kubernetes.

Список ресурсов

  • Get list of all namespaces
    kubectl get namespaces
    
  • Get list of all pods
    kubectl get pods
    
  • Get list of all pods with detailed information like IP, Node Name etc…
    kubectl get pods -o wide
    
  • Get list of all pods running on a particular node server
    kubectl get pods --field-selector=spec.nodeName=[server-name]
    
  • Get list of all replication controllers and services
    kubectl get replicationcontroller,services
    
  • Get list of daemonsets
    kubectl get daemonset
    

Создание ресурсов

  • Create a new namespace
    kubectl create namespace [namespace-name]
    
  • Create a new namespace from JSON or YAML file.
    kubectl create –f [filename]
    

Обновление ресурсов

To apply or update a resource use the kubectl apply command.

  • Create a new service with the definition contained in [service-config].yaml
    kubectl apply -f [service-config].yaml
    
  • Create a new replication controller with the definition contained in [controller-config].yaml
    kubectl apply -f [controller-config].yaml
    
  • Create the objects defined in any .yaml, .yml, or .json file in a directory
    kubectl apply -f [yaml-file/directory-name]
    
  • Edit a service config
    kubectl edit svc/[service-name]
    

    Above command opens the file in your default editor. To choose another editor, specify it in front of the command:

    KUBE_EDITOR=”[editor-name]” kubectl edit svc/[service-name]
    

Отображение состояния ресурсов

  • Get Details about Particular Node
    kubectl describe nodes [node-name]
    
  • Get Details about a Particular pod
    kubectl describe pods [pod-name]
    
  • Get Details about a Particular pod whose name and type are listed in pod.json
    kubectl describe –f pod.json
    
  • Get Details about a Particular pod managed by a specific replication controller
    kubectl describe pods [replication-controller-name]
    
  • Get Details about all pods
    kubectl describe pods
    

Удаление ресурсов

  • Delete a pod using the name and type mentioned in pod.yaml
    kubectl delete -f pod.yaml
    
  • Delete all pods and services with a specific label
    kubectl delete pods,services -l [label-key]=[label-value]
    
  • Delete all pods
    kubectl delete pods --all
    

Выполнить команду

  • Get output from a command run on the first container in a 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 received output comes from the first container
    kubectl exec -ti [pod-name] -- /bin/bash
    
  • Print Logs from Pod
    kubectl logs [pod-name]
    
  • Stream Logs from Pod
    kubectl logs -f [pod-name]
    
  • Tail Logs from Pod (Print last 200 logs from pod)
    kubectl logs --tail=200 [pod-name]
    

Изменение файлов Kubeconfig

kubectl config command lets you view and modify kubeconfig files.

  • Get Current Context
    kubectl config current-context
    
  • Set cluster entry in kubeconfig
    kubectl config set-cluster [cluster-name] --server=[server-name]
    
  • Unset an entry in kubeconfig
    kubectl config unset [property-name]