本文的環境如下:
作業系統: Mac OSX EI Caption
Docker:Docker version 18.03.1-ce, build 9ee9f40
Minikube:minikube version: v0.27.0
Kubernetes: v1.10.0
Minikube 介紹
Minikube支援Kubenetes的以下特性:
-
- DNS
- NodePorts
- ConfigMaps and Secrets
- Dashboards
- Container Runtime: Docker, rkt and CRI-O
- Enabling CNI (Container Network Interface)
- Ingress
Minikube 安裝
因為 Google 在國内沒辦法通路,我使用了阿裡版的 Minikube 。
Minikube運作要求安裝有VirtualBox或VMWare Fusion,我用的是VirtualBox。
VirtualBox安裝很簡單,從 官方下載下傳DMG 安裝即可。
下載下傳安裝 Minikube
curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v0.27.0/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
啟動 Minikube
minikube start --registry-mirror=https://registry.docker-cn.com
如果你遇到這個錯誤,Error restarting cluster: restarting kube-proxy: waiting for kube-proxy to be up for configmap update: timed out waiting for the condition
通過 minikube delete,minikube start 可以解決
打開 Minikube 控制台
minikube dashboard
随後浏覽器中會自動打開這個界面。
使用
Minikube在本地虛拟機環境中部署 Kubernetes。
啟動 Cluster
bogon:k8s rousseau$ minikube start
Starting local Kubernetes v1.10.0 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
Loading cached images from config file.
該指令建立并配置一個運作了單個Kubernetes節點的虛拟機。Minikube 啟動過程中,可以用
--extra-config=component.key=value
的形式給 Kubernetes 指定參數。
停止 Cluster
bogon:k8s rousseau$ minikube stop
删除 Cluster
bogon:k8s rousseau$ minikube delete
通路服務
minikube service [-n NAMESPACE] [--url] NAME
網絡
前面的原理圖說明了Minikube啟動了一個虛拟機,這個虛拟機的位址是Host-Only的,要獲得主機IP,執行。
bogon:k8s rousseau$ minikube ip
192.168.99.100
存儲
簡單的示例
建構一個 Node 服務
建構一個簡單的Node網頁程式,使用者通路的時候輸出hello world。
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);
可以在終端中執行
node server.js
并通過http://localhost:8080檢視效果
生成鏡像
編寫 Dockerfile 檔案
FROM node:6.9.2
EXPOSE 8080
COPY server.js .
CMD node server.js
因為我們使用的是 Minikube ,不需要把鏡像放到鏡像倉庫中,隻需要放在Minikube VM中。
bogon:k8s rousseau$ eval(minikube docker-env)
bogon:k8s rousseau$ docker build -t hello-node:v1 .
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM node:6.9.2
6.9.2: Pulling from library/node
75a822cd7888: Pull complete
57de64c72267: Pull complete
4306be1e8943: Pull complete
871436ab7225: Pull complete
0110c26a367a: Pull complete
1f04fe713f1b: Pull complete
ac7c0b5fb553: Pull complete
Digest: sha256:2e95be60faf429d6c97d928c762cb36f1940f4456ce4bd33fbdc34de94a5e043
Status: Downloaded newer image for node:6.9.2
---> faaadb4aaf9b
Step 2/4 : EXPOSE 8080
---> Running in a3229e6d0438
Removing intermediate container a3229e6d0438
---> f8e1aa12de28
Step 3/4 : COPY server.js .
---> 5254ea79c7a4
Step 4/4 : CMD node server.js
---> Running in 060ef7e6f713
Removing intermediate container 060ef7e6f713
---> 2fe0fd2e98d7
Successfully built 2fe0fd2e98d7
Successfully tagged hello-node:v1
部署
使用
kubectl
指令行部署應用。
bogon:k8s rousseau$ kubectl run hello-node --image=hello-node:v1 --port=8080
deployment.apps "hello-node" created
bogon:k8s rousseau$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-node 1 1 1 1 26s
bogon:k8s rousseau$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-node-658d8f6754-7mz28 1/1 Running 0 37s
Pods在Kubernetes中是一組容器的集合,在這個例子中隻有一個容器執行個體,是以看到的各項統計數字都是1。
建立服務
預設情況下,Pod隻能通過内部位址通路,為了讓 hello-node 可以從外部通路,需要将Pod暴露為 Kubernetes 的服務。
bogon:k8s rousseau$ kubectl expose deployment hello-node --type=LoadBalancer
service "hello-node" exposed
bogon:k8s rousseau$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-node LoadBalancer 10.108.143.198 <pending> 8080:32386/TCP 7s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1h
--type=LoadBalancer
參數表示我們希望将我們的服務暴露在外面,在雲的環境中支援負載均衡情況下,隻需要提供負載均衡的位址。在Minikube中,
LoadBanlancer
類型讓服務可以通過
minikube service
來通路
minikube service hello-node
這個指令會自動打開浏覽器視窗通路服務,使用本地IP。
更新應用
假設我們更新了應用,輸出新的一段話。
response.end('Hello World Again!');
生成鏡像
docker build -t hello-node:v2 .
更新部署
kubectl set image deployment/hello-node hello-node=hello-node:v2
檢視更新
minikube service hello-node
銷毀服務
kubectl delete service hello-node
kubectl delete deployment hello-node
參考資料:
1、Minikube - Kubernetes本地實驗環境
2、Hello Minikube
3、Running Kubernetes Locally via Minikube