官术网_书友最值得收藏!

  • Kubernetes on AWS
  • Ed Robinson
  • 572字
  • 2021-06-10 18:41:26

Building and launching a simple application on Minikube

Let's take our first steps to building a simple application on our local minikube cluster and getting it to run.

The first thing we need to do is build a container image for our application. The simplest way to do this is to create a Dockerfile and use the docker build command.

Use your favorite text editor to create a file called Dockerfile with the following content:

Dockerfile 
FROM nginx:alpine 
RUN echo "<h1>Hello World</h1>" > /usr/share/nginx/html/index.html 

To build the application, first ensure your Docker client is pointing to the Docker instance inside the Minikube VM by running:

    eval $(minikube docker-env)

Then use Docker to build the image. In this case, we are tagging the image hello, but you could use any tag you wanted:

    docker build -t hello:v1 .

Kubectl has a run command that we can use to quickly get a pod running on the Kubernetes cluster. In the background, it creates a Kubernetes deployment resource that ensures that a single instance of our hello container runs within a pod (we will learn more about this later):

    kubectl run hello --image=hello:v1 --image-pull-policy=Never \
    --port=80

We are setting --image-pull-policy=Never here to ensure that Kubernetes uses the local image that we just built, rather than the default of pulling the image from a remote repository, such as Docker Hub.

We can check that our container has started correctly with kubectl get:

    $ kubectl get pods
    NAME                     READY     STATUS    RESTARTS   AGE
    hello-2033763697-9g7cm   1/1       Running   0          1m

Our hello world application was simple enough to set up, but we need some way to access it for our experiment to be considered a success. We can use the kubectl expose command to create a service pointing to the pod in the deployment that was just created:

    kubectl expose deployment/hello --port=80 --type="NodePort" \
    --name=hello 

We have set the service type to NodePort in this case so that Kubernetes will expose a random port on the Minikube VM so that we can access our service easily. In Chapter 6, Planning for Production, we will discuss exposing our applications to the outside world in more detail.

When you create a service of the NodePort type, Kubernetes automatically allocates us a port number for the service to be exposed on. In a multi-node cluster, this port will be opened on every node in the cluster. Since we only have a single node, working out how to access the cluster is a little bit simpler.

First, we need to discover the IP address of the Minikube VM. Luckily, there is a simple command we can run to get this information:

    minikube ip
    192.168.99.100

It is more than likely that when the minikube VM started on your machine, it was allocated a different IP address from my own, so make a note of the IP address on your own machine.

Next, in order to discover the port that Kubernetes has exposed our service on, let's use kubectl get on our service:

    $ kubectl get svc/hello
    NAME      CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
    hello     10.0.0.104   <nodes>       80:32286/TCP   26m

You can see, in this case, that Kubernetes has exposed port 80 on our container as port 32286 on our node.

You should now be able to construct a URL that you can visit in your browser to test out the application. In my case, it is http://192.168.99.100:32286:

You should be able to visit your application with your web browser
主站蜘蛛池模板: 额敏县| 策勒县| 高陵县| 金沙县| 沁阳市| 徐汇区| 黔江区| 佳木斯市| 苍梧县| 独山县| 山阴县| 临夏市| 玉溪市| 和田县| 楚雄市| 安乡县| 临猗县| 孟村| 奉节县| 邻水| 卢湾区| 揭东县| 鞍山市| 上饶县| 株洲市| 通化县| 鄱阳县| 吉隆县| 泰兴市| 乡城县| 中江县| 潮州市| 昌宁县| 滨海县| 长武县| 什邡市| 海丰县| 胶州市| 龙泉市| 乌海市| 周至县|