Join our Telegram: @serverrental_wiki | BTC Analysis | Trading Signals | Telegraph
Kubernetes Basics for Beginners
Template:Infobox server administration
This article provides a foundational understanding of Kubernetes (K8s) for system administrators and developers new to container orchestration. We will cover core concepts such as Pods, Services, Deployments, and Namespaces, along with practical examples using the `kubectl` command-line tool.
Prerequisites
Before you begin, ensure you have the following:
- A Linux server (e.g., Ubuntu, CentOS) with root or sudo privileges. PowerVPS offers excellent managed VPS solutions perfect for learning: PowerVPS.
- Docker installed and running on your server.
- Basic understanding of containerization concepts.
- Internet access to download necessary tools and images.
- A local machine with `kubectl` installed (optional, for remote management).
What is Kubernetes?
Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications. It groups containers that make up an application into logical units for easy management and discovery.
Core Concepts
Pods
A Pod is the smallest deployable unit in Kubernetes. It represents a single instance of a running process in your cluster and can contain one or more containers that share resources, such as network namespaces and storage volumes.
Example: Creating a simple Pod 1. Create a YAML file named `nginx-pod.yaml`:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
2. Apply the configuration to your Kubernetes cluster:
kubectl apply -f nginx-pod.yaml
3. Verify the Pod is running:
kubectl get pods
You should see output similar to:
NAME READY STATUS RESTARTS AGE
nginx-pod 1/1 Running 0 10s
4. To view logs from the Pod:
kubectl logs nginx-pod
5. To delete the Pod:
kubectl delete pod nginx-pod
Deployments
A Deployment provides declarative updates for Pods and ReplicaSets. You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. Deployments are ideal for stateless applications.
Example: Creating a Deployment 1. Create a YAML file named `nginx-deployment.yaml`:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
2. Apply the configuration:
kubectl apply -f nginx-deployment.yaml
3. Check the Deployment status:
kubectl get deployments
Output will show:
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 1m
4. View the Pods created by the Deployment:
kubectl get pods -l app=nginx
5. To scale the Deployment to 5 replicas:
kubectl scale deployment nginx-deployment --replicas=5
6. To update the image to a new version (e.g., `nginx:1.21.6`):
kubectl set image deployment/nginx-deployment nginx=nginx:1.21.6
7. To delete the Deployment:
kubectl delete deployment nginx-deployment
Services
A Service is an abstraction that defines a logical set of Pods and a policy by which to access them. Services provide stable IP addresses and DNS names, making it easy for other applications to discover and communicate with your Pods, even if the Pods themselves are ephemeral.
There are several types of Services:
- ClusterIP: Exposes the Service on an internal IP in the cluster. This is the default.
- NodePort: Exposes the Service on each Node's IP at a static port.
- LoadBalancer: Exposes the Service externally using a cloud provider's load balancer.
Example: Creating a Service 1. Create a YAML file named `nginx-service.yaml`:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx # This must match the labels in your Deployment's Pod template
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP # Or NodePort, LoadBalancer
2. Apply the configuration:
kubectl apply -f nginx-service.yaml
3. Check the Service:
kubectl get services
You will see an IP address assigned to `nginx-service` (if ClusterIP).
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d
nginx-service ClusterIP 10.100.10.20 <none> 80/TCP 30s
4. If you used `NodePort`, you can access it via `http://<NodeIP>:<NodePort>`. If using `LoadBalancer`, you'll get an external IP.
Namespaces
Namespaces provide a mechanism for isolating groups of resources within a single cluster. They are primarily used to organize objects in a cluster and to control access to those objects. Common namespaces include `default`, `kube-system`, and `kube-public`.
Example: Creating and using a Namespace 1. Create a Namespace:
kubectl create namespace development
2. Verify the Namespace:
kubectl get namespaces
3. Deploy resources into the new Namespace:
kubectl apply -f nginx-pod.yaml -n development
4. List Pods in the `development` Namespace:
kubectl get pods -n development
5. To delete the Namespace and all its resources:
kubectl delete namespace development
Troubleshooting
- Pods stuck in `Pending` state: Often due to insufficient resources (CPU, memory) on cluster nodes, or issues with node scheduling. Check `kubectl describe pod <pod-name>` for more details.
- Pods stuck in `CrashLoopBackOff`: Indicates the container is repeatedly crashing. Check application logs using `kubectl logs <pod-name>`. The application might have a bug or configuration issue.
- Services not accessible: Ensure the Service `selector` correctly matches the labels on your Pods. Verify the Pods are running and healthy. Check firewall rules if accessing externally.
- `kubectl` commands failing: Ensure your `kubectl` context is set correctly to your cluster. Check the Kubernetes API server status.
Further Learning
This guide covers the fundamental building blocks of Kubernetes. As you become more comfortable, explore advanced topics like StatefulSets, DaemonSets, ConfigMaps, Secrets, and Ingress controllers. For high-performance GPU needs for AI/ML workloads, consider Immers Cloud GPU: Immers Cloud GPU.