Deployment¶
- A Deployment provides declarative updates for Pods and ReplicaSets.
- You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate
- A deployment can create a RS
- Do not manage ReplicaSets owned by a Deployment.
Rollout and rollbacks¶
A rollout is created in deployment a status of which can be shown as below
# check rollout status using below
kubectl rollout status deployment <nginx-deployment>
#To check the revisions and history of rollout, you can use following command
k rollout history deployment <myapp-deployment>
Update a deployment using rolling update¶
2 strategies are used mainly: 1. Re-create 2. Rolling update.
Remember
If you donโt specify a strategy while creating the deployment it will assume it to be rolling update. In other words rolling update is the default deployment strategy.
# here nginx indicates the Container the update will take place and nginx:1.16.1 indicates the new image and its tag.
k set image deployment <nginx-deployment> podName=nginx:1.16.1
# Example
k set image deployment frontend simple-webapp=kodekloud/webapp-color:v2
Rollback¶
Say for instance, once you upgrade your application you realize something is inferior right. Something is wrong with the new version of the build when you used to upgrade.
So you would like to roll back your update. Kubernetes deployments
allow you to roll back to a previous revision. To undo a change, run the following command.
$ kubectl rollout undo deployment/myapp-deployment
Create a deployment¶
Use the below template
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment # This name will become the basis for the ReplicaSets and Pods which are created later.
labels:
app: nginx
spec:
replicas: 3 # The Deployment creates a ReplicaSet that creates three replicated Pods, indicated by the .spec.replicas field.
selector:
matchLabels:
app: nginx # The .spec.selector field defines how the created ReplicaSet finds which Pods to manage.
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Create K8S deployment via Python¶
Install the K8S client using
$ pip install kubernetes
Create 2 files
create-deployment.py
k8s-deployment.yaml
Python file code¶
create-deployment.py
from os import path
import yaml
from kubernetes import client, config
def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
with open(path.join(path.dirname(__file__), "k8s-deployment.yaml")) as f:
dep = yaml.safe_load(f)
k8s_apps_v1 = client.AppsV1Api()
resp = k8s_apps_v1.create_namespaced_deployment(
body=dep, namespace="default")
print("Deployment created. status='%s'" % resp.metadata.name)
if __name__ == '__main__':
main()
Deployment file¶
k8s-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.4
ports:
- containerPort: 80
Creating deployment¶
python3 create-deployment.py