1. 程式人生 > >CentOS7.3利用kubeadm安裝kubernetes1.7.3完整版(官方文件填坑篇)

CentOS7.3利用kubeadm安裝kubernetes1.7.3完整版(官方文件填坑篇)

安裝前記:

近來容器對企業來說已經不是什麼陌生的概念,Kubernetes作為Google開源的容器執行平臺,受到了大家的熱捧。搭建一套完整的kubernetes平臺,也成為試用這套平臺必須邁過的坎兒。kubernetes1.5版本以及之前,安裝還是相對比較方便的,官方就有通過yum源在centos7安裝kubernetes。但是在kubernetes1.6之後,安裝就比較繁瑣了,需要證書各種認證,對於剛接觸kubernetes的人來說很不友好,按照官方文件在本地安裝“叢集”的的話,我覺得你肯定是跑不起來的,除非你突破了GFW的限制,還要懂得怎麼樣不斷修改引數。

令我驚訝的事,漫天都在吹捧kubernetes的強大,居然一篇可以幫助安裝的文件都找不到(你可以說我菜,但是我找的官方和非官方文件不下十幾篇,無一成功)。

我自己搞了一個星期,把kubernetes1.7.3版本(網路元件選用Calico),終於按照官方文件+填坑的方式部署成功,寫此文希望能幫助更多的人部署kubernetes1.7成功。

安裝

安裝準備

  • 作業系統:CentOS7.3
[[email protected]base-ok]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
  • 安裝機器:k8s-1為master節點,k8s-2、k8s-3為slave節點
 [[email protected]]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1
localhost localhost.localdomain localhost6 localhost6.localdomain6 k8s-1 192.168.80.28 k8s-2 192.168.80.35 k8s-3 192.168.80.14

安裝步驟

安裝docker 1.12(所有節點)

注意:現在docker已經更新到CE版本,但是kubernetes官方文件說在1.12上測試通過,最近版本的相容性未測試,為了避免後面出現大坑,我們還是乖乖安裝1.12版本的docker。

1.新建docker.repo檔案,將檔案移動到/etc/yum.repos.d/目錄下

[[email protected]]# cat /etc/yum.repos.d/docker.repo
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg

2.執行yum命令,找到需要安裝的docker版本

10:21 [[email protected]]# yum list|grep docker | sort -r
python2-avocado-plugins-runner-docker.noarch
python-dockerpty.noarch                 0.4.1-6.el7                    epel
python-dockerfile-parse.noarch          0.0.5-1.el7                    epel
python-docker-scripts.noarch            0.4.4-1.el7                    epel
python-docker-pycreds.noarch            1.10.6-1.el7                   extras
python-docker-py.noarch                 1.10.6-1.el7                   extras
kdocker.x86_64                          4.9-1.el7                      epel
golang-github-fsouza-go-dockerclient-devel.x86_64
docker.x86_64                           2:1.12.6-32.git88a4867.el7.centos
docker-v1.10-migrator.x86_64            2:1.12.6-32.git88a4867.el7.centos
docker-unit-test.x86_64                 2:1.12.6-32.git88a4867.el7.centos
docker-registry.x86_64                  0.9.1-7.el7                    extras
docker-registry.noarch                  0.6.8-8.el7                    extras
docker-python.x86_64                    1.4.0-115.el7                  extras
docker-novolume-plugin.x86_64           2:1.12.6-32.git88a4867.el7.centos
docker-lvm-plugin.x86_64                2:1.12.6-32.git88a4867.el7.centos
docker-logrotate.x86_64                 2:1.12.6-32.git88a4867.el7.centos
docker-latest.x86_64                    1.13.1-13.gitb303bf6.el7.centos
docker-latest-v1.10-migrator.x86_64     1.13.1-13.gitb303bf6.el7.centos
docker-latest-logrotate.x86_64          1.13.1-13.gitb303bf6.el7.centos
docker-forward-journald.x86_64          1.10.3-44.el7.centos           extras
docker-engine.x86_64                    17.05.0.ce-1.el7.centos        dockerrepo
docker-engine.x86_64                    1.12.6-1.el7.centos            @dockerrepo
docker-engine-selinux.noarch            17.05.0.ce-1.el7.centos        @dockerrepo
docker-engine-debuginfo.x86_64          17.05.0.ce-1.el7.centos        dockerrepo
docker-distribution.x86_64              2.6.1-1.el7                    extras
docker-devel.x86_64                     1.3.2-4.el7.centos             extras
docker-compose.noarch                   1.9.0-5.el7                    epel
docker-common.x86_64                    2:1.12.6-32.git88a4867.el7.centos
docker-client.x86_64                    2:1.12.6-32.git88a4867.el7.centos
docker-client-latest.x86_64             1.13.1-13.gitb303bf6.el7.centos
cockpit-docker.x86_64                   141-3.el7.centos               extras

3.找到對應版本後,執行yum install -y 包名+版本號,安裝1.12版本的docker-engine

[[email protected]]# yum install -y docker-engine.x86_64-1.12.6-1.el7.centos

4.執行docker version命令,驗證docker安裝版本,執行docker run命令,驗證docker是否安裝成功

 [[email protected]]# docker version
Client:
 Version:      1.12.6
 API version:  1.24
 Go version:   go1.6.4
 Git commit:   78d1802
 Built:        Tue Jan 10 20:20:01 2017
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.6
 API version:  1.24
 Go version:   go1.6.4
 Git commit:   78d1802
 Built:        Tue Jan 10 20:20:01 2017
 OS/Arch:      linux/amd64

5.設定開機啟動,啟動容器,docker安裝完成

[[email protected]base-ok]# systemctl enbale docker && systemctl start docker

安裝kubectl、kubelet、kubeadm(根據需求在不同節點安裝)

注意:此步驟是填坑的開始,因為官方文件的yum源在國內無法使用,安裝完成後注意觀察你的/var/log/message日誌,會瘋狂報錯,彆著急,跟著我一步一步來填坑。

1.新建kubernetes.repo檔案,將檔案移動到/etc/yum.repos.d/目錄下(所有節點)

[[email protected]]# cat /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0

2.通過yum安裝kubectl、kubelet、kubeadm(所有節點)

 [[email protected]]# cat /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0

3.修改kubelet配置,啟動kubelet(所有節點)

注意:時刻檢視/var/log/message的日誌輸出,會看到kubelet一直啟動失敗。

編輯10-kubeadm.conf的檔案,修改cgroup-driver配置:

[[email protected]]# cat /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
[Service]
Environment="KUBELET_KUBECONFIG_ARGS=--kubeconfig=/etc/kubernetes/kubelet.conf --require-kubeconfig=true"
Environment="KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true"
Environment="KUBELET_NETWORK_ARGS=--network-plugin=cni --cni-conf-dir=/etc/cni/net.d --cni-bin-dir=/opt/cni/bin"
Environment="KUBELET_DNS_ARGS=--cluster-dns=10.96.0.10 --cluster-domain=cluster.local"
Environment="KUBELET_AUTHZ_ARGS=--authorization-mode=Webhook --client-ca-file=/etc/kubernetes/pki/ca.crt"
Environment="KUBELET_CADVISOR_ARGS=--cadvisor-port=0"
Environment="KUBELET_CGROUP_ARGS=--cgroup-driver=cgroupfs"
ExecStart=
ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_SYSTEM_PODS_ARGS $KUBELET_NETWORK_ARGS $KUBELET_DNS_ARGS $KUBELET_AUTHZ_ARGS $KUBELET_CADVISOR_ARGS $KUBELET_CGROUP_ARGS $KUBELET_EXTRA_ARGS

將“--cgroup-driver=systems”修改成為“--cgroup-driver=cgroupfs”,重新啟動kubelet。

[[email protected]base-ok]# systemctl restart kubelet

4.下載安裝k8s依賴映象

注意:此步驟非常關鍵,kubenetes初始化啟動會依賴這些映象,天朝的網路肯定是拉不下來google的映象的,一般人過了上一關,這一關未必過的去,一定要提前把映象下載到本地,kubeadm安裝才會繼續,下面我會列出來master節點和node依賴的映象列表。(備註:考慮到隨著kubernetes版本一直更新,映象也可能會有變化,大家可以先執行 kubeadm init 生成配置檔案,日誌輸出到 [apiclient] Created API client, waiting for the control plane to become ready 這一行就會卡住不動了,你可以直接執行 ctrl + c 中止命令執行,然後檢視 ls -ltr /etc/kubernetes/manifests/
yaml檔案列表,每個檔案都會寫著映象的地址和版本)
在這裡我提一個可以解決下載google映象的方法,就是買一臺可以下載的機器,安裝代理軟體,在需要下載google映象的機器的docker設定 HTTP_PROXY 配置項,配置好自己的服務代理即可(也可以直接買可以訪問到google的伺服器安裝).

master節點:

REPOSITORY                                               TAG                 IMAGE ID            CREATED             SIZE
quay.io/calico/kube-policy-controller                    v0.7.0              fe3174230993        3 days ago          21.94 MB
kubernetesdashboarddev/kubernetes-dashboard-amd64        head                e2cadb73b2df        5 days ago          136.5 MB
quay.io/calico/node                                      v2.4.1              7643422fdf0f        6 days ago          277.4 MB
gcr.io/google_containers/kube-controller-manager-amd64   v1.7.3              d014f402b272        11 days ago         138 MB
gcr.io/google_containers/kube-apiserver-amd64            v1.7.3              a1cc3a3d8d0d        11 days ago         186.1 MB
gcr.io/google_containers/kube-scheduler-amd64            v1.7.3              51967bf607d3        11 days ago         77.2 MB
gcr.io/google_containers/kube-proxy-amd64                v1.7.3              54d2a8698e3c        11 days ago         114.7 MB
quay.io/calico/cni                                       v1.10.0             88ca805c8ddd        13 days ago         70.25 MB
gcr.io/google_containers/kubernetes-dashboard-amd64      v1.6.3              691a82db1ecd        2 weeks ago         139 MB
quay.io/coreos/etcd                                      v3.1.10             47bb9dd99916        4 weeks ago         34.56 MB
gcr.io/google_containers/k8s-dns-sidecar-amd64           1.14.4              38bac66034a6        7 weeks ago         41.81 MB
gcr.io/google_containers/k8s-dns-kube-dns-amd64          1.14.4              a8e00546bcf3        7 weeks ago         49.38 MB
gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64     1.14.4              f7f45b9cb733        7 weeks ago         41.41 MB
gcr.io/google_containers/etcd-amd64                      3.0.17              243830dae7dd        5 months ago        168.9 MB
gcr.io/google_containers/pause-amd64                     3.0                 99e59f495ffa        15 months ago       746.9 kB

node節點:

[[email protected]]# docker images
REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
kubernetesdashboarddev/kubernetes-dashboard-amd64     head                e2cadb73b2df        5 days ago          137MB
quay.io/calico/node                                   v2.4.1              7643422fdf0f        6 days ago          277MB
gcr.io/google_containers/kube-proxy-amd64             v1.7.3              54d2a8698e3c        11 days ago         115MB
quay.io/calico/cni                                    v1.10.0             88ca805c8ddd        13 days ago         70.3MB
gcr.io/google_containers/kubernetes-dashboard-amd64   v1.6.3              691a82db1ecd        2 weeks ago         139MB
nginx                                                 latest              b8efb18f159b        2 weeks ago         107MB
hello-world                                           latest              1815c82652c0        2 months ago        1.84kB
gcr.io/google_containers/pause-amd64                  3.0                 99e59f495ffa        15 months ago       747kB

5.利用kubeadm初始化服務(master節點)

注意:如果你在上一步執行過 kubeadm init 命令,沒有關係,此步執行只需要執行時加上 --skip-preflight-checks 這個配置項即可。

注意:執行 kubeadm init 的 --pod-network-cidr 引數和選擇的網路元件有關係,詳細可以看官方文件說明,本文選用的網路元件為 Calico

[[email protected]base-ok]# kubeadm init --pod-network-cidr=192.168.0.0/16 --apiserver-advertise-address=0.0.0.0 --apiserver-cert-extra-sans=192.168.80.28,192.168.80.14,192.168.80.35,127.0.0.1,k8s-1,k8s-2,k8s-3,192.168.0.1 --skip-preflight-checks

引數說明:

引數名稱 必選 引數說明
pod-network-cidr Yes For certain networking solutions the Kubernetes master can also play a role in allocating network ranges (CIDRs) to each node. This includes many cloud providers and flannel. You can specify a subnet range that will be broken down and handed out to each node with the --pod-network-cidr flag. This should be a minimum of a /16 so controller-manager is able to assign /24 subnets to each node in the cluster. If you are using flannel with this manifest you should use --pod-network-cidr=10.244.0.0/16. Most CNI based networking solutions do not require this flag.
apiserver-advertise-address Yes This is the address the API Server will advertise to other members of the cluster. This is also the address used to construct the suggested kubeadm join line at the end of the init process. If not set (or set to 0.0.0.0) then IP for the default interface will be used.
apiserver-cert-extra-sans Yes Additional hostnames or IP addresses that should be added to the Subject Alternate Name section for the certificate that the API Server will use. If you expose the API Server through a load balancer and public DNS you could specify this with.

其它的 kubeadm 引數設定請參照 官方文件

6.做一枚安靜的美男子,等待安裝成功,安裝成功後你會看到日誌如下(master節點):

注意:記錄這段日誌,後面新增node節點要用到。

[apiclient] All control plane components are healthy after 22.003243 seconds
[token] Using token: 33729e.977f7b5d0a9b5f3e
[apiconfig] Created RBAC rules
[addons] Applied essential addon: kube-proxy
[addons] Applied essential addon: kube-dns

Your Kubernetes master has initialized successfully!

To start using your cluster, you need to run (as a regular user):

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  http://kubernetes.io/docs/admin/addons/

You can now join any number of machines by running the following on each node
as root:

  kubeadm join --token xxxxxxx 192.168.80.28:6443

7.建立kube的目錄,新增kubectl配置(master節點)

 mkdir -p $HOME/.kube
 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
 sudo chown $(id -u):$(id -g) $HOME/.kube/config

8.用 kubectl 新增網路元件Calico(master節點)

kubectl apply -f http://docs.projectcalico.org/v2.4/getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml

注意:此處坑為該檔案未必下載的到,建議還是提前下載到本地,然後執行 kubectl apply -f <本地路徑>

9.確認安裝是否成功(master節點)

9.1 開啟你的/var/log/messages,檢視是否有報錯,理論上,執行完上一步過去5分鐘,日誌應該不會有任何錯誤出現,如果持續報錯,並且過了10分鐘錯誤依然沒有消失,檢查之前的步驟是否有問題

9.2 執行 kubectl get pods --all-namespaces 檢視結果,如果STATUS都為Running,恭喜你,你的master已經安裝成功了。

注意:你的結果顯示的條數未必和我完全一樣,因為我這裡有node節點的相關資訊,而你還沒有新增node節點。

[[email protected]]# kubectl get pods --all-namespaces
NAMESPACE     NAME                                       READY     STATUS    RESTARTS   AGE
default       nginx-app-1666850838-4z2tb                 1/1       Running   0          3d
kube-system   calico-etcd-0ssdd                          1/1       Running   0          3d
kube-system   calico-node-1zfxd                          2/2       Running   1          3d
kube-system   calico-node-s2gfs                          2/2       Running   1          3d
kube-system   calico-node-xx30v                          2/2       Running   1          3d
kube-system   calico-policy-controller-336633499-wgl8j   1/1       Running   0          3d
kube-system   etcd-k8s-1                                 1/1       Running   0          3d
kube-system   kube-apiserver-k8s-1                       1/1       Running   0          3d
kube-system   kube-controller-manager-k8s-1              1/1       Running   0          3d
kube-system   kube-dns-2425271678-trmxx                  3/3       Running   1          3d
kube-system   kube-proxy-79kkh                           1/1       Running   0          3d
kube-system   kube-proxy-n1g6j                           1/1       Running   0          3d
kube-system   kube-proxy-vccr6                           1/1       Running   0          3d
kube-system   kube-scheduler-k8s-1                       1/1       Running   0          3d

10.安裝node節點,執行在master節點執行成功輸出的日誌語句(node節點執行)

注意:執行如下語句的之前,一定要確認node節點下載了上文提到的映象,否則因為映象下載不成功會導致node節點初始化失敗;第二點,一定要時刻檢視/var/log/messages日誌,如果映象版本發生變化,在日誌裡會提示需要下載的映象;第三點,就是要有耐心,如果你的網路可以下載到映象,你當個安靜的美男子就可以了,因為 kubeadm 會幫你做一切,知道你發現/var/log/messages不再有錯誤日誌出現,說明它已經幫你搞定了所有事情,你可以開心的玩耍了。

[[email protected]]# kubeadm join --token xxxxxxxx 192.168.80.28:6443
  1. 驗證子節點,在master節點執行 kubectl get nodes 檢視節點狀態。

注意:node的狀態會變化,新增成功後才是Ready。

[[email protected]]# kubectl get nodes
NAME      STATUS    AGE       VERSION
k8s-1     Ready     3d        v1.7.3
k8s-2     Ready     3d        v1.7.3
k8s-3     Ready     3d        v1.7.3

12.恭喜你,你可以開心的進行kubernetes1.7.3之旅了

安裝後記

Kubernetes,想說愛你不容易啊 ,歡迎其它團隊或者個人與我們團隊進行交流,有意向可以評論區給我留言。

補充:目前官方說dashboard的HEAD版本支援1.7,但是我試了下dashboard確實不行,希望官方加快修復,還有就是多些錯誤定位的方法,否則很難提出具體的問題。