Docker学习第六天——Kubernetes安装环境搭建(一)

该文描述了在CentOS7环境下搭建Kubernetes(K8S)的基本步骤,包括实验环境规划,如主机名配置、网络设置;使用脚本自动化配置hosts文件、交换分区关闭、SSH密钥对生成以实现节点间免密登录,以及内核参数调整、防火墙和SELINUX的关闭、时间同步等操作。此外,还涉及了ip_vs模块的加载和软件源的配置。
摘要由CSDN通过智能技术生成

目录

一、Kubernetes(K8S)基本环境

1.1 实验环境规划

1.2 配置机器主机名

1.3 编写余下环境搭建脚本


一、Kubernetes(K8S)基本环境

角色IP地址主机名
master192.168.1.11/24hd1.com
node1192.168.1.12/24hd2.com
node2192.168.1.13/24hd3.com

1.1 实验环境规划

        操作系统:CentOS7

        配置:4G内存、2核CPU、100G硬盘

        网络:NAT

1.2 配置机器主机名

        通过以下命令执行:

hostnamectl sett-hostname hd1.com &&bash
hostnamectl sett-hostname hd2.com &&bash
hostnamectl sett-hostname hd3.com &&bash

1.3 编写余下环境搭建脚本

vim k8sinit.sh

#!/bin/bash
#安装expect
which expect || yum -y install expect
#修改hosts文件
echo -e "192.168.1.11 hd1.com \n192.168.1.12 hd2.com \n192.168.1.13 hd3.com" >> /etc/hosts
#通过expect将hosts文件发送给node1和node2
for i in 2 3
do
        expect -c "
        spawn scp /etc/hosts hd${i}.com:/etc/
        expect \"Are you sure*\"
        send \"yes\n\"
        expect \"*password:\"
        send \"123.com\n\"
        expect eof
        "
done

#生成密钥对
expect -c "
spawn ssh-keygen
expect \"Enter file*\"
send \"\r\"
expect \"Enter passphrase*\"
send \"\r\"
expect \"Enter same*\"
send \"\r\"
expect eof 
"

#发送公钥,实现master对node1和node2的免密登录
expect -c "
spawn ssh-copy-id hd1.com
expect \"Are you sure*\"
send \"yes\n\"
expect \"*password:\"
send \"123.com\n\"
expect eof
"
for i in 2 3 
do
        expect -c "
        spawn ssh-copy-id hd${i}.com
        expect \"*password:\"
        send \"123.com\n\"
        expect eof
        "
done

#关闭swap分区
for i in 1 2 3;

#关闭swap分区
for i in 1 2 3
do
	ssh hd${i}.com swapoff -a &&sed -i '/swap/d' /etc/fstab
done

#修改内核参数
for i in 1 2 3
do 
	ssh hd${i}.com  modprobe br_netfilter && echo "modprobe br_netfilter" >> /etc/profile && echo -e "net.bridge.bridge-nf-call-ip6tables = 1\nnet.bridge.bridge-nf-call-iptables = 1\nnet.ipv4.ip_forward = 1\n" > /etc/sysctl.d/k8s.conf && sysctl -p /etc/sysctl.d/k8s.conf
done

#关闭防火墙及SELINUX
 for i in 1 2 3
do 
	ssh hd${i}.com  systemctl stop firewalld && systemctl disable firewalld && setenforce 0 && sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
done

#配置时间同步
for i in 1 2 3
	do ssh hd${i}.com  yum install -y ntpdate && ntpdate cn.pool.ntp.org
done

#加载ip_vs模块
for i in 1 2 3
do 
	scp /root/ipvs.modules hd${i}.com:/etc/sysconfig/modules/ && ssh hd${i}.com  bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep ip_vs && echo -e "-----------------------------\nhd${i}.com"
done

#配置源
for i in 1 2 3
do 
	ssh hd${i}.com yum -y install lrzsz openssh-clients  && wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/Centos-7.repo && ls -l /etc/yum.repos.d/Centos-7.repo
done
for i in 1 2 3
do 
	ssh hd${i}.com  yum -y install yum-utils && yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && yum -y install epel-release
done
echo -e "[kubernetes]\nname=Kubernetes\nbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/\nenabled=1\ngpgcheck=0" > /etc/yum.repos.d/kubernetes.repo
for i in 2 3
do 
	scp /etc/yum.repos.d/kubernetes.repo  hd${i}.com:/etc/yum.repos.d/
done
for i in 1 2 3
do 
	ssh hd${i}.com yum -y install yum-utils device-mapper-persistent-data lvm2 wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat  conntrack ntpdate telnet ipvsadm
done

ipvs模块文件内容:

#!/bin/bash
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in ${ipvs_modules}; do
 /sbin/modinfo -F filename ${kernel_module} > /dev/null 2>&1
 if [ 0 -eq 0 ]; then
 /sbin/modprobe ${kernel_module}
 fi
done

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值