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
+150 -319
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
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. {{< codenew language="conf" file="minikube/Dockerfile" >}}
* 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 >}}
```shell
brew cask install minikube
```
Install the HyperKit driver, as described by the {{< note >}}
[Minikube driver installation guide](https://github.com/kubernetes/minikube/blob/master/docs/drivers.md#hyperkit-driver). **Note:** If you installed Minikube locally, run
```shell
minikube start
```
{{< /note >}}
Use Homebrew to download the `kubectl` command-line tool, which you can 2. Open the Kubernetes dashboard in a browser:
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,59 +76,54 @@ 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
kubectl get deployments
```
```shell Output:
kubectl get deployments
```
Output: ```shell
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-node 1 1 1 1 1m
```
3. View the Pod:
```shell ```shell
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE kubectl get pods
hello-node 1 1 1 1 3m ```
``` Output:
View the Pod: ```shell
NAME READY STATUS RESTARTS AGE
hello-node-5f76cf6ccf-br9b5 1/1 Running 0 1m
```
4. View cluster events:
```shell ```shell
kubectl get pods kubectl get events
``` ```
Output: 5. View the `kubectl` configuration:
```shell
```shell kubectl config view
NAME READY STATUS RESTARTS AGE ```
hello-node-714049816-ztzrb 1/1 Running 0 6m
``` {{< note >}}
**Note:** For more information about `kubectl`commands, see the
View cluster events: [kubectl overview](/docs/user-guide/kubectl-overview/).
{{< /note >}}
```shell
kubectl get events
```
View the `kubectl` configuration:
```shell
kubectl config view
```
For more information about `kubectl`commands, see the
[kubectl overview](/docs/user-guide/kubectl-overview/).
## Create a Service ## Create a Service
@@ -275,142 +132,126 @@ 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
``` ```
Output: 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
outside of the cluster. On cloud providers that support load balancers, outside of the cluster. On cloud providers that support load balancers,
an external IP address would be provisioned to access the Service. On Minikube, 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 ```shell
serves your app and shows the "Hello World" message. minikube service hello-node
```
Assuming you've sent requests to your new web service using the browser or curl, 4. Katacoda environment only: Click the plus sign, then click **Select port to view on Host 1**.
you should now be able to see some logs:
```shell 5. Katacoda environment only: Type in the Port number following `8080:`, and then click **Display Port**.
kubectl logs <POD-NAME>
```
## Update your app This opens up a browser window that serves your app and shows the "Hello World" message.
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
minikube service hello-node
```
## 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
``` ```
Output: Output:
```shell ```shell
- storage-provisioner: enabled - addon-manager: enabled
- kube-dns: enabled - coredns: disabled
- registry: disabled - dashboard: enabled
- registry-creds: disabled - default-storageclass: enabled
- addon-manager: enabled - efk: disabled
- dashboard: disabled - freshpod: disabled
- default-storageclass: enabled - heapster: disabled
- coredns: disabled - ingress: disabled
- heapster: disabled - kube-dns: enabled
- efk: disabled - metrics-server: disabled
- ingress: disabled - nvidia-driver-installer: disabled
``` - nvidia-gpu-device-plugin: disabled
- registry: disabled
- registry-creds: disabled
- storage-provisioner: enabled
```
{{< 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
``` ```
Output:
Output: ```shell
heapster was successfully enabled
```
```shell 3. View the Pod and Service you just created:
heapster was successfully enabled
```
View the Pod and Service you just created: ```shell
kubectl get po,svc -n kube-system
```
```shell Output:
kubectl get po,svc -n kube-system
```
Output: ```shell
NAME READY STATUS RESTARTS AGE
pod/heapster-9jttx 1/1 Running 0 26s
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
```shell NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
NAME READY STATUS RESTARTS AGE service/heapster ClusterIP 10.96.241.45 <none> 80/TCP 26s
pod/heapster-zbwzv 1/1 Running 0 2m service/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP 34m
pod/influxdb-grafana-gtht9 2/2 Running 0 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
```
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE 4. Disable `heapster`:
service/heapster NodePort 10.0.0.52 <none> 80:31655/TCP 2m
service/monitoring-grafana NodePort 10.0.0.33 <none> 80:30002/TCP 2m
service/monitoring-influxdb ClusterIP 10.0.0.43 <none> 8083/TCP,8086/TCP 2m
```
Open the endpoint to interacting with heapster in a browser: ```shell
minikube addons disable heapster
```
Output:
```shell ```shell
minikube addons open heapster heapster was successfully disabled
``` ```
Output:
```shell
Opening kubernetes service kube-system/monitoring-grafana in default browser...
```
## 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 %}}