前人种树后人乘凉,以下内容大部分内容是李振良老师进行编写,原文章使用虚拟机环境,我基于腾讯云服务器cvm的操作验证,在文档基础上修改了部分内容

1. 安装要求

在开始之前,部署Kubernetes集群机器需要满足以下几个条件:

  • 一台或多台机器,操作系统 CentOS7.x-86_x64
  • 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘30GB或更多
  • 集群中所有机器之间网络互通
  • 可以访问外网,需要拉取镜像
  • 禁止swap分区

2. 学习目标

  1. 在所有节点上安装Docker和kubeadm
  2. 部署Kubernetes Master
  3. 部署容器网络插件
  4. 部署 Kubernetes Node,将节点加入Kubernetes集群中
  5. 部署Dashboard Web页面,可视化查看Kubernetes资源

3. 准备环境

kubernetes大数据集群搭建 kubernetes集群部署_k8s

角色

IP

k8s-master

1.13.7.246

k8s-node1

1.13.3.5

k8s-node2

1.13.7.133

关闭防火墙:
$ systemctl stop firewalld
$ systemctl disable firewalld

关闭selinux:
$ sed -i 's/enforcing/disabled/' /etc/selinux/config  # 永久
$ setenforce 0  # 临时

关闭swap:
$ swapoff -a  # 临时
$ vim /etc/fstab  # 永久

设置主机名:
$ hostnamectl set-hostname <hostname> 
举例来说,在master节点上就需要设置hostnamectl set-hostname k8s-master

在master添加hosts:
$ cat >> /etc/hosts << EOF
1.13.7.246 k8s-master
1.13.3.5 k8s-node1
1.113.7.133 k8s-node2
EOF

将桥接的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  # 生效

时间同步:
$ yum install ntpdate -y
$ ntpdate time.windows.com

4. 所有节点安装Docker/kubeadm/kubelet

Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。

4.1 安装Docker

$ 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
$ systemctl enable docker && systemctl start docker
$ docker --version
Docker version 18.06.1-ce, build e68fc7a
# cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF

4.2 添加阿里云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=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

4.3 安装kubeadm,kubelet和kubectl

由于版本更新频繁,这里指定版本号部署:

$ yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
$ systemctl enable kubelet

5. 部署Kubernetes Master

在1.13.7.246(Master)执行。

这里需要注意apiserver-advertise-address的地址需要是内网地址,不能是外网地址,如果是外网地址会失败
$ kubeadm init \
  --apiserver-advertise-address=10.206.0.6 \
  --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

由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。

如果在这里执行失败了,检查一下10.206.0.6这个ip地址的80端口是否开放了,检查后执行如下
kubeadm reset
同时还需要把/etc/kubernetes/下的manifests删除

使用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 get nodes

7. 加入Kubernetes Node

在1.13.3.5(Node1) 1.13.7.133(Node2)执行。

向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:

$ kubeadm join .10.206.0.6:6443 --token esce21.q6hetwm8si29qxwn \
    --discovery-token-ca-cert-hash sha256:00603a05805807501d7181c3d60b478788408cfe6cedefedb1f97569708be9c5

默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,操作如下:

# kubeadm token create
# kubeadm token list
# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
63bca849e0e01691ae14eab449570284f0c3ddeea590f8da988c07fe2729e924

# kubeadm join 10.206.0.6:6443 --token nuja6n.o3jrhsffiqs9swnu --discovery-token-ca-cert-hash sha256:63bca849e0e01691ae14eab449570284f0c3ddeea590f8da988c07fe2729e924

kubeadm token create --print-join-command

https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-join/

7. 网络方案(CNI)

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/#pod-network

7.1 Flannel(Flannel和Calico二选一,推荐Flannel)

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

确保能够访问到quay.io这个registery。

7.2 Calico(Flannel和Calico二选一,推荐Flannel)

https://docs.projectcalico.org/getting-started/kubernetes/quickstart

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

(反正我没改,因为改的话,需要把这个yaml下载下来才可以)下载完后还需要修改里面配置项:

  • 根据实际网络规划修改Pod CIDR(CALICO_IPV4POOL_CIDR)
  • 选择工作模式(CALICO_IPV4POOL_IPIP),支持BGP(Never)IPIP(Always)CrossSubnet(开启BGP并支持跨子网)

修改完后应用清单:

# kubectl apply -f calico.yaml
# kubectl get pods -n kube-system

验证完成以后,可以通过以下命令查询pods情况

kubectl get pods -n kube-system

会显示如下结果

NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-65d7476764-drgg7   1/1     Running   0          108m
calico-node-75hgc                          1/1     Running   0          108m
calico-node-dv9bm                          1/1     Running   0          108m
calico-node-gxgz4                          1/1     Running   0          108m
coredns-7ff77c879f-4c2p8                   1/1     Running   0          5h15m
coredns-7ff77c879f-grd49                   1/1     Running   0          5h15m
etcd-k8s-master                            1/1     Running   0          5h15m
kube-apiserver-k8s-master                  1/1     Running   0          5h15m
kube-controller-manager-k8s-master         1/1     Running   0          5h15m
kube-flannel-ds-9rmph                      1/1     Running   0          4h54m
kube-flannel-ds-ts586                      1/1     Running   0          4h54m
kube-flannel-ds-zlwmz                      1/1     Running   0          4h54m
kube-proxy-6fngz                           1/1     Running   0          4h56m
kube-proxy-jb57r                           1/1     Running   0          4h57m
kube-proxy-rtvkj                           1/1     Running   0          5h15m
kube-scheduler-k8s-master                  1/1     Running   0          5h15m

8. 测试kubernetes集群

  • 验证Pod工作
  • 验证Pod网络通信
  • 验证DNS解析

在Kubernetes集群中创建一个pod,验证是否正常运行:

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

获得如下内容

NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1     <none>        443/TCP        16h
service/nginx        NodePort    10.111.2.84   <none>        80:31807/TCP   12h

访问地址:http://NodeIP:Port 使用10.111.2.84:80可以访问集群内的,对外映射的是31807

我们可以通过外网IP地址1.13.7.246:31807进行访问nginx

9. 部署 Dashboard

$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml

默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部:

注:如果想要修改yaml,那么就需要把yaml下载下来进行修改才可以

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  type:NodePort
  ports:
    - port: 443
      targetPort: 8443
  selector:
    k8s-app: kubernetes-dashboard

验证是否成功了,可以输入如下命令

kubectl get pod,svc -n kubernetes-dashboard

得到如下结果

NAME                                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)         AGE
service/dashboard-metrics-scraper   ClusterIP   10.104.95.49    <none>        8000/TCP        150m
service/kubernetes-dashboard        NodePort    10.101.59.132   <none>        443:30035/TCP   150m

访问地址: 外网ip地址1.13.7.246:30035(注:只有火狐浏览器可以正确访问)

kubectl get pods --all-namespaces -o wide   #可以查看到pod具体被分配到哪
kubectl get service --all-namespaces        #查看service

创建service account并绑定默认cluster-admin管理员集群角色:

kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

使用输出的token登录Dashboard。

eyJhbGciOiJSUzI1NiIsImtpZCI6ImM0dUdudGZaTlhOMnUtUjBBVlUwX013RW9VRHVvSXQ2M192YUFsUEhrYjAifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tY3NwdngiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiODI4NTFmNjktM2RjYy00ZDVkLTkzODUtNTM0YzRmNjQxMTgzIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.lODBMg4768pUmbmONjeB8jAR4l-TAw_4l7PFdaZfRuaMMNx09qw8hTxrU5-EEPOQVH9zxpwi370KzaILCNGnjOEEyasYRXLB4JAK8PdpEPXiJU9Vxq66KWIFI_CT7_5PVZf1wmbT5wFIM57cH3ClHd9Vfr-ue-IXhFXswngBniO8Eh24Q9y8lev7Rgv8Y4XOi9x9t96H4gGl-YVByBGwksRyDSriRz8sk1hb3SnE_LD13Z0CNvcGK6iCbL6Sa0dHhjKI5pU5XltkgtP_zyOE5GOfgpOnmtysO8agM4pYx9EMSXa1vC81aznPIjc3__faWBR9CWtxHD6C8gJLMddq_Q