Reputation: 745
Let be the following service :
serivce1.yml
apiVersion: v1
kind: Service
metadata:
name: service1
spec:
type: ClusterIP
ports:
- port: 90
name: port0
targetPort: 40000
selector:
app: nginx
I apply as follow : kubectl apply -f service1.yml
Now I want to change the ports section. I could edit the yml and apply again but I prefere to use patch :
kubectl patch service service1 -p '{"spec":{"ports": [{"port": 80,"name":"anotherportspec"}]}}'
service/service1 patched
but this command adds a new port and keeps the old one :
$ kubectl describe svc service1
Name: service1
Namespace: abdelghani
Labels: <none>
Annotations: <none>
Selector: app=nginx
Type: ClusterIP
IP Families: <none>
IP: 10.98.186.21
IPs: <none>
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: <none>
My question: Is it possible to change the ports sections by replacing the old section with the one passed as parameter?
Thx
Upvotes: 1
Views: 7409
Reputation: 848
Just an FYI, when all else fails and you just need to get it done sed, awk, perl can help. here is an example of changing a service from ClusterIP to NodePort and using a specific port.
kubectl get service/kubernetes-dashboard -n kubernetes-dashboard -o yaml | sed -E "s/(targetPort:\s[0-9]{0,5})/\1\n\ \ \ \ nodePort: 32323/" | sed 's/type: ClusterIP/type: NodePort/' | kubectl replace -f -
*Please remember to make a quick backup first.
Upvotes: 2
Reputation: 7023
As we've (with @Abdelghani) discussed the problem is in patch strategy. Using following command:
$ kubectl patch svc service1 --type merge -p '{"spec":{"ports": [{"port": 80,"name":"anotherportspec"}]}}'
solves the problem.
Flag --type merge
will enable replace the existing values.
Read more: kubectl-patch.
As an alternative for patching you have couple options:
1. Edit your service using kubectl edit
command:
In prompt $ kubectl edit svc <service_name> -n <namespace>
i - to edit the service
ESC, :wq - update your service
Paste proper port and save file.
2. You can also manually edit service conf file:
vi your-service.yaml
update port number and apply changes
$ kubectl apply -f your-service.yaml
Upvotes: 6