k8s之minikube搭建

weixin_33912445發表於2018-01-23

Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.

本文主要介紹在mac中安裝minikube,使用minikube啟動k8s

安裝 Minikube

  1. 首先保證你的電腦BIOS開啟了VT-x或者AMD-v虛擬化支援
  2. 安裝Virtual Box
> brew cask install virtualbox
  1. 安裝kubectl
> brew install kubectl
> kubectl version                                                                                                            
Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.1", GitCommit:"3a1c9449a956b6026f075fa3134ff92f7d55f812", GitTreeState:"clean", BuildDate:"2018-01-04T19:58:48Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.0", GitCommit:"0b9efaeb34a2fc51ff8e4d34ad9bc6375459c4a4", GitTreeState:"clean", BuildDate:"2017-11-29T22:43:34Z", GoVersion:"go1.9.1", Compiler:"gc", Platform:"linux/amd64"}
#啟用autocompletion,對於zsh或者使用了oh-my-zsh
> source <(kubectl completion zsh)
  1. 安裝minikube
    下載minikube,在這裡使用的是最新發布版本0.24.1,下載好之後,加入到path下
> mv minikube-darwin-amd64 /usr/local/bin/minikube
> chmod +x /usr/local/bin/minikube
> minikube version                                                                                                          
minikube version: v0.24.1

5.啟動
5.1
minikube start
如果正常的話,到這裡已經安裝成功了,可惜。。
5.2 出現的第一個錯誤,是提示minikube-v0.23.6.iso和localkube-v1.8.0這個檔案無法下載,猜測是被牆的原因,這裡的解決方式是

> wget https://storage.googleapis.com/minikube/iso/minikube-v0.23.6.iso
> mv ~/Downloads/minikube-v0.23.6.iso ~/.minikube/cache/iso
> wget https://storage.googleapis.com/minikube/k8sReleases/v1.8.0/localkube-linux-amd64
>  mv ~/Downloads/localkube-linux-amd64 .minikube/cache/localkube/localkube-v1.8.0

再重新執行啟動沒有報錯了,輸出如下

> minikube start
Starting local Kubernetes v1.8.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.

5.3 執行hello-minikube

> kubectl run hello-minikube --image=k8s.gcr.io/echoserver:1.4 --port=8080
deployment "hello-minikube" created
> kubectl get pod                                                                                                            
NAME                              READY     STATUS              RESTARTS   AGE
hello-minikube-7844bdb9c6-zwcnr   0/1       ContainerCreating   0          0s

貌似看起來一切正常,可惜等了10幾分鐘狀態依然是ContainerCreating,看了下日誌

> minikube logs

這裡截圖沒有了。。,大概的意思就是無法從https://gcr.io/v2/下載image,肯定也是牆的問題,我目前的mac環境使用的shadowsocks翻的但是終端沒法直接使用它,需要配置一下,另外由於ss採用的socks5協議,所以還需要轉換下,所以先安裝了privoxy,並配置了下,我本機開的代理埠在1080,ip地址不要用127.0.0.1或者localhost,否則minikube起來後還是無法使用,具體配置如下

> brew install privoxy
#修改的配置
forward-socks5t   /               127.0.0.1:1080 .
listen-address  192.168.94.1:8118
#啟動
> privoxy /usr/local/etc/privoxy/config

啟動好之後先校驗下,使用curl加代理訪問之前的網址測試下

> curl -x http://192.168.94.1:8118 "https://gcr.io/v2/" -v

好了之後,更改啟動命令,配置minikube使用代理,NO_PROXY的作用是對於192.168.99.0/24該網段的地址不要走代理,原因是minikube內部啟動的容器使用該網段,都代理就不會通了

minikube start --docker-env HTTP_PROXY=http://192.168.94.1:8118 --docker-env HTTPS_PROXY=http://192.168.94.1:8118 --docker-env NO_PROXY=192.168.99.0/24

好,一切就緒之後,再來看看pod的狀態

> kubectl get pod
NAME                              READY     STATUS    RESTARTS   AGE
hello-minikube-7844bdb9c6-zwcnr   1/1       Running   0          1h

狀態已經正常
5.4 暴露埠
可以發現hello-minikube服務已經暴露

> kubectl expose deployment hello-minikube --type=NodePort
> minikube service hello-minikube --url
http://192.168.99.100:31158

5.5 測試

> curl "http://192.168.99.100:31158"                                                                                          
CLIENT VALUES:
client_address=172.17.0.1
command=GET
real path=/
query=nil
request_version=1.1
request_uri=http://192.168.99.100:8080/

SERVER VALUES:
server_version=nginx: 1.10.0 - lua: 10001

HEADERS RECEIVED:
accept=*/*
host=192.168.99.100:31158
user-agent=curl/7.43.0
BODY:
-no body in request-%

到此ok了

相關文章