1.1.概念

  kubernetes(通常稱為k8s)用於自動部署、擴展和管理容器化應用程序的開源系統。它旨在提供“跨主機集群的自動部署、擴展以及運行應用程序容器的平台”。支持一系列容器工具  ,包括Docker等。

 

1.2.特點

  1)可移植:支持公有雲、私有雲、混合雲、多重雲

  2)可擴展:模塊化、插件化、可掛載、可組合

  3)自動化:自動部署、自動重啟、自動復制、自動伸縮/擴展

  4)快速部署應用,快速擴展應用

  5)無縫對接新的應用功能

  6)節約資源、優化硬件資源的使用

 

2.kubernetes中的相關概念

2.1.Cluster

  計算、存儲和網絡資源的集合,Kubernetes利用這些資源運行各種基於容器的應用

2.2Master

  是Cluster的大腦,主要職責是調度,即決定將應用放在哪里運行。Master運行Linux操作系統,可以是物理機或者虛擬機。為了是實現高可用,可以運行多個Master。

2.3Node

  職責是運行容器應用。Node 由 Master 管理,Node 負責監控並匯報容器的狀態,並根據 Master 的要求管理容器的生命周期。Node 運行在 Linux 操作系統,可以是物理機或者是虛擬機。

2.4Pod

  Pod 是 Kubernetes 的最小工作單元。每個 Pod 包含一個或多個容器。Pod 中的容器會作為一個整體被 Master 調度到一個 Node 上運行。

2.5Controller

Kubernetes 通常不會直接創建 Pod,而是通過 Controller 來管理 Pod 的。Controller 中定義了 Pod 的部署特性,比如有幾個副本,在什么樣的 Node 上運行等。為了滿足不同的業務場景,
Kubernetes 提供了多種 Controller,包括 Deployment、ReplicaSet、DaemonSet、StatefuleSet、Job 等, Deployment 是最常用的 Controller,比如前面在線教程中就是通過創建 Deployment 來部署應用的。Deployment 可以管理 Pod 的多個副本,並確保 Pod 按照期望的狀態運行。 ReplicaSet 實現了 Pod 的多副本管理。使用 Deployment 時會自動創建 ReplicaSet,也就是說 Deployment 是通過 ReplicaSet 來管理 Pod 的多個副本,我們通常不需要直接使用
ReplicaSet。 DaemonSet 用於每個 Node 最多只運行一個 Pod 副本的場景。正如其名稱所揭示的,DaemonSet 通常用於運行 daemon。 StatefuleSet 能夠保證 Pod 的每個副本在整個生命周期中名稱是不變的。而其他 Controller 不提供這個功能,當某個 Pod 發生故障需要刪除並重新啟動時,Pod 的名稱會發生變化。
同時 StatefuleSet 會保證副本按照固定的順序啟動、更新或者刪除。 Job 用於運行結束就刪除的應用。而其他 Controller 中的 Pod 通常是長期持續運行。

安裝Minikube

Linux

curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.7.1/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
# 也可以下載最新版,但可能和本文執行環境不一致,會有坑 curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/ minikube -h

由於kubenetes域名背牆(gcr.io),如kubernetes-dashboard服務依賴不能正常使用。

阿里雲提供了minikube修改版

下載golang和docker

cd /etc/yum.repos.d/ && curl -o docker-ce.repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
yum clean all
yum makecache
yum install go docker-ce -y

啟動

minikube start --registry-mirror=https://docker.mirrors.ustc.edu.cn/

 

安裝Kubectl

kubectl即kubernetes的客戶端,通過他可以進行類似docker run等容器管理操作

curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/

kubectl -h

查看版本

[root@yanglin5 ~]# minikube version
minikube version: v1.0.1
[root@yanglin5 ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.2", GitCommit:"66049e3b21efe110454d67df4fa62b08ea79a19b", GitTreeState:"clean", BuildDate:"2019-05-16T16:23:09Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?

 

啟動程序

啟動minikube

minikube start

首次啟動會下載localkube,下載過程可能會失敗,會有如下提示,重試幾次即可

Starting local Kubernetes v1.8.0 cluster...
Starting VM...
Downloading Minikube ISO
 64.70 MB / 140.01 MB [====================>-----------------------]  46.21% 14s
E0105 14:06:03.884826   10434 start.go:150] Error starting host: Error attempting to cache minikube ISO from URL: Error downloading Minikube ISO: failed to download: failed to download to temp file: failed to copy contents: read tcp 10.0.2.15:47048->172.217.24.16:443: read: connection reset by peer.

================================================================================
An error has occurred. Would you like to opt in to sending anonymized crash
information to minikube to help prevent future errors?
To opt out of these messages, run the command:
    minikube config set WantReportErrorPrompt false
================================================================================
Please enter your response [Y/n]:

如果下載成功,但是報了諸如VBoxManage not found這樣的錯誤,如下

Starting local Kubernetes v1.8.0 cluster...
Starting VM...
Downloading Minikube ISO
 140.01 MB / 140.01 MB [============================================] 100.00% 0s
E0105 14:10:00.035369   10474 start.go:150] Error starting host: Error creating host: Error executing step: Running precreate checks.
: VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path.

 Retrying.
E0105 14:10:00.035780   10474 start.go:156] Error starting host:  Error creating host: Error executing step: Running precreate checks.
: VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path
================================================================================
An error has occurred. Would you like to opt in to sending anonymized crash
information to minikube to help prevent future errors?
To opt out of these messages, run the command:
    minikube config set WantReportErrorPrompt false
================================================================================
Please enter your response [Y/n]:

解決辦法是安裝 VirtualBox【對於windows或者mac】 再重新啟動;當然如果你是Linux,也可以執行如下命令啟動minikube,此時就不需要安裝VirtualBox了。

因為minikube默認需要虛擬機來初始化kunernetes環境,但Linux是個例外,可以追加–vm-driver=none參數來使用自己的環境,說明見https://github.com/kubernetes/minikube#quickstart

# linux 下獨有,不依賴虛擬機啟動
sudo minikube start --vm-driver=none

# 如果是Mac or Windows,安裝VirtualBox后再重新start即可
sudo minikube start

如果安裝了虛擬機,或者使用了–vm-driver=none參數,並且下載完畢,會有如下提示運行成功

Starting local Kubernetes v1.8.0 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Downloading localkube binary
 148.25 MB / 148.25 MB [============================================] 100.00% 0s
 0 B / 65 B [----------------------------------------------------------]   0.00%
 65 B / 65 B [======================================================] 100.00% 0sSetting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
===================
WARNING: IT IS RECOMMENDED NOT TO RUN THE NONE DRIVER ON PERSONAL WORKSTATIONS
    The 'none' driver will run an insecure kubernetes apiserver as root that may leave the host vulnerable to CSRF attacks

When using the none driver, the kubectl config and credentials generated will be root owned and will appear in the root home directory.
You will need to move the files to the appropriate location and then set the correct permissions.  An example of this is below:

    sudo mv /root/.kube $HOME/.kube # this will write over any previous configuration
    sudo chown -R $USER $HOME/.kube
    sudo chgrp -R $USER $HOME/.kube

    sudo mv /root/.minikube $HOME/.minikube # this will write over any previous configuration
    sudo chown -R $USER $HOME/.minikube
    sudo chgrp -R $USER $HOME/.minikube

This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
Loading cached images from config file.

啟動一個容器服務