Thursday 30 July 2020

Kubernetes: Create a service using yaml file

This is a continuation to my previous post. In my previous post, I explained how to expose a service using

 

Step 1: Create a deployment using yml file.

 

empService.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: employee-service-deployment
  labels:
    app: employee-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: employee-service
      labels:
        app: employee-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: employee-service-container
        image: jboss/wildfly

  replicas: 1
  selector:
    matchLabels:
      app: employee-service

Step 2: Create a deployment by executing the below command.

kubectl create -f empService.yml

$kubectl create -f empService.yml 
deployment.apps/employee-service-deployment created

Step 3: Create yml definition file to create a service.

 

createServiceToEmpService.yml

apiVersion: v1
kind: Service
metadata:
  name: emp-serv
spec:
  selector:
    app: employee-service
  ports:
  - port: 8080
    name: whatever
  type: NodePort

You can get the selector details from the below command.

kubectl describe deployment employee-service-deployment

 

Create service by executing the below command.

$kubectl create -f createServiceToEmpService.yml 
service/emp-serv created

Query all the services.

$kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
emp-serv     NodePort    10.102.82.169   <none>        8080:32765/TCP   18s
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          31d
Step 4: Get the Kubernetes URL for the specified service in your local cluster.
$minikube service emp-serv
|-----------|----------|---------------|-----------------------------|
| NAMESPACE |   NAME   |  TARGET PORT  |             URL             |
|-----------|----------|---------------|-----------------------------|
| default   | emp-serv | whatever/8080 | http://192.168.99.100:32765 |
|-----------|----------|---------------|-----------------------------|
🎉  Opening service default/emp-serv in default browser...

You will see WildFly server home page in the browser.


You can delete the service by executing the below command.

kubectl delete service emp-serv

$kubectl delete service emp-serv
service "emp-serv" deleted
$
$kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   31d

You can delete the deployment by executing the below command.

$kubectl delete deployment employee-service-deployment
deployment.apps "employee-service-deployment" deleted
$
$kubectl get deployments
No resources found in default namespace.




Previous                                                    Next                                                    Home

No comments:

Post a Comment