kubectl resource management¶
kubectl resource management involves using the kubectl command-line tool to perform imperative (command-based) and declarative (configuration file-based) operations on Kubernetes resources such as Namespaces, Deployments, Pods, and Services^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
Namespace Management¶
Namespaces (ns) are used to segregate resources within a cluster.^[400-devops-06-kubernetes-k8s-paas-03k8s.md]
- List namespaces:
[kubectl](<./kubectl.md>) get namespace # or [kubectl](<./kubectl.md>) get ns - Create:
kubectl create ns <name>^[400-devops-06-kubernetes-k8s-paas-03k8s.md] - Delete:
kubectl delete namespace <name>^[400-devops-06-kubernetes-k8s-paas-03k8s.md]
Deployments and Pods¶
Deployments manage the state of Pods, ensuring a specified number of replicas are running.^[400-devops-06-kubernetes-k8s-paas-03k8s.md]
Creation¶
Deployments can be created directly from the command line.^[400-devops-06-kubernetes-k8s-paas-03k8s.md]
[kubectl](<./kubectl.md>) create deployment <name> --image=<image-url> -n <namespace>
kubectl get deploy -n <namespace>
* List Pods: kubectl get pods -o wide -n <namespace>
Scaling¶
The number of Pod replicas can be adjusted dynamically using the scale command^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
[kubectl](<./kubectl.md>) scale deployment <name> --replicas=<number> -n <namespace>
Deletion¶
- Delete a Pod: Deleting a Pod managed by a Deployment causes the controller to recreate it to satisfy the desired replica count^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
kubectl delete [Pod](<./pod.md>) <pod-name> -n <namespace> [--force --grace-period=0]
- Delete a Deployment: Removes the controller and all associated Pods^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
kubectl delete deploy <name> -n <namespace>
Service Management¶
Services provide network access to applications by exposing a stable network endpoint^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
- Expose: Creates a Service for a Deployment^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
[kubectl](<./kubectl.md>) expose deployment <name> --port=<port> -n <namespace> - List:
kubectl get svc -n <namespace>
Declarative Management¶
Resources can be managed declaratively using YAML configuration files, which is often referred to as "Resource Configuration" or "Infrastructure as Code"1.^[400-devops-06-kubernetes-k8s-paas-03k8s.md]
Configuration Files¶
- Get YAML: To generate a template or view the configuration of an existing resource^[400-devops-06-kubernetes-k8s-paas-03k8s.md]:
kubectl get <resource> <name> -o yaml -n <namespace> - Explain: To understand the structure of a specific resource field^[400-devops-06-kubernetes-k8s-paas-03k8s.md]:
kubectl explain <resource>.<field>
Apply and Delete¶
- Create/Apply:
kubectl apply -f <file.yaml>^[400-devops-06-kubernetes-k8s-paas-03k8s.md] - Delete:
kubectl delete -f <file.yaml>^[400-devops-06-kubernetes-k8s-paas-03k8s.md]
Editing¶
Resources can be modified "online" (live) or "offline" (via the config file)^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
* Online: kubectl edit svc <name> opens the live configuration in an editor^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
Interaction and Debugging¶
- Exec: Execute commands inside a container^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
[kubectl](<./kubectl.md>) exec -ti <pod-name> /bin/bash -n <namespace>-t: Allocates a pseudo-TTY.-i: Passes STDIN to the container.
- Logs: Print the logs for a container^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
kubectl logs -f <pod-name> - Describe: View detailed state of a resource^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
kubectl describe deployment <name> -n <namespace>
Sources¶
^[400-devops-06-kubernetes-k8s-paas-03k8s.md]
-
Inferred from the context of using YAML files to define and manage resources. ↩