kubectl is the command-line tool for interacting with Kubernetes clusters. It allows you to deploy applications, inspect and manage cluster resources, view logs, and perform administrative tasks. This tutorial will guide you through the fundamentals of using kubectl, including installation, basic commands, resource management, and advanced features.
Before you begin, ensure that you have:
kubectl installed on your local machine or in your development environment.Download the latest release:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
Make the binary executable:
chmod +x ./kubectl
Move the binary to a directory in your PATH:
sudo mv ./kubectl /usr/local/bin/kubectl
Verify the installation:
kubectl version --client
Use Homebrew (recommended):
brew install kubectl
Verify the installation:
kubectl version --client
Use Chocolatey (recommended):
choco install kubernetes-cli
Verify the installation:
kubectl version --client
To get help with kubectl, use:
kubectl help
For more specific commands, you can use:
kubectl <command> --help
Get cluster details:
kubectl cluster-info
List all nodes in the cluster:
kubectl get nodes
Describe a node:
kubectl describe node <node-name>
Create a Pod from a YAML file:
# pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: nginx
image: nginx:latest
Apply the configuration:
kubectl apply -f pod.yaml
List all Pods in a namespace (default):
kubectl get pods
Describe a Pod:
kubectl describe pod <pod-name>
Delete a Pod:
kubectl delete pod <pod-name>
Create a Deployment from a YAML file:
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
Apply the configuration:
kubectl apply -f deployment.yaml
List all Deployments:
kubectl get deployments
Describe a Deployment:
kubectl describe deployment <deployment-name>
Scale a Deployment:
kubectl scale deployment <deployment-name> --replicas=5
Create a Service from a YAML file:
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: example-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
Apply the configuration:
kubectl apply -f service.yaml
List all Services:
kubectl get services
Describe a Service:
kubectl describe service <service-name>
You can execute commands inside running Pods:
kubectl exec -it <pod-name> -- /bin/sh
Port forwarding allows you to access a Pod's port directly from your local machine:
kubectl port-forward pod/<pod-name> 8080:80
Now, you can access the application running in the Pod at http://localhost:8080.
View logs for a specific container within a Pod:
kubectl logs <pod-name>
To follow logs in real-time:
kubectl logs -f <pod-name>
kubectl is a powerful tool for managing Kubernetes clusters. By mastering its basic commands and advanced features, you can effectively deploy, manage, and maintain applications in a Kubernetes environment. Always refer to the official Kubernetes documentation for the latest information and best practices.