基于kubeadm搭建Kubernetes集群
1 所有节点需要配置的操作
1.1 关闭防火墙
# systemctl stop firewalld
# systemctl disable firewalld
1.2 关闭selinux
# sed -i 's/enforcing/disabled/' /etc/selinux/config
1.3 禁用swap
# swapoff -a
# vim /etc/fstab
# 将与swap有关的配置注释,重启系统即可
1.3 修改主机名和/etc/hosts文件
# hostnamectl set-hostname {k8s-m|k8s-node1|k8s-node2}
# vim /etc/hosts
10.120.10.190 k8s-m
10.120.10.191 k8s-node1
10.120.10.192 k8s-node2
10.120.10.193 k8s-node3
1.4 重启主机
# reboot
1.5 开启路由转发
# echo "net.ipv4.ip_forward = 1">>/etc/sysctl.conf
# sysctl -p
1.6 设置仓库为阿里云仓库
# cd /etc/yum.repos.d/
# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup #备份原文件
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# yum clean all
# yum makecache
# yum install -y epel-release
1.7 配置Docker的阿里云yum安装源,并安装
# cd /etc/yum.repos.d/
# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# yum clean all
# yum makecache
1.8 在每个节点上安装docker-ce-18.06.0并设置开机自启动
# yum install docker-ce-18.06.2.ce
# docker -v
# systemctl start docker
# systemctl enable docker
# systemctl status docker
1.9 启用bridge-nf-call-iptables和bridge-nf-call-ip6tables
# echo 'net.bridge.bridge-nf-call-iptables = 1'>>/etc/sysctl.conf
# echo 'net.bridge.bridge-nf-call-ip6tables = 1'>>/etc/sysctl.conf
# sysctl -p
1.10 配置kubernetes的阿里云apt安装源
# vim /etc/yum.repos.d/kubernetes.repo
[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
# yum clean all
# yum makecache
1.11 在每个节点上安装kubeadm,kubelet,kubectl并设置kubelet开机自启动
# yum install kubelet-1.12.1-0 kubeadm-1.12.1-0 kubectl-1.12.1-0
# kubeadm version
# systemctl enable kubelet
2 Master节点:
2.1 导入kube-apiserver,kube-proxy等kubernetes1.12.1版本的docker镜像
docker pull registry.aliyuncs.com/google_containers/coredns:1.2.2
docker tag be0e8485fd78 k8s.gcr.io/coredns:1.2.2
docker pull registry.aliyuncs.com/google_containers/etcd:3.2.24
docker tag dcf9f4b0065d k8s.gcr.io/etcd:3.2.24
docker pull registry.aliyuncs.com/google_containers/pause:3.1
docker tag 97bc1dcec76a k8s.gcr.io/pause:3.1
docker pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.12.1
docker tag dcb029b5e3ad k8s.gcr.io/kube-apiserver:v1.12.1
docker pull registry.aliyuncs.com/google_containers/kube-controller-manager:v1.12.1
docker tag aa2dd57c7329 k8s.gcr.io/kube-controller-manager:v1.12.1
docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.12.1
docker tag 68987659c19a k8s.gcr.io/kube-proxy:v1.12.1
docker pull registry.aliyuncs.com/google_containers/kube-scheduler:v1.12.1
docker tag af194c06e677 k8s.gcr.io/kube-scheduler:v1.12.1
docker pull quay-mirror.qiniu.com/coreos/flannel:v0.10.0-amd64
docker tag f0fad859c909 quay.io/coreos/flannel:v0.10.0-amd64
2.2 初始化master
# kubeadm init --kubernetes-version=1.12.1 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.120.10.190
## 上述步骤运行成功后,会输出以下内容:
Your Kubernetes master has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
You can now join any number of machines by running the following on each node
as root:
kubeadm join 10.120.10.190:6443 --token d4557c.fh4vjxkiyoj2zt5y --discovery-token-ca-cert-hash sha256:b17d4618ab1149934f69582c727f58e53cd51f89dd9844fd3f7b776a0e3d389a
2.3 Master节点配置root用户管理集群:
2.3.1 使用root用户运行kubernetes
# mkdir -p $HOME/.kube
# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
2.3.2 下载flannel文件 下载路径https://download.csdn.net/download/twjjava/12458299
安装flannel
kubectl apply -f kube-flannel.yml
2.3.3 查看集群状态
# kubectl get nodes
3 Node节点:
3.1 导入kube-proxy,pause和flannel,kubernetes1.12.1版本的docker镜像
## 镜像请从共享服务器下载:(\\10.120.1.100\安装软件\Kubernetes-1.12.1\kubernetes-1.12.1镜像\ "安装集群所需镜像")
docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.12.1
docker pull registry.aliyuncs.com/google_containers/pause:3.1
docker pull quay-mirror.qiniu.com/coreos/flannel:v0.10.0-amd64
kubeadm init --kubernetes-version=1.12.1 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.192.128
3.1 加入master节点:
kubeadm join 10.120.10.190:6443 --token s5gi75.o950l49ripas5e0y --discovery-token-ca-cert-hash sha256:b17d4618ab1149934f69582c727f58e53cd51f89dd9844fd3f7b776a0e3d389a
3.1 在Master上查看集群状态
# kubectl get nodes