集群规划

master     centos7      2U3G  30G     192.168.188.130
node01     centos7      2U3G  30G     192.168.188.131
node02     centos7      2U3G  30G     192.168.188.132

备注:以下操作18在所有节点执行,910在master节点执行,11~12在node节点执行
最后的测试在master节点执行

1.关闭防火墙

systemctl stop firewalld

2.关闭selinux

setenforce 0
or
vim /etc/selinux/config 将SELINUX=enforce改成SELINUX=disabled 保存,重启

3.关闭swap

swapoff -a  临时关闭
or
vim /etc/fstab  注释掉swap的那一行 永久关闭
free 查看是否关闭

4.添加主机名与ip对应关系

vim /etc/hosts 添加如下内容
192.168.188.130 k8s-master
192.168.188.131 k8s-node01
192.168.188.132 k8s-node02

5.将桥接的IPV4流量传递到iptables 的链

$ cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
$ sysctl --system

6.安装docker

1)下载并安装
$ wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O/etc/yum.repos.d/docker-ce.repo
$ yum -y install docker-ce-18.06.1.ce-3.el7
2)设置开机启动
$ systemctl enable docker
$ systemctl start docker
3)查看docker版本
$ docker --version
Docker version 18.06.1-ce, build e68fc7a
4)配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://opmv8tfb.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

7.添加阿里云kubernetes YUM软件源

cat > /etc/yum.repos.d/kubernetes.repo << EOF
[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

8.安装kubeadm,kubelet和kubectl

yum install kubelet-1.14.1-0.x86_64 --nogpgcheck
yum install kubeadm-1.14.1-0.x86_64 --nogpgcheck
yum install kubectl-1.14.1-0.x86_64 --nogpgcheck
systemctl enable kubelet

9.部署Kubernetes Master

1)初始化kubeadm
kubeadm init \
--apiserver-advertise-address=192.168.188.130 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.14.1 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
若出现如下结果表示初始化顺利

k8s集群搭建_docker

kubeadm join 192.168.188.130:6443 --token q25g7a.g0l60io5eheqjdij \
    --discovery-token-ca-cert-hash sha256:168c90c4793d96fe60fe845db8958704632baecb72fd24de7150d1a5288a78a1
2)使用kubectl工具
复制如下命令直接执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config 
下面就可以直接使用kubectl命令了

k8s集群搭建_nginx_02

10.安装Pod网络插件(CNI)

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml

k8s集群搭建_centos_03

2)查看是否部署成功
kubectl get pods -n kube-system

k8s集群搭建_nginx_04

有镜像拉取失败,手动拉取镜像
docker pull quay-mirror.qiniu.com/coreos/flannel:v0.11.0-amd64
docker tag quay-mirror.qiniu.com/coreos/flannel:v0.11.0-amd64 quay.io/coreos/flannel:v0.11.0-amd64
过几分钟用kubectl get pods -n kube-system命令查看是否重新部署成功

k8s集群搭建_centos_05

3)再次查看node,可以看到状态为ready
kubectl get node

k8s集群搭建_centos_06

11.Node节点手动拉取镜像

docker pull quay-mirror.qiniu.com/coreos/flannel:v0.11.0-amd64
docker tag quay-mirror.qiniu.com/coreos/flannel:v0.11.0-amd64 quay.io/coreos/flannel:v0.11.0-amd64

12.Node节点加入集群

复制9.1的join命令将节点加入集群
kubeadm join 192.168.188.130:6443 --token c0rjh9.6mvp2gg9dbmnimvc \
    --discovery-token-ca-cert-hash sha256:168c90c4793d96fe60fe845db8958704632baecb72fd24de7150d1a5288a78a1
若加入成功显示如下

k8s集群搭建_linux_07

13.测试kubernetes集群

$ kubectl create deployment nginx --image=nginx
$ kubectl expose deployment nginx --port=80 --type=NodePort
$ kubectl get pod,svc

k8s集群搭建_docker_08

kubectl get pod,svc -o wide

k8s集群搭建_nginx_09

通过浏览器访问:http://192.168.188.130:30372 可以正常访问

k8s集群搭建_centos_10
k8s集群搭建_nginx_11