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

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
主站蜘蛛池模板: 聂拉木县| 六枝特区| 佳木斯市| 文安县| 白玉县| 革吉县| 珲春市| 汽车| 南丹县| 鹤岗市| 类乌齐县| 九江市| 颍上县| 贵定县| 惠水县| 泊头市| 长葛市| 桦川县| 宁海县| 福建省| 松滋市| 望城县| 澄城县| 恩施市| 阜康市| 桓台县| 西乡县| 万盛区| 柯坪县| 大洼县| 卢氏县| 涿州市| 北票市| 山东省| 凭祥市| 锦屏县| 囊谦县| 海南省| 改则县| 陆良县| 从江县|