K8S Definition File
A K8s definition file always contains the 4 top-level fields:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
apiVersion
indicates the API version used by the object. In this case, it is v1
, which means we are using the first stable version of the Kubernetes API.
kind: Pod
kind
specifies the type of object. In this case, it is Pod
, which means we are creating a Pod object.
metadata:
name: my-pod
labels:
app: my-app
type: frontend
metadata
contains metadata about the object, such as its name. In this case, we set the Pod name to my-pod
.
spec:
containers: # list of containers in the Pod
- name: my-container
image: my-image # Image on the Docker repository
spec
contains the specification of the object. In this case, we define the containers that will run in the Pod. We create a single container named my-container
with the image my-image
.
Kind | Version |
---|---|
Pod | v1 |
Service | v1 |
Deployment | apps/v1 |
StatefulSet | apps/v1 |
ReplicaSet | apps/v1 |
Command to create a Pod from a definition file:
kubectl create -f my-pod.yaml
Command to show the list of Pods:
kubectl get pods
Detailed information about a Pod:
kubectl describe pod my-pod
Delete a Pod:
kubectl delete pod my-pod
Extract the definition to a file:
kubectl get pod my-pod -o yaml > my-pod-new.yaml
Modify the properties of the pod:
kubectl edit pod my-pod
Run a Pod without a definition file
kubectl run my-pod --image=my-image
This command will create a Pod named my-pod
using the specified image my-image
. This is a quick way to create a Pod without needing to write a full definition file.
kubectl replace --force -f my-pod.yaml
This command will replace the existing Pod with the definition provided in the my-pod.yaml
file. The --force
flag is used to delete the existing Pod and create a new one with the updated definition. This is useful for updating the Pod configuration without deleting it first.
Exec
kubectl exec -it my-pod -- /bin/bash
This command will open an interactive terminal session inside the my-pod
Pod, allowing you to run commands within the container.
kubectl exec my-pod -- ls /app
This command will execute the ls /app
command inside the my-pod
Pod and display the output.
kubectl exec my-pod -- cat /etc/hosts
This command will display the contents of the /etc/hosts
file inside the my-pod
Pod.
kubectl exec my-pod -- env
This command will display the environment variables set in the my-pod
Pod.