Creating a Kubernetes service is the first step in exposing your application to the world. A Kubernetes service defines how an application is exposed to the outside world. It also defines how load balancing and routing are handled for that application. In this article, we will create a Kubernetes service a web application running Nginx.
To create a Kubernetes service, you will need to have the kubectl command-line tool installed on your machine and configured with your Kubernetes cluster.
Using a manifest file to create a service is always better than creating a service using a kubectl command.
In the following YAML configuration file, I’m creating a deployment and service at the same time. the — is a sign that a new configuration kind is starting. The service configuration exposes port 80.
If you look at the service configuration (2nd section) you will notice that I’m using a LoadBalancer service to route the traffic to the application.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selector: matchLabels: app: web-app template: metadata: labels: app: web-app spec: containers: - name: web-app image: nginx restartPolicy: Always --- apiVersion: v1 kind: Service metadata: name: web-app spec: type: LoadBalancer selector: app: web-app ports: - port: 80