how to change port of a kubernetes container using kubectl patch?

8/29/2019

I want to change the port of a container running on my kubernetes clusture. Manually I know this can be changed in the underlying YAML file itself.But I want to do this using a command like "kubectl patch" to change the port.

Nginx.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels: 
    app: nginx 
spec:
  type: NodePort     
  ports:
  - name: nginxport
    port: 80
    targetPort: 80
    nodePort: 30000
  selector:
    app: nginx
    tier: frontend    

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nginx
        tier: frontend
    spec:

      containers:
      - image: suji165475/devops-sample:#{Build.BuildId}#
        name: nginx
        ports:
        - containerPort: 80
          name: nginxport

Can anybody show me an example of the "kubectl patch" command using my nginx.yaml as an example for changing the container properties like the containerport,targetport,nodeport,port. And i also want to know on what basis the kubectl patch is applied.I mean how does it know what container to patch and on what criteria like containerid,name etc because later I will be creating a html button to do a kubectl patch based on some criteria like the containerid or name.So kindly help.

-- Vignesh Swaminathan
docker
kubernetes

1 Answer

8/29/2019

say, you want to update the target port to 8080 in service. follow the below steps

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels: 
    app: nginx 
spec:
  type: NodePort     
  ports:
  - name: nginxport
    port: 80
    targetPort: 80
    nodePort: 30000
  selector:
    app: nginx
    tier: frontend 

Patch the nginx service using the below command

# kubectl patch svc nginx --patch \
>   '{"spec": { "type": "NodePort", "ports": [ { "nodePort": 30000, "port": 80, "protocol": "TCP", "targetPort": 8080 } ] } }'
service/nginx patched

to update nodeport and targetport use the below command

kubectl patch svc nginx --patch \
  '{"spec": { "type": "NodePort", "ports": [ { "nodePort": 32000, "port": 80, "protocol": "TCP", "targetPort": 8080 } ] } }'

verify that the targetPort is updated to 8080

master $ kubectl get svc nginx -oyamlapiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2019-08-29T11:08:45Z"
  labels:
    app: nginx
  name: nginx
  namespace: default
  resourceVersion: "5837"
  selfLink: /api/v1/namespaces/default/services/nginx
  uid: 5e7f6165-ca4d-11e9-be03-0242ac110042
spec:
  clusterIP: 10.105.220.186
  externalTrafficPolicy: Cluster
  ports:
  - name: nginxport
    nodePort: 30000
    port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: nginx
    tier: frontend
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

follow similar approach for deployment using

kubectl patch deploy nginx --patch .....
-- P Ekambaram
Source: StackOverflow