Run a Stateless Application Using a Deployment

This page shows how to run an application using a Kubernetes Deployment object.

Objectives

  • Create an nginx deployment.
  • Use kubectl to list information about the deployment.
  • Update the deployment.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

Your Kubernetes server must be at or later than version v1.9. To check the version, enter kubectl version.

Creating and exploring an nginx deployment

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 a Deployment that runs the nginx:1.14.2 Docker image:

application/deployment.yaml Run a Stateless Application Using a Deployment - 图1

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: nginx
  9. replicas: 2 # tells deployment to run 2 pods matching the template
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx
  17. image: nginx:1.14.2
  18. ports:
  19. - containerPort: 80
  1. Create a Deployment based on the YAML file:

    1. kubectl apply -f https://k8s.io/examples/application/deployment.yaml
  2. Display information about the Deployment:

    1. kubectl describe deployment nginx-deployment

    The output is similar to this:

    1. Name: nginx-deployment
    2. Namespace: default
    3. CreationTimestamp: Tue, 30 Aug 2016 18:11:37 -0700
    4. Labels: app=nginx
    5. Annotations: deployment.kubernetes.io/revision=1
    6. Selector: app=nginx
    7. Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
    8. StrategyType: RollingUpdate
    9. MinReadySeconds: 0
    10. RollingUpdateStrategy: 1 max unavailable, 1 max surge
    11. Pod Template:
    12. Labels: app=nginx
    13. Containers:
    14. nginx:
    15. Image: nginx:1.14.2
    16. Port: 80/TCP
    17. Environment: <none>
    18. Mounts: <none>
    19. Volumes: <none>
    20. Conditions:
    21. Type Status Reason
    22. ---- ------ ------
    23. Available True MinimumReplicasAvailable
    24. Progressing True NewReplicaSetAvailable
    25. OldReplicaSets: <none>
    26. NewReplicaSet: nginx-deployment-1771418926 (2/2 replicas created)
    27. No events.
  3. List the Pods created by the deployment:

    1. kubectl get pods -l app=nginx

    The output is similar to this:

    1. NAME READY STATUS RESTARTS AGE
    2. nginx-deployment-1771418926-7o5ns 1/1 Running 0 16h
    3. nginx-deployment-1771418926-r18az 1/1 Running 0 16h
  4. Display information about a Pod:

    1. kubectl describe pod <pod-name>

    where <pod-name> is the name of one of your Pods.

Updating the deployment

You can update the deployment by applying a new YAML file. This YAML file specifies that the deployment should be updated to use nginx 1.16.1.

application/deployment-update.yaml Run a Stateless Application Using a Deployment - 图2

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: nginx
  9. replicas: 2
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx
  17. image: nginx:1.16.1 # Update the version of nginx from 1.14.2 to 1.16.1
  18. ports:
  19. - containerPort: 80
  1. Apply the new YAML file:

    1. kubectl apply -f https://k8s.io/examples/application/deployment-update.yaml
  2. Watch the deployment create pods with new names and delete the old pods:

    1. kubectl get pods -l app=nginx

Scaling the application by increasing the replica count

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:

application/deployment-scale.yaml Run a Stateless Application Using a Deployment - 图3

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: nginx
  9. replicas: 4 # Update the replicas from 2 to 4
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx
  17. image: nginx:1.16.1
  18. ports:
  19. - containerPort: 80
  1. Apply the new YAML file:

    1. kubectl apply -f https://k8s.io/examples/application/deployment-scale.yaml
  2. Verify that the Deployment has four Pods:

    1. kubectl get pods -l app=nginx

    The output is similar to this:

    1. NAME READY STATUS RESTARTS AGE
    2. nginx-deployment-148880595-4zdqq 1/1 Running 0 25s
    3. nginx-deployment-148880595-6zgi1 1/1 Running 0 25s
    4. nginx-deployment-148880595-fxcez 1/1 Running 0 2m
    5. nginx-deployment-148880595-rwovn 1/1 Running 0 2m

Deleting a deployment

Delete the deployment by name:

  1. kubectl delete deployment nginx-deployment

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 added to Kubernetes, replicated applications were configured using a ReplicationController.

What’s next