ansible安装二进制kubernetes-1.14.1

时间:2021-03-15 22:50:05

主机信息:

主机IP 主机名 角色
10.10.3.181 k8s-m1  kube-apiserver,kube-controller-manager,kube-scheduler,etcd
10.10.3.182 k8s-n1 kubelet,kube-proxy,etcd
10.10.3.183 k8s-n2 kubelet,kube-proxy,etcd
10.10.3.184 k8s-n3 kubelet,kube-proxy

安装配置ansible

安装

yum install ansible -y

配置

$ vim /etc/ansible/ansible.cfg
...
#去掉这个配置的#号
host_key_checking = False
...
#安装自己的情况修改
$ vim /etc/ansible/hosts
[kubemaster]
10.10.3.181 hostname=k8s-m1 etcd01ip=10.10.3.181 etcd02ip=10.10.3.182 etcd03ip=10.10.3.183
[kubenode]
10.10.3.182 hostname=k8s-n1
10.10.3.183 hostname=k8s-n2
10.10.3.184 hostname=k8s-n3
[allnode]
10.10.3.181
10.10.3.182
10.10.3.183
10.10.3.184
[allnode:vars]
ansible_ssh_user=root
ansible_ssh_pass=
ansible_ssh_port= [etcd]
10.10.3.181 etcdname=etcd01 etcd01ip=10.10.3.181 etcd02ip=10.10.3.182 etcd03ip=10.10.3.183
10.10.3.182 etcdname=etcd02
10.10.3.183 etcdname=etcd03

配置免密钥

1.生产密钥

ssh-keygen -t rsa

2.分发密钥

ansible allnode -m  authorized_key -a "user=root key='{{lookup('file','/root/.ssh/id_rsa.pub')}}'"

安装kubernetes

修改主机名

$ ansible allnode -m hostname -a 'name={{ hostname }}'

git clone

$ git clone https://github.com/jaxzhai/kubernetes-ansible.git

修改kubernetes-ansible/group_vars/all.yaml参数

1、修改ETCD_URL。修改成自己规划的IP即可

2、修改ETCD_ENDPOINTS。修改成自己规划的IP即可

3、修改IFACE。既网卡的名称

开始运行安装

1、setup.yaml

这里我们选择升级到最新内核

ansible-playbook setup.yaml  -e kernel=ture

2、ca.yaml

创建我们所需要的所有证书

ansible-playbook ca.yaml

3、kubectl.yaml

创建kubectl,如果手动下载的话加上download=false标签。自己下载kubernetes-server-linux-amd64.tar.gz到role/kubectl/files目录下

ansible-playbook kubectl.yaml -e download=false

4、etcd.yaml

ansible-playbook etcd.yaml

查看状态

$ ETCDCTL_API=  etcdctl -w table --cacert=/etc/kubernetes/pki/ca.pem --cert=/etc/kubernetes/pki/etcd.pem --key=/etc/kubernetes/pki/etcd-key.pem --endpoints=https://10.10.3.181:2379,https://10.10.3.182:2379,https://10.10.3.183:2379 endpoint status
+--------------------------+------------------+---------+---------+-----------+-----------+------------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+--------------------------+------------------+---------+---------+-----------+-----------+------------+
| https://10.10.3.181:2379 | 29fd839438b13490 | 3.3.12 | 20 kB | false | 2 | 8 |
| https://10.10.3.182:2379 | 15e01ae39b43687d | 3.3.12 | 20 kB | false | 2 | 8 |
| https://10.10.3.183:2379 | deaefeb573250ff1 | 3.3.12 | 20 kB | true | 2 | 8 |
+--------------------------+------------------+---------+---------+-----------+-----------+------------+

5、flannel.yaml

ansible-playbook flannel.yaml

查看结果

$ ansible allnode -m shell -a "/usr/sbin/ip addr show flannel.1|grep -w inet"
10.10.3.183 | CHANGED | rc= >>
inet 172.30.216.0/ scope global flannel. 10.10.3.184 | CHANGED | rc= >>
inet 172.30.176.0/ scope global flannel. 10.10.3.182 | CHANGED | rc= >>
inet 172.30.240.0/ scope global flannel. 10.10.3.181 | CHANGED | rc= >>
inet 172.30.88.0/ scope global flannel.

6、kube-api.yaml

ansible-playbook kube-api.yaml

查看

$ systemctl status kube-apiserver |grep 'Active:'
Active: active (running) since Tue -- :: CST; 55s ago

查看日志

$ journalctl -u kube-apiserver

7、kube-controller-manager.yaml

ansible-playbook kube-controller-manager.yaml

查看

$ systemctl status kube-controller-manager|grep Active
   Active: active (running) since Tue 2019-04-16 14:27:19 CST; 4min 20s ag

查看日志

$ journalctl -u kube-controller-manager

8、kube-scheduler.yaml

ansible-playbook kube-scheduler.yaml

查看

$ systemctl status kube-scheduler|grep Active
Active: active (running) since Tue -- :: CST; 15s ago

查看日志

$ journalctl -u kube-scheduler

9、docker.yaml

ansible-playbook docker.yaml
$ docker info
Containers:
Running:
Paused:
Stopped:
Images:
Server Version: 18.09.
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: systemd
......

10、kubelet.yaml

ansible-playbook kubelet.yaml

11、kube-proxy.yaml

ansible-playbook kube-proxy.yaml

这样就完成了简单的k8s集群搭建。这里只适合玩一玩。当初写这个的原因也是为了练习ansible。此方法不能作为生产集群。