<p>Let be the following service :</p>
<pre><code>serivce1.yml
apiVersion: v1
kind: Service
metadata:
  name: service1
spec:
  type: ClusterIP
  ports:
   - port: 90
     name: port0
     targetPort: 40000
  selector:
    app: nginx
</code></pre>
<p>I apply as follow : kubectl apply -f service1.yml</p>
<p>Now I want to change the ports section. I could edit the yml and apply again but I prefere to use patch :</p>
<pre><code>kubectl patch service service1 -p '{&quot;spec&quot;:{&quot;ports&quot;: [{&quot;port&quot;: 80,&quot;name&quot;:&quot;anotherportspec&quot;}]}}'
service/service1 patched
</code></pre>
<p>but this command adds a new port and keeps the old one :</p>
<pre><code>$ kubectl describe svc service1
Name:              service1
Namespace:         abdelghani
Labels:            &lt;none&gt;
Annotations:       &lt;none&gt;
Selector:          app=nginx
Type:              ClusterIP
IP Families:       &lt;none&gt;
IP:                10.98.186.21
IPs:               &lt;none&gt;
Port:              anotherportspec  80/TCP
TargetPort:        80/TCP
Endpoints:         10.39.0.3:80
Port:              port0  90/TCP
TargetPort:        40000/TCP
Endpoints:         10.39.0.3:40000
Session Affinity:  None
Events:            &lt;none&gt;
</code></pre>
<p>My question:
Is it possible to change the ports sections by replacing the old section with the one passed as parameter?</p>
<p>Thx</p>

Abdelghani
<p>As we've (with @Abdelghani) discussed the problem is  in patch strategy. Using following command:</p>
<p><code>$ kubectl patch svc service1 --type merge -p '{&quot;spec&quot;:{&quot;ports&quot;: [{&quot;port&quot;: 80,&quot;name&quot;:&quot;anotherportspec&quot;}]}}'</code> solves the problem.</p>
<p>Flag <code>--type merge</code> will enable replace the existing values.</p>
<p>Read more: <a href="https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/" rel="nofollow noreferrer">kubectl-patch</a>.</p>
<p>As an alternative for patching you have couple options:</p>
<p><strong>1.</strong>   Edit your service using <code>kubectl edit</code> command:
In prompt  <code>$ kubectl edit svc &lt;service_name&gt; -n &lt;namespace&gt;</code></p>
<pre><code>i - to edit the service

ESC, :wq - update your service
</code></pre>
<p>Paste proper port and save file.</p>
<p><strong>2.</strong> You can also manually edit service conf file:</p>
<pre><code>vi your-service.yaml
</code></pre>
<p>update port number and apply changes</p>
<pre><code>$ kubectl apply -f your-service.yaml
</code></pre>
