本次采用centos7,3台虚拟机,如图
#网络为桥接模式,修改网络。
vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static #修改成static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=bf5337ab-c044-4af7-9143-12da0d493b89
DEVICE=eno16777736
ONBOOT=yes #修改成yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPADDR=192.168.1.150 # 自定义虚拟机的ip地址,必须与主机在同一网段
NETMASK=255.255.255.0 #设置子网掩码,跟宿主一样
GETWAY=192.168.1.1 #默认网关,跟宿主一样
DNS1=192.168.1.1 #DNS,跟宿主一样
#添加网关,并修改主机名字
vi
/etc/sysconfig/network
NETWORKING=yes
HOSTNAME=xxxx #
GATEWAY=192.168.1.1 #默认网关和宿主机相同
#添加DNS
vi /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.1.1 #DNS,
#重启网络
service network restart
- 修改hosts,所有机器都修改
Vi /etc/hosts
192.168.1.151 master
192.168.1.152 node1
-
-
-
- node2
-
-
先禁用swap
swapoff -a
free -m
设置交换分区开机不启动
因为没有注释掉swap的自动挂载,机器重启后,swap还是会自动启用,从而导致kubelet无法启动
修改 /etc/fstab文件
vim /etc/fstab
- 1
- 1
命令查看一下
如果 Swap 都为0 则禁用成功
#关闭防火墙
systemctl stop firewalld & systemctl disable firewalld
#关闭selinux
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config && setenforce 0
#时间校对(三台机器都做时间校对)
ntpdate ntp1.aliyun.com
hwclock
#配置yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
安装Kubernetes,配置dockers镜像
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
配置kubernetes仓库
vim kubernetes.repo
[kubernetes]
name=kubernetes Repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
enabled=1
执行命令
# wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
# rpm --import rpm-package-key.gpg
# yum repolist
安装k8s docker
yum -y install docker-ce kubelet kubeadm kubectl
环境变量
[root@master ~]# systemctl daemon-reload
启动docker
[root@master ~]# systemctl start docker
设置开机启动
[root@master ~]# systemctl enable docker
[root@master ~]# docker info
保证输出的都是1
[root@master ~]# cat /proc/sys/net/bridge/bridge-nf-call-ip6tables
1
[root@master ~]# cat /proc/sys/net/bridge/bridge-nf-call-iptables
1
不是1的话需要改为1
echo ‘1’ >/proc/sys/net/bridge/bridge-nf-call-ip6tables
echo ‘1’ >/proc/sys/net/bridge/bridge-nf-call-iptables
[root@master ~]# rpm -ql kubelet
/etc/kubernetes/manifests #清单目录
/etc/sysconfig/kubelet #配置文件
/etc/systemd/system/kubelet.service
/usr/bin/kubelet #主程序
设置开机启动
[root@master ~]# systemctl enable kubelet
配置Master(只用在Master下执行)
初始化,遇问题如下
[root@master ~]# kubeadm init --kubernetes-version=v1.20.5 --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=Swap
报错信息,解决办法:
[root@master ~]# vim /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
又由于k8s被墙了,所以需要单独拉取镜像
修改docker源
[root@master ~]# vi /etc/docker/daemon.json
{
"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]
}
systemctl daemon-reload
systemctl restart docker
查看k8s需要安装的包kubeadm config images list
然后再执行
[root@master ~]# kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.20.5 --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=Swap
如果报错,重置命令是kubeadm reset
成功如下图
在master节点 /etc/profile 下面增加如下环境变量
export KUBECONFIG=/etc/kubernetes/admin.conf
# 重起kubelet
systemctl daemon-reload
systemctl restart kubelet
执行成功后配置kubectl认证信息
[root@master ~]# mkdir -p $HOME/.kube
[root@master ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
检测组件运行是否正常
[root@master ~]# kubectl get cs
注意这一条命令需要保存好(添加集群使用)
kubeadm join 192.168.1.151:6443 --token pal2cg.ucc5dddx0e798ru2 --discovery-token-ca-cert-hash sha256:c1458a186bb19b06ffd9db7c6be918dec3e0f6fcb6e107038c0c24d6f2d924ba --ignore-preflight-errors=Swap
如果下载不了,需要通过脚本执行下列命令:
#!/bin/sh
docker pull aiotceo/kube-apiserver:v1.20.5
docker pull aiotceo/kube-controller-manager:v1.20.5
docker pull aiotceo/kube-scheduler:v1.20.5
docker pull aiotceo/kube-proxy:v1.20.5
docker pull aiotceo/pause:3.2
docker pull bitnami/etcd:3.4.13
docker pull aiotceo/coredns:1.7.0
docker tag aiotceo/kube-proxy:v1.20.5 k8s.gcr.io/kube-proxy:v1.20.5
docker tag aiotceo/kube-apiserver:v1.20.5 k8s.gcr.io/kube-apiserver:v1.20.5
docker tag aiotceo/kube-controller-manager:v1.20.5 k8s.gcr.io/kube-controller-manager:v1.20.5
docker tag aiotceo/kube-scheduler:v1.20.5 k8s.gcr.io/kube-scheduler:v1.20.5
docker tag bitnami/etcd:3.4.13 k8s.gcr.io/etcd:3.4.13-0
docker tag aiotceo/coredns:1.7.0 k8s.gcr.io/coredns:1.7.0
docker tag aiotceo/pause:3.2 k8s.gcr.io/pause:3.2
docker rmi aiotceo/kube-proxy:v1.20.5
docker rmi aiotceo/kube-apiserver:v1.20.5
docker rmi aiotceo/kube-controller-manager:v1.20.5
docker rmi aiotceo/kube-scheduler:v1.20.5
docker rmi bitnami/etcd:3.4.13
docker rmi aiotceo/coredns:1.7.0
docker rmi aiotceo/pause:3.2
systemctl daemon-reload & systemctl start kubelet
查看并验证节点信息是否成功
[root@master ~]# kubectl get nodes
安装flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
查看pod运行情况
[root@master ~]# kubectl get pods -n kube-system
配置文件传到node1、node2
[root@master ~]# scp /usr/lib/systemd/system/docker.service node1:/usr/lib/systemd/system/docker.service
[root@master ~]# scp /etc/sysconfig/kubelet node1:/etc/sysconfig/
[root@master ~]# scp /usr/lib/systemd/system/docker.service node2:/usr/lib/systemd/system/docker.service
[root@master ~]# scp /etc/sysconfig/kubelet node2:/etc/sysconfig/
node1服务器配置
[root@node1 ~]# wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
[root@node1 ~]# rpm --import rpm-package-key.gpg
安装docker、kubelet
[root@node1 ~]# yum -y install docker-ce kubelet kubeadm
设置开机自启,启动docker和kubelet
[root@node1 ~]# systemctl enable docker docker
[root@node1 ~]# systemctl enable docker kubelet
systemctl daemon-reload
systemctl start kubelet
systemctl start docker
[root@node1 ~]# docker info
添加主集群里
kubeadm join 192.168.1.151:6443 --token pal2cg.ucc5dddx0e798ru2 --discovery-token-ca-cert-hash sha256:c1458a186bb19b06ffd9db7c6be918dec3e0f6fcb6e107038c0c24d6f2d924ba --ignore-preflight-errors=Swap
查看下载镜像
[root@node1 ~]# docker image ls
在master服务器上可以查节点信息
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 3h v1.11.3
node1 Ready 7m v1.11.3
[root@master ~]# kubectl get pods -n kube-system -o wide
Node2重复node1步骤
至此结束