### Overview Deployments are the default controller for a container deployed via the `kubectl run` command. Deployments manage ReplicaSets and Pods, providing mechanisms for updates, rollbacks, and scaling. Labels, although not API resources, are essential for administration and resource management in Kubernetes. ### Deployments Deployments ensure a specified number of pod replicas are running at any given time. They manage rolling updates server-side, preventing issues related to client-side updates such as connectivity loss. #### Creating a Deployment You can create a Deployment using the `kubectl create deployment` command: ```bash $ kubectl create deployment dev-web --image=nginx:1.13.7-alpine deployment "dev-web" created ``` This command generates a Deployment that manages a ReplicaSet, which in turn manages the Pods. #### Deployment Configuration A Deployment can be defined using a YAML or JSON spec file. Here’s an example: ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: dev-web spec: replicas: 3 selector: matchLabels: app: dev-web template: metadata: labels: app: dev-web spec: containers: - name: nginx image: nginx:1.13.7-alpine ports: - containerPort: 80 ``` Create the Deployment with: ```bash $ kubectl apply -f deployment.yaml ``` ### Scaling and Rolling Updates Deployments support scaling and rolling updates to manage application lifecycle seamlessly. #### Scaling a Deployment To scale a Deployment, use the `kubectl scale` command: ```bash $ kubectl scale deploy/dev-web --replicas=4 deployment "dev-web" scaled ``` This command increases the number of replicas to four. #### Rolling Updates Rolling updates allow updating the application version without downtime. For example, to change the nginx version: ```bash $ kubectl edit deployment dev-web ``` Modify the `image` field to the desired version, triggering a rolling update. You can monitor the update with: ```bash $ kubectl rollout status deployment/dev-web ``` ### Deployment Rollbacks Deployments keep a history of ReplicaSets, allowing rollbacks to previous versions. For example, if an update fails: ```bash $ kubectl set image deployment/dev-web nginx=nginx:1.16 --record $ kubectl rollout undo deployment/dev-web ``` You can roll back to a specific revision with the `--to-revision` option: ```bash $ kubectl rollout undo deployment/dev-web --to-revision=2 ``` ### Using DaemonSets DaemonSets ensure that a copy of a Pod runs on all or some nodes in the cluster. They are useful for running system services like log collection or monitoring. #### Example: DaemonSet YAML ```yaml apiVersion: apps/v1 kind: DaemonSet metadata: name: fluentd spec: selector: matchLabels: name: fluentd template: metadata: labels: name: fluentd spec: containers: - name: fluentd image: fluentd:latest ``` Create the DaemonSet with: ```bash $ kubectl apply -f daemonset.yaml ``` ### Labels Labels are key-value pairs attached to objects. They are crucial for selection and organization within the cluster. For example, to label a Pod: ```bash $ kubectl label pods dev-web-xxx environment=production ``` You can query Pods based on labels: ```bash $ kubectl get pods -l environment=production ``` ### Node Selectors Node selectors constrain a Pod to run on specific nodes by matching labels on nodes. #### Example: Node Selector ```yaml apiVersion: v1 kind: Pod metadata: name: nginx spec: containers: - name: nginx image: nginx nodeSelector: disktype: ssd ``` Add the label to the desired node: ```bash $ kubectl label nodes <node-name> disktype=ssd ``` ### Conclusion Deployments and other controllers like DaemonSets and ReplicaSets are fundamental for managing application lifecycle in Kubernetes. Labels provide a flexible mechanism for resource selection and organization, enhancing cluster administration. Leveraging Kubernetes for machine learning workloads further amplifies its utility, offering robust solutions for scalable and resilient deployments. Continue: [[07-Volumes and Data]]