安装k8s
三台服务器:一台做主master,两台做从node01,nide02
master处理器数量改成 2
#为每个节点设置主机名例如:
hostnamectl set-hostname k8s-master
su
hostnamectl set-hostname k8s-node01
su
hostnamectl set-hostname k8s-node02
su
#使节点可以相互关联,在主节点输入IP地址然后发送给其他节点
vim /etc/hosts
scp /etc/hosts root@k8s-node01:/etc/hosts
#测试可以ping通
ping k8s-node01
ping k8s-node02
#安装依赖包
yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git
#设置防火墙
systemctl stop firewalld && systemctl disable firewalld
yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
#关闭SELINUX和swap功能,swap会影响pod的工作
swapoff -a && sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#调整内核参数
cat > kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_00m=0
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
EOF
cp kubernetes.conf /etc/sysctl.d/kubernetes.conf
sysctl -p /etc/sysctl.d/kubernetes.conf
#关闭不需要的服务
systemctl stop postfix &&systemctl disable postfix
#设置rsyslogd和systemd journald日志
mkdir /var/log/journal
mkdir /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF
[Journal]
#持久化保存到磁盘
Storage=persistent
#压缩历史日志
Compress=yes
SyncIntervalSec=5m
RatelimitInterval=30s
RatelimitBurst=1000
#最大占用空间10G
SystemMaxUse=10G
#单日志文件最大200M
SystemMaxFileSize=200M
#日志保存时间2周
MaxRetentionSec=2week
#不将日志转发到syslog
ForwardToSyslog=no
EOF
#更新系统内核
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install -y kernel-lt
#设置开机从新内核启动
grub2-set-default "CentOS Linux (4.4.189-1.e17.elrepo.x86_64) 7 (Core)"
#重启
init 6
#查看是否是4.4版本
uname -r
#kube-proxy开启ipvs的前置条件
modprobe br_netfilter
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules &&
lsmod | grep -e ip_vs -e nf_conntrack_ipv4
③ 安装docker,主从节点都有要做
#安装docker依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
#加载阿里云docker镜像,YUM仓库
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#更新并安装docker时间很长
yum update -y && yum install -y docker-ce
#docker安装好了之后一定要重启系统,此步骤重启时一定要看!!!!!如果光标不是在4.4版本上停留,一定要手动选择4.4版本
grub2-set-default "CentOS Linux (4.4.189-1.e17.elrepo.x86_64) 7 (Core)"
reboot
#开机自启动
systemctl start docker && systemctl enable docker
#配置 daemon.
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
}
}
EOF
mkdir ‐p /etc/systemd/system/docker.service.d
#重启docker服务
systemctl daemon-reload && systemctl restart docker && systemctl enable docker
④ 安装kubeadm,主从节点都有要做
#导入阿里云K8Syum仓库
cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_ gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum -y install kubeadm-1.15.1 kubect1-1.15.1 kubelet-1.15.1
#开机自启动
systemctl enable kubelet.service
#创建/abc解压kubeadm文件到opt
mkdir /opt
tar zxfv kubeadm-basic.images.tar.gz -C /opt
#在opt编写一个脚本
vim load-images.sh
#!/bin/bash
ls /opt/kubeadm-basic.images > /opt/k.txt
cd /opt/kubeadm-basic.images
for i in `cat /opt/k.txt`
do
docker load -i $i
done
rm -rf /opt/k.txt
#加权限然后发送到各节点执行
chmod a+x load-images.sh
./load-images.sh
⑤ 初始化主节点,node节点不需要操作此步骤
#获取kube初始化模板
kubeadm config print init-defaults > /opt/kubeadm-config.yaml
vim kubeadm-config.yaml
#修改其中的内容
#12行改为当前本机IP,冒号后面要有空格
advertiseAddress: 1.2.3.4
#34行当前版本为:v1.15.1
kubernetesVersion: v1.14.0
#在37行附近serviceSubnet: 10.96.0.0/12
#添加pod节点网段
podSubnet: "10.244.0.0/16"
#在39行下面插入默认调度方式为:ipvs
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
featureGates :
SupportIPVSProxyMode: true
mode: ipvs
#开始初始化
kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs | tee kubeadm-init.log
#查看vim kubeadm-init.log日志文件,在最下面可以看到
复制出来,作为命令输入
#查看节点状态
kubectl get node
#移动重要的文件
cd /opt
mkdir k-install
mv kubeadm-init.log kubeadm-config.yaml k-install/
cd k-install/
mkdir code
mv * code/
cd code/
cd ..
mkdir plugin
cd plugin/
mkdir flannel
cd flannel/
#先到etc/hots里面添加网址,不然要科技上网才可以
#然后在flannel:
wget --no-check-certificate https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#会生成kube-flannel.yml文件
要把所有的quay.io替换为quay-mirror.qiniu.com
#替换完成后 重新加载 kubectl apply -f kube-flannel.yml
#查看k8s组件
kubectl get pod -n kube-system
(删除kubectl delete -f kube-flannel.yml)
#将master日志最后一条命令输入到两台node
#查看状态
kubectl get node
k8s已经开启,实验成功