visit
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
tier: frontend
app: nginx
spec:
selector:
matchLabels:
app: myapp
replicas: 3
template:
metadata:
name: nginx
labels:
app: myapp
spec:
containers:
- name: nginx
image: nginx
apiVersion - This defines the Kubernetes API version that we want to use in our YAML file. For Deployment Objects, we use the apps/v1 apiVersion. kind - This defines what kind of Kubernetes Object we want to create. For Deployments, we use the kind Deployment (simple enough right?)
metadata - This is data that helps us uniquely identify the object that we want to create. Here we can provide a name for our app, as well as apply labels to our Deployment. spec - This defines the state that we want for our object.
Within our spec definition, we need to specify the desired behavior of our Deployment. So in this file, I'm using the following attributes:selector - Within our selector, I'm using the matchLabels attribute. In Deployments, we use this as a label selector for the pods. Essentially, the Pods that are managed by the existing ReplicaSet will be the Pods that are affected by our Deployment. So in order for this Deployment object to apply to those Pods, we must use the pod's template labels in our Deployment definition. template - This describes the pods that will be created. So in our file, we are creating a Pod called myapp using the nginx image for our container. replicas - This states the number of desired Pods.
kubectl apply -f ourdeployment.yaml
kubectl create -f ourdeployment.yaml
kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
myapp-deployment 0/3 0 0 1s
NAME - This column shows the names of the Deployments in our Kubernetes namespace. READY - This displays how many replicas of our application are ready for our users. In this output, 0 means that there are zero replicas ready, and 3 means that we want 3 replicas to be ready.
UP-TO-DATE - This shows how many replicas have been updated to achieve our desired state. AVAILABLE - This shows how many replicas are available to our users.
AGE - This shows how long the app has been running.
kubectl rollout status <name-of-deployment>
kubectl rollout history <name-of-deployment>
kubectl rollout undo <name-of-deployment>
kubectl scale deployment <name-of-deployment> --replicas=10
kubectl autoscale deployment --min=3 --max=10 --cpu-percent=70
Recreate - This will kill all of your existing Pods before creating the new Pods that you have defined in your Deployment. This will cause application downtime!.
RollingUpdate - In this strategy, updates will roll out, meaning that Pods will be rolled out and the old Pods will only be killed off if the new version of your Pods is working. We can define how many Pods will be unavailable and how many Pods will be created as part of the RollingUpdate. This type of Deployment strategy is the default in Kubernetes.