| name | kubectl-ops |
| description | Apply manifests, manage rollouts, scale deployments, configure services, and manage secrets on Kubernetes clusters using kubectl. Trigger: when deploying to Kubernetes, using kubectl, applying manifests, managing K8s deployments, scaling Kubernetes workloads, Kubernetes rollouts, kubectl port-forward |
| version | 1 |
| argument-hint | [apply|get|rollout|scale|exec|port-forward|secrets] |
| allowed-tools | ["bash","read","write","edit","glob","grep"] |
Kubernetes Operations
You are now operating in Kubernetes operations mode using kubectl.
Prerequisites
brew install kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl && sudo mv kubectl /usr/local/bin/
kubectl version --client
kubectl cluster-info
Context and Namespace Management
kubectl config get-contexts
kubectl config use-context my-cluster
kubectl config current-context
kubectl config set-context --current --namespace=my-namespace
kubectl config view
kubectl get pods -n my-namespace
kubectl get pods --context=my-other-cluster
Applying Manifests
kubectl apply -f deployment.yaml
kubectl apply -f ./manifests/
kubectl apply -f ./k8s/ -R
cat deployment.yaml | kubectl apply -f -
kubectl apply -f deployment.yaml
kubectl rollout status deployment/my-app -n my-namespace
kubectl apply -f deployment.yaml --dry-run=client
kubectl diff -f deployment.yaml
Viewing Resources
kubectl get pods
kubectl get pods -n my-namespace
kubectl get pods -n my-namespace -o json
kubectl get pods -n my-namespace -o wide
kubectl get pods -n my-namespace --watch
kubectl get pods -l app=my-app -n my-namespace
kubectl get deployments -n my-namespace
kubectl get services -n my-namespace
kubectl get all -n my-namespace
kubectl get pods -A
kubectl get deployments -A
kubectl get pod my-pod-xyz -n my-namespace -o yaml
kubectl get pvc -n my-namespace
Describing Resources (Detailed View)
kubectl describe pod <pod-name> -n my-namespace
kubectl describe deployment my-app -n my-namespace
kubectl describe service my-svc -n my-namespace
kubectl describe node <node-name>
Rollout Management
kubectl rollout status deployment/my-app -n my-namespace
kubectl rollout history deployment/my-app -n my-namespace
kubectl rollout history deployment/my-app -n my-namespace --revision=2
kubectl rollout undo deployment/my-app -n my-namespace
kubectl rollout undo deployment/my-app -n my-namespace --to-revision=2
kubectl rollout pause deployment/my-app -n my-namespace
kubectl rollout resume deployment/my-app -n my-namespace
kubectl rollout restart deployment/my-app -n my-namespace
Scaling
kubectl scale deployment/my-app --replicas=3 -n my-namespace
kubectl scale statefulset/my-db --replicas=3 -n my-namespace
kubectl scale deployment/my-app --replicas=3 -n my-namespace
kubectl rollout status deployment/my-app -n my-namespace
kubectl autoscale deployment/my-app --min=2 --max=10 --cpu-percent=80 -n my-namespace
kubectl get hpa -n my-namespace
Updating Images
kubectl set image deployment/my-app \
my-container=myregistry/my-app:v2.0.0 \
-n my-namespace
kubectl set image deployment/my-app \
my-container=myregistry/my-app:v2.0.0 \
-n my-namespace && \
kubectl rollout status deployment/my-app -n my-namespace
Viewing Logs
kubectl logs <pod-name> -n my-namespace
kubectl logs <pod-name> -n my-namespace -f
kubectl logs <pod-name> -c my-container -n my-namespace
kubectl logs <pod-name> --previous -n my-namespace
kubectl logs -l app=my-app -n my-namespace --prefix
kubectl logs <pod-name> -n my-namespace --tail=100
kubectl logs <pod-name> -n my-namespace --since=1h
Executing Commands in Pods
kubectl exec -it <pod-name> -n my-namespace -- sh
kubectl exec -it <pod-name> -n my-namespace -- bash
kubectl exec <pod-name> -n my-namespace -- ls /app
kubectl exec -it <pod-name> -c my-container -n my-namespace -- sh
kubectl exec <pod-name> -n my-namespace -- env | sort
kubectl exec <pod-name> -n my-namespace -- curl -s http://other-service/health
Port Forwarding
kubectl port-forward pod/<pod-name> 8080:80 -n my-namespace
kubectl port-forward svc/my-service 8080:80 -n my-namespace
kubectl port-forward deployment/my-app 8080:80 -n my-namespace
kubectl port-forward svc/my-service 8080:80 -n my-namespace --address 0.0.0.0
kubectl port-forward svc/my-service 8080:80 -n my-namespace &
PORT_FORWARD_PID=$!
kill $PORT_FORWARD_PID
Secrets Management
kubectl create secret generic my-secret \
--from-literal=DATABASE_URL="postgresql://..." \
--from-literal=API_KEY="sk_live_..." \
-n my-namespace
kubectl create secret generic my-secret \
--from-file=tls.crt=./cert.pem \
--from-file=tls.key=./key.pem \
-n my-namespace
kubectl create secret tls my-tls-secret \
--cert=./tls.crt \
--key=./tls.key \
-n my-namespace
kubectl create secret docker-registry regcred \
--docker-server=registry.example.com \
--docker-username=myuser \
--docker-password=mypass \
-n my-namespace
kubectl get secrets -n my-namespace
kubectl describe secret my-secret -n my-namespace
kubectl get secret my-secret -n my-namespace \
-o jsonpath='{.data.DATABASE_URL}' | base64 --decode
kubectl apply -f secret.yaml
kubectl delete secret my-secret -n my-namespace
ConfigMaps
kubectl create configmap my-config \
--from-literal=LOG_LEVEL=info \
--from-literal=MAX_CONNECTIONS=100 \
-n my-namespace
kubectl create configmap app-config \
--from-file=config.yaml \
-n my-namespace
kubectl get configmap my-config -n my-namespace -o yaml
kubectl edit configmap my-config -n my-namespace
Resource Monitoring
kubectl top pods -n my-namespace
kubectl top nodes
kubectl top pods -n my-namespace --sort-by=cpu
kubectl top pods -n my-namespace --sort-by=memory
Deleting Resources
kubectl delete pod <pod-name> -n my-namespace
kubectl delete deployment my-app -n my-namespace
kubectl delete -f deployment.yaml
kubectl delete pods -l app=my-app -n my-namespace
kubectl delete namespace my-namespace
Common Manifest Examples
Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
namespace: my-namespace
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: myregistry/my-app:v1.0.0
ports:
- containerPort: 8080
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: my-secret
key: DATABASE_URL
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
readinessProbe:
httpGet:
path: /health/ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: /health/live
port: 8080
initialDelaySeconds: 10
periodSeconds: 30
Service
apiVersion: v1
kind: Service
metadata:
name: my-app
namespace: my-namespace
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
type: ClusterIP
Safety Rules
- Always use
kubectl diff before kubectl apply in production to preview changes.
- Never print secret values to logs or stdout. Use
kubectl describe secret to view metadata only.
- Use
kubectl rollout undo immediately if a deployment causes errors after apply.
- Set resource
requests and limits on all containers to prevent resource exhaustion.
- Test manifest changes with
--dry-run=client before applying to production.
- Use namespaces to isolate environments (dev, staging, production) within a cluster.
- Prefer
kubectl apply over kubectl create for idempotent operations.