Kubernetes kubectl basiscommando's uitleg

Kubectl is het command-line gereedschap waarmee je commando’s kunt uitvoeren tegen Kubernetes clusters. In dit overzicht bespreken we de belangrijkste categorieën en componenten van Kubernetes (K8S) en koppelen we de relevante kubectl-commando’s voor snelle referentie aan elk onderwerp. Het doel is om een maximale, compacte en samenhangende uitleg te bieden door bestaande zinnen uit het provided draft te herordenen en waar nodig logisch aan elkaar te plakken.

Algemene opties en flags

Common options in kubectl include flags that control output, namespace, resource selection, and execution behavior.

echo 'alias k=kubectl' >>~/.bashrc-o=json - Output format in JSON.-o=yaml - Output format in YAML.-o=wide - Output in the plain-text format with any additional information, and for pods, the node name is included.-n - Alias for namespace.kubectl get pods -n=<namespace_name>-f - Filename, directory, or URL to files to use to create a resource.kubectl create -f ./<file name>-l - Filter using a specified label.kubectl logs -l name=<label name>-h - Help for kubectl.--watch - Stream resource changes in real time.

Kubectl get deployment my-deploy --watch

--grace-period - Set termination grace period in seconds.kubectl delete pod my-pod --grace-period=10--force - Force actions like deletion.kubectl delete pod my-pod --grace-period=0 --force

Pro tip: Use kubectl explain <resource> to view API documentation for any Kubernetes resource directly from the CLI.

Configuratie bestanden en commands

In Kubernetes definiëren configuratiebestanden de gewenste toestand van resources in een declaratief YAML-formaat.

Read more about kubectl apply vs.

Configuration files commands: In Kubernetes, configuration files (often called manifest files or YAML files) define the desired state of cluster resources using a declarative format.

Clusterbeheer en context commands

Cluster management in Kubernetes refers to operations that retrieve or manipulate information about the cluster itself, such as nodes, namespaces, roles, en API resources.

-A - List pods, services, daemonsets, deployments, replicasets, statefulsets, jobs, and CronJobs in all namespaces, not custom resource types. kubectl get all --all-namespaces

DaemonSets

A Kubernetes DaemonSet ensures dat een specifieke pod draait op alle (of geselecteerde) nodes in een cluster.

Deployment

A Deployment specifies how many Pods should run, what container images they gebruiken, en hoe ze moeten worden bijgewerkt. It works with ReplicaSets to ensure the correct number of pod replicas are always running.

Events

A Kubernetes event is a record of a state change or occurrence within the cluster, typically used for debugging of auditing.

Namespaces

A Kubernetes namespace is een logische scheiding binnen een cluster die resources organiseert en isolatie biedt.

Kubectl api-resources --namespaced=true - Show which Kubernetes resource types are namespace-scoped vs.

Nodes

A Kubernetes node is een fysieke of virtuele machine die workloads runt die door het Kubernetes control plane worden beheerd.

All containers in a pod run on the same node and are tightly coupled. Pods are ephemeral by default.

Belangrijke node- en pod commando’s

kubectl exec <pod_name> -c <container_name> <command> - Execute a command against a container in a pod.

kubectl logs <pod_name> - View logs for the main container in a pod.

kubectl wait --for=condition=Ready pod/<pod_name> - Block until the pod is ready.

Replication Controllers en ReplicaSets

A Replication Controller in Kubernetes ensures that a specified number of pod replicas are running at any given time.

A Kubernetes ReplicaSet ensures a specified number of identical pod replicas are running at all times.

Secrets en Services

A Kubernetes Secrets object stores sensitive data, such as passwords, tokens, or SSH keys, in base64-encoded form.

A Kubernetes Service is an abstraction that defines a stable network endpoint for a set of Pods. Since Pods can be created and destroyed dynamically, a Service ensures that clients can reliably reach them using a consistent IP and DNS name.

Service Accounts

Service Accounts in Kubernetes are special accounts used by pods to authenticate with the Kubernetes API server and other services within the cluster. By default, every pod is assigned a Service Account, typically with default in its namespace. Each Service Account has an associated secret that includes a token used for API access, which is mounted into the pod.

StatefulSet

A StatefulSet in Kubernetes is a controller used to manage stateful applications, where each pod needs a stable identity and persistent storage. They also work with PersistentVolumeClaims to provide stable storage that remains even if the pod is deleted.

Kubernetes componenten en architectuur

Kubernetes volgt een client-server architectuur. Het cluster bestaat uit een control plane en nodes. De API-server, etcd voor state storage, de scheduler en controller managers onderhouden de gewenste toestand van resources. Het CLI-gereedschap kubectl biedt een eenvoudige en krachtige interface om toegang te krijgen tot het K8s-control plane en resources te creëren, bij te werken en te verwijderen.

Hoe kubectl resources opzoekt en filtert

To list and filter Kubernetes resources, use kubectl get command with optional flags like -l (label selector), -field-selector to narrow results, en -all-namespaces to list across all namespaces.

To inspect and debug a Pod start with kubectl get pods, then kubectl describe pod <pod-name> for details over lifecycle, events en failure reasons. Use kubectl logs <pod-name> to view stdout/stderr output. If a Pod is in CrashLoopBackOff or Pending, check related events, image pull issues, or resource limits.

Best practices en patterns

To avoid complex management, follow a pattern for kubectl commands. Use namespaces to isolate environments. Apply labels across resources for easier management, filtering, and grouping. Define declarative configurations and use kubectl apply to create or update resources. Set resource limits to avoid overprovisioning. For repetitive tasks, automate with shell scripts that use kubectl commands. Be mindful of security context and keep kubectl up-to-date.

Overzicht van kubectl commands uit het draft

Note: sommige secties in het draft beschrijven meerdere use-cases en CLI syntaxis. De belangrijkste commando’s die regelmatig voorkomen zijn:

  • kubectl get - lijst resources
  • kubectl describe - gedetailleerde informatie
  • kubectl logs - logs
  • kubectl exec -it -- /bin/sh - interactieve shell
  • kubectl apply -f - apply declaratieve configuratie
  • kubectl delete -f - verwijder resources uit configuratie
  • kubectl delete pod - verwijder pod
  • kubectl get all --all-namespaces - overzicht over alle namespaces
  • kubectl config set-context --current --namespace= - wissel namespace

Video en afbeeldingen

Video-inhoud ter verduidelijking van kubectl-werkwijze kan helpen bij het begrijpen van interacties met pods, deployments en services.

Kubectl basiscommando's - Een pod maken en debuggen in een Minikube-cluster | Kubernetes-zelfstudie 18

Infographics en schema’s kunnen worden gebruikt om de relaties tussen Deployment, ReplicaSet, Pod en Service zichtbaar te maken. Hieronder worden relevante visuele elementen geplaatst.

Infographic: Kubernetes-architectuur en kubectl-werkstroom

Tabel met kernconcepten

ConceptKernpunt
ClusterGeheel van knooppunten die workloads hosten
NodeFysieke/virtuele machine die pods draait
PodKleinste uitvoerbare eenheid; één of meerdere containers
DeploymentDeclaratieve updates voor pods en ReplicaSets
ServiceStabiele netwerkendpoint voor een set pods
NamespaceLogische scheiding van resources binnen een cluster

tags: #kc #command #geberal #graador