1、集群搭建
-
环境准备,hosts文件中的配置
192.168.241.140 kubernetes-master 192.168.241.141 kubernetes-node1 192.168.241.142 kubernetes-node2 192.168.241.143 kubernetes-node3 192.168.241.140 k8s.apiserver.com
-
安装初始化环境
-
执行脚本
# 在 master 节点和 worker 节点都要执行 curl -sSL https://kuboard.cn/install-script/v1.16.0/install-kubelet.sh | sh
-
脚本内容如下,主要目的初始化环境安装:docker、kubelet kubeadm kubectl
#!/bin/bash # 在 master 节点和 worker 节点都要执行 # 安装 docker # 参考文档如下 # https://docs.docker.com/install/linux/docker-ce/centos/ # https://docs.docker.com/install/linux/linux-postinstall/ # 卸载旧版本 yum remove -y docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-selinux \ docker-engine-selinux \ docker-engine # 设置 yum repository yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 安装并启动 docker yum install -y docker-ce-18.09.7 docker-ce-cli-18.09.7 containerd.io systemctl enable docker systemctl start docker # 安装 nfs-utils # 必须先安装 nfs-utils 才能挂载 nfs 网络存储 yum install -y nfs-utils # 关闭 防火墙 systemctl stop firewalld systemctl disable firewalld # 关闭 SeLinux setenforce 0 sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config # 关闭 swap swapoff -a yes | cp /etc/fstab /etc/fstab_bak cat /etc/fstab_bak |grep -v swap > /etc/fstab # 修改 /etc/sysctl.conf # 如果有配置,则修改 sed -i "s#^net.ipv4.ip_forward.*#net.ipv4.ip_forward=1#g" /etc/sysctl.conf sed -i "s#^net.bridge.bridge-nf-call-ip6tables.*#net.bridge.bridge-nf-call-ip6tables=1#g" /etc/sysctl.conf sed -i "s#^net.bridge.bridge-nf-call-iptables.*#net.bridge.bridge-nf-call-iptables=1#g" /etc/sysctl.conf # 可能没有,追加 echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.conf echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf # 执行命令以应用 sysctl -p # 配置K8S的yum源 cat <<EOF > /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 EOF # 卸载旧版本 yum remove -y kubelet kubeadm kubectl # 安装kubelet、kubeadm、kubectl yum install -y kubelet-1.16.0 kubeadm-1.16.0 kubectl-1.16.0 # 修改docker Cgroup Driver为systemd # # 将/usr/lib/systemd/system/docker.service文件中的这一行 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock # # 修改为 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --exec-opt native.cgroupdriver=systemd # 如果不修改,在添加 worker 节点时可能会碰到如下错误 # [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". # Please follow the guide at https://kubernetes.io/docs/setup/cri/ sed -i "s#^ExecStart=/usr/bin/dockerd.*#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --exec-opt native.cgroupdriver=systemd#g" /usr/lib/systemd/system/docker.service # 设置 docker 镜像,提高 docker 镜像下载速度和稳定性 # 如果您访问 https://hub.docker.io 速度非常稳定,亦可以跳过这个步骤 curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io # 重启 docker,并启动 kubelet systemctl daemon-reload systemctl restart docker systemctl enable kubelet && systemctl start kubelet docker version
-
-
将安装初始化环境的虚拟机克隆三台,IP分布为以上的说明
192.168.241.141 kubernetes-node1 192.168.241.142 kubernetes-node2 192.168.241.143 kubernetes-node3
-
初始化master
-
下载flannel配置文件
curl -O https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
-
编写脚本文件,init-master.sh
#!/bin/bash # 只在 master 节点执行 MASTER_IP=192.168.241.140 # 替换 apiserver.demo 为 您想要的 dnsName (不建议使用 master 的 hostname 作为 APISERVER_NAME) APISERVER_NAME=k8s.apiserver.com # kubeadm init # 根据您服务器网速的情况,您需要等候 3 - 10 分钟 kubeadm init --pod-network-cidr=10.244.0.0/16 --control-plane-endpoint=${APISERVER_NAME}:6443 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --upload-certs # 配置 kubectl rm -rf /root/.kube/ mkdir /root/.kube/ cp -i /etc/kubernetes/admin.conf /root/.kube/config # 安装 flannel 网络插件 kubectl apply -f kube-flannel.yml
-
执行脚步完成集群的安装
sh init-master.sh
-
-
注册worker
-
获取token
# 只在 master 节点执行 kubeadm token create --print-join-command
-
在worker节点执行以上命令执行之后的输出
kubeadm join k8s.apiserver.com:6443 --token edbh39.vk3gqwxbvmszruek --discovery-token-ca-cert-hash sha256:2f9a195441cae739f5116019cdb6dc1d71c4614e2bc38851e5867160b6b93feb
-
-
查看集群中的节点
kubectl get nodes
2、安装metallb
Metallb是一个软负载均衡的实现,可以用于实现在私有云环境下对LoadBalancer类型的service分配IP地址。
-
下载配置
curl -O https://raw.githubusercontent.com/google/metallb/v0.8.1/manifests/metallb.yaml
-
安装
kubectl apply -f metallb.yaml
-
创建configmap文件,设置IP分配区间,configmap.yaml
apiVersion: v1 kind: ConfigMap metadata: namespace: metallb-system name: config data: config: | address-pools: - name: default protocol: layer2 addresses: - 192.168.241.200-192.168.241.230 # 需要和集群是在一个网段
-
应用
kubectl apply -f configmap.yaml