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

  • 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
主站蜘蛛池模板: 焉耆| 分宜县| 若羌县| 湖南省| 商南县| 马龙县| 雅安市| 巴南区| 台安县| 永登县| 晋江市| 托克逊县| 宁南县| 旌德县| 聂荣县| 泰和县| 景泰县| 汉川市| 神池县| 建始县| 和静县| 阿巴嘎旗| 石渠县| 会昌县| 望谟县| 搜索| 三明市| 绵阳市| 尖扎县| 台湾省| 南京市| 周宁县| 河津市| 长海县| 中卫市| 卓尼县| 罗定市| 江门市| 磴口县| 巴楚县| 巫溪县|