Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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
$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 8080:32765/TCP 18s
kubernetes ClusterIP 10.96.0.1 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 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


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Kubernetes: Create a service using yaml file

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×