Saturday, October 27, 2018

Minikube cheat sheet

This article records the command commands for Minikube from https://kubernetes.io/docs/tutorials/hello-minikube/

1. Start Minikube cluster using "Docker for Mac".

minikube start --vm-driver=hyperkit

2. Set the context which determines which cluster kubectl is interacting with.

kubectl config use-context minikube

3. Verify that kubectl is configured to communicate with your cluster.

kubectl cluster-info

4. Open Kubernetes dashboard

minikube dashboard

5. Create Node.js application inside "server.js".

var http = require('http');

var handleRequest = function(request, response) {
  console.log('Received request for URL: ' + request.url);
  response.writeHead(200);
  response.end('Hello World!');
};
var www = http.createServer(handleRequest);
www.listen(8080);

6. Create Docker container image file based on above application.

Create a file named "Dockerfile":
FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD node server.js
Point the 'docker' command to your Minikube’s Docker daemon:
eval $(minikube docker-env)
Build your Docker image named "hello-node:v1":
docker build -t hello-node:v1 .

7. List the images in Minikube’s Docker registry

minikube ssh docker images

8. Create a Deployment named "hello-node" that manages a Pod based on above Docker (local) image named "hello-node:v1"

kubectl run hello-node --image=hello-node:v1 --port=8080 --image-pull-policy=Never

9. List the Deployment

kubectl get deployments

NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-node   1         1         1            1           1m

10. List the Pod

kubectl get pods

NAME                          READY   STATUS    RESTARTS   AGE
hello-node-57c6b66f9c-7j2k6   1/1     Running   0          1m

11. View cluster events

kubectl get events

LAST SEEN   FIRST SEEN   COUNT   NAME                                              KIND         SUBOBJECT                         TYPE     REASON                    SOURCE                  MESSAGE
22m         22m          1       minikube.15619424ad09a7a1                         Node                                           Normal   Starting                  kube-proxy, minikube    Starting kube-proxy.
2m          2m           1       hello-node.1561953f3a1d817f                       Deployment                                     Normal   ScalingReplicaSet         deployment-controller   Scaled up replica set hello-node-57c6b66f9c to 1
2m          2m           1       hello-node-57c6b66f9c.1561953f3c3d1c9e            ReplicaSet                                     Normal   SuccessfulCreate          replicaset-controller   Created pod: hello-node-57c6b66f9c-7j2k6
2m          2m           1       hello-node-57c6b66f9c-7j2k6.1561953f4f86ec39      Pod                                            Normal   SuccessfulMountVolume     kubelet, minikube       MountVolume.SetUp succeeded for volume "default-token-rs6s2"
2m          2m           1       hello-node-57c6b66f9c-7j2k6.1561953f3e24dbb8      Pod                                            Normal   Scheduled                 default-scheduler       Successfully assigned hello-node-57c6b66f9c-7j2k6 to minikube
2m          2m           1       hello-node-57c6b66f9c-7j2k6.1561953f7764cf06      Pod          spec.containers{hello-node}       Normal   Started                   kubelet, minikube       Started container
2m          2m           1       hello-node-57c6b66f9c-7j2k6.1561953f6c184d6f      Pod          spec.containers{hello-node}       Normal   Created                   kubelet, minikube       Created container
2m          2m           1       hello-node-57c6b66f9c-7j2k6.1561953f6866f7d5      Pod          spec.containers{hello-node}       Normal   Pulled   

12. Expose the Pod to the public internet as a service

kubectl expose deployment hello-node --type=LoadBalancer

13. List the Service

kubectl get services

NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
hello-node   LoadBalancer   10.xxx.xx.xxx   <pending>     8080:32354/TCP   8s
kubernetes   ClusterIP      10.96.0.1       <none>        443/TCP          1d

Open Browser using a local IP address that serves your app:
minikube service hello-node

14. View Pod logs

kubectl logs <POD-NAME>
kubectl logs hello-node-57c6b66f9c-7j2k6

15. Update the server.js

response.end('Hello World Again!');

16. Build a new version of the Docker image named "hello-node:v2"

docker build -t hello-node:v2 .

17. Update the image of your Deployment

kubectl set image deployment/hello-node hello-node=hello-node:v2

18. Run your app again to view the new message

minikube service hello-node

19. List Kubernetes Addons

minikube addons list
To enable heapster addon:
minikube addons enable heapster
View the pod and service created:
kubectl get pod,service -n kube-system
Interacting with heapster in a browser:
minikube addons open heapster

20. Cleanup and Shutdown Cluster

Delete the service:
kubectl delete service hello-node
Delete the deployment:
kubectl delete deployment hello-node
Optionally, force removal of the Docker images created:
docker rmi hello-node:v1 hello-node:v2 -f
Optionally, stop the Minikube VM:
minikube stop
eval $(minikube docker-env -u)
Optionally, delete the Minikube VM:
minikube delete



No comments:

Post a Comment

Popular Posts