k8s集群的搭建部署

1.三个节点上:关闭swap交换分区,否则kubelet会启动不了。

[root@server1 ~]# swapoff -a
[root@server1 ~]# vim /etc/fstab
注释swap开机启动

2.配置k8s的yum源,并安装

[root@server1 ~]# vim k8s.repo
#写入:
[k8s]
name=v1.15
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=0
[root@server1 ~]# vim /etc/yum.conf
# 将keepcache=0改成=1
[root@server1 ~]# yum install kubeadm-1.15.0 kubelet-1.15.0 kubectl-1.15.0 -y #需要联网

k8s deployment 启动rockmq_vim


k8s deployment 启动rockmq_vim_02

下载好的软件包在 /var/cache/yum/x86_64/7Server/k8s/packages/中(为了方便,可以将软件包改名,去掉前面的代码,方便使用)

k8s deployment 启动rockmq_用户名_03


下载得到5个安装包

yum install * -y给三台虚拟机挨个导入镜像

[root@server1 ~]# for i in *.tar; do docker load -i $i ; done

k8s deployment 启动rockmq_vim_04

3、三个节点上:开启kubelet服务,并将该服务设置为开机自启动

systemctl start kubelet.service 
systemctl enable kubelet.service

4、在server1(master节点)上:初始化集群

[root@server1 k8s]# kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=172.25.28.1
显示下面的信息,表示集群初始化成功(其中设置dokcer服务开机自启的waring可以不用管)

k8s deployment 启动rockmq_初始化_05


k8s deployment 启动rockmq_用户名_06

根据上一步结果(初始化集群的结果)的提示信息进行操作

[root@server1 k8s]# useradd k8s   #添加一个普通用户,用户名随意给。我这里指定的是k8s用户
[root@server1 k8s]# vim /etc/sudoers   #编辑/etc/sudoers文件,给k8s用户赋予所有的权限。按wq!保存退出(或者编辑visudo文件,按wq保存退出)
92 k8s     ALL=(ALL)       NOPASSWD: ALL
[root@server1 k8s]# su - k8s   #切换到普通用户进行下面的操作
[k8s@server1 ~]# mkdir -p $HOME/.kube
[k8s@server1 ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[k8s@server1 ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config

5、server2/3加入集群

[root@server2 ~]# kubeadm join 172.25.83.1:6443 --token owrfh4.9gbsw8k4kszo1fi1 --discovery-token-ca-cert-hash sha256:c4c6007dd0aca8439c87c6ff9b1c5f39ce2933ce064479c00fba963ddae5c7d3
     
  #server3上的操作同上

k8s deployment 启动rockmq_用户名_07

显示如下信息,表示加入集群的操作成功了

k8s deployment 启动rockmq_初始化_08

(如果第一次加入集群失败,需要执行命令"kubeadm reset"进行重置,重置之后再执行加入集群的命令)

给三台虚拟机都导入flannel镜像

[root@server1 ~]# docker load -i flannel.tar
scp flannel.tar server2:
scp flannel.tar server3:
cp kube-flannel.yml /home/kubeadm/
su - kubeadm

k8s deployment 启动rockmq_用户名_09


k8s deployment 启动rockmq_初始化_10

[kubeadm@server1 ~]# kubectl apply -f kube-flannel.yml

k8s deployment 启动rockmq_用户名_11

[kubeadm@server1 ~]# echo "source <(kubectl completion bash)" >> .bashrc
[kubeadm@server1 ~]# logout #再login就可以补全命令了

k8s deployment 启动rockmq_初始化_12

[kubeadm@server1 ~]# kubectl  get nodes
[kubeadm@server1 ~]# kubectl get pods --all-namespaces

k8s deployment 启动rockmq_初始化_13


k8s deployment 启动rockmq_用户名_14

显示全部为running则配置成功。如果没有成功,则联网后等待一段时间再查看

6.导入kuberbetes镜像

[root@server1 ~]# docker load -i kubernetes-dashboard.tar 
##server2/server3也需要导入此镜像
[root@server1 ~]# su - kubeadm
[kubeadm@server1 ~]# kubectl create -f  kubernetes-dashboard.yaml 
[kubeadm@server1 ~]# kubectl describe svc kubernetes-dashboard -n kube-system #此时查看,Type为ClusterIP
[kubeadm@server1 ~]# kubectl edit service kubernetes-dashboard -n kube-system #倒数第三行改为type: NodePort
[kubeadm@server1 ~]# kubectl describe svc kubernetes-dashboard -n kube-system #改好后查看Type为NodePort

k8s deployment 启动rockmq_初始化_15

[kubeadm@server1 ~]$ vim dashboard-admin.yaml
#写入:
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kube-system

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kube-system

k8s deployment 启动rockmq_vim_16

[kubeadm@server1 ~]$ kubectl create -f dashboard-admin.yaml

k8s deployment 启动rockmq_用户名_17

[kubeadm@server1 ~]$ kubectl get secrets -n kube-system | grep kubernetes-dashboard
kubernetes-dashboard-certs                       Opaque                                0      53m
kubernetes-dashboard-key-holder                  Opaque                                2      12m
kubernetes-dashboard-token-r7t22                 kubernetes.io/service-account-token   3      53m
[kubeadm@server1 ~]$ kubectl get secrets -n kube-system | grep admin #查看用户名
admin-user-token-vqb7m                           kubernetes.io/service-account-token   3      3m5s
[kubeadm@server1 ~]$ kubectl describe secrets admin-user-token-vqb7m -n kube-system #根据刚才查看的用户名查看token值

浏览器 输入:https://172.25.28.1,点token 粘贴查看的token值即可进入图形化页面
容易出的错:
1.pots 虚拟机要联网
2.镜像每个虚拟机都要导入