當 Kubernetes 遇到阿裏雲 之 快速部署1.6.1版本
# 當 Kubernetes 遇到阿裏雲 之 快速部署1.6.1版本
阿裏雲提供了豐富多樣的雲產品支持,包括ECS、VPC網絡、經典網絡、負載均衡SLB等等,可以幫助Docker應用輕鬆在雲端運行。阿裏雲除了推出容器服務提供了一站式的容器應用管理解決方案,也在不斷推動其他開源容器技術和阿裏雲的集成更好地滿足用戶的多樣化需求。
本文是一個How To文章,盡量用最簡單的方式讓您在阿裏雲上一最快的方式一鍵部署起來一個Kubernetes集群。本文基於Kubernetes最新版1.6.1版本。並且集成了Kubernetes的阿裏雲CloudProvider,讓你能方便的使用阿裏雲上提供的各種服務,如VPC網絡,阿裏雲SLB,NAS文件存儲等等。
前置條件
- 支持阿裏雲CentOS 7.2-x64版本及Ubuntu 16.04版本
- 支持阿裏雲VPC網絡
- 準備阿裏雲賬號KeyID與KeySecret,參見
- 如果您需要下載任何牆外的鏡像,請移步使用阿裏雲鏡像服務加速器。
- 請至少準備兩個ECS實例,其中 node1 將作為master節點,node2作為工作節點
安裝Kubernetes
安裝Kubernetes的過程非常簡單,總共分兩步,1.創建Master;2.添加slave節點。
創建Master節點
創建Master隻需要兩個參數,阿裏雲賬號的ACCESS_KEY_ID,ACCESS_KEY_SECRET,從這裏獲得.注意記錄輸出中的 token 及endpoint.
[root@master ~]# export ACCESS_KEY_ID=your_key_id
[root@master ~]# export ACCESS_KEY_SECRET=your_key_secret
[root@master ~]# curl -sSL https://aliacs-k8s.oss-cn-hangzhou.aliyuncs.com/installer/kubemgr-1.6.1.sh \
| bash -s nice --node-type master --key-id ${ACCESS_KEY_ID} --key-secret ${ACCESS_KEY_SECRET}
.......
準備中... ################################# [100%]
正在升級/安裝...
1:kubernetes-cni-0.5.1-0 ################################# [ 20%]
2:kubelet-1.6.1-0 ################################# [ 40%]
3:kubectl-1.6.1-0 ################################# [ 60%]
4:kubeadm-1.6.1-0 ################################# [ 80%]
5:ossfs-1.80.0-1 ################################# [100%]
TOKEN: 612391.bcb426dc8367e04f
[kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters.
[init] Using Kubernetes version: v1.6.0
[init] Using Authorization mode: RBAC
[init] WARNING: For cloudprovider integrations to work --cloud-provider must be set for all kubelets in the cluster.
(/etc/systemd/system/kubelet.service.d/10-kubeadm.conf should be edited for this purpose)
......
[certificates] Generated front-proxy client certificate and key.
[certificates] Valid certificates and keys now exist in "/etc/kubernetes/pki"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/admin.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/controller-manager.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/scheduler.conf"
[apiclient] Created API client, waiting for the control plane to become ready
.......
Your Kubernetes master has initialized successfully!
To start using your cluster, you need to run (as a regular user):
sudo cp /etc/kubernetes/admin.conf $HOME/
sudo chown $(id -u):$(id -g) $HOME/admin.conf
export KUBECONFIG=$HOME/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://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 aea23c.721b254c602d82c6 10.24.2.46:6443
添加Slave節點
創建另一個ECS作為Kubernetes集群的Slave節點,SSH登錄到Slave節點,在上一步中記錄輸出的TOKEN=aea23c.721b254c602d82c6
,ENDPOINT=10.24.2.46:6443
[root@node1 ~]# export ACCESS_KEY_ID=your_key_id
[root@node1 ~]# export ACCESS_KEY_SECRET=your_key_secret
[root@node1 ~]# curl -sSL https://aliacs-k8s.oss-cn-hangzhou.aliyuncs.com/installer/kubemgr-1.6.1.sh \
| bash -s nice --node-type node --key-id ${ACCESS_KEY_ID} --key-secret ${ACCESS_KEY_SECRET} --token ${TOKEN} --endpoint ${ENDPOINT}
.......
[preflight] Skipping pre-flight checks
[discovery] Trying to connect to API Server "10.24.2.46:6443"
[discovery] Created cluster-info discovery client, requesting info from "https://10.24.2.46:6443"
[discovery] Cluster info signature and contents are valid, will use API Server "https://10.24.2.46:6443"
[discovery] Successfully established connection with API Server "10.24.2.46:6443"
[bootstrap] Detected server version: v1.6.1-2+555a0aa47c5afb
[bootstrap] The server supports the Certificates API (certificates.k8s.io/v1beta1)
[csr] Created API client to obtain unique certificate for this node, generating keys and certificate signing request
[csr] Received signed certificate from the API server, generating KubeConfig...
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
Node join complete:
* Certificate signing request sent to master and response
received.
* Kubelet informed of new secure connection details.
Run 'kubectl get nodes' on the master to see this machine join.
到此一個最小化的Kubernetes集群就已經創建出來了。您可以多次重複添加節點步驟來為集群添加更多的節點。
使用Kubernetes集群
登錄到master上麵可以通過kubectl命令來操作集群ssh root@master
, 如下:運行一個nginx應用,並使用--type=LoadBalancer
來使用阿裏雲CloudProvider創建阿裏雲SLB。
[root@master ~]# export KUBECONFIG=/etc/kubernetes/admin.conf
[root@master ~]# kubectl get po --namespace=kube-system
[root@master ~]# kubectl run nginx --image=registry.cn-hangzhou.aliyuncs.com/spacexnice/nginx:latest --replicas=2 --labels run=nginx
[root@master ~]# kubectl expose deployment nginx --port=80 --target-port=80 --type=LoadBalancer
同時我們也提前為您部署了Kubernetes的dashboard. 您可以通過以下命令來查看dashboard的NodePort
[root@master ~]# kubectl --namespace=kube-system get svc kubernetes-dashboard
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-dashboard 172.19.52.104 <nodes> 80:31432/TCP 3h
上麵顯示端口為31432,然後打開瀏覽器,通過https://masterip:31432
就可以訪問到dashboard了。如果沒有PORT,可以修改SVC的type=NodePort. kubectl --namespace=kube-system edit svc kubernetes-dashboard
Enjoy your Kubernetes!
最後更新:2017-04-20 20:01:25