Kubernetes Tips for Cloud Native Application Developers

What is Kubernetes?

Kubernetes, a "Container Orchestration Platform", that runs container workloads efficiently in a distributed environment. Application workloads are defined as microservices or macroservices that run in an isolated environment and interact with other services.


#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".

 

blog-kubernetes made easy

 

 

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

Like this information? Share it:

You may also want to review these recent posts.

Create a Multi-node Production-like Kubernetes Development Environment
Create a Multi-node Production-like Kubernetes Development Environment
25 October, 2021

How you can run and test containerized applications on a local systems. For Developers to run and test their containeriz...

How to Validate the Interoperability of Microservices in Development
How to Validate the Interoperability of Microservices in Development
26 October, 2021

Learn how validating the interoperability of the microservices in development avoids service integration issues prior to...

See How Fast & Easy it is to Create and Share a Kubernetes Cluster
See How Fast & Easy it is to Create and Share a Kubernetes Cluster
1 November, 2021

See how easy it is for developers to enable and share a Kubernetes multi-tenant cluster and share it with others outside...