
【前言】本文介绍了Kubernetes基于Centos7构建基础环境,作者:姜新灿(同创永益架构总监)。
环境准备
准备三台虚拟机,每台虚机请参照Kubernetes基于Centos7构建基础环境(一)、Kubernetes基于Centos7构建基础环境(二)进行安装构建
一、 三台虚拟机配置表
1. 服务器配置

2. 工具版本
docker version: v19.03.11
Kubernetes version: v1.18.2
参考地址kubernetes监控中级方案:kubernetes监控终极方案-kube-promethues - skyflask - 博客园
二、 修改虚机域名
依次修改各个虚拟机域名为master55.xincan.cn、 slave56.xincan.cn、 slave57.xincan.cn

三、 配置三台机器互相用域名、别名访问
修改/etc/hosts文件设置,3台服务器同时增加如下代码
192.168.1.55 master55.xincan.cn master55
192.168.1.56 slave56.xincan.cn slave56
192.168.1.57 slave57.xincan.cn slave57

四、 重启三台虚拟机,执行reboot
分别链接3台服务器,由之前的localhost已经改成服务器别名

五、 三台服务器时间同步
1. 3台服务器都安装ntp,提示Complete!则安装成功

2. 3台服务器同时设置,查看当前系统时间、并设置当前时间为上海

3. master55配置server ntp.aliyun.com iburst,然后通过sudo systemctl start ntpd启动服
务,稍等一会执行ntpq -p查看是否同步,如果出现前面的*则,同步成功
systemctl start ntpd 启动ntp
systemctl restart ntpd 重启ntp
systemctl enable ntpd.service 开机启动
ntpdc -c loopinfo 查看与时间同步服务器的时间差

4. slave56、slave57配置master55域名地址server master55.xincan.cn iburst,然后通过sudo
systemctl start ntpd启动服务,稍等一会执行ntpq -p查看是否同步,如果出现前面的*则,同步成功
slave56设置

slave57设置

六、 三台服务器同时配置kubernetes网桥
设置网桥为值为1
1. master55设置

2. slave56设置

3. slave57设置

七、 三台服务器同时配置kubernetes下载源,关闭SELinux
1. master55设置

2. slave56设置

3. slave57设置

八、 查看三台服务器下载源列表,如下成功

九、 三台服务器设置免密登录
1:manager节点执行:ssh-keygen -t rsa 一路回车到结束,在/root/.ssh/下面会生成一个公钥文件id_rsa.pub

2. 将公钥追加到authorized_keys

3. 修改authorized_keys权限:

4. 将~/.ssh从master55节点分发到slave56、slave57节点,执行:scp -r ~/.ssh/ root@slave56:~/.ssh/ scp -r ~/.ssh/ root@slave57:~/.ssh/ 过程中需要填写yes,然后提示输入slave56、slave57两个节点的登录密码

5. 验证master55、slave56、slave57三个节点免密登录
master55节点验证

slave56节点验证,第一次链接需要输入目标服务密码,后续则不用

slave57节点验证,第一次链接需要输入目标服务密码,后续则不用

十、 三台服务器同时安装kubelet kubeadm kubectl
安装完成之后,启动,并设置开机启动

十一、以master55为Kubernetes主节点进行Kubernetes初始化
1. 执行命令进行初始化
sudo kubeadm init −−image-repository registry.aliyuncs.com/google_containers − −kubernetes-version v1.18.2 −−apiserver-advertise-address 192.168.1.55 −−pod-network−cidr=10.244.0.0/16 −−token-ttl 0

2. 根据初始化提示,在master55节点上执行如下命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

3. 根据初始化提示,在slave56、slave57节点上分别执行如下命令
kubeadm join 192.168.1.55:6443 −−token 991hr9.scqkkyphn1cjjcl7 −−discovery-token-ca�
cert-hash
sha256:1dcf2607e09f83160ce9bc99a941d9a6bd74e99b6b8d3adb63af800ffee19baf

十二、Kubernetes命令自动补全(墙裂建议)

十三、k8s查看所有节点
master55节点查看所有节点,当前三台服务器的状态都是NotReady

十四、k8s查看所有命名空间下所有pod
发现coredns一直处在pending状态,需要安装k8s网络插件

十五、k8s安装网络插件
我们这里选取calico网络插件(提供企业级支持)
1. 在master55节点上创建文件夹,用于存放下载的网络插件,我这里下载的是calico-3.13.1.yaml

2. 安装calico-3.13.1.yaml

十六、再次查看node和pod
需要等待一会时间
1. 查看nodes,发现状态已经为Ready

2. 查看pods,发现状态都为Running

十七、安装git

十八、kube-promethues部署
1. 下载安装源码,切换到上述创建的k8s目录下,进行git clone https://github.com/coreos/kube-prometheus.git拉取
2. 安装文件都在kube-prometheus/manifests/ 目录下,切换到此目录

3. 官方把所有文件都放在一起,这里我复制了然后分类下,切换到k8s目录下进行操作

4. 将kube-prometheus/manifests/setup下的文件都复制到/prometheus/operator/下

5. 将prometheus/operator/下0namespace-namespace.yaml文件移动到/k8s/prometheus/下

6. 切换到prometheus/目录下,进行文件归置

7. 注意:新版本的默认label变了,需要修改选择器为beta.kubernetes.io/os,不然安装的时候会卡住,修改选择器

8. 注意:镜像使用dockerhub上的


9. 生成namespace

10. 安装operater

11. 依次安装其他组件

12. 查看整体状态

13. k8s查看所有svc

14. k8s暴露grafana外网访问端口
修改type: ClusterIP为 type: NodePort ,默认外网端口NodePort对应的是31533,找到如下代码

15. 最终效果图(部分效果图)



十九、 安装kubernetes-dashboard
下载dashboard所需资源
git clone https://github.com/xincan/kubernetes.git

二十、 Token令牌登录

二十、 KubeConfig登录



网友评论