Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes: create service vs expose deployment

I am new to Kubernetes. I was going through some tutorials related to Kubernetes deployment. I am seeing two different commands which looks like doing similar 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 a different place along with the 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 them to the outer world.

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

Would anyone please clarify this to me?

like image 667
Monaj Avatar asked Dec 18 '19 17:12

Monaj


People also ask

What is the difference between service and deployment in Kubernetes?

What's the difference between a Service and a Deployment in Kubernetes? A deployment is responsible for keeping a set of pods running. A service is responsible for enabling network access to a set of pods. We could use a deployment without a service to keep a set of identical pods running in the Kubernetes cluster.

Does a deployment create a service?

Deployment doesnt automatically create service object. you need to define service definition in a YAML targeting the ports from the pod definition inside deployment manifests. You need to deploy both deployment and service objects. you can deploy then separately or bundle them together in a single YAML and deploy.


1 Answers

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

like image 61
Nicolas Cordova Avatar answered Sep 23 '22 00:09

Nicolas Cordova