67

The command kubectl get service returns a list of services that were created at one point in time:

NAME                     CLUSTER-IP   EXTERNAL-IP   PORT(S)                         AGE
car-example-service      10.0.0.129   <nodes>       8025:31564/TCP,1025:31764/TCP   10h
circle-example-service   10.0.0.48    <nodes>       9000:30362/TCP                  9h
demo-service             10.0.0.9     <nodes>       8025:30696/TCP,1025:32047/TCP   10h
example-servic           10.0.0.168   <nodes>       8080:30231/TCP                  1d
example-service          10.0.0.68    <nodes>       8080:32308/TCP                  1d
example-service2         10.0.0.184   <nodes>       9000:32727/TCP                  13h
example-webservice       10.0.0.35    <nodes>       9000:32256/TCP                  1d
hello-node               10.0.0.224   <pending>     8080:32393/TCP                  120d
kubernetes               10.0.0.1     <none>        443/TCP                         120d
mouse-example-service    10.0.0.40    <nodes>       9000:30189/TCP                  9h
spring-boot-web          10.0.0.171   <nodes>       8080:32311/TCP                  9h
spring-boot-web-purple   10.0.0.42    <nodes>       8080:31740/TCP                  9h

I no longer want any of these services listed, because when I list resources: % kubectl get rs

I am expecting that I only see the spring-boot-web resource listed.

NAME                         DESIRED   CURRENT   READY     AGE
spring-boot-web-1175758536   1         1         0         18m

Please help clarify why I am seeing services that are listed , when the resources only show 1 resource.

Kevin Ghaboosi
  • 606
  • 10
  • 20
joe the coder
  • 739
  • 2
  • 8
  • 15
  • 1
    deleting deployment: `kubectl delete deployment `, deleting pod: `kubectl delete pod `, deleting service: `kubectl delete service ` – Ozal Zarbaliyev May 28 '22 at 13:27

7 Answers7

99

Simply call this command.

  1. Get all available services:
kubectl get service -o wide
  1. Then you can delete any services like this:
kubectl delete svc <YourServiceName>
holydragon
  • 6,158
  • 6
  • 39
  • 62
Reza
  • 1,906
  • 1
  • 17
  • 21
44

show deployment

$ kubectl get deployments;

NAME              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
spring-hello      1         1         1            1           22h
spring-world      1         1         1            1           22h
vfe-hello-wrold   1         1         1            1           14m

show services

$kubectl get services;

NAME              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes        ClusterIP   10.96.0.1        <none>        443/TCP          2d
spring-hello      NodePort    10.103.27.226   <none>        8081:30812/TCP   23h
spring-world      NodePort    10.102.21.165    <none>        8082:31557/TCP   23h
vfe-hello-wrold   NodePort    10.101.23.36     <none>        8083:31532/TCP   14m

delete deployment

$ kubectl delete deployments vfe-hello-wrold

deployment.extensions "vfe-hello-wrold" deleted

delete services

$ kubectl delete service vfe-hello-wrold

service "vfe-hello-wrold" deleted
取一个好的名字
  • 1,677
  • 14
  • 16
6

Kubernetes objects like Service and Deployment/ReplicaSet/Pod are independent and their deletions do not cascade to each other (like it does between say Deployment/RS/Pod). You need to manage your services independently from other objects, so you just need to delete the ones that are still lingering behind.

Radek 'Goblin' Pieczonka
  • 21,554
  • 7
  • 52
  • 48
6

If you want to delete multiple related or non related objects at the same time

 kubectl delete <objType>/objname <objType>/objname <objType>/objname

Example

 kubectl delete service/myhttpd-clusterip service/myhttpd-nodeport

 kubectl delete service/myhttpd-lb deployment/myhttpd

This also works

kubectl delete deploy/httpenv svc/httpenv-np
Abhash Kumar
  • 1,168
  • 8
  • 12
5

To delete ALL services in ALL namespaces just run:

kubectl delete --all services --namespace=*here-you-enter-namespace

The other option is to delete the deployment with:

kubectl delete deployment deployment-name

That will delete the service as well!

IMPORTANT: And watch out when you run this command in production!

Cheers!

Dragomir Ivanov
  • 775
  • 1
  • 6
  • 4
  • Please consider [edit]ing your answer to add some explanation and details. While it might answer the question, just adding some code does not help OP or future community members understand the issue or solution. – hongsy Jan 24 '20 at 16:41
  • 3
    imagine I was here to delete my app in prod and copied this - deleting all apps in prod... – wilmol Oct 04 '21 at 03:33
  • 4
    @wilmol imagine your manager finding out that you copy-pasted something from StackOverflow into production without knowing what it was doing. – likejudo Jun 04 '22 at 17:52
  • I have a manager? :D What does he manage? :P Oh no, I may be the manager! :S O tempora, o mores! Quis custodiet ipsos custodes? The code seems self explanatory, but the warining is important – ntg Dec 07 '22 at 07:10
0

First find the service:

kubectl get service -A

Note the namespace of the service you want to delete. Then delete using

kubectl delete service <YourServiceName> --namespace <YourServiceNameSpace>

Also, check -carefully- if answer by @Dragomir Ivanov better fits your needs

ntg
  • 12,950
  • 7
  • 74
  • 95
0

If you're having trouble you probably forgot to specify the namespace:

-n some-namespace

This tripped me up quite a bit.

John Hunt
  • 4,265
  • 8
  • 45
  • 59