当 Kubernetes 遇到阿里云 之 快速部署1.6.7版本
阿里云提供了丰富多样的云产品支持,包括ECS、VPC网络、经典网络、负载均衡SLB等等,可以帮助Docker应用轻松在云端运行。阿里云除了推出容器服务提供了一站式的容器应用管理解决方案,也在不断推动其他开源容器技术和阿里云的集成更好地满足用户的多样化需求。
本文是一个How To文章,尽量用最简单的方式让您在阿里云上一最快的方式一键部署起来一个Kubernetes集群。本文基于Kubernetes最新版1.6.7版本。并且集成了Kubernetes的阿里云CloudProvider,让你能方便的使用阿里云上提供的各种服务,如VPC网络,阿里云SLB,NAS文件存储等等。
同时,您还可以通过阿里云的ROS模板的方式来快速部署本文的Kubernetes。参见
前置条件
- 支持阿里云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 http://aliacs-k8s.oss-cn-hangzhou.aliyuncs.com/installer/kubemgr-1.6.7.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.7-0 ################################# [ 40%]
3:kubectl-1.6.7-0 ################################# [ 60%]
4:kubeadm-1.6.7-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:
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 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 http://aliacs-k8s.oss-cn-hangzhou.aliyuncs.com/installer/kubemgr-1.6.7.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.7-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,然后打开浏览器,通过http://masterip:31432
就可以访问到dashboard了。如果没有PORT,可以修改SVC的type=NodePort. kubectl --namespace=kube-system edit svc kubernetes-dashboard
Enjoy your Kubernetes!