想学习k8s,但是又不会搭环境?陈老师教你简单搭建k8s测试/学习环境!
设置hosts文件以及主机名
192.168.56.210 k8s-master
192.168.56.220 k8s-node01
192.168.56.230 k8s-node02
hostnamectl set-hostname k8s-master # 依次设置好主机名
配置yum源
配置阿里云的软件源链接:https://opsx.alibaba.com/mirror
[root@k8s-master yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #配置dokcer源
[root@k8s-master ~]# cat <<EOF > /etc/yum.repos.d/kubernetes.repo #配置kubernetes源
> [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
关闭selinux
setenforce 0
# sed -i 's@^\(SELINUX=\).*@\1disabled@' /etc/selinux/config
关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
下载相关软件
[root@k8s-master yum.repos.d]# yum repolist #查看可用源
[root@k8s-master yum.repos.d]# yum install -y docker-ce kubelet kubeadm kubectl
配置镜像加速:
[root@k8s-master ~]# mkdir /etc/docker
[root@k8s-master ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
docker 1.13版本后会自动设置iptables 规则的forward 为drop 需要改成ACCEPT
[root@k8s-master ~]# cat /usr/lib/systemd/system/docker.service
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExexStartPost=/usr/sbin/iptables -P FORWARD ACCEPT # 这里加一行
启动docker
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl start docker
[root@k8s-master ~]# systemctl enable docker
关闭swap
[root@k8s-master ~]# vim /etc/sysconfig/kubelet #修改kubelet禁止提示swap警告,最好关闭swap
KUBELET_EXTRA_ARGS="--fail-swap-on=false" #如果配置了swap不然提示出错信息
更改kubelet配置,不提示swap警告信息,最好关闭swap
[root@k8s-master ~]# swapoff -a #关闭swap
# 修改系统文件是的机器bridge模式开启
[root@k8s-master ~]#echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
设置kubelet开机自起
[root@k8s-master ~]#systemctl enable kubelet.service
---------------------------------
以上可以在三台主机上都执行
----------------------------------
# 下面命令只需要在master上执行就可以了
初始化集群
[root@k8s-master ~]#kubeadm init --kubernetes-version=v1.15.0 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap --apiserver-advertise-address=0.0.0.0 --image-repository registry.aliyuncs.com/google_containers
[root@k8s-master ~]#mkdir -p $HOME/.kube
[root@k8s-master ~]#sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s-master ~]#sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 在node上执行 node节点加入集群,注意:每个人的token是不一样,初始化集群后,命令结果会显示加入集群的命令,直接复制即可
[root@k8s-node~]#kubeadm join 192.168.56.210:6443 --ignore-preflight-errors=Swap --token 1n1j1d.21fchn5z1nwh2j16 \
--discovery-token-ca-cert-hash sha256:ce6a7755de5fcb23d1c54b07b82c392a64cbf9438efe6b994813e10a788ea247
token 默认24小时过期,过期后可执行先得命令重新加入:
[root@k8s-master ~]# kubeadm token create --print-join-command
kubeadm join 192.168.56.210:6443 --token jjquvs.9hd22iqm33eqb77g --discovery-token-ca-cert-hash sha256:ce6a7755de5fcb23d1c54b07b82c392a64cbf9438efe6b994813e10a788ea247
配置flannel网络 #在所有主机上执行
[root@k8s-master ~]#kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
稍等片刻后检查
[root@k8s-master ~]# kubectl get po -o wide --all-namespaces
[root@k8s-master ~]# kubectl get node