1. 环境要求

  • 操作系统CentOS7.x-86_x64
  • 每台虚拟机硬件配置:2GB内存以上,2核以上,硬盘30GB以上
  • 集群中所有机器之间网络互通
  • 可以访问外网(拉取镜像)
  • 禁止swap分区

2. 主机节点准备

角色 IP
master 10.4.7.31
node1 10.4.7.32
node2 10.4.7.33

3. 主机初始化

  1. # 修改ip
  2. [root@localhost ~]# sed -i 's#10\.4\.7\.100#10.4.7.x#g' /etc/sysconfig/network-scripts/ifcfg-eth0
  3. [root@localhost ~]# systemctl restart network
  4. # 关闭防火墙
  5. [root@localhost ~]# systemctl stop firewalld && systemctl disable firewall
  6. # 关闭selinux
  7. [root@localhost ~]# sed 's#=enforcing#=disabled#' /etc/selinux/config && setenforce 0
  8. # 关闭swap分区
  9. [root@localhost ~]# swapoff -a && sed -ir 's/.*swap.*/#&/' /etc/fstab
  10. # 设置主机名
  11. [root@localhost ~]# hostnamectl set-hostname k8s-master
  12. [root@localhost ~]# hostnamectl set-hostname k8s-node1
  13. [root@localhost ~]# hostnamectl set-hostname k8s-node2
  14. # master添加host
  15. [root@k8s-master ~]# cat >> /etc/hosts <<eof
  16. 10.4.7.31 k8s-master
  17. 10.4.7.32 k8s-node1
  18. 10.4.7.33 k8s-node2
  19. eof
  20. # 将桥接的ipv4流量传递到iptables链
  21. [root@k8s-master ~]# cat >> /etc/sysctl.d/k8s.conf <<eof
  22. net.bridge.bridge-nf-call-ip6tables = 1
  23. net.bridge.bridge-nf-call-iptables = 1
  24. eof
  25. [root@k8s-master ~]# sysctl --system
  26. # 时间同步
  27. [root@k8s-master ~]# yum install ntpdate -y
  28. [root@k8s-master ~]# ntpdate time.windows.com

4. 安装docker

# 三台主机操作
yum -y install wget
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-re.repo
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl start docker && systemctl enable docker

[root@k8s-master ~]# docker --version
Docker version 18.06.1-ce, build e68fc7a


# 添加阿里云仓库
cat > /etc/docker/daemon.json << eof
{
   "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
eof

5. 部署Kubernetes

# 1.添加阿里云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

# 2.安装 kubeadm、kuletet和kubectl
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet

# 3.初始化:master 执行
kubeadm init \
--apiserver-advertise-address=10.4.7.31 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16

初始化成功:

Your Kubernetes control-plane 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/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 10.4.7.31:6443 —token jq017n.kvgqkqtb38ksa1l2 \ —discovery-token-ca-cert-hash sha256:b208992e1eed3efd345c781cd066105b9509be30212f008429c99763ae4fcc9f

# 4.创建目录
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# 5.节点加入集群,node节点主机操作,node1,node2
kubeadm join 10.4.7.31:6443 --token jq017n.kvgqkqtb38ksa1l2 \
    --discovery-token-ca-cert-hash sha256:b208992e1eed3efd345c781cd066105b9509be30212f008429c99763ae4fcc9f
注意:默认token有效期为24小时,过期需要重新创建,如下:
kubeadm token create --print-join-command

kubectl get nodes
# 6.部署CNI网络插件
wget  https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

# 下载flannel镜像并更改标签,所有节点
docker pull registry.cn-hangzhou.aliyuncs.com/zxyle/flannel:v0.13.1-rc1
docker tag registry.cn-hangzhou.aliyuncs.com/zxyle/flannel:v0.13.1-rc1 quay.io/coreos/flannel:v0.13.1-rc1
kubectl apply -f kube-flannel.yml

# 验证
[root@k8s-master ~]# kubectl get pods -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
coredns-7ff77c879f-cd6hh             1/1     Running   0          78m
coredns-7ff77c879f-hrm49             1/1     Running   0          78m
etcd-k8s-master                      1/1     Running   1          78m
kube-apiserver-k8s-master            1/1     Running   1          78m
kube-controller-manager-k8s-master   1/1     Running   1          78m
kube-flannel-ds-8tnmf                1/1     Running   0          3m52s
kube-flannel-ds-9sbqt                1/1     Running   0          3m52s
kube-flannel-ds-nr655                1/1     Running   0          3m52s
kube-proxy-nrhn6                     1/1     Running   1          76m
kube-proxy-tvrjk                     1/1     Running   1          78m
kube-proxy-vtpjp                     1/1     Running   1          76m
kube-scheduler-k8s-master            1/1     Running   1          78m

[root@k8s-master ~]# kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   79m   v1.18.0 
k8s-node1    Ready    <none>   76m   v1.18.0
k8s-node2    Ready    <none>   76m   v1.18.0

7. 验证集群

# master节点创建一个pod,验证是否正常运行
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort

[root@k8s-master ~]# kubectl get pod,svc
NAME                        READY   STATUS    RESTARTS   AGE
pod/nginx-f89759699-5gh6d   1/1     Running   0          3m47s

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        86m
service/nginx        NodePort    10.97.36.156   <none>        80:31891/TCP   61s