新手搭建 kubernetes 測試環境

candyleer發表於2019-02-23

minikube

剛剛接觸容器化的東西,很想自己搭建一個kubernetes(k8s)環境用於開發和測試,幫助學習,記錄一下從零學習瞭解k8s的過程(網上的資源是在太多太雜).

準備

新手學習的第一步就是首先去官網和github觀摩一圈,看一下quick start 之類的. 官方教程大概分為了這麼幾類,乍一看還是比較清晰明瞭的.

image.png

我首先開啟了安裝頁面,嘗試獲取一些安裝相關的知識

image.png

根據 tutorials 中的提示,我選擇最快捷的minikube的方式進行單機安裝(主要是真正的安裝需要較多的伺服器資源,而且還有環境問題,對於新手來說不是太友好),minikube 將這些全部封裝,解除了這些後顧之憂.

mac上進行安裝minikube:

brew cask install minikube
複製程式碼

minikube 實際就是本地建立了一個虛擬機器,裡面執行了kubernetes 的一些必要的環境,相當於 k8s 的服務環境,建立 pod,service,deployment... 等等都是在裡面進行建立和管理.

mac 上面安裝 kubectl,客戶端操作工具

brew install kubernetes-cli
複製程式碼

啟動叢集

minikube start
複製程式碼

啟動完成後會分配新的虛擬機器地址,可以通過kubectl cluster-info進行檢視

image.png

配置 proxy 代理

由於官方案例中的 docker 映象訪問不通了,為了學習和測試過程順利,如果方便,還是建議配置相關代理.

需要相關能訪問官方資源代理工具,我這裡用到的ss, 比如我的http代理啟動在http://127.0.0.1:1087,並且確保可以進行 http 的代理(如果沒有,可以使用將一些其他工具將 socks5 協議轉為 http 協議),因為很多預設只有socks5 代理

通過上面可知,虛擬機器的 ip 地址為192.168.99.100,我的宿主機的閘道器和地址為192.168.99.1,將代理工具設定為區域網可以訪問或者0.0.0.0,便於虛擬機器訪問.

查詢 docker 官網的代理配置(docs.docker.com/config/daem…),總結為以下幾個步驟,以下指令碼需要在minikube ssh 進入虛擬機器內部執行(可以先在宿主機寫好後,用 scp 複製到虛擬機器中),大概意思就是為 minikube 裡面的 docker 設定到宿主機的代理,並且重啟:

sudo mkdir /etc/systemd/system/docker.service.d
echo '[Service]
Environment="HTTP_PROXY=http://192.168.99.1:1087"' > /tmp/docker-http-proxy.txt
echo '[Service]
Environment="HTTPS_PROXY=http://192.168.99.1:1087"' > /tmp/docker-https-proxy.txt
sudo cp /tmp/docker-http-proxy.txt /etc/systemd/system/docker.service.d/http-proxy.conf
sudo cp /tmp/docker-https-proxy.txt /etc/systemd/system/docker.service.d/https-proxy.conf
echo "copy successfully!"
sudo systemctl daemon-reload
echo "daemon-reload success!"
sudo systemctl restart docker
echo "docker successfully restarted!"
複製程式碼

執行成功後,docker pull命令的執行就能成功了.

執行結果

 $ kubectl describe pods kubernetes-bootcamp-5c69669756-52pjw
Name:           kubernetes-bootcamp-5c69669756-52pjw
Namespace:      default
Node:           minikube/192.168.99.100
Start Time:     Fri, 28 Dec 2018 10:43:21 +0800
Labels:         pod-template-hash=1725225312
                run=kubernetes-bootcamp
Annotations:    <none>
Status:         Running
IP:             172.17.0.2
Controlled By:  ReplicaSet/kubernetes-bootcamp-5c69669756
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://0a334692877ac3beb07856c381e74cf63290e4784bf053713744dab9653c84a1
    Image:          gcr.io/google-samples/kubernetes-bootcamp:v1
    Image ID:       docker-pullable://gcr.io/google-samples/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 28 Dec 2018 11:08:22 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-kkjv7 (ro)
Conditions:
  Type           Status
  Initialized    True
  Ready          True
  PodScheduled   True
Volumes:
  default-token-kkjv7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-kkjv7
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason                  Age                    From               Message
  ----     ------                  ----                   ----               -------
  Normal   Scheduled               25m                    default-scheduler  Successfully assigned kubernetes-bootcamp-5c69669756-52pjw to minikube
  Normal   SuccessfulMountVolume   25m                    kubelet, minikube  MountVolume.SetUp succeeded for volume "default-token-kkjv7"
  Warning  FailedCreatePodSandBox  19m (x12 over 24m)     kubelet, minikube  Failed create pod sandbox: rpc error: code = Unknown desc = failed pulling image "k8s.gcr.io/pause-amd64:3.1": Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
  Normal   SuccessfulMountVolume   14m                    kubelet, minikube  MountVolume.SetUp succeeded for volume "default-token-kkjv7"
  Warning  FailedCreatePodSandBox  13m (x3 over 14m)      kubelet, minikube  Failed create pod sandbox: rpc error: code = Unknown desc = failed pulling image "k8s.gcr.io/pause-amd64:3.1": Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
  Warning  FailedCreatePodSandBox  8m54s (x22 over 13m)   kubelet, minikube  Failed create pod sandbox: rpc error: code = Unknown desc = failed pulling image "k8s.gcr.io/pause-amd64:3.1": Error response from daemon: Get https://k8s.gcr.io/v2/: proxyconnect tcp: dial tcp 192.168.99.1:1087: getsockopt: connection refused
  Normal   SuccessfulMountVolume   3m53s                  kubelet, minikube  MountVolume.SetUp succeeded for volume "default-token-kkjv7"
  Warning  FailedCreatePodSandBox  3m38s                  kubelet, minikube  Failed create pod sandbox: rpc error: code = Unknown desc = failed pulling image "k8s.gcr.io/pause-amd64:3.1": error during connect: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.31/images/create?fromImage=k8s.gcr.io%2Fpause-amd64&tag=3.1: EOF
  Warning  FailedCreatePodSandBox  2m26s (x5 over 3m23s)  kubelet, minikube  Failed create pod sandbox: rpc error: code = Unknown desc = failed pulling image "k8s.gcr.io/pause-amd64:3.1": Error response from daemon: Get https://k8s.gcr.io/v2/: proxyconnect tcp: dial tcp 192.168.99.1:1087: getsockopt: connection refused
  Normal   Pulling                 2m9s                   kubelet, minikube  pulling image "gcr.io/google-samples/kubernetes-bootcamp:v1"
  Normal   Pulled                  13s                    kubelet, minikube  Successfully pulled image "gcr.io/google-samples/kubernetes-bootcamp:v1"
  Normal   Created                 13s                    kubelet, minikube  Created container
  Normal   Started                 13s                    kubelet, minikube  Started container
複製程式碼

接下來會繼續介紹k8s 相關的概念和元件等.

相關文章