DEV Community

Mayank Gaikwad
Mayank Gaikwad

Posted on

3 Things to make your Kubernetes Deployment look good

Kubernetes Do's and Don'ts for Cloud Native Application Developers.

What is Kubernetes?

Kubernetes, a "Container Orchestration Platform", runs container workloads efficiently in a distributed environment.

Application workloads might mean Microservices or Macroservices that run in an isolated environment and interact with other services.

Pod-Service Communication

#1 Use Service

Pods are the smallest deployable units of computing that can be created and managed in Kubernetes.

Application runs inside pod in a container. As Kubernetes assigns internal IP Address to each pod, all deployed applications can easily communicate over Pod IP within the cluster.

However, pods may be rescheduled in case of failures that leads to a new IP assignment. So relying on Pod IP for application networking is not preferred.

Service acts as an internal Load Balancer to the pods defined within. Service enables pod discovery and traffic routing. Now application can communicate with the service over ClusterIP without worrying about Pod IP.

#2 Define container resource limit

Always define container resource limits as per expected application's footprint. Computing resources are CPU and Memory. Missing limit declaration may exhaust entire node resource and may lead running pod eviction.

#3 Handle Errors Gracefully
Application should handle the errors gracefully instead of an abrupt exit. Kubernetes will restart terminated containers repeatedly unless pod restartPolicy is set to "Never".

foo.yaml : Create a foo-pod with two containers. One container can be your main application and another container may be supporting application to the main application (or sidecar).

apiVersion: v1
kind: Pod
metadata:
  name: foo
  labels:
    app: foo-lab
spec:
  containers:
  - name: foss
    image: foss:v1
    resources:
      limits:
        memory: "128Mi"
        cpu: "200m"
    ports:
      - name: foss
        containerPort: 8080
  - name: fox
    image: fox:v1
    resources:
      limits:
        memory: "128Mi"
        cpu: "200m"
    ports:
      - name: fox
        containerPort: 8081

foo-service.yaml: Create foo-services with two ports to point two different ports exposed by the foo-pod.

apiVersion: v1
kind: Service
metadata:
  name: foo-service
spec:
  selector:
    app: foo-lab
  ports:
  - name: foss-tcp
    port: 80
    targetPort: 8080
  - name: fox-tcp
    port: 81
    targetPort: 8081

bar.yaml: Create a bar-pod and use environment variable to point foo-service.

apiVersion: v1
kind: Pod
metadata:
  name: bar
  labels:
    app: bar-lab
spec:
  containers:
  - name: bar
    image: bar:v1
    env:
    - name: FOSS_SERVICE
      value: foo-service:80
    - name: FOX_SERVICE
      value: foo-service:81
    resources:
      limits:
        memory: "128Mi"
        cpu: "200m"
    ports:
      - name: bar-port
        containerPort: 8080

Latest comments (0)