Skip to main content

Basic Pods operations

Basic Operations with Pods

Check Pods status

Check the list of Namespaces:

kubectl get namespaces
kubectl get ns

List Pods in the default Namespace:

kubectl get pods

List Pods in the kube-system Namespace:

kubectl get pods -n kube-system
kubectl get pods -n kube-system -o wide

List Pods in all Namespaces:

kubectl get pods -A
kubectl get pods -A -o wide

Check Pod details:

kubectl describe pod -n kube-system kube-apiserver-cp1

Create a basic Pod application

Create the app Namespace:

kubectl create ns app

Create a basic Pod in the app Namespace:

kubectl run -n app app --image=nginx:1.23.3

Check the Pod status and wait until the status changes to Running:

kubectl get pod -n app app -o wide -w

Check the logs of the app Pod container;

kubectl logs -n app app

Open the terminal to the app Pod container:

kubectl exec -n app -ti app -- sh
# ls
# exit

Clean up the app Pod:

kubectl delete pod -n app app
kubectl delete ns app

Generate a Pod template

Create a new Namespace:

kubectl create ns frontend
kubectl get ns

Generate the webapp Pod template:

kubectl run webapp -n frontend --image=nginx:1.22 --dry-run=client -o yaml

Save the Pod template as the yaml manifest:

kubectl run webapp -n frontend --image=nginx:1.22 --dry-run=client -o yaml > pod-webapp.yaml
See `pod-webapp.yaml`
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: webapp
name: webapp
namespace: frontend
spec:
containers:
- image: nginx:1.23.3
name: webapp
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}

Apply the Pod manifest to a cluster:

kubectl apply -f pod-webapp.yaml

Verify the operation on the cluster:

kubectl get pods -n frontend -o wide

Retrieve logs from the webapp Pod container:

kubectl logs -n frontend webapp

Clean up the environment:

kubectl delete namespace frontend