2016-09-23 22:40:21 +00:00
|
|
|
---
|
2017-06-08 20:15:43 +00:00
|
|
|
title: Run a Stateless Application Using a Deployment
|
2018-02-13 18:23:50 +00:00
|
|
|
min-kubernetes-server-version: v1.9
|
2018-05-05 16:00:51 +00:00
|
|
|
content_template: templates/tutorial
|
2018-05-20 04:52:49 +00:00
|
|
|
weight: 10
|
2016-09-23 22:40:21 +00:00
|
|
|
---
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture overview %}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
This page shows how to run an application using a Kubernetes Deployment object.
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture objectives %}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
* Create an nginx deployment.
|
|
|
|
* Use kubectl to list information about the deployment.
|
|
|
|
* Update the deployment.
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture prerequisites %}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture lessoncontent %}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2017-01-18 18:18:37 +00:00
|
|
|
## Creating and exploring an nginx deployment
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
You can run an application by creating a Kubernetes Deployment object, and you
|
|
|
|
can describe a Deployment in a YAML file. For example, this YAML file describes
|
2020-03-17 17:48:23 +00:00
|
|
|
a Deployment that runs the nginx:1.14.2 Docker image:
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2018-06-25 19:25:21 +00:00
|
|
|
{{< codenew file="application/deployment.yaml" >}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
1. Create a Deployment based on the YAML file:
|
|
|
|
|
2018-06-26 16:34:52 +00:00
|
|
|
kubectl apply -f https://k8s.io/examples/application/deployment.yaml
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
1. Display information about the Deployment:
|
|
|
|
|
2018-05-25 06:55:32 +00:00
|
|
|
kubectl describe deployment nginx-deployment
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2017-10-02 19:32:28 +00:00
|
|
|
The output is similar to this:
|
|
|
|
|
2017-10-23 17:16:08 +00:00
|
|
|
user@computer:~/website$ kubectl describe deployment nginx-deployment
|
2016-09-23 22:40:21 +00:00
|
|
|
Name: nginx-deployment
|
|
|
|
Namespace: default
|
|
|
|
CreationTimestamp: Tue, 30 Aug 2016 18:11:37 -0700
|
|
|
|
Labels: app=nginx
|
2017-08-24 21:19:23 +00:00
|
|
|
Annotations: deployment.kubernetes.io/revision=1
|
2016-09-23 22:40:21 +00:00
|
|
|
Selector: app=nginx
|
2017-08-24 21:19:23 +00:00
|
|
|
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
|
2016-09-23 22:40:21 +00:00
|
|
|
StrategyType: RollingUpdate
|
|
|
|
MinReadySeconds: 0
|
|
|
|
RollingUpdateStrategy: 1 max unavailable, 1 max surge
|
2017-08-24 21:19:23 +00:00
|
|
|
Pod Template:
|
|
|
|
Labels: app=nginx
|
|
|
|
Containers:
|
|
|
|
nginx:
|
2020-03-17 17:48:23 +00:00
|
|
|
Image: nginx:1.14.2
|
2017-08-24 21:19:23 +00:00
|
|
|
Port: 80/TCP
|
|
|
|
Environment: <none>
|
|
|
|
Mounts: <none>
|
|
|
|
Volumes: <none>
|
2017-03-18 07:01:18 +00:00
|
|
|
Conditions:
|
|
|
|
Type Status Reason
|
|
|
|
---- ------ ------
|
|
|
|
Available True MinimumReplicasAvailable
|
2017-08-24 21:19:23 +00:00
|
|
|
Progressing True NewReplicaSetAvailable
|
2016-09-23 22:40:21 +00:00
|
|
|
OldReplicaSets: <none>
|
|
|
|
NewReplicaSet: nginx-deployment-1771418926 (2/2 replicas created)
|
|
|
|
No events.
|
|
|
|
|
|
|
|
1. List the pods created by the deployment:
|
|
|
|
|
2018-05-25 06:55:32 +00:00
|
|
|
kubectl get pods -l app=nginx
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2017-10-02 19:32:28 +00:00
|
|
|
The output is similar to this:
|
|
|
|
|
2016-09-23 22:40:21 +00:00
|
|
|
NAME READY STATUS RESTARTS AGE
|
|
|
|
nginx-deployment-1771418926-7o5ns 1/1 Running 0 16h
|
|
|
|
nginx-deployment-1771418926-r18az 1/1 Running 0 16h
|
|
|
|
|
2020-04-16 12:53:21 +00:00
|
|
|
1. Display information about a Pod:
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2018-05-25 06:55:32 +00:00
|
|
|
kubectl describe pod <pod-name>
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2020-04-16 12:53:21 +00:00
|
|
|
where `<pod-name>` is the name of one of your Pods.
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2017-01-18 18:18:37 +00:00
|
|
|
## Updating the deployment
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
You can update the deployment by applying a new YAML file. This YAML file
|
2020-04-05 15:16:16 +00:00
|
|
|
specifies that the deployment should be updated to use nginx 1.16.1.
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2018-06-26 16:34:52 +00:00
|
|
|
{{< codenew file="application/deployment-update.yaml" >}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
1. Apply the new YAML file:
|
|
|
|
|
2018-06-26 16:34:52 +00:00
|
|
|
kubectl apply -f https://k8s.io/examples/application/deployment-update.yaml
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
1. Watch the deployment create pods with new names and delete the old pods:
|
|
|
|
|
2018-05-25 06:55:32 +00:00
|
|
|
kubectl get pods -l app=nginx
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2017-01-18 18:18:37 +00:00
|
|
|
## Scaling the application by increasing the replica count
|
2016-10-11 21:36:18 +00:00
|
|
|
|
|
|
|
You can increase the number of pods in your Deployment by applying a new YAML
|
|
|
|
file. This YAML file sets `replicas` to 4, which specifies that the Deployment
|
|
|
|
should have four pods:
|
|
|
|
|
2018-06-26 16:34:52 +00:00
|
|
|
{{< codenew file="application/deployment-scale.yaml" >}}
|
2016-10-11 21:36:18 +00:00
|
|
|
|
|
|
|
1. Apply the new YAML file:
|
|
|
|
|
2018-06-26 16:34:52 +00:00
|
|
|
kubectl apply -f https://k8s.io/examples/application/deployment-scale.yaml
|
2016-10-11 21:36:18 +00:00
|
|
|
|
|
|
|
1. Verify that the Deployment has four pods:
|
|
|
|
|
2018-05-25 06:55:32 +00:00
|
|
|
kubectl get pods -l app=nginx
|
2016-10-11 21:36:18 +00:00
|
|
|
|
|
|
|
The output is similar to this:
|
|
|
|
|
|
|
|
NAME READY STATUS RESTARTS AGE
|
|
|
|
nginx-deployment-148880595-4zdqq 1/1 Running 0 25s
|
|
|
|
nginx-deployment-148880595-6zgi1 1/1 Running 0 25s
|
|
|
|
nginx-deployment-148880595-fxcez 1/1 Running 0 2m
|
|
|
|
nginx-deployment-148880595-rwovn 1/1 Running 0 2m
|
|
|
|
|
2017-01-18 18:18:37 +00:00
|
|
|
## Deleting a deployment
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
Delete the deployment by name:
|
|
|
|
|
|
|
|
kubectl delete deployment nginx-deployment
|
|
|
|
|
2017-05-04 01:10:06 +00:00
|
|
|
## ReplicationControllers -- the Old Way
|
|
|
|
|
|
|
|
The preferred way to create a replicated application is to use a Deployment,
|
|
|
|
which in turn uses a ReplicaSet. Before the Deployment and ReplicaSet were
|
2018-08-20 19:39:55 +00:00
|
|
|
added to Kubernetes, replicated applications were configured using a
|
2017-05-04 01:10:06 +00:00
|
|
|
[ReplicationController](/docs/concepts/workloads/controllers/replicationcontroller/).
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture whatsnext %}}
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2017-04-19 17:56:47 +00:00
|
|
|
* Learn more about [Deployment objects](/docs/concepts/workloads/controllers/deployment/).
|
2016-09-23 22:40:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
|
|
|
|
2016-09-23 22:40:21 +00:00
|
|
|
|