Kubernetes basic usage
Introduction
Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. This guide covers the essential concepts and commands for working with Kubernetes using kubectl, the command-line interface for Kubernetes.
If you want to install Kubernetes, there are many ways to do it. You can install it on a single machine, on a local machine, on a cloud provider, or on a home lab:
- Install it on a home lab with K3s
- Install it on your local machine with Minikube, Kind or K3s
- Install it on a CI with Kind
- Install it on a bare metal/cloud provider with Kubespray or kubeadm
- Use a managed version with a cloud provider like EKS, GKE, AKS, Kapsule...
Kubectl Basics
Installation and Configuration
Install kubectl using curl (check the architecture of your machine):
Verify the installation:
Configure kubectl to use your cluster:
View current context:
List all contexts:
Set namespace for current context:
Understanding kubectl Output Formats
Default output (wide format):
Detailed output in YAML format:
JSON output format:
Custom columns output:
Save output to file:
Resource Management Commands
Get Resources
List all resources of a specific type:
Common resource type shortcuts:
- pods (po)
- deployments (deploy)
- services (svc)
- configmaps (cm)
- secrets
- nodes (no)
- namespaces (ns)
- persistentvolumeclaims (pvc)
- persistentvolumes (pv)
Get resources with specific labels:
Get resources in a specific namespace:
Watch resources in real-time:
Get resources with field selectors:
Describe Resources
Get detailed information about a resource:
Example commands:
Get events related to a resource:
Create and Apply Resources
Create from file:
Apply changes (create or update):
Create from URL:
Create with client-side dry-run:
Create with server-side validation:
Edit and Update Resources
Edit resource in default editor:
Patch resource:
Replace resource:
Scale resources:
Delete Resources
Delete by name:
Delete by file:
Delete by label:
Delete with grace period:
Force delete:
Debugging and Troubleshooting
Logs
Get pod logs:
Get logs from previous instance:
Follow logs in real-time:
Get logs from specific container:
Get logs with timestamps:
Get logs from last N lines:
Exec and Port Forward
Execute command in container:
Start interactive shell:
Port forward to pod:
Port forward to service:
Debugging Tools
Get resource events:
Get resource metrics:
Debug pod issues:
Copy files to container:
Copy files from container:
Advanced kubectl Usage
Resource Queries
Get resources with custom output:
Get specific fields:
Get multiple fields:
Get resources with label selectors:
Configuration Management
View current config:
View specific context:
Set namespace for current context:
Create new context:
Standard Kubernetes Objects
Kubernetes provides several built-in objects to manage containerized applications:
Pods
The smallest deployable unit in Kubernetes.
Create a new pod:
Get pod details:
Delete a pod:
Deployments
Manages the desired state for Pods and ReplicaSets.
Create a new deployment:
Scale a deployment:
Update a deployment:
Services
Exposes applications running on Pods to the network.
Create a new service:
Get service details:
ConfigMaps and Secrets
Store configuration data and sensitive information.
Create a new ConfigMap:
Create a new Secret:
Namespaces
Provide scope for resources and enable resource isolation.
Create a new namespace:
List resources in a namespace:
PersistentVolumes and PersistentVolumeClaims
Manage storage resources.
Create a new PVC:
List PVCs:
Custom Resource Definitions (CRDs)
CRDs extend Kubernetes' API to create custom resources.
Creating a CRD
Using Custom Resources
Create a custom resource from file:
List all custom resources:
Get detailed information about a custom resource:
Operators and Controllers
Understanding Operators
Operators are a method of packaging, deploying, and managing a Kubernetes application. They extend the Kubernetes API to create, configure, and manage instances of complex applications like databases, message brokers, etc.
Working with Operator CRDs
Finding Operator CRDs
List all CRDs in the cluster:
Get details about a specific CRD:
Get the API version and kind:
Understanding CRD Relationships
Find related resources using owner references:
Find resources managed by an operator:
Find resources belonging to an instance:
Check the status of operator-managed resources:
Troubleshooting Operators
Get operator pod status:
Check operator logs:
Check CRD status:
Verify CRD schema:
List all resources managed by the operator:
Check for failed resources: