上一篇文章我们介绍了使用minikube快速部署kubernetes1.3到单机上. 多台机器构成的集群,本次介绍kubernetes-ansible来进行安装。ansible是自动化部署一大神器,接下来就让我们来看看使用神器的效果吧。

构成说明

master和etcd共用一台机器,只有一个minion的超级mini构成,只是为演示只用。

No

type

IP

OS

1

master

192.168.32.31

CENTOS7.2

2

etcd

192.168.32.31

CENTOS7.2

3

minion

192.168.32.32

CENTOS7.2

Step 1:安装ansible

在192.168.32.31上安装ansible

[root@host31 local]# yum -y install epel-release
[root@host31 local]# yum -y install ansible

确认安装

[root@host31 local]# ansible --version
ansible 2.1.0.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
[root@host31 local]#

Step 2:设定ssh通路和ansible

分别在两台机器上生成ssh的key

[root@host31 ~]# ssh-keygen
[root@host32 ~]# ssh-keygen

设定2台机器的/etc/hosts

[root@host31 ~]# grep host3 /etc/hosts
192.168.32.31 host31
192.168.32.32 host32
[root@host31 ~]#
[root@host32 ~]# grep host3 /etc/hosts
192.168.32.31 host31
192.168.32.32 host32
[root@host32 ~]#

在2台机器上都作如下设定,保证ssh通路畅通

# ssh-copy-id -i host31
# ssh-copy-id -i host32

在ansible所安装的机器上,追加机器信息到/etc/ansible/hosts中

[root@host31 ansible]# grep host3 /etc/ansible/hosts
host31
host32
[root@host31 ansible]#

确认ansible正常动作

[root@host31 ~]# ansible localhost -m ping
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@host31 ~]#

Step 3:下载contrib

contrib里面有kubernets生态中很多有用的组件,但不属于kubernetes的core的部分。首先我们使用git从​​https://github.com/kubernetes/contrib​​​上clong下来整个contrib,而用来方便安装的kubernetes-ansible,只是其中的一个部分:​​https://github.com/kubernetes/contrib/tree/master/ansible​

[root@host31 local]# cd ..
[root@host31 /]# mkdir -p /local; cd /local
[root@host31 local]# git clone https://github.com/kubernetes/contrib.git
Cloning into 'contrib'...
remote: Counting objects: 27980, done.
remote: Compressing objects: 100% (40/40), done.
remote: Total 27980 (delta 13), reused 0 (delta 0), pack-reused 27940
Receiving objects: 100% (27980/27980), 30.05 MiB | 173.00 KiB/s, done.
Resolving deltas: 100% (13411/13411), done.
[root@host31 local]#

Step 4:生成kubernetes-ansible设定文件

按照构成说明中的2台机器构成,设定inventory文件

[root@host31 ansible]# pwd
/local/contrib/ansible
[root@host31 ansible]# ll
total 28
-rw-r--r--. 1 root root 1630 Jul 29 08:09 deploy-cluster.yml
drwxr-xr-x. 2 root root 20 Jul 29 08:09 group_vars
-rw-r--r--. 1 root root 115 Jul 29 08:09 inventory.example.ha
-rw-r--r--. 1 root root 156 Jul 29 08:09 inventory.example.single_master
drwxr-xr-x. 3 root root 18 Jul 29 08:09 playbooks
-rw-r--r--. 1 root root 2207 Jul 29 08:09 README.md
drwxr-xr-x. 15 root root 4096 Jul 29 08:09 roles
-rwxr-xr-x. 1 root root 711 Jul 29 08:09 setup.sh
drwxr-xr-x. 2 root root 4096 Jul 29 08:09 vagrant
[root@host31 ansible]# cp inventory.example.ha inventory
[root@host31 ansible]#




vi /local/contrib/ansible/inventory/inventory
[masters]
192.168.198.136


[etcd]
192.168.198.136


[nodes]
192.168.198.134
192.168.198.135


[docker]
192.168.198.134
192.168.198.135


[network-service-install]
192.168.198.136
192.168.198.134
192.168.198.135


[pre-ansible]
192.168.198.136
192.168.198.134
192.168.198.135

将inventory文件的内容设定为如下

[root@host31 ansible]# cat inventory

[masters]
host31

[etcd:children]
host31

[nodes]
host32
[root@host31 ansible]#

Step 6:安装所需package

依赖关系整理的真不错,只有一个package在github的说明中虽然没有提到,但是实际是需要提前安装的

No

package

机器

安装命令

1

python-netaddr

192.168.32.31

yum -y install python-netaddr

Step 7:执行安装文件

执行安装文件setup.sh

[root@host31 ansible]# pwd
/local/contrib/ansible
[root@host31 ansible]# ./setup.sh

Step 8:确认kubernetes

确认node

[root@host31 ansible]# kubectl get nodes
NAME STATUS AGE
host31 Ready 1m
host32 Ready 1m
[root@host31 ansible]#

确认services

[root@host31 ansible]# kubectl get services
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 12m
[root@host31 ansible]#

确认版本

[root@host31 ansible]# kubectl version
Client Version: version.Info{Major:"1", Minor:"2", GitVersion:"v1.2.0", GitCommit:"a4463d9a1accc9c61ae90ce5d314e248f16b9f05", GitTreeState:"clean"}
Server Version: version.Info{Major:"1", Minor:"2", GitVersion:"v1.2.0", GitCommit:"a4463d9a1accc9c61ae90ce5d314e248f16b9f05", GitTreeState:"clean"}
[root@host31 ansible]#

吐槽: 居然是1.2,再到github上仔细一看,四个月没有更新了。是要放弃kubernetes-ansible转用minikube的意图么,对我们来说明明非常友好可用的东西,不像minikube有问题了的话只能眼巴巴地看着它那个go语言的二进制文件,眼睁睁的看着它取个版本信息都要联一下google。对1.2和1.3之间区别不是特别在意的朋友,严重推荐此种方式进行安装,甚至生产环境或者测试环境也完全可以在此基础上进行定制和改进安装与部署。