在你开始之前
本教程提供了从以下文件构建的容器图像:
minikube/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);
minikube/Dockerfile
FROM node:6.14.2EXPOSE 8080COPY server.js .CMD node server.js
有关该docker build请阅读令信息,请阅读Docker文档。
创建一个Minikube集群
启动终端打开浏览器Kubernetes仪表板:minikube start
minikube dashboard创建部署
Kubernetes Pod一组一个或多个容器用于管理和联网。Pod只有一个Container。Kubernetes 部署会检查Pod并在Pod终止时重新启动Pod容器。部署是管理Pod推荐创建和扩展的方法。
使用该kubectl create命令创建管理Pod的Deployment。Pod根据提供的Docker镜像运行Container。kubectl create deployment hello-node --image=gcr.io/hello-minikube-zero-install/hello-node查看部署:
kubectl get deployments输出:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEhello-node 1 1 1 1 1m查看Pod:
kubectl get pods输出:
NAME READY STATUS RESTARTS AGEhello-node-5f76cf6ccf-br9b5 1/1 Running 0 1m查看群集事件:
kubectl get events查看kubectl配置:
kubectl config view
创建服务注意:有关kubectl更多命令信息,请参考kubectl概述。
默认情况下,Pod只能通过Kubernetes集群内部IP访问地址hello-nodeKubernetes外部访问虚拟网络Container,您必须将Pod公开为Kubernetes 服务。
使用以下kubectl expose命令将Pod公开到公共Internet :kubectl expose deployment hello-node --type=LoadBalancer --port=8080该--type=LoadBalancer标志表示您将在集群外公开您的服务。查看刚刚创建的服务:
kubectl get services输出:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEhello-node LoadBalancer 10.108.144.78 <pending> 8080:30369/TCP 21skubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23m在支持负载平衡器的云提供商上,将配置外部IP访问服务地址Minikube上,LoadBalancer服务可以通过类型minikube service 命令访问。
minikube service hello-node启用插件
Minikube本地有一组内置插件Kubernetes在环境中使用、禁用和打开。
列出当前支持的插件:minikube addons list
输出:
addon-manager: enabledcoredns: disableddashboard: enableddefault-storageclass: enabledefk: disabledfreshpod: disabledheapster: disabledingress: disabledkube-dns: enabledmetrics-server: disablednvidia-driver-installer: disablednvidia-gpu-device-plugin: disabledregistry: disabledregistry-creds: disabledstorage-provisioner: enabled例如,启用插件heapster:
minikube addons enable heapster
输出:
heapster was successfully enabled查看刚刚创建的Pod和服务:
kubectl get pod,svc -n kube-system
输出:
NAME READY STATUS RESTARTS AGEpod/heapster-9jttx 1/1 Running 0 26spod/influxdb-grafana-b29w8 2/2 Running 0 26spod/kube-addon-manager-minikube 1/1 Running 0 34mpod/kube-dns-6dcb57bcc8-gv7mw 3/3 Running 0 34mpod/kubernetes-dashboard-5498ccf677-cgspw 1/1 Running 0 34mpod/storage-provisioner 1/1 Running 0 34mNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/heapster ClusterIP 10.96.241.45 <none> 80/TCP 26sservice/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP 34mservice/kubernetes-dashboard NodePort 10.109.29.1 <none> 80:30000/TCP 34mservice/monitoring-grafana NodePort 10.99.24.54 <none> 80:30002/TCP 26sservice/monitoring-influxdb ClusterIP 10.111.169.94 <none> 8083/TCP,8086/TCP 26s禁用heapster:
minikube addons disable heapster
输出:
heapster was successfully disabled清理
现在,您可以清理集群创建的资源:
kubectl delete service hello-nodekubectl delete deployment hello-node
(可选)停止Minikube虚拟机(VM):
minikube stop
(可选)删除Minikube VM:
minikube delete