Kubernetes - Deployment Controller and why we create deployments

Sometime you must have heard about the application where multiple servers are running and an application is replicated on all servers to share the load of incoming requests. Its called load balancing by deploying multiple replicas of same application on all servers. 

Now, how would you do this in Kubernetes?

In Kubernetes , you no need to even ask to server team to setup this kind of deployment setup and it is too easy to do it. You just need to add few lines in your Kubernetes  configuration files. 

So, how Kubernetes  handling it?

in Kubernetes, you need create Deployment kind resources instead of Pod. In deployment configuration, you can declare how many replicas do you need to run your application and that is it. Now, Kubernetes  will create number of pods you mentioned in replicas configuration and whenever you update your application  code, deployment controller will handle to and will update all pods with updated application  code automatically .

Here is very simple example to create the deployment in Kubernetes :

 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  labels:
    app: my-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-deployment
  template:
    metadata:
      labels:
        app: my-deployment
    spec:
      containers:
        - name: my-apache
          image: phpstorm/php-73-apache-xdebug-27
          ports:
            - containerPort: 80
          lifecycle:
              postStart:
                exec:
                  command: ["/bin/sh", "-c", "echo '<?php sleep(5);echo \"Server Id is \".$_SERVER[\"SERVER_ADDR\"]; ?>' > index.php"]
---

apiVersion: v1
kind: Service
metadata:
  name: my-service-1
spec:
  selector:
    app: my-deployment
  type: NodePort
  ports:  
  - name: http
    port: 80
    targetPort: 80
    nodePort: 30037
    protocol: TCP
---

 

Tags