弹药库 弹药库

好记性不如烂笔头

目录
搭建kubernetes集群
/  

搭建kubernetes集群 置顶!

软件安装

  • docker
  • kubernetes-cni
  • kubeadm
  • kubectl
  • kubelet

前置条件

  1. 关闭防火墙
  2. 关闭selinux
  3. 关闭swapoffswapoff -a
  4. 设置系统环境
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf
  1. 开启ipvs
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_fo ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack_ipv4"
for kernel_module in \${ipvs_modules}; do
    /sbin/modinfo -F filename \${kernel_module} > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        /sbin/modprobe \${kernel_module}
    fi
done
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep ip_vs

生成kubernetes配置文件

kubeadm config print init-defaults ClusterConfiguration >kubeadm.conf

或者

kubeadm config print init-defaults >kubeadm-config.yaml

修改kubeadm.conf

imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
kubernetesVersion: v1.18.1

修改API服务器地址

localAPIEndpoint:
  advertiseAddress: masterIP
  bindPort: 6443

配置子网络

networking:
  dnsDomain: cluster.local
  podSubnet: 10.244.0.0/16
  serviceSubnet: 10.96.0.0/12
scheduler: {}

添加proxy的mode为ipvs

---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
featureGates:
  SupportIPVSProxyMode: true
mode: ipvs

查看需要的镜像

kubeadm config images list --config kubeadm.conf

拉取镜像

kubeadm config images pull --config kubeadm.conf

初始化主节点

kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs |tee kubeadm-init.log

--experimental-upload-certs 在搭建高可用集群时用到

node节点加入主节点

执行初始化主节点最后输出的命令即可

配置网络

sysctl net.bridge.bridge-nf-call-iptables=1
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl create -f kube-flannel.yml

注意事项:查看配置文件里是否有指定网卡选项,根据实际情况进行修改

185       containers:
186       - name: kube-flannel
187         image: quay.io/coreos/flannel:v0.12.0-amd64
188         command:
189         - /opt/bin/flanneld
190         args:
191         - --ip-masq
192         - --kube-subnet-mgr
193         - --iface=ens32

查看节点

kubectl get nodes

查看运行pod

# 查看命名空间为kube-system的pod
kubectl get pod -n kube-system -o wide

# 查看命名空间为default的pod
kubectl get pod -o wide

标题:搭建kubernetes集群
作者:zhhui