It exposes the service on a cluster-internal IP.
When we expose a pod using kubectl expose
command , we are creating a service object in API.
Choosing this value makes the service only reachable from within the cluster. This is the default ServiceType.
We can see the Service
spec using --dry-run
& --output=yaml
There are a few scenarios where you would use the Kubernetes proxy to access your services.
Debugging your services, or connecting to them directly from your laptop for some reason Allowing internal traffic, displaying internal dashboards, etc. Because this method requires you to run kubectl as an authenticated user, you should NOT use this to expose your service to the internet or use it for production services.
piVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx-ctr
image: nginx:1.15.4
ports:
- containerPort: 80
$ kubectl apply -f deploy.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
labels:
app: nginx
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: nginx
$ kubectl apply -f clusterip-svc.yaml
$ kubectl get svc
$ kubectl run demo --it --rm --image=nginx:latest /bin/sh
$ apt-get update; apt-get install dnsutils; nslookup nginx-svc
If everything works then you should see a response.
$ kubectl delete svc nginx-svc
$ kubectl delete deploy nginx-deployment