学习k8s开始搭建k8s,由于没有科学上网途径,安装过程痛不欲生,头皮发麻,各种下载失败。
后面经过两天的研究,一个一个问题反复研磨,最终搭建起来了。基于国内的镜像源搭建。过程如下:

**
1、查看防火墙关闭防火墙
2、关闭分区
3、修改国内yum源,下载docker kubelet kubeadm kubectl
4、执行脚本拉取镜像
5、设置iptables规制,进行流量传递
6、设置kubeadm初始化参数**
**
注意:cpus>=2(cup核数大于等于2);
要启动docker、kubelet。
**
一、查看防火墙关闭防火墙
firewall-cmd --state
停止防火墙
systemctl stop firewalld.service
设置禁止开机启动防火墙
systemctl disable firewalld.service
二、关闭分区
swapoff -a
swapon -a
三、修改国内yum源,下载docker kubelet kubeadm kubectl
a、centos7 修改为阿里的yum源

  1. 备份本地yum源
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak
    2.获取阿里yum源配置文件
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    3.更新cache
    yum makecache
    4.查看
    yum -y update

b、修改yum源
vi /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

c、安装kubelet kubeadm kubectl
yum install -y kubelet kubeadm kubectl
启动kubelet、并设置开机启动
service kubelet start
systemctl enble kubelet.service
d、安装docker
yum install -y docker
启动docker、并设置开机启动
service docker start
systemctl enble docker.service


设置docker阿里镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-‘EOF’
{
“registry-mirrors”: [“https://o8mtz9g4.mirror.aliyuncs.com”]
}
EOF

sudo systemctl daemon-reload
sudo systemctl restart docke

四、执行脚本拉取镜像

touch images.sh
images=( # 下面的镜像应该去除"k8s.gcr.io/"的前缀,版本换成上面获取到的版本
kube-apiserver:v1.12.1
kube-controller-manager:v1.12.1
kube-scheduler:v1.12.1
kube-proxy:v1.12.1
pause:3.1
etcd:3.2.24
coredns:1.2.2
)
for imageName in i m a g e s [ @ ] ; d o d o c k e r p u l l r e g i s t r y . c n − h a n g z h o u . a l i y u n c s . c o m / g o o g l e c o n t a i n e r s / {images[@]} ; do docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/ images[@];dodockerpullregistry.cnhangzhou.aliyuncs.com/googlecontainers/imageName
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/ i m a g e N a m e k 8 s . g c r . i o / imageName k8s.gcr.io/ imageNamek8s.gcr.io/imageName
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
done

sh images.sh

五、设置iptables规制,进行流量传递
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables

六、设置kubeadm初始化参数
kubeadm init --apiserver-advertise-address=192.168.0.104 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.17.3 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16

以下是出现的问题及解决办法

1、centos7配置网络
编辑 /etc/sysconfig/network-scripts/ifcfg-eth0

TYPE=Ethernet
BOOTPROTO=static 静态ip
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=34bbe4fa-f0b9-4ced-828a-f7f7e1094e4a
DEVICE=eno16777736
ONBOOT=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPADDR=192.168.179.3 ip地址
NETMASK=255.255.255.0 子网掩码
GATEWAY=192.168.179.2 网关

运行 service network restart

修改dns地址
编辑/etc/resolv.conf
修改文件内容 nameserver 114.114.114.114

常用dns地址
114.114.114.114
114.114.115.115
223.5.5.5 阿里
223.6.6.6 阿里
180.76.76.76 百度

2、问题输入错误kubeadm初始化参数,停止初始化,并重新初始化出现的问题及解决办法:
execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR FileAvailable–etc-kubernetes-manifests-kube-apiserver.yaml]: /etc/kubernetes/manifests/kube-apiserver.yaml already exists
[ERROR FileAvailable–etc-kubernetes-manifests-kube-controller-manager.yaml]: /etc/kubernetes/manifests/kube-controller-manager.yaml already exists
[ERROR FileAvailable–etc-kubernetes-manifests-kube-scheduler.yaml]: /etc/kubernetes/manifests/kube-scheduler.yaml already exists
[ERROR FileAvailable–etc-kubernetes-manifests-etcd.yaml]: /etc/kubernetes/manifests/etcd.yaml already exists
[ERROR FileContent–proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1
[ERROR Swap]: running with swap on is not supported. Please disable swap
解决办法:
kubeadm reset
swapoff -a
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d49edd50a5f4 d109c0821a2b “kube-scheduler --…” 8 seconds ago Up 6 seconds k8s_kube-scheduler_kube-scheduler-localhost.localdomain_kube-system_1afcd7c93da0246e5f356ca5c6678a62_0
4269d2268fe8 b0f1517c1f4b “kube-controller-m…” 8 seconds ago Up 6 seconds k8s_kube-controller-manager_kube-controller-manager-localhost.localdomain_kube-system_a601581c2b8d6e06f0d7fa3e4fdb23f2_0
c4c5483a6f6a 90d27391b780 “kube-apiserver --…” 8 seconds ago Up 6 seconds k8s_kube-apiserver_kube-apiserver-localhost.localdomain_kube-system_f7714c5b63cfc22b0ff1e38346375939_0
88cdec3884d2 registry.aliyuncs.com/google_containers/pause:3.1 “/pause” 10 seconds ago Up 8 seconds k8s_POD_kube-apiserver-localhost.localdomain_kube-system_f7714c5b63cfc22b0ff1e38346375939_0
d0688c7dcd47 registry.aliyuncs.com/google_containers/pause:3.1 “/pause” 11 seconds ago Up 8 seconds k8s_POD_kube-controller-manager-localhost.localdomain_kube-system_a601581c2b8d6e06f0d7fa3e4fdb23f2_0
4b19ad24bab8 registry.aliyuncs.com/google_containers/pause:3.1 “/pause” 11 seconds ago Up 8 seconds k8s_POD_kube-scheduler-localhost.localdomain_kube-system_1afcd7c93da0246e5f356ca5c6678a62_0
7ae56b941055 registry.aliyuncs.com/google_containers/pause:3.1 “/pause” 11 seconds ago Up 8 seconds k8s_POD_etcd-localhost.localdomain_kube-system_e64c3bd8441eb51970551573308a1241_0

$ docker ps // 查看所有正在运行容器
$ docker stop containerId // containerId 是容器的ID
$ docker ps -a // 查看所有容器
$ docker ps -a -q // 查看所有容器ID
$ docker stop $(docker ps -a -q) // stop停止所有容器
$ docker rm -f $(docker ps -a -q) // remove删除所有容器
重新执行步骤六设置kubeadm初始化参数:
kubeadm init --apiserver-advertise-address=192.168.0.107 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.17.3 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16

Logo

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

更多推荐