Update tutorial to work with Katacoda, No Install (#10797)

* Update tutorial to work with Katacoda, No Install

* Update hello-minikube.md
This commit is contained in:
Cody Clark
2018-11-05 11:31:11 -08:00
committed by k8s-ci-robot
parent 18343eb44b
commit a0b97a3e5d
+79 -248
View File
@@ -12,47 +12,33 @@ menu:
{{% capture overview %}} {{% capture overview %}}
The goal of this tutorial is for you to turn a simple Hello World Node.js app This tutorial shows you how to run a simple Hello World Node.js app
into an application running on Kubernetes. The tutorial shows you how to on Kubernetes using [Minikube](/docs/getting-started-guides/minikube) and Katacoda.
take code that you have developed on your machine, turn it into a Docker Katacoda provides a free, in-browser Kubernetes environment.
container image and then run that image on [Minikube](/docs/getting-started-guides/minikube).
Minikube provides a simple way of running Kubernetes on your local machine for free. {{< note >}}
**Note:** You can also follow this tutorial if you've installed Minikube locally.
{{< /note >}}
{{% /capture %}} {{% /capture %}}
{{% capture objectives %}} {{% capture objectives %}}
* Run a hello world Node.js application. * Deploy a hello world application to Minikube.
* Deploy the application to Minikube. * Run the app.
* View application logs. * View application logs.
* Update the application image.
{{% /capture %}} {{% /capture %}}
{{% capture prerequisites %}} {{% capture prerequisites %}}
* For macOS, you can use [Homebrew](https://brew.sh) to install Minikube. This tutorial provides a container image built from the following files:
{{< note >}} {{< codenew language="js" file="minikube/server.js" >}}
**Note:** If you see the following Homebrew error when you run `brew update` after you update your computer to macOS 10.13:
```shell {{< codenew language="conf" file="minikube/Dockerfile" >}}
Error: /usr/local is not writable. You should change the ownership
and permissions of /usr/local back to your user account:
sudo chown -R $(whoami) /usr/local
```
You can resolve the issue by reinstalling Homebrew:
```shell
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
```
{{< /note >}}
* [NodeJS](https://nodejs.org/en/) is required to run the sample application.
* Install Docker. On macOS, we recommend
[Docker for Mac](https://docs.docker.com/engine/installation/mac/).
For more information, read the Docker documentation for [docker build](https://docs.docker.com/engine/reference/commandline/build/)
{{% /capture %}} {{% /capture %}}
@@ -60,150 +46,26 @@ Minikube provides a simple way of running Kubernetes on your local machine for f
## Create a Minikube cluster ## Create a Minikube cluster
This tutorial uses [Minikube](https://github.com/kubernetes/minikube) to 1. Click **Launch Terminal**
create a local cluster. This tutorial also assumes you are using
[Docker for Mac](https://docs.docker.com/engine/installation/mac/)
on macOS. If you are on a different platform like Linux, or using VirtualBox
instead of Docker for Mac, the instructions to install Minikube may be
slightly different. For general Minikube installation instructions, see
the [Minikube installation guide](/docs/getting-started-guides/minikube/).
Use Homebrew to install the latest Minikube release: {{< kat-button >}}
{{< note >}}
**Note:** If you installed Minikube locally, run
```shell ```shell
brew cask install minikube minikube start
``` ```
{{< /note >}}
Install the HyperKit driver, as described by the 2. Open the Kubernetes dashboard in a browser:
[Minikube driver installation guide](https://github.com/kubernetes/minikube/blob/master/docs/drivers.md#hyperkit-driver).
Use Homebrew to download the `kubectl` command-line tool, which you can
use to interact with Kubernetes clusters:
```shell
brew install kubernetes-cli
```
Determine whether you can access sites like [https://cloud.google.com/container-registry/](https://cloud.google.com/container-registry/) directly without a proxy, by opening a new terminal and using
```shell
curl --proxy "" https://cloud.google.com/container-registry/
```
Make sure that the Docker daemon is started. You can determine if docker is running by using a command such as:
```shell
docker images
```
If NO proxy is required, start the Minikube cluster:
```shell
minikube start --vm-driver=hyperkit
```
If a proxy server is required, use the following method to start Minikube cluster with proxy setting:
```shell
minikube start --vm-driver=hyperkit --docker-env HTTP_PROXY=http://your-http-proxy-host:your-http-proxy-port --docker-env HTTPS_PROXY=http(s)://your-https-proxy-host:your-https-proxy-port
```
The `--vm-driver=hyperkit` flag specifies that you are using Docker for Mac. The
default VM driver is VirtualBox.
Now set the Minikube context. The context is what determines which cluster
`kubectl` is interacting with. You can see all your available contexts in the
`~/.kube/config` file.
```shell
kubectl config use-context minikube
```
Verify that `kubectl` is configured to communicate with your cluster:
```shell
kubectl cluster-info
```
Open the Kubernetes dashboard in a browser:
```shell ```shell
minikube dashboard minikube dashboard
``` ```
## Create your Node.js application 3. Katacoda environment only: At the top of the terminal pane, click the plus sign, and then click **Select port to view on Host 1**.
The next step is to write the application. Save this code in a folder named `hellonode` 4. Katacoda environment only: Type 30000, and then click **Display Port**.
with the filename `server.js`:
{{< codenew language="js" file="minikube/server.js" >}}
Run your application:
```shell
node server.js
```
You should be able to see your "Hello World!" message at http://localhost:8080/.
Stop the running Node.js server by pressing **Ctrl-C**.
The next step is to package your application in a Docker container.
## Create a Docker container image
Create a file, also in the `hellonode` folder, named `Dockerfile`. A Dockerfile describes
the image that you want to build. You can build a Docker container image by extending an
existing image. The image in this tutorial extends an existing Node.js image.
{{< codenew language="conf" file="minikube/Dockerfile" >}}
This recipe for the Docker image starts from the official Node.js LTS image
found in the Docker registry, exposes port 8080, copies your `server.js` file
to the image and starts the Node.js server.
By default, Docker will create images and store them in your local machine's Docker registry.
In this tutorial, we will not use your local machine's Docker registry; we will use the
Docker registry of the Docker daemon running _inside_ Minikube's vm instance. To point the
'docker' command to your Minikube's Docker daemon, type (unix shells):
```shell
eval $(minikube docker-env)
```
or in powershell:
```shell
minikube docker-env | Invoke-Expression
```
{{< note >}}
**Note:** Later, when you no longer wish to use the Minikube host, you can undo
this change by running `eval $(minikube docker-env -u)`.
{{< /note >}}
Build your Docker image, using the Minikube Docker daemon (mind the trailing dot):
```shell
docker build -t hello-node:v1 .
```
check that the image is in Minikube's Docker registry:
```shell
minikube ssh docker images
```
Output:
```shell
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-node v1 f82485ca953c 3 minutes ago 655MB
...
node 6.9.2 faaadb4aaf9b 20 months ago 655MB
```
Now the Minikube VM can run the image you built.
## Create a Deployment ## Create a Deployment
@@ -214,17 +76,14 @@ tutorial has only one Container. A Kubernetes
Pod and restarts the Pod's Container if it terminates. Deployments are the Pod and restarts the Pod's Container if it terminates. Deployments are the
recommended way to manage the creation and scaling of Pods. recommended way to manage the creation and scaling of Pods.
Use the `kubectl run` command to create a Deployment that manages a Pod. The 1. Use the `kubectl run` command to create a Deployment that manages a Pod. The
Pod runs a Container based on your `hello-node:v1` Docker image. Set the Pod runs a Container based on the provided Docker image.
`--image-pull-policy` flag to `Never` to always use the local image, rather than
pulling it from your Docker registry (since you haven't pushed it there):
```shell ```shell
kubectl run hello-node --image=hello-node:v1 --port=8080 --image-pull-policy=Never kubectl run hello-node --image=gcr.io/hello-minikube-zero-install/hello-node --port=8080
``` ```
View the Deployment: 2. View the Deployment:
```shell ```shell
kubectl get deployments kubectl get deployments
@@ -232,41 +91,39 @@ kubectl get deployments
Output: Output:
```shell ```shell
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-node 1 1 1 1 3m hello-node 1 1 1 1 1m
``` ```
View the Pod: 3. View the Pod:
```shell ```shell
kubectl get pods kubectl get pods
``` ```
Output: Output:
```shell ```shell
NAME READY STATUS RESTARTS AGE NAME READY STATUS RESTARTS AGE
hello-node-714049816-ztzrb 1/1 Running 0 6m hello-node-5f76cf6ccf-br9b5 1/1 Running 0 1m
``` ```
View cluster events: 4. View cluster events:
```shell ```shell
kubectl get events kubectl get events
``` ```
View the `kubectl` configuration: 5. View the `kubectl` configuration:
```shell ```shell
kubectl config view kubectl config view
``` ```
For more information about `kubectl`commands, see the {{< note >}}
**Note:** For more information about `kubectl`commands, see the
[kubectl overview](/docs/user-guide/kubectl-overview/). [kubectl overview](/docs/user-guide/kubectl-overview/).
{{< /note >}}
## Create a Service ## Create a Service
@@ -275,14 +132,13 @@ Kubernetes cluster. To make the `hello-node` Container accessible from outside t
Kubernetes virtual network, you have to expose the Pod as a Kubernetes virtual network, you have to expose the Pod as a
Kubernetes [*Service*](/docs/concepts/services-networking/service/). Kubernetes [*Service*](/docs/concepts/services-networking/service/).
From your development machine, you can expose the Pod to the public internet 1. Expose the Pod to the public internet using the `kubectl expose` command:
using the `kubectl expose` command:
```shell ```shell
kubectl expose deployment hello-node --type=LoadBalancer kubectl expose deployment hello-node --type=LoadBalancer
``` ```
View the Service you just created: 2. View the Service you just created:
```shell ```shell
kubectl get services kubectl get services
@@ -292,8 +148,8 @@ Output:
```shell ```shell
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-node ClusterIP 10.0.0.71 <pending> 8080/TCP 6m hello-node LoadBalancer 10.108.144.78 <pending> 8080:30369/TCP 21s
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 14d kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23m
``` ```
The `--type=LoadBalancer` flag indicates that you want to expose your Service The `--type=LoadBalancer` flag indicates that you want to expose your Service
@@ -302,52 +158,23 @@ an external IP address would be provisioned to access the Service. On Minikube,
the `LoadBalancer` type makes the Service accessible through the `minikube service` the `LoadBalancer` type makes the Service accessible through the `minikube service`
command. command.
```shell 3. Run the following command:
minikube service hello-node
```
This automatically opens up a browser window using a local IP address that
serves your app and shows the "Hello World" message.
Assuming you've sent requests to your new web service using the browser or curl,
you should now be able to see some logs:
```shell
kubectl logs <POD-NAME>
```
## Update your app
Edit your `server.js` file to return a new message:
```javascript
response.end('Hello World Again!');
```
Build a new version of your image (mind the trailing dot):
```shell
docker build -t hello-node:v2 .
```
Update the image of your Deployment:
```shell
kubectl set image deployment/hello-node hello-node=hello-node:v2
```
Run your app again to view the new message:
```shell ```shell
minikube service hello-node minikube service hello-node
``` ```
4. Katacoda environment only: Click the plus sign, then click **Select port to view on Host 1**.
5. Katacoda environment only: Type in the Port number following `8080:`, and then click **Display Port**.
This opens up a browser window that serves your app and shows the "Hello World" message.
## Enable addons ## Enable addons
Minikube has a set of built-in addons that can be enabled, disabled and opened in the local Kubernetes environment. Minikube has a set of built-in addons that can be enabled, disabled and opened in the local Kubernetes environment.
First list the currently supported addons: 1. List the currently supported addons:
```shell ```shell
minikube addons list minikube addons list
@@ -356,20 +183,28 @@ minikube addons list
Output: Output:
```shell ```shell
- storage-provisioner: enabled - addon-manager: enabled
- coredns: disabled
- dashboard: enabled
- default-storageclass: enabled
- efk: disabled
- freshpod: disabled
- heapster: disabled
- ingress: disabled
- kube-dns: enabled - kube-dns: enabled
- metrics-server: disabled
- nvidia-driver-installer: disabled
- nvidia-gpu-device-plugin: disabled
- registry: disabled - registry: disabled
- registry-creds: disabled - registry-creds: disabled
- addon-manager: enabled - storage-provisioner: enabled
- dashboard: disabled
- default-storageclass: enabled
- coredns: disabled
- heapster: disabled
- efk: disabled
- ingress: disabled
``` ```
{{< note >}}
**Note:**
Minikube must be running for these commands to take effect.
{{< /note >}}
Minikube must be running for these commands to take effect. To enable `heapster` addon, for example: 2. Enable an addon, for example, `heapster`:
```shell ```shell
minikube addons enable heapster minikube addons enable heapster
@@ -381,7 +216,7 @@ Output:
heapster was successfully enabled heapster was successfully enabled
``` ```
View the Pod and Service you just created: 3. View the Pod and Service you just created:
```shell ```shell
kubectl get po,svc -n kube-system kubectl get po,svc -n kube-system
@@ -391,25 +226,31 @@ Output:
```shell ```shell
NAME READY STATUS RESTARTS AGE NAME READY STATUS RESTARTS AGE
pod/heapster-zbwzv 1/1 Running 0 2m pod/heapster-9jttx 1/1 Running 0 26s
pod/influxdb-grafana-gtht9 2/2 Running 0 2m pod/influxdb-grafana-b29w8 2/2 Running 0 26s
pod/kube-addon-manager-minikube 1/1 Running 0 34m
pod/kube-dns-6dcb57bcc8-gv7mw 3/3 Running 0 34m
pod/kubernetes-dashboard-5498ccf677-cgspw 1/1 Running 0 34m
pod/storage-provisioner 1/1 Running 0 34m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/heapster NodePort 10.0.0.52 <none> 80:31655/TCP 2m service/heapster ClusterIP 10.96.241.45 <none> 80/TCP 26s
service/monitoring-grafana NodePort 10.0.0.33 <none> 80:30002/TCP 2m service/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP 34m
service/monitoring-influxdb ClusterIP 10.0.0.43 <none> 8083/TCP,8086/TCP 2m service/kubernetes-dashboard NodePort 10.109.29.1 <none> 80:30000/TCP 34m
service/monitoring-grafana NodePort 10.99.24.54 <none> 80:30002/TCP 26s
service/monitoring-influxdb ClusterIP 10.111.169.94 <none> 8083/TCP,8086/TCP 26s
``` ```
Open the endpoint to interacting with heapster in a browser: 4. Disable `heapster`:
```shell ```shell
minikube addons open heapster minikube addons disable heapster
``` ```
Output: Output:
```shell ```shell
Opening kubernetes service kube-system/monitoring-grafana in default browser... heapster was successfully disabled
``` ```
## Clean up ## Clean up
@@ -421,17 +262,10 @@ kubectl delete service hello-node
kubectl delete deployment hello-node kubectl delete deployment hello-node
``` ```
Optionally, force removal of the Docker images created:
```shell
docker rmi hello-node:v1 hello-node:v2 -f
```
Optionally, stop the Minikube VM: Optionally, stop the Minikube VM:
```shell ```shell
minikube stop minikube stop
eval $(minikube docker-env -u)
``` ```
Optionally, delete the Minikube VM: Optionally, delete the Minikube VM:
@@ -442,7 +276,6 @@ minikube delete
{{% /capture %}} {{% /capture %}}
{{% capture whatsnext %}} {{% capture whatsnext %}}
* Learn more about [Deployment objects](/docs/concepts/workloads/controllers/deployment/). * Learn more about [Deployment objects](/docs/concepts/workloads/controllers/deployment/).
@@ -450,5 +283,3 @@ minikube delete
* Learn more about [Service objects](/docs/concepts/services-networking/service/). * Learn more about [Service objects](/docs/concepts/services-networking/service/).
{{% /capture %}} {{% /capture %}}