Kubernetes: create service vs expose deployment

12/18/2019

I am new to kubernetes. I was going through some tutorials related to kubernetes deployment. I am seeing two different command which looks like doing slimier things.

  1. The below command is from google code lab (URL : https://codelabs.developers.google.com/codelabs/cloud-springboot-kubernetes/index.html?index=..%2F..index#7 )

    $ kubectl create service loadbalancer hello-java --tcp=8080:8080

  2. Another command is being seen in different place along with Kubernetes site (https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/)

    $ kubectl expose deployment hello-world --type=LoadBalancer --name=my-service

Now as per my understanding both the command are creating services from deployments with loadbalancer and exposing to outer world.

I don't think there will be two separate command for same task. There should be some difference which I am not able to understand.

Would anyone please clarify this to me.

-- Monaj
google-cloud-platform
google-kubernetes-engine
kubernetes

2 Answers

12/18/2019

I hope this helps a little : Here the key would be to understand the difference between services and deployments. As per this link [1] you will notice that a deployment deals with the mortality of Pods automatically. However , if a Pod is terminated and then another is spun up how do the Pods continue to communicate when their IPs change? They use Services : “a Service is an abstraction which defines a logical set of Pods and a policy by which to access them”. Additionally, it may be of interest to view this link [2] as it describes that the kubectl expose command creates a service which in turn creates an external IP and a Load Balancer. As a beginner it may be of help to review the command language used with Kubernetes, this link [3] describes (as mentioned in another answer) that the kubectl create command is used to be more specific about the objects it creates. As well using the create command you can create a larger variety of objects.

[1]:Service :https://kubernetes.io/docs/concepts/services-networking/service/ [2]:Deploying a containerized web application :https://cloud.google.com/kubernetes-engine/docs/tutorials/hello-app#step_6_expose_your_application_to_the_internet [3]:How to create objects: https://kubernetes.io/docs/tasks/manage-kubernetes-objects/imperative-command/#how-to-create-objects

-- Derek Fender
Source: StackOverflow

12/18/2019

The main differences can be seen from the docs.

1.- kubectl create command

Create a resource from a file or from stdin.

JSON and YAML formats are accepted.

2.- kubectl expose command

Expose a resource as a new Kubernetes service.

Looks up a deployment, service, replica set, replication controller or pod by name and uses the selector for that resource as the selector for a new service on the specified port. [...]


Even though both achieve the same thing in the examples you provided, the create command is kind of a more global one, with it you can create all resources by using the command line or a yaml/json file. However, the expose command will only create a service resource, and it's mainly used to expose other already existing resources.

Source: K8s Docs

-- Cristian Cordova
Source: StackOverflow