kubeadm部署单机kubernetes集群
简介
kubernetes是谷歌开源的一个大规模应用容器技术,它解决了负载均衡的问题,拥有故障自愈以及流量分发能力,并且大大简化了运维部署和后期维护难度,本文以kubeadm为例,快速构建一个单机kubenetes集群。
环境准备
centos 7 并保证访问到外网(需要下载镜像,提前准备好可无需外网)
master ip:192.168.1.202
安装
环境准备
升级系统
yum update -y
关闭防火墙及selinux
systemctl stop firewalld && systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
setenforce 0
修改文件打开限制
echo "ulimit -n 65536" >> /etc/profileecho "root soft nofile 65536" >> /etc/security/limits.conf
echo "root hard nofile 65536" >> /etc/security/limits.conf
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf
关闭swap分区
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab
修改主机名
hostnamectl set-hostname k8s-master1
添加内核参数
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
modprobe br_netfilter
开启ipvs参数并写入开机启动
modprobe -- ip_vs
modprobe -- ip_vs_sh
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- nf_conntrack_ipv4
lsmod |grep ip_vs
yum install ipset ipvsadm -y
chmod +x /etc/rc.local
cat >> /etc/rc.local << EOF
modprobe -- ip_vs
modprobe -- ip_vs_sh
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- nf_conntrack_ipv4
EOF
这一步可以不做,ipvs比iptables模式更快,适用于大规模集群,iptables模式中小集群即可。
安装docker、kubeadm、kubelet
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
EOF
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum clean all
yum makecache fast
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum list docker-ce --showduplicates |sort -r
yum clean all
yum makecache fast
yum install -y docker-ce-18.09.9-3.el7
systemctl enable docker && systemctl start docker
systemctl status docker
version=1.16.4
yum install -y kubeadm-$version-0.x86_64 kubelet-$version-0.x86_64
systemctl enable kubelet
导入镜像
cd /usr/local/src/
wget http://download.zhufunin.com/k8s_1.16/addon.tar.gz
wget http://download.zhufunin.com/k8s_1.16/apiserver_1_16.tar.gz
wget http://download.zhufunin.com/k8s_1.16/cordns_1_6_2.tar.gz
wget http://download.zhufunin.com/k8s_1.16/etcd_3_3_15.tar.gz
wget http://download.zhufunin.com/k8s_1.16/kube-controller-manager_1_16.tar.gz
wget http://download.zhufunin.com/k8s_1.16/kube-proxy_1_16.tar.gz
wget http://download.zhufunin.com/k8s_1.16/kube-scheduler_1_16.tar.gz
wget http://download.zhufunin.com/k8s_1.16/kubernetes-dashboard_1_10.tar.gz
wget http://download.zhufunin.com/k8s_1.16/metrics-server-amd64_0_3_1.tar.gz
wget http://download.zhufunin.com/k8s_1.16/pause_3_1.tar.gz
wget http://download.zhufunin.com/k8s_1.16/traefik_1_7_9.tar.gz
#手动解压镜像
docker load -i apiserver_1_16.tar.gz
docker load -i cordns_1_6_2.tar.gz
docker load -i etcd_3_3_15.tar.gz
docker load -i kube-controller-manager_1_16.tar.gz
docker load -i kube-proxy_1_16.tar.gz
docker load -i kubernetes-dashboard_1_10.tar.gz
docker load -i kube-scheduler_1_16.tar.gz
docker load -i metrics-server-amd64_0_3_1.tar.gz
docker load -i pause_3_1.tar.gz
docker load -i traefik_1_7_9.tar.gz
docker load -i addon.tar.gz
初始化构建集群
kubeadm init --apiserver-advertise-address 192.168.1.202 --kubernetes-version=v1.16.4 --pod-network-cidr=10.244.0.0/16
kubectl get nodes
部署flannel网络
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
修改网络模式为ipvs
修改mode
kubectl edit cm kube-proxy -n kube-system
...
ipvs:
excludeCIDRs: null
minSyncPeriod: 0s
scheduler: ""
strictARP: false
syncPeriod: 30s
kind: KubeProxyConfiguration
metricsBindAddress: 127.0.0.1:10249
mode: "ipvs"
...
修改完成后,查看pod日志可以看到ipvs模式生效。
I1103 02:57:02.282354 1 node.go:135] Successfully retrieved node IP: 192.168.1.202
I1103 02:57:02.282523 1 server_others.go:177] Using ipvs Proxier.
W1103 02:57:02.283215 1 proxier.go:415] IPVS scheduler not specified, use rr by default
I1103 02:57:02.283791 1 server.go:529] Version: v1.16.4
I1103 02:57:02.284512 1 conntrack.go:52] Setting nf_conntrack_max to 131072
I1103 02:57:02.285589 1 config.go:313] Starting service config controller
I1103 02:57:02.285627 1 shared_informer.go:197] Waiting for caches to sync for service config
I1103 02:57:02.285659 1 config.go:131] Starting endpoints config controller
I1103 02:57:02.285668 1 shared_informer.go:197] Waiting for caches to sync for endpoints config
I1103 02:57:02.385974 1 shared_informer.go:204] Caches are synced for endpoints config
I1103 02:57:02.386049 1 shared_informer.go:204] Caches are synced for service config
至此单机集群搭建完成!