What is a Deployment?

last updated May 10, 2019

There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors. -- Leon Bambrick

In Kubernetes, a DeploymentIn Kubernetes, a Deployment is an object which describes how an application is deployed. A Deployment defines the images to launch, how to configure them, and how they should be deployedRead More... is an object which describes how an application is deployed. It consists of a few basic pieces of data, including some metadata like the name of your deployment (for example one of ours is "nginx", but the name is arbitrary), a selector, which is a set of arbitrary labels, and a template, which defines the Pods in this deployment.

Deployments are one of the core building blocks in Kubernetes, and is suitable for almost all application hosting needs, but the term "deployment" can be confusing and a bit overused - so it's important to understand that you do not need to create a new Deployment to deploy new code! Take an example nginx server:

Apply
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:1.15
name: nginx
ports:
- containerPort: 80
name: http
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
.apiVersionKube resource version to use
.kindWhat kind of resource is this yaml doc defining
.metadata.nameName of your resource
.specUsed to define how your deployment behaves. Some example properties might be: * selector * minReadySeconds
.spec.replicasReiplicas control how many instances of your deployment you want running at any given time.
.spec.selector.matchLabelsSelector for the user defined labels to identify your template
.spec.templateRequired object that describes the Pods Kubernetes should create as part of the deployment

Should we want to upgrade to nginx:1.16, we simply update the image string on the deployment! You can do this on the commandline with:

kubectl edit deployment DEPLOYMENT_NAME

or more directly

kubectl set image deployment nginx nginx=nginx:1.16

You might have noticed replicas: 1, which gives a hint at some of the ways you can ensure that the upgrade we just performed is done safely, with zero downtime, and no compromises.

Resources

Some of our favorite resources for learning more about Deployments in Kubernetes:

  1. The offical Kubernetes guides
  2. Kubernetes deployment strategies by container-solutions
  3. A very gentle guide from Mirantis

Real-world Example

Here is a real-world example of our API's DeploymentIn Kubernetes, a Deployment is an object which describes how an application is deployed. A Deployment defines the images to launch, how to configure them, and how they should be deployedRead More... definition - not every option in the Deployment API is used, but many of these options help us accomplish truely zero-downtime deployments! Hover over any part of the document to learn more!

Apply
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
selector:
matchLabels:
app: api
tier: service
minReadySeconds: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 2
maxUnavailable: 0
replicas: 3
template:
metadata:
labels:
app: api
tier: service
logs: json
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- topologyKey: kubernetes.io/hostname
volumes:
- name: ssl-volume
secret:
secretName: examplecom
terminationGracePeriodSeconds: 15
containers:
- name: api
image: examplecom:latest
imagePullPolicy: Always
command: ["node", "src/api"]
lifecycle:
preStop:
exec:
command: ["sleep", "15"]
ports:
- name: https-ports
containerPort: 8000
envFrom:
- secretRef:
name: api
env:
- name: LOG_LEVEL
value: info
volumeMounts:
- name: ssl-volume
mountPath: /app/secrets
resources:
requests:
cpu: 50m
memory: 100Mi
limits:
cpu: 2
memory: 1500Mi
readinessProbe:
httpGet:
path: /health
port: https-ports
scheme: HTTPS
httpHeaders:
- name: Host
value: api.examplecom.com
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: /healthz
port: https-ports
scheme: HTTPS
httpHeaders:
- name: Host
value: api.examplecom.com
initialDelaySeconds: 10
periodSeconds: 10
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
.apiVersionKube resource version to use
.kindWhat kind of resource is this yaml doc defining
.metadata.nameName of your resource
.specUsed to define how your deployment behaves. Some example properties might be: * selector * minReadySeconds
.spec.selector.matchLabelsSelector for the user defined labels to identify your template
.spec.minReadySecondsHow long to wait for your app to be ready
.spec.strategySpecifies the strategy used to replace old Pods by new ones. `.spec.strategy.type` can be one of: * Recreate * RollingUpdate "RollingUpdate" is the default value
.spec.replicasReiplicas control how many instances of your deployment you want running at any given time.
.spec.templateRequired object that describes the Pods Kubernetes should create as part of the deployment
.spec.template.spec.affinity.podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution# hello * hi

At KubeSail we're working hard to build tools that will enable you to rapidly iterate on Kubernetes Deployments. If you have any thoughts or requests, please join our chat!

Back