Exposing Nginx container and View the Service

3/28/2020

1.) Execute the following command to generate a random number which is used in the later steps

NUMBER=$[ ( $RANDOM % 1000 ) + 1 ]

echo $NUMBER

Note: Replace the sentence your random number with the number that you have generated wherever you have found the sentence.

Your task is to start a Kubernetes Engine managed by Kubernetes Cluster with the name mycluster-your random number and configure it to run 2 nodes.

2.) Run and Deploy a Container Here, you need to launch a single instance of the Nginx container (with version 1.10.0) from the cloud shell.

Execute the following command to view the pod that is running in the nginx container.**

3.) First, you need to expose the Nginx container to the internet.

Kubernetes will create a service with an external load balancer with a public IP address. You can view your service by executing the following command.

kubectl get services

Now, you will get the external IP address of the Nginx cluster. Open the new web browser tab and paste the Cluster External IP address. You should get the default home page of the Nginx browser.

I have used the below code so far, but the lb is not working:

gcloud container clusters create mycluster-5 --zone=us-central1-a
kubectl create deployment mycluster --image=gcr.io/cloud-marketplace/google/nginx1
kubectl set image deployment nginx nginx=nginx:1.9.1
kubectl expose deployment mycluster-727 --type LoadBalancer --port 80 --target-port 8080
service/mycluster-727 exposed
-- sassofgeeks
docker
google-cloud-platform
kubernetes
nginx

2 Answers

3/30/2020

Welcome to Stack Overflow!

The commands you've posted are not working because you have a typo and the containers ports don't match.

Problem explanation:

Here you are creating a new deployment named mycluster but you are not exposing any port. kubectl create deployment mycluster --image=gcr.io/cloud-marketplace/google/nginx1

Here you are exposing a deployment named mycluster-727 on port 80 and to target port 8080:

kubectl expose deployment mycluster-727 --type LoadBalancer --port 80 --target-port 8080

Here you are setting image on differents deployment nginx and with a different version that was asked 1.10.0: kubectl set image deployment nginx nginx=nginx:1.9.1

Fixing the problem

I've checked, and the images gcr.io/cloud-marketplace/google/nginx1 and nginx:1.10.0 and both of them use port 80 to expose the application, so instead use --targer-port=8080 you need use port 80, but you also need to expose the container por when creating the deployment.

Based on @nischay goayl answer, the following command will create a deployment and expose on port 80: kubectl run mycluster --image=nginx:1.10.0 --port=80

Then, create a service exposing the application: kubectl expose deployment mycluster --type LoadBalancer --port 80 --target-port 80

Wait for EXTERANL-IP and try to reach your application.

If you want to test internally, use a test pod with curl image to reach the service:

apiVersion: v1
kind: Pod
metadata:
  name: curl
  namespace: default
spec:
  containers:
  - name: curl
    image: curlimages/curl
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
  restartPolicy: Always

And then use the command:

kubectl exec -it curl -- curl -IL http://mycluster

response:

HTTP/1.1 200 OK
Server: nginx/1.10.0
Date: Mon, 30 Mar 2020 09:30:07 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 26 Apr 2016 15:17:57 GMT
Connection: keep-alive
ETag: "571f86a5-264"
Accept-Ranges: bytes
-- KoopaKiller
Source: StackOverflow

3/29/2020

The reason it's not working because the port is not exposed by the Pod. Please run the below command instead of the second command.

kubectl run mycluster --image=gcr.io/cloud-marketplace/google/nginx1 --port=80

This command should create the deployment and exposed the containerPort on 80 as well which your service would be able to hit.

-- nischay goyal
Source: StackOverflow