kuberneteskubernetes-podkubernetes-servicekubernetes-deployment

Why Kubernetes Services are created before Deployment/Pods?


If I have to deploy a workload on Kubernetes and also have to expose it as a service, I have to create a Deployment/Pod and a Service. If the Kubernetes offering is from Cloud and we are creating a LoadBalancer service, it makes sense to create the the service first before the workload as the url creation for the service takes time. But in case the Kubernetes deployed on non-cloud platform there is no point in creating the Service before the workload. So why I have to create a service first then the workload?


Solution

  • There is no requirement to create a service before a deployment or vice versa. You can create the deployment before the service or the service before the deployment.

    If you create the deployment before the service, then the application packaged in the deployment will not be accessible externally until you create the LoadBalancer.

    Conversely, if you create the LoadBalancer first, the traffic for the application will not be routed to the application as it hasn't been created yet, giving 503s to the caller.

    You are declaring to Kubernetes how you want the state of the infrastructure to be. e.g. "I want a deployment and a service". Kubernetes will go off and create those, but they may not necessarily end up being creating in a predictable order. For example, LoadBalancers take a while to assign IPs from your Cloud provider, so even though the resource is created in the cluster, it's not actually getting any traffic.