Issue
I'm running a deployment with three nginx pods and a service but when I try to connect to the service cluster-IP it doesn't connect. Below are my YAML files and kubectl commands.
nginx-deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
nginx-service:
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- port: 80
protocol: TCP
targetPort: 80
ubuntu@k8s-t2-cp:~/nginx$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-55f598f8d-6d25c 1/1 Running 0 23m 192.168.26.152 ip-172-30-1-183 <none> <none>
nginx-deployment-55f598f8d-6j92r 1/1 Running 0 23m 192.168.26.151 ip-172-30-1-183 <none> <none>
nginx-deployment-55f598f8d-rhpwv 1/1 Running 0 23m 192.168.26.150 ip-172-30-1-183 <none> <none>
ubuntu@k8s-t2-cp:~/nginx$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 125m
nginx ClusterIP 10.106.154.85 <none> 80/TCP 23m
The curl command just hangs:
ubuntu@k8s-t2-cp:~/nginx$ curl 10.106.154.85
Solution
ClusterIP is not for external access as I can see you are using the curl command from your Ubuntu machine. ClusterIP is only accessible inside the namespace on the pod level. If you want to check if the service is working or not without shifting service type you can do as follow.
Exec into any nginx pod by using this command kubectl exec -it my-pod -- /bin/bash
In your case it will be like that kubectl exec -it nginx-deployment-55f598f8d-6d25c -- /bin/bash
Now you will be inside of pod and curl command should work.
You may have to install curl manually depending upon which Linux image is used by your pods.
Another way to test your services is to convert your ClusterIP into nodePort or Loadbalancer.
Answered By - user19238163 Answer Checked By - Candace Johnson (WPSolving Volunteer)