0

I have a docker nginx container image nha/my-nginx-img. And I have a one-node kubernetes cluster, installed on bare metal. I am trying to deploy and expose this nginx container (which works fine locally otherwise).

I ran kubectl apply -f nginx.yaml on this file:

apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx labels: app: nginx spec: selector: matchLabels: run: my-nginx replicas: 2 template: metadata: labels: run: my-nginx spec: containers: - name: my-nginx image: nha/my-nginx-img ports: - containerPort: 80 - containerPort: 443 imagePullSecrets: - name: regcred # # Expose Service # apiVersion: v1 kind: Service metadata: name: my-nginx labels: run: my-nginx spec: # I thought using NodePort here # would expose the port on the node? type: NodePort ports: - name: http protocol: TCP port: 80 targetPort: 80 - name: https protocol: TCP port: 443 targetPort: 443 selector: run: my-nginx 

I can see them running:

kubectl get pods -l run=my-nginx -o wide NAME READY STATUS RESTARTS AGE IP NODE my-nginx-5ccbf78584-4lxsn 1/1 Running 0 1d 10.233.102.181 node1 my-nginx-5ccbf78584-6qkml 1/1 Running 0 1d 10.233.102.182 node1 

However: - the IPs show in the resulting command above are NOT the IP of my machine - when I curl the IP of my machine, I do not get a reply on either the port 80 or 443

How do I get this static content to be served by nginx?

Additional information

kubectl get services gives:

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 2d my-nginx NodePort 10.233.16.91 <none> 80:31081/TCP,443:31856/TCP 5h 

(10.233.16.91 is NOT my IP)

And kubectl describe service my-nginx:

Name: my-nginx Namespace: default Labels: run=my-nginx Annotations: kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"run":"my-nginx"},"name":"my-nginx","namespace":"default"},"spec":{"ports":[... Selector: run=my-nginx Type: NodePort IP: 10.233.16.91 Port: http 80/TCP TargetPort: 80/TCP NodePort: http 31081/TCP Endpoints: 10.233.102.181:80,10.233.102.182:80 Port: https 443/TCP TargetPort: 443/TCP NodePort: https 31856/TCP Endpoints: 10.233.102.181:443,10.233.102.182:443 Session Affinity: None External Traffic Policy: Cluster Events: <none> 

Again, I do not see my IP anywhere in there.

Also, I created the cluster using kubespray.

2 Answers 2

1

You'll want the service, not the pod. The pod is not exposed to anything except the local kubernetes network, this is why you create the service.

apiVersion: v1 kind: Service metadata: name: my-nginx labels: run: my-nginx spec: # I thought using NodePort here # would expose the port on the node? type: NodePort ports: - name: http protocol: TCP port: 80 targetPort: 80 - name: https protocol: TCP port: 443 targetPort: 443 selector: run: my-nginx 

This here defines the service, and it does expose the port on the node, but the IP you see is the internal "cluster IP" which can only be accessed by other pods.

So you might try kubectl get services

This will show you the external ip of the port that is exposed.

Also check out kubectl describe service yourServiceName

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I don't completely get "You'll want the service, not the pod." - I miss a part in my yaml definition? Also I ran the commands and I cannot see my machine's IP in there. I actually do not know where these IPs come from.
@nha No, you didn't miss a part in your YAML definition, I think you just don't understand it. Your pod is never exposed so you will never see the node IP if you look up kubectl describe pods. The service is what exposes the pod so you must look up the service. You should try kubectl describe service yourServiceName, I cannot check right now but I believe the correct ip is in there next to a marker that says "nodePort"
Yes I am very new to kubernetes. I edited my question to include the results of the commands you pointed out. The node port is there indeed, but not the value I expected. And you are correct, there is no "EXTERNAL-IP" for my-nginx.
@nha The NodePort shows the port your service is exposed on, which is 31081, so if you curl your machines IP on that port, then you will get the response. If you want to manually specify a nodeport you must do that in your YAML definition of the service.
1

How do I get this static content to be served by nginx?

You created service of NodePort type. Use <NodeIP>:<NodePort>. In your example, http://<NodeIP>:31081, https://<NodeIP>:31856. Also <ClusterIP>:<Port> works inside the cluster.

Strongly recommended docs: https://kubernetes.io/docs/concepts/services-networking/service/

4 Comments

I was going to comment on @RubyJunk answer - it looks like I should be able to reach it indeed, however I do not get any reply. Any advice on how to debug that?
@nha "however I do not get any reply", what's the exact response when you curl <NodeIP>:<NodePort>? I've used the yaml you provided (only with different image:nginx:1.7.9) and it works. And no network issue (firewall, etc.)?
No network issue that I know of (I can ssh into the box, start a simple python server on port 8081 and see it in the browser). curl: (7) Failed to connect to 12.34.56.78 port 31081: Connection refused. I can run my image locally too docker run -p 10001:80 nha/my-nginx-img locally -> localhost:10001 will return a static page.
It turns out my images was misconfigured - thanks a lot for the help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.