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.
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
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/