安裝前記:
近來容器對企業來說已經不是什么陌生的概念,Kubernetes作為Google開源的容器運行平台,受到了大家的熱捧。搭建一套完整的kubernetes平台,也成為試用這套平台必須邁過的坎兒。kubernetes1.5版本以及之前,安裝還是相對比較方便的,官方就有通過yum源在centos7安裝kubernetes。但是在kubernetes1.6之后,安裝就比較繁瑣了,需要證書各種認證,對於剛接觸kubernetes的人來說很不友好,按照官方文檔在本地安裝“集群”的的話,我覺得你肯定是跑不起來的,除非你突破了GFW的限制,還要懂得怎么樣不斷修改參數。
令我驚訝的事,漫天都在吹捧kubernetes的強大,居然一篇可以幫助安裝的文檔都找不到(你可以說我菜,但是我找的官方和非官方文檔不下十幾篇,無一成功)。
我自己搞了一個星期,把kubernetes1.7.3版本(網絡組件選用Calico),終於按照官方文檔+填坑的方式部署成功,寫此文希望能幫助更多的人部署kubernetes1.7成功。
安裝
安裝准備
- 操作系統:CentOS7.3
[root@centos7-base-ok]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
- 安裝機器:k8s-1為master節點,k8s-2、k8s-3為slave節點
[root@centos7-base-ok]# 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/目錄下
[root@centos7-base-ok]# 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 [root@centos7-base-ok]# 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
[root@centos7-base-ok]# yum install -y docker-engine.x86_64-1.12.6-1.el7.centos
4.執行docker version命令,驗證docker安裝版本,執行docker run命令,驗證docker是否安裝成功
[root@centos7-base-ok]# 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安裝完成
[root@centos7-base-ok]# systemctl enbale docker && systemctl start docker
安裝kubectl、kubelet、kubeadm(根據需求在不同節點安裝)
注意:此步驟是填坑的開始,因為官方文檔的yum源在國內無法使用,安裝完成后注意觀察你的/var/log/message日志,會瘋狂報錯,別着急,跟着我一步一步來填坑。
1.新建kubernetes.repo文件,將文件移動到/etc/yum.repos.d/目錄下(所有節點)
[root@centos7-base-ok]# 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(所有節點)
[root@centos7-base-ok]# 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配置:
[root@centos7-base-ok]# 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。
[root@centos7-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節點:
[root@centos7-base-ok]# 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
[root@centos7-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節點。
[root@centos7-base-ok]# 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不再有錯誤日志出現,說明它已經幫你搞定了所有事情,你可以開心的玩耍了。
[root@centos7-base-ok]# kubeadm join --token xxxxxxxx 192.168.80.28:6443
- 驗證子節點,在master節點執行
kubectl get nodes
查看節點狀態。
注意:node的狀態會變化,添加成功后才是Ready。
[root@centos7-base-ok]# 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確實不行,希望官方加快修復,還有就是多些錯誤定位的方法,否則很難提出具體的問題。