systemctl stop firewalld

systemctl disable firewalld

#关闭selinux

sed -i ‘s/enforcing/disabled/’ /etc/selinux/config

setenforce 0

关闭swapoff

swapoff -a

#脚本动态根据服务器IP设置主机名

cd /tmp/k8s_install

sh ip_sethostname.sh

#绑定IP和主机名

cat >> /etc/hosts << EOF

10.0.0.17 master

10.0.0.11 k8s-node1

10.0.0.12 k8s-node1

10.0.0.13 k8s-node3

10.0.0.14 k8s-node4

10.0.0.15 k8s-node5

10.0.0.16 k8s-node6

EOF

#设置流量转发

cat > /etc/sysctl.d/k8s.conf << EOF

net.bridge.bridge-nf-call-ip6tables = 1

net.bridge.bridge-nf-call-iptables = 1

EOF

#执行生效

sysctl --system

#配置阿里源并安装docker,docker版本一定要是18以上

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

yum -y install docker-ce-18.06.1.ce-3.el7

systemctl enable docker && systemctl start docker

docker --version

#安装必要依赖

yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git socat

#同步服务器时间,这一步很重要,如果用的是云服务器,则找对应服务器厂商的ntp服务即可

ntpdate time.windows.com

#配置阿里镜像加速器

mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<-‘EOF’

{

“registry-mirrors”: [“https://vkayqpp3.mirror.aliyuncs.com”]

}

EOF

systemctl daemon-reload

systemctl restart docker

#添加阿里云yum源

cat > /etc/yum.repos.d/kubernetes.repo << EOF

[kubernetes]

name=Kubernetes

baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64

enabled=1

gpgcheck=1

repo_gpgcheck=1

gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

EOF

yum clean all

yum -y makecache

#安装k8s相关组件,最新的稳定版为1.19.0,此次先安装1.17.0,验证脚本无误后,后期尝试1.19.0

yum install -y kubelet-1.17.0 kubeadm-1.17.0 kube

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

ctl-1.17.0

#设置开机启动

systemctl enable kubelet

#初始化k8s

#master节点输出的加入命令需要手动复制

cd /tmp/k8s_install

sh k8s_clusters_init1.sh

sh k8s_clusters_init2.sh

sh k8s_clusters_init3.sh

#脚本在主节点执行完成之后,将join命令贴到join.sh脚本中,再将脚本发送到各个node节点

#查看节点状态

kubectl get nodes -n kube-system

2 ip_sethostname.sh


#!/bin/bash

#LC_ALL=C ifconfig | grep ‘inet addr:’ | cut -d: -f2 | awk ‘{ print $1}’

ip=ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'

#根据固定字符串拼接ip最后一位来设置节点主机名

ip_end=${ip##*.}

echo $ip_end

host_code=${ip_end:1:2}

echo $ip_end

if [ $ip_end -lt 17 ]

then

host_name=“k8s-node”+$host_code

else

host_name=“master”

fi

hostnamectl set-hostname $host_name

echo hostname

3 三个文件

k8s_clusters_init1.sh

#!/bin/bash

#LC_ALL=C ifconfig | grep ‘inet addr:’ | cut -d: -f2 | awk ‘{ print $1}’

ip=ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'

#ip_end=${ip:-1}

ip_end=${ip##*.}

#如果是主节点,则初始化后,执行固定配置命令

if [ $ip_end -lt 17 ]

then

echo “当前节点非master”

else

kubeadm init \

–apiserver-advertise-address=10.0.0.17 \

–image-repository registry.aliyuncs.com/google_containers \

–kubernetes-version v1.17.0 \

–service-cidr=10.1.0.0/16 \

–pod-network-cidr=10.244.0.0/16

fi

k8s_clusters_init2.sh

#该脚本需输入y确认

#!/bin/bash

#LC_ALL=C ifconfig | grep ‘inet addr:’ | cut -d: -f2 | awk ‘{ print $1}’

ip=ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'

#ip_end=${ip:-1}

ip_end=${ip##*.}

if [ $ip_end -lt 17 ]

then

echo “跳过当前步骤”

else

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown ( i d − u ) : (id -u): (idu):(id -g) $HOME/.kube/config

fi

k8s_clusters_init3.sh

#!/bin/bash

#LC_ALL=C ifconfig | grep ‘inet addr:’ | cut -d: -f2 | awk ‘{ print $1}’

ip=ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'

#ip_end=${ip:-1}

ip_end=${ip##*.}

if [ $ip_end -lt 17 ]

then

sh join.sh

else

echo “当前为主节点”

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

fi

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐