- 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:

- 工業(yè)機器人虛擬仿真實例教程:KUKA.Sim Pro(全彩版)
- 電氣自動化專業(yè)英語(第3版)
- 基于LabWindows/CVI的虛擬儀器設計與應用
- 程序設計語言與編譯
- Visual C# 2008開發(fā)技術詳解
- 數(shù)據(jù)庫原理與應用技術
- 完全掌握AutoCAD 2008中文版:綜合篇
- Windows游戲程序設計基礎
- 運動控制系統(tǒng)應用與實踐
- Extending Ansible
- INSTANT Puppet 3 Starter
- HBase Essentials
- Drupal高手建站技術手冊
- Spark Streaming實時流式大數(shù)據(jù)處理實戰(zhàn)
- Intel Edison Projects