I have a kubernetes service, and I wanna it to target pods.
This is how I define my service:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: myapp
ports:
- port: 77
targetPort: 80
nodePort: 32766
type: NodePort
and this is how I define my deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 5
selector:
matchLabels:
app: myapp
template:
metadata:
name: my-pod
labels:
app: myapp
spec:
containers:
- name: httd
image: httpd
imagePullPolicy: Always
ports:
- containerPort: 80
Basically what I did is linking port 80 in the pod to port 77 in the service to port 32766 in the node.
I already know that my container is running on port 80 because If I do this:
docker run -p 8989:80 httpd
and ask for localhost:8989 I can see the page.
If I do kubetctl get services I get:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 46h
my-service NodePort 10.100.157.161 <none> 77:32766/TCP 20m
I tried calling:
10.100.157.161:32766
10.100.157.161:77
But both give connection error.
What did I miss?
Use the below YAMLs. It works
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: myapp
name: my-deployment
spec:
replicas: 5
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: httpd
name: httpd
imagePullPolicy: Always
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: myapp
ports:
- port: 77
targetPort: 80
nodePort: 32766
type: NodePort
master $ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15m
my-service NodePort 10.110.219.232 <none> 77:32766/TCP 12s
master $
master $
master $ curl 10.110.219.232:77
<html><body><h1>It works!</h1></body></html>
master $
master $
master $ curl $(hostname -i):32766
<html><body><h1>It works!</h1></body></html>
master $