PODS || Deployments, Replicaset, Daemonset

PODS || Deployments, Replicaset, Daemonset

Deployment

When you specify a desired state in a deployment, the deployment controller gradually shifts the current state towards the desired state. You can specify deployments to make new ReplicaSets or to delete current deployments and replace them with new deployments that use all of their resources.

Use Cases

  • Create a Deployment to rollout a ReplicaSet

  • Declare the new state of the Pods

  • Scale up the Deployment to facilitate more load.

  • Pause the rollout of a Deployment

  • Clean up older ReplicaSets

Creating a Deployment

  • Here is the .yaml file for creating a deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

ReplicaSet

The goal of a ReplicaSet is to keep a consistent group of replica Pods active at all times. Because of this, it is frequently utilised to ensure the availability of a certain number of similar Pods.

How does it work?

A ReplicaSet is defined by a number of attributes, such as a selector that describes how to identify Pods it can acquire, a number of replicas that indicates how many Pods it should be keeping, and a pod template that specifies the data of new Pods it should produce to satisfy the number of replicas requirement. In order to attain the specified number, a ReplicaSet creates and deletes Pods as necessary. A ReplicaSet uses its Pod template to produce new Pods as necessary.

When to use?

A ReplicaSet makes sure that at any one moment, a certain number of pod replicas are active. A Deployment, on the other hand, is a higher-level notion that handles ReplicaSets and offers declarative updates to Pods in addition to many other helpful capabilities. Therefore, unless you need customised update orchestration or don't need updates at all, we advise utilising Deployments rather than using ReplicaSets directly.

yaml file to create replica

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # modify replicas according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3

Daemonset

A DaemonSet makes sure that a copy of a Pod is running on all (or some) Nodes. Pods are added to nodes when they are introduced to the cluster as nodes. Those Pods are trash collected as nodes are eliminated from the cluster. A DaemonSet's produced Pods will be removed when it is deleted.

Some typical uses of a DaemonSet are:

  • running a cluster storage daemon on every node

  • running a logs collection daemon on every node

  • running a node monitoring daemon on every node

Creating a DaemonSet

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  namespace: kube-system
  labels:
    k8s-app: fluentd-logging
spec:
  selector:
    matchLabels:
      name: fluentd-elasticsearch
  template:
    metadata:
      labels:
        name: fluentd-elasticsearch
    spec:
      tolerations:
      # these tolerations are to have the daemonset runnable on control plane nodes
      # remove them if your control plane nodes should not run pods
      - key: node-role.kubernetes.io/control-plane
        operator: Exists
        effect: NoSchedule
      - key: node-role.kubernetes.io/master
        operator: Exists
        effect: NoSchedule
      containers:
      - name: fluentd-elasticsearch
        image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: varlog
          mountPath: /var/log
      terminationGracePeriodSeconds: 30
      volumes:
      - name: varlog
        hostPath:
          path: /var/log