使用shell脚本自动部署单master节点多node k8s集群。

使用shell脚本自动部署单master节点多node k8s集群。

一、前言

因公司软件运行环境都是kubeadm+k8s+docker+rancher的方式,有的项目有需求K8S需要做高可用,有的只需要做单master即可。
项目比较多基本上每个客户都有正式环境、开发环境、测试环境,为了避免做重复性的工作,做一个合格的运维懒人这里通过shell脚本来做一个自动化部署k8s集群。
脚本使用首先需要定义环境变量,即当前目录下的:environment.sh文件,所有的关键信息都通过environment.sh进行修改。

shell脚本地址:https://gitee.com/ws123w/kubeadm-single-master.git

注:网络使用ipvs,所以系统内核必须升级到4.4或以上!
否则会有coredns无法解析的问题!

二、脚本目录结构

clone项目到服务器,目录结构如下:

[root@k8s-01 kubeadm-single-master]# ls -l
total 20
-rw-r--r--. 1 root root 2675 Feb 11 10:21 deploy-k8s-single.sh     #部署脚本 bash ./deploy-k8s-single.sh 执行。
-rw-r--r--. 1 root root 2431 Feb 11 10:21 environment.sh             #环境变量配置文件,脚本会从环境变量中取master 、node 的IP地址。
-rw-r--r--. 1 root root 1154 Feb 11 10:21 README.md
drwxr-xr-x. 2 root root 4096 Feb 11 10:21 scripts                     #脚本文件
drwxr-xr-x. 2 root root 4096 Feb 11 10:21 soft                         #离线软件包
drwxr-xr-x. 2 root root   29 Feb 11 10:21 work                         #临时工作目录

三、准备工作

注:这里使用三台服务器作为演示。
172.29.6.187 k8s-01 master
172.29.6.161 k8s-02 work
172.29.6.164 k8s-03 work

3.1、修改服务器计算机名称,设置hosts。

修改计算机名

[root@localhost kubeadm-single-master]# hostnamectl set-hostname k8s-01

断开连接重新登录就能看到计算机名是否修改成功。

设置主机hosts

[root@k8s-01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.29.6.187 k8s-01
172.29.6.161 k8s-02
172.29.6.164 k8s-03
3.2、运行脚本的机器(默认使用k8s master机器执行脚本)做ssh免密登录其他服务器。

生产ssh私钥公钥

[root@k8s-01 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? 
[root@k8s-01 ~]# 

添加互信,依次所有node节点添加进来。

[root@k8s-01 ~]# ssh-copy-id root@172.29.6.187
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.29.6.187 (172.29.6.187)' can't be established.
ECDSA key fingerprint is SHA256:Ip3mEORwf47ub60M8lCI3SBApfz4rWmOYVac7vs1JwU.
ECDSA key fingerprint is MD5:52:96:42:ec:8b:55:f7:50:94:a6:eb:c5:ba:27:3c:87.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-cop
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值