Skip to content

Instantly share code, notes, and snippets.

@orsenthil
Forked from lvthillo/deployment.yaml
Created March 26, 2024 05:50
Show Gist options
  • Save orsenthil/cf66a107d4ab554e3b9dde63d65f15e7 to your computer and use it in GitHub Desktop.
Save orsenthil/cf66a107d4ab554e3b9dde63d65f15e7 to your computer and use it in GitHub Desktop.
Example of NodePort service in Kubernetes
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
selector:
matchLabels:
run: my-app
replicas: 2
template:
metadata:
labels:
run: my-app
spec:
containers:
- name: my-app
image: lvthillo/python-flask-docker
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: my-app
labels:
run: my-app
spec:
type: NodePort
ports:
- port: 8080
protocol: TCP
selector:
run: my-app
@orsenthil
Copy link
Author

orsenthil commented Mar 26, 2024

Step 1. Launch the above deployment.

kubectl apply -f https://gist.githubusercontent.com/lvthillo/96e0c2514329e5a1f1150fbc8a709ff9/raw/1c97bbed442316ada1dd2e6ba15447e308483b5c/deployment.yaml

Step 2. Create a curl pod to access this

Save as curl.yaml

apiVersion: v1
kind: Pod
metadata:
  name: curl-pod
spec:
  containers:
  - name: curl-container
    image: curlimages/curl
    command: ['sh', '-c', 'while true; do sleep 30; done;']

kubectl apply -f curl.yaml

Get to curl pod

kubectl exec -it curl-pod -- /bin/sh

External Access from curl pod

curl http://example.com

Acccess the service created above using Cluster-IP and Port

~ $ curl http://10.100.29.171:8080
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index page</title>
</head>
<body>
The hostname of the container is <b>my-app-85bcd5f479-dsbqw</b> and its IP is <b>192.168.42.47</b>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment