目录
一、Kubernetes(K8S)基本环境
角色 | IP地址 | 主机名 |
master | 192.168.1.11/24 | hd1.com |
node1 | 192.168.1.12/24 | hd2.com |
node2 | 192.168.1.13/24 | hd3.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