Search results
Essential Kubernetes Commands
Genereate Kubernetes config file (kubeconfig-vsrv-k3s.yaml)
# SSH into your Kubernetes host and copy the k3s config
ssh <your user>@<your kubernetes host> "sudo cat /etc/rancher/k3s/k3s.yaml" > $HOME/.kube/kubeconfig-vsrv-k3s.yaml
# Fixes the server IP (change 127.0.0.1 with your <your kubernetes host> IP)
sed -i '' 's/127.0.0.1/<your kubernetes host>/g' $HOME/.kube/kubeconfig-vsrv-k3s.yaml
# Sets permissions
chmod 600 $HOME/.kube/kubeconfig-vsrv-k3s.yaml
Setup & Authentication
# Lists all namespaces to verify kubectl can connect to your Kubernetes cluster.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml get namespaces
Secrets Management
# Creates a secret of type kubernetes.io/dockerconfigjson to authenticate with GHCR for pulling private images.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml create secret docker-registry ghcr-secret \
--docker-server=ghcr.io \
--docker-username=<your docker username> \
--docker-password=$CR_PAT \
--docker-email=<your email address>
# Stores AWS credentials as a Kubernetes secret of type Opaque for your Spring Boot app to use.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml create secret generic aws-s3-themes-credentials \
--from-literal=AWS_ACCESS_KEY_ID='<your-aws-access-key>' \
--from-literal=AWS_SECRET_ACCESS_KEY='<your-aws-secret-key>'
# Lists all secrets in the current namespace.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml get secrets
NAME TYPE DATA AGE
aws-s3-themes-credentials Opaque 2 4d11h
ghcr-secret kubernetes.io/dockerconfigjson 1 4d11h
ConfigMap Management
# Edits a ConfigMap from a manifest file.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml apply -f kubernetes/configmap.yaml
# View basic ConfigMap info.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml get configmap springboot3-thymeleaf-themes-configmap
# View full ConfigMap definition including all data.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml get configmap springboot3-thymeleaf-themes-configmap -o yaml
Deployment Management
# Applies a deployment from a manifest file.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml apply -f kubernetes/deployment.yaml
# Applies all manifests files in the kubernetes/ directory at once.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml apply -f kubernetes/
# Restarts deployment. Forces new pod(s) creation and image pull.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml rollout restart deployment springboot3-thymeleaf-themes
Service Management
# Creates or updates the service from a manifest file.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml apply -f kubernetes/service.yaml
# Shows service info including type, cluster IP, external IP, and ports.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml get svc springboot3-thymeleaf-themes-service
# Shows service endpoints, pod IPs the service is routing traffic to (useful for verifying load balancing).
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml get endpoints springboot3-thymeleaf-themes-service
Pod Management
# List pods by label
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml get pods -l app=springboot3-thymeleaf-themes
# List pods with detailed information (including node and IP)
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml get pods -l app=springboot3-thymeleaf-themes -o wide
# Forces pod deletion and recreation with fresh image pull.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml delete pods -l app=springboot3-thymeleaf-themes
# Shows detailed information including events, conditions, and recent activity.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml describe pods -l app=springboot3-thymeleaf-themes
Logs & Debugging
# Shows last 100 lines of logs from all matching pods.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml logs -l app=springboot3-thymeleaf-themes --tail=100
# Shows logs in real-time (like tail -f).
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml logs -l app=springboot3-thymeleaf-themes --tail=10 -f
# Runs a command inside the container, for instance, to verify mounted ConfigMap files.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml exec -it deployment/springboot3-thymeleaf-themes -- ls -la /workspace/config
Port Forwarding
# Maps the service port 80 to localhost:8080 for local testing.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml port-forward svc/springboot3-thymeleaf-themes-service 8080:80
Cleanup
# Deletes a deployment.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml delete -f kubernetes/deployment.yaml
# Deletes a service.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml delete -f kubernetes/service.yaml
# Deletes resources defined in the kubernetes folder.
kubectl --kubeconfig=$HOME/.kube/kubeconfig-vsrv-k3s.yaml delete -f kubernetes/
