A pod is a group of containers that are scheduled onto the same host. Pods serve as units of scheduling, deployment, and horizontal scaling/replication. Pods share fate, and share some resources, such as storage volumes and IP addresses.
Single-container pods can be created with the run
command. The
pod’s properties are specified with flags on the command line.
The run
command creates a Deployment to monitor the pod(s).
The Deployment watches for failed pods and will start up new pods as required
to maintain the specified number.
Note: If you don’t want a Deployment to monitor your pod (e.g. your pod
is writing non-persistent data which won’t survive a restart, or your pod is
intended to be very short-lived), you can
create a pod directly with the create
command.
To create a pod using the run
command:
$ kubectl run NAME
--image=image
[--port=port]
[--replicas=replicas]
[--labels=key=value,key=value,...]
Where:
kubectl run
creates a Deployment named “nginx” on Kubernetes cluster >= v1.2. If you are running older versions, it creates replication controllers instead. If you want to obtain the old behavior, use --generator=run/v1
to create replication controllers. See kubectl run
for more details.NAME
(required) is the name of the container to create. This value is also
applied as the name of the Deployment, and as the prefix of the
pod name. For example:
$ kubectl run example --image=nginx
deployment "example" created
$ kubectl get pods -l run=example
NAME READY STATUS RESTARTS AGE
example-1934187764-scau1 1/1 Running 0 13s
--image=IMAGE
(required) is the Docker container image to use for this
container.--port=PORT
is the port to expose on the container.--replicas=NUM
is the number of replicated pods to create. If not specified,
one pod will be created.--labels=key=value
specifies one or more labels to attach to the pod. In
addition to any labels specified here, run
attaches a label of
the format run=NAME
. This is used by the Deployment
to target the pods created by the command.There are additional flags that can be specified. For a complete list, run:
$ kubectl run --help
To view a specific pod, use the kubectl get
command:
$ kubectl get pod NAME
NAME READY STATUS RESTARTS AGE
example-1934187764-scau1 1/1 Running 0 2d
To return the name of the node on which the pod is scheduled, use the -o wide
option:
$ kubectl get pod NAME -o wide
NAME READY STATUS RESTARTS AGE NODE
example-1934187764-scau1 1/1 Running 0 2d gke-example-c6a38-node-xij3
For more details about a pod, including events, use describe
in place of
get
:
$ kubectl describe pod NAME
Name: example-1934187764-scau1
Namespace: default
Image(s): kubernetes/example-php-redis:v2
Node: gke-example-c6a38461-node-xij3/10.240.34.183
Labels: name=frontend
Status: Running
Reason:
Message:
IP: 10.188.2.10
Replication Controllers: example (5/5 replicas created)
Containers:
php-redis:
Image: kubernetes/example-php-redis:v2
Limits:
cpu: 100m
State: Running
Started: Tue, 04 Aug 2015 09:02:46 -0700
Ready: True
Restart Count: 0
Conditions:
Type Status
Ready True
Events:
FirstSeen LastSeen Coun From SubobjectPath Reason Message
Thu, 06 Aug 2015 11:49:44 -0700 Thu, 06 Aug 2015 11:49:44 -0700 1 {kubelet gke-example-c6a38461-node-xij3} spec.containers{example} started Started with docker id 5705bffa65e2
To list all pods running on a cluster:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
example-1934187764-scau1 1/1 Running 0 1m
frontend-7kdod 1/1 Running 0 1d
If your pod was created using the run
command, kubernetes creates a
Deployment
to manage the pod. Pods managed by a Deployment are rescheduled if
they go away, including being deleted by kubectl delete pod
. To permanently
delete the pod, delete its Deployment.
First, find the Deployment’s name:
$ kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
example 1 1 1 1 1m
Then, delete
the Deployment:
$ kubectl delete deployment DEPLOYMENT_NAME