Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

In this demo:

-------------
We will create the sample deployment and expose it using nodePort service. Finally
we will test it!

***********************************************************************************
****************

1. Creating Sample Deployment and Service "Declaratively (Using YAML file)":


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

a) Deployment YAML file:


------------------------
# nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx-app
spec:
replicas: 1
selector:
matchLabels:
app: nginx-app
template:
metadata:
labels:
app: nginx-app
spec:
containers:
- name: nginx-container
image: nginx:1.18
ports:
- containerPort: 80

b) NodePort Service YAML file:


------------------------------
# Service
# nginx-svc-np.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
labels:
app: nginx-app
spec:
selector:
app: nginx-app
type: NodePort
ports:
- nodePort: 31111
port: 80
targetPort: 80

c) Deploying Applications:
--------------------------
kubectl apply -f nginx-deploy.yaml
kubectl apply -f nginx-svc-np.yaml

***********************************************************************************
****************

2. Creating Deployment and NodePort Service Application "Imperatively (From Command


promp)":
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~

Deployment:
-----------
kubectl create deployment [DEPLOYMENT-NAME] --image=[CONTAINER-IMAGE]--
replicas=[REPLICA-COUNT]

Service:
--------
kubectl expose deployment [DEPLOYMENT-NAME] --type=NodePort --name=[SERVICE-NAME]
--port=[PORT-NUMBER]

***********************************************************************************
****************

Second option:

a. Create Sample Deployment


---------------------------
kubectl create deployment test --image=nginx --replicas=3

b. Next, expose the previous Deployment:


-----------------------------------------
kubectl expose deployment test --type=NodePort --name=test-svc --port=80

c. Now, get the NodePort service number on which this service is exposed on:
-----------------------------------------------------------------------------
kubectl get svc

d. Next, get the external IP address of the worker node on which this Pod is
running
-----------------------------------------------------------------------------------
--
kubectl get pods -o wide

e. Then, you can open the any browser or execute curl command as mentioned below.
The syntax is the external IP of the worker node followed by nodePort number
-----------------------------------------------------------------------------

root@master:~# curl http://node_IP:Node_port/


<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>

***********************************************************************************
****************

4. Cleanup
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kubectl delete deploy [NAME]
kubectl delete svc [NAME]

***********************************************************************************
****************

You might also like