Getting Started With Kubernetes - Deployments and DaemonSets

avatar

Welcome back to another article to get you started with Kubernetes. In todays post we are going to introduce you to Deployments and DaemonSets, which are both useful in helping you organise and deploy your pods onto your Kubernetes clusters. We introduced ReplicationControllers and ReplicaSets in our last post and hopefully we will be able to clear up why you would want to use a Deployment and Daemonset instead of simply using ReplicaSets.

If all this information goes over your head a little, don't worry, we have more acticles that cover some more of the basics and architecture of Kubernetes including "the Basic Concepts Of Kubernetes", an introduction to "Deploying Pods" and details on "Using and Creating Services and Namespaces".

Deployments


A Deployment is a kubernetes object that allows us to upgrade underlying instances seamlessly. Just like a replicaset a Deployment allows you to manage your pod and define specifics on how the pods is able to use resources within the cluster. A Deployment gives you a lot more configuration options available to help you manage your pods and will also automatically create a replicaset when the Deployment is created.

In today's post, we will just touch on the basics of creating a Deployment and will move further on the more advanced features and configuration options at a later date. To create a definition for a deployment is the same as you would create a replicaset but instead of the "kind" being "ReplicaSet", it is "Deployment". We can see this in the following example:

1 apiVersion: apps/v1
2 kind: Deployment
3 metadata:
4   name: myapp-replicaset
5   labels:
6     app: myapp
7     type: front-end
8 spec:
9   template:
10   metadata:
11      name: nginx
12      labels:
13        app: myapp
14    spec: 
15      containers:
16        - name: nginx-container
17          image: nginx
18  replicas: 3
19  selector: 
20    matchLabels:
21      type: front-end
22

As you can see from the definitiomn above the definition is pretty self explanitory. We speicify the kind as Deployment in line 2, and we then set up the details of the pod within the spec.template of the Deployment, in this instance we are creating an nginx pod for our front end host. The pod details are set up between lines 14 to 17. Line 18 then allows us to specify the number of replicas of the pod, which we then match to the pod label of "front-end" from lines 19 to 21.

We create the Deployment as we have with all our other definitions using the create kubectl command:

kubectl create -f <deployment_yaml>
deployment/myapp created

We can then get details of the Deployment using the get deployments command. We can get a more details output using the describe kubectl command:

kubectl get deployments
NAME       DESIRED   CURRENT   READY   AGE
myapp   3         3         3       24s

And even though we have created a Deployment, a ReplicaSet is also created:

kubectl get replicasets
NAME       DESIRED   CURRENT   READY   AGE
myapp   3         3         3       24s

We can also create a deployment using the run command, you can use a similar command to the one below, which sets up the name of the pods and images being used, then outputs in yaml format. Using the --dry-run=client option, will not actually create the Deployment but still output the YAML file allowing us to then make adjustments when needed:

kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml

Daemonsets


These are like Deployments, but will make sure one copy of your pod is running on each node in your cluster. These are perfect for log monitoring agents on your cluster or a networking solution and you should use a Deployment more for your applications you are wanting to deploy with pods.

Creating a daemonset is exactly the same as creating a Deployment, except you use a different kind in your definition as noted in the example below:

1 apiVersion: apps/v1
2 kind: DaemonSet
3 metadata:
4   name: myapp-replicaset
5 spec:
6   selector:
7     matchLabels:
8       app: myapp
9   template:
10    metadata:
11      name: nginx
12      labels:
13        app: myapp
14    spec: 
15      containers:
16        - name: nginx-container
17          image: nginx

Kubernetes uses the scheduler and nodeAffinity rules to make sure pods are then deployed on each of the nodes in the cluster.
Daemonsets. This is why we do not need to configure the replicas as we did with a Deployment as each node in the cluster will have one of these pods running on it.

We create the Deployment as we have using the create kubectl command. We can then get further details of the DaemonSet using the "get" and "describe" commands.

I hope we have been able to join the dots and enhance your knowledge a little further providing you with the basic details on Deployments and Daemonsets. Make sure you keep an eye out for our future posts coming out weekly.

About The Author
I am a DevOps Engineer, Endurance Athlete and Author. As a DevOps Engineer I specialize in Linux and Open Source Applications. Particularly interested in Search Marketing and Analytic’s, and is currently developing my skills in devops, continuous integration, security, and development(Python).

Posted with STEMGeeks



0
0
0.000
3 comments