305-998-7702 | 415-800-2922 info@rx-m.com

Practice Drill

Learn how to put the latest open source technology into practice with hands-on training, delivered by industry experts, aligned to your desired business outcomes

Run the following command:

kubectl run --image nginx nginx-drill

Create a NodePort service that allows you to send a curl request to the nginx-drill pod at port 80 through your machine’s IP address.

Practice Drill: Answer

First, run the command to create the pod:


$ kubectl run --generator run-pod/v1 --image nginx nginx-drill

pod/nginx-drill created

$

Then, use kubectl expose with the --type NodePort flag to create a nodePort service imperatively. Make sure to expose the pod since that is the was created by the initial run command:


$ kubectl expose --type NodePort --port 80 pod nginx-drill

service/nginx-drill exposed

$

After exposing the pod, list the services. You will see the nginx-drill NodePort service maps port 80 to a port within the 30000 range:


$ kubectl get svc

NAME              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes        ClusterIP   10.96.0.1                443/TCP        33d
nginx-drill       NodePort    10.101.103.201           80:32402/TCP   10s

$

Finally, try to send a curl request to the nginx pod using your machine IP address:


$ ip a s | head

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:b3:d9:13 brd ff:ff:ff:ff:ff:ff
    inet 192.168.229.134/24 brd 192.168.229.255 scope global ens33
       valid_lft forever preferred_lft forever

$ curl 192.168.229.134:32402

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

$

As another exercise, create a ClusterIP service called other-svc using kubectl create and use a label selector to associate it with the nginx-drill deployment created above.