<p>After <a href="https://github.com/GoogleCloudPlatform/click-to-deploy/blob/master/k8s/prometheus/README.md" rel="nofollow noreferrer">getting Prometheus up for a gke cluster</a>, I ran the step to add an external ip address for Grafana:</p>
<pre><code>kubectl patch svc &quot;prometheus-1-grafana&quot; --namespace &quot;cluster-1&quot; \
  -p '{&quot;spec&quot;: {&quot;type&quot;: &quot;LoadBalancer&quot;}}'
</code></pre>
<p>but now no longer want Grafana to be available via an external ip.</p>
<p>I've tried running with <code>-p '{&quot;spec&quot;: {&quot;type&quot;: &quot;ClusterIP&quot;}}'</code> but I just get the error:</p>
<pre><code>The Service &quot;prometheus-1-prometheus&quot; is invalid: 
spec.ports[0].nodePort: Forbidden: may not be used when
`type` is 'ClusterIP'
</code></pre>
<p>How do I do the above <code>kubectl patch svc</code> command to remove the external ip?</p>

<p>When you change the service to <code>LoadBalancer</code>, a <code>NodePort</code> is attributed to the service.</p>

<p>In order to return to <code>ClusterIP</code> you need to also remove the <code>NodePort</code>.</p>

<ul>
<li>Using <code>kubectl patch</code> we will set the <code>NodePort</code> to <code>NULL</code>, here is the command:</li>
</ul>

<pre><code>kubectl patch svc "prometheus-1-grafana" --namespace "cluster-1" --type="merge" \
-p '{"spec":{"ports":[{"nodePort":null,"port":&lt;PORT_NUMBER&gt;}],"type":"ClusterIP"}}'
</code></pre>

<p><strong>Note:</strong> Kubernetes will not allow you to set the <code>nodePort</code> to <code>null</code> alone, because the <code>Port</code> field is obligatory, make sure to check the correct port and change it, I'm using an http server as example.</p>

<ul>
<li>Optionally, you can create a <code>patch.yaml</code>:</li>
</ul>

<pre><code>spec:
  ports:
  - port: &lt;PORT_NUMBER&gt;
    protocol: TCP
    targetPort: &lt;TARGET_PORT_NUMBER&gt;
  type: ClusterIP
</code></pre>

<p>and Apply it:</p>

<pre><code>kubectl patch svc "prometheus-1-grafana" --namespace "cluster-1" \
--type="merge" --patch "$(cat patch.yaml)"  
</code></pre>

<hr>

<p><strong>Reproduction:</strong></p>

<pre><code>$ kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
echo-svc     ClusterIP   10.0.13.9    &lt;none&gt;        80/TCP    65m

$ kubectl patch svc "echo-svc" -p '{"spec": {"type": "LoadBalancer"}}'                                                                
service/echo-svc patched

$ kubectl get svc
NAME         TYPE           CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
echo-svc     LoadBalancer   10.0.13.9    &lt;pending&gt;     80:32021/TCP   65m

$ kubectl patch svc "echo-svc" --type="merge" -p '{"spec":{"ports":[{"nodePort":null,"port":80}],"type":"ClusterIP"}}'
service/echo-svc patched

$ kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
echo-svc     ClusterIP   10.0.13.9    &lt;none&gt;        80/TCP    66m

$ kubectl patch svc "echo-svc" -p '{"spec": {"type": "LoadBalancer"}}'                                                                
service/echo-svc patched

$ kubectl get svc
NAME         TYPE           CLUSTER-IP   EXTERNAL-IP      PORT(S)        AGE
echo-svc     LoadBalancer   10.0.13.9    35.223.145.193   80:30394/TCP   66m

$ cat patch.yaml 
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  type: ClusterIP

$ kubectl patch svc "echo-svc" --type="merge" --patch "$(cat patch.yaml)"                                                             
service/echo-svc patched

$ kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
echo-svc     ClusterIP   10.0.13.9    &lt;none&gt;        80/TCP    66m
</code></pre>

<p><strong>References:</strong></p>

<ul>
<li><a href="http://jsonpatch.com/" rel="nofollow noreferrer">Json Patch</a></li>
<li><a href="https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/" rel="nofollow noreferrer">Update API Object with Kubectl Patch</a></li>
</ul>
