AWS EKS Deployment updating the existing deployment

2/26/2020

I'm working on EKS AWS and stuck because when ever I want to deployment the same code again after some changes I have to delete the previous deployment and create with back with new image, after pulling the new image in ECR (kubectl delete abcproj.json) which destroys the old pods(load balancer) and creates new one in the result its always give me new external IP. I want to prevent this problem and cannot find proper solution on internet.

Thanks in Advance!

-- Fahad
amazon-web-services
eks
kubernetes

1 Answer

2/26/2020

From Kubernetes point of view you could try to do the following:

  • Create a deployment
  • Create a separate service object type of LoadBalancer that will point to your application
  • Test it
  • Create a new deployment in place of old one
  • Test it again

Example with YAML's:

Create a deployment

Below is example deployment of hello application:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
spec:
  selector:
    matchLabels:
      app: hello
      version: 1.0.0
  replicas: 1
  template:
    metadata:
      labels:
        app: hello
        version: 1.0.0
    spec:
      containers:
      - name: hello
        image: "gcr.io/google-samples/hello-app:1.0"
        env:
        - name: "PORT"
          value: "50001"

Take a specific look on parts of matchLabels.

Create a service object type of LoadBalancer that will point to your application

Below is example service that will give access to hello application:

apiVersion: v1
kind: Service
metadata:
  name: hello-service
spec:
  selector:
    app: hello
  ports:
    - port: 50001
      targetPort: 50001
  type: LoadBalancer

Take once again a specific look on selector. It will match the pods by label named app with value of hello.

You could refer to official documentation: HERE!

Test it

Apply both YAML definitions and wait for ExternalIP to be assigned. After that check if application works.

Output of web browser with old application version:

Hello, world!
Version: 1.0.0
Hostname: hello-549db57dfd-g746m

Create a new deployment in place of old one

On this step you could try to:

  • run kubectl apply -f DEPLOYMENT.yaml on new version to apply the differences
  • try first to delete the Deployment and create a new one in place of old one.

In this step do not delete your existing LoadBalancer.

Using example Deployment above we could simulate a version change of your image by changing the:

        image: "gcr.io/google-samples/hello-app:1.0"

to:

        image: "gcr.io/google-samples/hello-app:2.0"

After that you can apply it or recreate your deployment.

LoadBalancer should not have changed IP address as it's not getting recreated.

Test it again

Output of web browser with new application version:

Hello, world!
Version: 2.0.0
Hostname: hello-84d554cbdf-rbwgx

Let me know if this solution helped you.

-- Dawid Kruk
Source: StackOverflow