<p>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.</p>

<p>Thanks in Advance!</p>

Fahad
<p>From Kubernetes point of view you could try to do the following: </p>

<ul>
<li>Create a deployment </li>
<li>Create a <strong>separate</strong> service object type of <code>LoadBalancer</code> that will point to your application </li>
<li>Test it </li>
<li>Create a new deployment in place of old one </li>
<li>Test it again </li>
</ul>

<hr>

<p>Example with <code>YAML</code>'s: </p>

<h3>Create a deployment</h3>

<p>Below is example deployment of hello application:</p>

<pre><code>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"
</code></pre>

<p>Take a specific look on parts of <code>matchLabels</code>.</p>

<h3>Create a service object type of <code>LoadBalancer</code> that will point to your application</h3>

<p>Below is example service that will give access to hello application:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: hello-service
spec:
  selector:
    app: hello
  ports:
    - port: 50001
      targetPort: 50001
  type: LoadBalancer
</code></pre>

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

<p>You could refer to official documentation: <a href="https://aws.amazon.com/premiumsupport/knowledge-center/eks-kubernetes-services-cluster/" rel="nofollow noreferrer">HERE!</a></p>

<h3>Test it</h3>

<p>Apply both <code>YAML</code> definitions and wait for <code>ExternalIP</code> to be assigned. After that check if application works. </p>

<p>Output of web browser with old application version: </p>

<pre class="lang-sh prettyprint-override"><code>Hello, world!
Version: 1.0.0
Hostname: hello-549db57dfd-g746m
</code></pre>

<h3>Create a new deployment in place of old one</h3>

<p>On this step you could try to:</p>

<ul>
<li>run <code>kubectl apply -f DEPLOYMENT.yaml</code> on new version  to apply the differences </li>
<li>try first to delete the <code>Deployment</code> and create a new one in place of old one. </li>
</ul>

<p><strong>In this step do not delete your existing <code>LoadBalancer</code>.</strong> </p>

<p>Using example <code>Deployment</code> above we could simulate a version change of your image by changing the: </p>

<pre><code>        image: "gcr.io/google-samples/hello-app:1.0"
</code></pre>

<p>to: </p>

<pre><code>        image: "gcr.io/google-samples/hello-app:2.0"
</code></pre>

<p>After that you can apply it or recreate your deployment. </p>

<p><code>LoadBalancer</code> should not have changed IP address as it's not getting recreated. </p>

<h3>Test it again</h3>

<p>Output of web browser with new application version: </p>

<pre class="lang-sh prettyprint-override"><code>Hello, world!
Version: 2.0.0
Hostname: hello-84d554cbdf-rbwgx
</code></pre>

<p>Let me know if this solution helped you. </p>
