1 Star 0 Fork 1

赵海/k8s-ansible

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
install-start.sh 5.18 KB
一键复制 编辑 原始数据 按行查看 历史
赵海 提交于 2022-09-08 16:28 . 完善安装脚本
#!/usr/bin/env bash
ansible='192.168.1.100'
image='192.168.1.100'
mip='192.168.1.20'
nip='192.168.1.21 192.168.1.22 192.168.1.23'
port=22
user=root
password=2000
pwd=$(pwd)
echo "初始化安装环境!"
if [ -e /root/.ssh/id_rsa ] && [ -e /root/.ssh/id_rsa.pub ]; then #密钥环境
for i in $image; do
ssh-copy-id root@$i #发送密钥给master主机
done
for i in $mip; do
ssh-copy-id root@$i #发送密钥给master主机
done
for i in $nip; do
ssh-copy-id root@$i #发送密钥给node主机
done
ssh-copy-id root@$ansible #发送密钥给ansible控制主机
chmod +x $pwd/system.sh && bash $pwd/system.sh
else
ssh-keygen
for i in $image; do
ssh-copy-id root@$i #发送密钥给master主机
done
for i in $mip; do
ssh-copy-id root@$i #发送密钥给master主机
done
for i in $nip; do
ssh-copy-id root@$i #发送密钥给node主机
done
ssh-copy-id root@$ansible #发送密钥给ansible控制主机
chmod +x $pwd/system.sh && bash $pwd/system.sh
fi
ansible --version
if [ $? -ne 1 ]; then
yum -y install $pwd/ansible_centos7/* #安装ansible
fi
#初始化ansible配置
echo \
"[defaults]
inventory =$pwd/inventory" >$pwd/ansible.cfg
echo \
"---
- hosts: $ansible
tasks:
- name: 写入主机清单信息
blockinfile:
path: $pwd/inventory
block: |" >$pwd/inventory.yml
echo \
"[ansible]
$ansible" >$pwd/inventory
echo " [image]" >>$pwd/inventory.yml
for a in $image; do
echo " $a" >>$pwd/inventory.yml
done
echo " [master]" >>$pwd/inventory.yml
for b in $mip; do
echo " $b" >>$pwd/inventory.yml
done
echo " [node]" >>$pwd/inventory.yml
for c in $nip; do
echo " $c" >>$pwd/inventory.yml
done
#设置hosts主机文件
echo " [hosts]" >>$pwd/inventory.yml
d=1
for e in $image; do
echo " $e hostname=image$d ansible_ssh_port=$port ansible_ssh_user=$user ansible_ssh_pass=$password" >>$pwd/inventory.yml
d=$((d + 1))
done
d=1
for f in $mip; do
echo " $f hostname=master$d ansible_ssh_port=$port ansible_ssh_user=$user ansible_ssh_pass=$password" >>$pwd/inventory.yml
d=$((d + 1))
done
d=1
for g in $nip; do
echo " $g hostname=node$d ansible_ssh_port=$port ansible_ssh_user=$user ansible_ssh_pass=$password" >>$pwd/inventory.yml
d=$((d + 1))
done
#植入playbook变量
d=1
for h in $image; do
ansible ansible -m lineinfile -a "dest=$pwd/image.yml insertafter='vars(.*)' line=' image$d: $h'"
ansible ansible -m lineinfile -a "dest=$pwd/master.yml insertafter='vars(.*)' line=' image$d: $h'"
ansible ansible -m lineinfile -a "dest=$pwd/node.yml insertafter='vars(.*)' line=' image$d: $h'"
done
for j in $mip; do
ansible ansible -m lineinfile -a "dest=$pwd/image.yml insertafter='vars(.*)' line=' master$d: $j'"
ansible ansible -m lineinfile -a "dest=$pwd/master.yml insertafter='vars(.*)' line=' master$d: $j'"
ansible ansible -m lineinfile -a "dest=$pwd/node.yml insertafter='vars(.*)' line=' master$d: $j'"
done
for k in $nip; do
ansible ansible -m lineinfile -a "dest=$pwd/image.yml insertafter='vars(.*)' line=' node$d: $k'"
ansible ansible -m lineinfile -a "dest=$pwd/master.yml insertafter='vars(.*)' line=' node$d: $k'"
ansible ansible -m lineinfile -a "dest=$pwd/node.yml insertafter='vars(.*)' line=' node$d: $k'"
done
#docker配置文件
echo "\
{
\"exec-opts\": [\"native.cgroupdriver=systemd\"],
\"registry-mirrors\": [\"https://hub-mirror.c.163.com\"],
\"insecure-registries\":[\"$image:5000\", \"registry:5000\"]
}" >$pwd/docker/daemon.json
#yum配置
echo \
"[ftp-yum]
name=ftp-yum
baseurl=ftp://$image/docker/
enabled=1
gpgcheck=0" >$pwd/myyum.repo
#修改源码配置信息
sed -i "12s/master-ip/$mip/" $pwd/v1.17.6/config/kubeadm-init.yaml
sed -i "32s/image-ip/$image/" $pwd/v1.17.6/config/kubeadm-init.yaml
sed -i "172s/quay.io\/coreos\/flannel/$image:5000\/flannel/" $pwd/v1.17.6/flannel/kube-flannel.yml
sed -i "186s/quay.io\/coreos\/flannel/$image:5000\/flannel/" $pwd/v1.17.6/flannel/kube-flannel.yml
#剧本的流程控制
echo '
################################################################################################
# #
# \\\\\ /// #
# \\ // ================== #
# \ / || \\ || #
# \\\ || \ || #
# \\ <===============================> #
# \ || \\ || #
# / || ===============|| #
# // || #
# /// || #
# //// ^ // #
# ||// #
# 正在执行安装程序,请稍后。。。。。。 #
################################################################################################
'
ansible-playbook $pwd/inventory.yml #执行主机清单信息剧本
ansible-playbook $pwd/image.yml #执行镜像仓库剧本
ansible-playbook $pwd/master.yml #执行master剧本
ansible-playbook $pwd/node.yml #执行node节点剧本
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
YAML
1
https://gitee.com/zhao-hai2000/k8s-ansible.git
[email protected]:zhao-hai2000/k8s-ansible.git
zhao-hai2000
k8s-ansible
k8s-ansible
master

搜索帮助