0
votes

I have deployed and exposed Nginx with the following commands:

sudo kubectl create deployment mynginx1 --image=nginx
sudo kubectl expose deployment mynginx1 --type NodePort --port 8080

I access using http://: or http://172.17.135.42:31788

But I am getting Error 404. Help appreciated.

gtan@master:~$ kubectl get pods -owide -A NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES default mynginx1-f544c49cb-g92w2 1/1 Running 0 3m19s 172.168.10.2 slave1 kube-system coredns-66bff467f8-92r4n 1/1 Running 0 7m56s 172.168.10.2 master kube-system coredns-66bff467f8-gc7tc 1/1 Running 0 7m56s 172.168.10.3 master kube-system etcd-master 1/1 Running 0 8m6s 172.17.82.100 master kube-system kube-apiserver-master 1/1 Running 0 8m6s 172.17.82.100 master kube-system kube-controller-manager-master 1/1 Running 0 8m6s 172.17.82.100 master kube-system kube-flannel-ds-amd64-24pwc 1/1 Running 3 4m58s 172.17.82.110 slave1 kube-system kube-flannel-ds-amd64-q5qwg 1/1 Running 0 5m28s 172.17.82.100 master kube-system kube-proxy-hf59b 1/1 Running 0 4m58s 172.17.82.110 slave1 kube-system kube-proxy-r7pz6 1/1 Running 0 7m56s 172.17.82.100 master kube-system kube-scheduler-master 1/1 Running 0 8m5s 172.17.82.100 master gtan@master:~$

gtan@master:~$ curl -IL http://172.17.82.100:30131 curl: (7) Failed to connect to 172.17.82.100 port 30131: Connection refused

where "172.17.82.100" is the master node ip address.

gtan@master:~$ kubectl get services -o wide -A NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR default kubernetes ClusterIP 10.96.0.1 443/TCP 15m default mynginx1 NodePort 10.102.106.240 80:30131/TCP 10m app=mynginx1 kube-system kube-dns ClusterIP 10.96.0.10 53/UDP,53/TCP,9153/TCP 15m k8s-app=kube-dns

2

2 Answers

0
votes

What is the architecture of your setup? do you have worker node and master node on same machine?

check the nginx pod status with :

kubectl get pods

If the pod is running without issue then hit your worker machine IP with NodePort http:/Workernode_IP:Nodeport

0
votes

The default nginx container port is 80 as you can see here. Just change the container port from 8080 to 80 in your second command:

sudo kubectl expose deployment mynginx1 --type NodePort --port 80

and try to reach the service using the NodePort showed in the output of the command, for example:

$kubectl get svc

NAME       TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
mynginx1   NodePort   10.97.142.170   <none>        80:31591/TCP   8m9s

Altenatively, you can use this yaml spec to configure your pod and service:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - name: http
          containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: NodePort

Testing using curl:

$ curl -IL http://localhost:31591
HTTP/1.1 200 OK
Server: nginx/1.17.10
Date: Tue, 12 May 2020 10:05:04 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 14 Apr 2020 14:19:26 GMT
Connection: keep-alive
ETag: "5e95c66e-264"
Accept-Ranges: bytes

Also, I recommend you to reserve a time to take a look in these documentations pages:

Kuberentes Concepts

Services