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

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
主站蜘蛛池模板: 牙克石市| 兰考县| 吉安市| 锡林郭勒盟| 昂仁县| 汾阳市| 新乡县| 海口市| 仁怀市| 安陆市| 广平县| 秦皇岛市| 绥芬河市| 广宗县| 琼海市| 赤峰市| 公主岭市| 美姑县| 宜兴市| 砚山县| 壶关县| 广安市| 满城县| 海兴县| 文化| 吕梁市| 安国市| 土默特左旗| 高台县| 桐庐县| 霸州市| 日喀则市| 永安市| 称多县| 富顺县| 和林格尔县| 大城县| 巴塘县| 调兵山市| 泸水县| 玉屏|