当前位置:   article > 正文

基于K8S搭建企业级高可用集群

基于K8S搭建企业级高可用集群

目录

1.项目架构图

1.1项目介绍

1.2项目说明

2.项目步骤

1.规划设计整个集群的架构,部署k8s单master的集群环境,1个master2个node点、相关的服务器。

1.1 部署k8s集群

2.部署jumpserver堡垒机,接入node节点

3.将自己的开发的web接口系统制作成镜像,部署到k8s里作为web应用,采用HPA技术,当cpu使用率达到50%的时候,进行水平扩缩,最小20个,最多40个pod。

3.1使用HPA的配置文件如下

4.部署nfs服务器,为整个web集群提供数据,让所有的web 业务pod都取访问,通过pv和pvc、卷挂载实现。

4.1搭建nfs服务器

5.部署主从复制的Mysql集群,以及Redis集群提供数据库服务

6.安装promethues对所有集群(cpu,内存,网络带宽,web服务,数据库服务,磁盘IO等)进行监控包括k8s集群

7.使用ingress给web业务做负载均衡

8.使用探针(liveless、readiness、startup)的httpGet和exec方法对web业务pod进行监控,一旦出现问题马上重启,增强业务pod的可靠性。

9.对整个k8s集群和相关的服务器进行压力测试


1.项目架构图

1.1项目介绍

该架构图主要从安全,稳定,效率,成本四个角度出发

1.安全方面:在业务入口处,使用阿里云的WAF防火墙产品,以及阿里云的DDOS高防包,可以有效地防止sql注入,DDos攻击等百分之95的安全攻击

2.稳定方面:考虑到项目部署在一个机房的一个地区。如果这个地区出现故障将导致整体项目不可用,所以采用同一地区,双机房的部署方式,项目的数据库和redis互相连通,在一个机房出现问题时,可以快速切换到另一个机房,最短的时间恢复业务

3.效率方面:如果说业务机房在广州,考虑到用户量的增大,用户分布在全国各地,新疆,河北这一片区域的用户,访问卡顿问题,购买云产商阿里云的CDN服务,可以避免出现这种地域访问卡顿问题,同时整体统一使用Prometheus+Grafana的监控告警,以及阿里云的SLS日志产品,对整个双地区的日志进行收集,方便快速定位日志

4.成本方面:在保障业务高可用的情况下,可以有效的节约成本,减少损失

1.2项目说明

由于本地虚拟机搭建,本项目没有购买CDN,WAF,DDOS,SLS等服务,将会搭建一套k8s集群,在生产环境可以使用以上服务

2.项目步骤

1.规划设计整个集群的架构,部署k8s单master的集群环境,1个master2个node点、相关的服务器。

1.1 部署k8s集群

  1. 注意:以下操作标注每台和master执行
  2. 1.关闭防火墙和selinux(每台)
  3. [root@scmaster ~]# service firewalld stop
  4. Redirecting to /bin/systemctl stop firewalld.service
  5. [root@scmaster ~]# systemctl disable firewalld
  6. Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
  7. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  8. [root@scmaster ~]# vim /etc/selinux/config
  9. SELINUX=disabled
  10. [root@scmaster ~]# getenforce
  11. Enforcing
  12. 2.安装docker(每台)
  13. 每台节点服务器上都操作master和node上进行
  14. 卸载原来安装过的docker,如果没有安装可以不需要卸载
  15. yum remove docker \
  16. docker-client \
  17. docker-client-latest \
  18. docker-common \
  19. docker-latest \
  20. docker-latest-logrotate \
  21. docker-logrotate \
  22. docker-engine
  23. 安装yum相关的工具,下载docker-ce.repo文件
  24. [root@cali ~]# yum install -y yum-utils -y
  25. [root@cali ~]#yum-config-manager \
  26. --add-repo \
  27. https://download.docker.com/linux/centos/docker-ce.repo
  28. 下载docker-ce.repo文件存放在/etc/yum.repos.d
  29. [root@cali yum.repos.d]# pwd
  30. /etc/yum.repos.d
  31. [root@cali yum.repos.d]# ls
  32. CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo docker-ce.repo
  33. CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo CentOS-x86_64-kernel.repo nginx.repo
  34. 安装docker-ce软件
  35. [root@cali yum.repos.d]#yum install docker-ce-20.10.18 docker-ce-cli-20.10.18 containerd.io docker-compose-plugin -y
  36. container engine 容器引擎
  37. docker是一个容器管理的软件
  38. docker-ce 是服务器端软件 server
  39. docker-ce-cli 是客户端软件 client
  40. docker-compose-plugin 是compose插件,用来批量启动很多容器,在单台机器上
  41. containerd.io 底层用来启动容器的
  42. [root@scmaster ~]# docker --version
  43. Docker version 20.10.18, build b40c2f6
  44. 4.启动docker服务(每台)
  45. [root@scmaster ~]# systemctl start docker
  46. [root@scmaster ~]# ps aux|grep docker
  47. root 53288 1.5 2.3 1149960 43264 ? Ssl 15:11 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  48. root 53410 0.0 0.0 112824 984 pts/0 S+ 15:11 0:00 grep --color=auto docker
  49. 5.设置docker服务开机启动
  50. [root@scmaster ~]# systemctl enable docker
  51. Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
  52. 3.配置 Docker使用systemd作为默认Cgroup驱动
  53. 每台服务器上都要操作,master和node上都要操作执行下面的脚本,会产生 /etc/docker/daemon.json文件
  54. cat <<EOF > /etc/docker/daemon.json
  55. {
  56. "exec-opts": ["native.cgroupdriver=systemd"]
  57. }
  58. EOF
  59. #重启docker
  60. [root@scmaster docker]# systemctl restart docker
  61. [root@web1 yum.repos.d]# cat /etc/docker/daemon.json
  62. {
  63. "exec-opts": ["native.cgroupdriver=systemd"]
  64. }
  65. 关闭swap分区
  66. 因为k8s不想使用swap分区来存储数据,使用swap会降低性能
  67. 每台服务器都需要操作
  68. swapoff -a # 临时关闭
  69. sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab #永久关闭
  70. 修改主机名(每台)
  71. [root@prometheus ~]# cat >> /etc/hosts << EOF
  72. 192.168.159.137 prometheus
  73. 192.168.159.131 web1
  74. 192.168.159.132 web2
  75. 192.168.159.133 web3
  76. EOF
  77. 修改内核参数(每台)
  78. cat <<EOF >> /etc/sysctl.conf
  79. net.bridge.bridge-nf-call-ip6tables = 1
  80. net.bridge.bridge-nf-call-iptables = 1
  81. net.ipv4.ip_nonlocal_bind = 1
  82. net.ipv4.ip_forward = 1
  83. vm.swappiness=0
  84. EOF
  85. cat /etc/sysctl.conf (每台)
  86. # sysctl settings are defined through files in
  87. # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
  88. #
  89. # Vendors settings live in /usr/lib/sysctl.d/.
  90. # To override a whole file, create a new file with the same in
  91. # /etc/sysctl.d/ and put new settings there. To override
  92. # only specific settings, add a file with a lexically later
  93. # name in /etc/sysctl.d/ and put new settings there.
  94. #
  95. # For more information, see sysctl.conf(5) and sysctl.d(5).
  96. net.bridge.bridge-nf-call-ip6tables = 1
  97. net.bridge.bridge-nf-call-iptables = 1
  98. net.ipv4.ip_nonlocal_bind = 1
  99. net.ipv4.ip_forward = 1
  100. vm.swappiness=0
  101. 每台机器都做
  102. [root@prometheus ~]# sysctl -p
  103. net.bridge.bridge-nf-call-ip6tables = 1
  104. net.bridge.bridge-nf-call-iptables = 1
  105. net.ipv4.ip_nonlocal_bind = 1
  106. net.ipv4.ip_forward = 1
  107. vm.swappiness = 0
  108. 6.安装kubeadm,kubelet和kubectl(每台)
  109. kubeadm --》k8s的管理程序--》在master上运行的--》建立整个k8s集群,背后是执行了大量的脚本,帮助我们去启动k8s
  110. kubelet --》在node节点上用来管理容器的--》管理docker,告诉docker程序去启动容器,管理docker容器,告诉docker程序去启动pod
  111. 一个在集群中每个节点(node)上运行的代理。 它保证容器(containers)都运行在 Pod 中。
  112. kubectl --》在master上用来给node节点发号施令的程序,用来控制node节点的,告诉它们做什么事情的,是命令行操作的工具
  113. # 添加kubernetes YUM软件源(每台)
  114. cat > /etc/yum.repos.d/kubernetes.repo << EOF
  115. [kubernetes]
  116. name=Kubernetes
  117. baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
  118. enabled=1
  119. gpgcheck=0
  120. repo_gpgcheck=0
  121. gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  122. EOF
  123. #安装kubeadm,kubelet和kubectl ,并且指定版本(每台)
  124. yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
  125. --》最好指定版本,因为1.24的版本默认的容器运行时环境不是docker了
  126. 下面这个网站有解决方法
  127. https://www.docker.com/blog/dockershim-not-needed-docker-desktop-with-kubernetes-1-24/
  128. #设置开机自启,因为kubelet是k8s在node节点上的代理,必须开机要运行的(每台)
  129. systemctl enable kubelet
  130. #提前准备coredns:1.8.4的镜像,后面需要使用,需要在每台机器上下载镜像
  131. [root@master ~]# docker pull coredns/coredns:1.8.4
  132. [root@master ~]# docker tag coredns/coredns:1.8.4 registry.aliyuncs.com/google_containers/coredns:v1.8.4
  133. 部署Kubernetes Master
  134. master主机执行
  135. #初始化操作在master服务器上执行
  136. [root@master ~]#kubeadm init \
  137. --apiserver-advertise-address=192.168.237.180 \
  138. --image-repository registry.aliyuncs.com/google_containers \
  139. --service-cidr=10.1.0.0/16 \
  140. --pod-network-cidr=10.244.0.0/16
  141. #192.168.92.132 是master的ip
  142. # --service-cidr string Use alternative range of IP address for service VIPs. (default "10.96.0.0/12") 服务发布暴露--》dnat
  143. # --pod-network-cidr string Specify range of IP addresses for the pod network. If set, the control plane will automatically allocate CIDRs for every node.
  144. Your Kubernetes control-plane has initialized successfully!
  145. To start using your cluster, you need to run the following as a regular user
  146. (在master上执行这三个命令)
  147. mkdir -p $HOME/.kube
  148. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  149. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  150. Alternatively, if you are the root user, you can run:
  151. (再执行这个命令)
  152. export KUBECONFIG=/etc/kubernetes/admin.conf
  153. You should now deploy a pod network to the cluster.
  154. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  155. https://kubernetes.io/docs/concepts/cluster-administration/addons/
  156. Then you can join any number of worker nodes by running the following on eac
  157. (最后复制产生的下面些命令去node)
  158. kubeadm join 192.168.159.137:6443 --token 4aw7ag.a8qzacdm0jcd3hxs \
  159. --discovery-token-ca-cert-hash sha256:09511c561866693a9e7f574c1162b3bc28c7
  160. 加入k8s集群(node服务器)

2.部署jumpserver堡垒机,接入node节点

1.下载Jumpserver官网提供的文件一键部署(提前部署阿里YUM)

curl -sSL https://github.com/jumpserver/jumpserver/releases/download/v2.24.0/quick_start.sh | bash

2.启动jumpserver服务

  1. cd /opt/jumpserver-installer-v2.24.0 ##(默认安装目录)
  2. #启动
  3. ./jmsctl.sh start ## 或者 ./jmsctl.sh restart/down/uninstall来管理服务的状态

3.查看镜像和容器是否存在

  1. docker images
  2. ##验证
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. jumpserver/mariadb 10.6 aac2cf878de9 2 months ago 405MB
  5. jumpserver/redis 6.2 48da0c367062 2 months ago 113MB
  6. jumpserver/web v2.24.0 a9046484de3d 6 months ago 416MB
  7. jumpserver/core v2.24.0 83b8321cf9e0 6 months ago 1.84GB
  8. jumpserver/koko v2.24.0 708386a1290e 6 months ago 770MB
  9. jumpserver/lion v2.24.0 81602523a0ac 6 months ago 351MB
  10. jumpserver/magnus v2.24.0 e0a90a2217ad 6 months ago 157MB
  11. docker ps | grep jumpser
  12. ##验证 查看容器是否健康
  13. 1c26e0acbc8e jumpserver/core:v2.24.0 "./entrypoint.sh sta…" 8 hours ago Up 8 hours (healthy) 8070/tcp, 8080/tcp
  14. d544ec4a155d jumpserver/magnus:v2.24.0 "./entrypoint.sh" 8 hours ago Up 8 hours (healthy) 15211-15212/tcp, 0.0.0.0:33060-33061->33060-33061/tcp, :::33060-33061->33060-33061/tcp, 54320/tcp, 0.0.0.0:63790->63790/tcp, :::63790->63790/tcp jms_magnus
  15. 1d409d8d4a62 jumpserver/lion:v2.24.0 "./entrypoint.sh" 8 hours ago Up 8 hours (healthy) 4822/tcp
  16. b6bbd8bf21e8 jumpserver/koko:v2.24.0 "./entrypoint.sh" 8 hours ago Up 8 hours (healthy) 0.0.0.0:2222->2222/tcp, :::2222->2222/tcp, 5000/tcp
  17. 5774d0475eef jumpserver/web:v2.24.0 "/docker-entrypoint.…" 8 hours ago Up 8 hours (healthy) 0.0.0.0:80->80/tcp, :::80->80/tcp
  18. 18c1f9eecbaf jumpserver/core:v2.24.0 "./entrypoint.sh sta…" 8 hours ago Up 8 hours (healthy) 8070/tcp, 8080/tcp
  19. 2767e8938563 jumpserver/mariadb:10.6 "docker-entrypoint.s…" 19 hours ago Up 8 hours (healthy) 3306/tcp
  20. 635f74cc0e43 jumpserver/redis:6.2 "docker-entrypoint.s…" 19 hours ago Up 8 hours (healthy) 6379/tcp

4.访问当前部署虚拟机的IP地址

  1. http://192.168.xxx.xxx
  2. 默认用户: admin 默认密码: admin (超级用户)

5.接入Master和Node节点(参考jumpserver使用教程)

3.将自己的开发的web接口系统制作成镜像,部署到k8s里作为web应用,采用HPA技术,当cpu使用率达到50%的时候,进行水平扩缩,最小20个,最多40个pod。

3.1使用HPA的配置文件如下

  1. [root@k8s-master HPA]# cat hpa.yaml
  2. apiVersion: autoscaling/v2beta2
  3. kind: HorizontalPodAutoscaler
  4. metadata:
  5. name: nginx-hpa
  6. spec:
  7. scaleTargetRef:
  8. apiVersion: apps/v1
  9. kind: Deployment
  10. name: nginx
  11. minReplicas: 20
  12. maxReplicas: 40
  13. metrics:
  14. - type: Resource
  15. resource:
  16. name: cpu
  17. target:
  18. type: Utilization
  19. averageUtilization: 50
  1. [root@k8s-master HPA]# cat nginx.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: nginx
  6. labels:
  7. app: nginx
  8. spec:
  9. replicas: 2
  10. selector:
  11. matchLabels:
  12. app: nginx
  13. template:
  14. metadata:
  15. labels:
  16. app: nginx
  17. spec:
  18. containers:
  19. - name: nginx
  20. image: nginx:latest # replace it with your exactly <image_name:tags>
  21. ports:
  22. - containerPort: 80
  23. resources:
  24. requests: ##必须设置,不然HPA无法运行。
  25. cpu: 500m
  1. [root@k8s-master HPA]# kubectl get hpa
  2. NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
  3. nginx-hpa Deployment/nginx 0%/50% 20 40 3 20h

4.部署nfs服务器,为整个web集群提供数据,让所有的web 业务pod都取访问,通过pv和pvc、卷挂载实现。

4.1搭建nfs服务器

  1. 使用nfs的数据流程
  2. pod-->volume-->pvc-->pv-->nfs
  3. 在每个节点都要安装nfs服务器
  4. yum install nfs-utils -y
  5. [root@master 55]# service nfs restart
  6. 2.设置共享目录
  7. [root@master 55]# vim /etc/exports
  8. [root@master 55]# cat /etc/exports
  9. /sc/web 192.168.2.0/24(rw,no_root_squash,sync)
  10. [root@master 55]#
  11. 3.新建共享目录和index.html网页
  12. [root@master 55]# mkdir /sc/web -p
  13. [root@master 55]# cd /sc/web/
  14. [root@master web]# echo "welcome to sanchuang" >index.html
  15. [root@master web]# ls
  16. index.html
  17. [root@master web]# cat index.html
  18. welcome to sanchuang
  19. [root@master web]#
  20. 4.刷新nfs或者重新输出共享目录
  21. [root@master web]# exportfs -a 输出所有共享目录
  22. [root@master web]# exportfs -v 显示输出的共享目录
  23. /sc/web 192.168.2.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
  24. [root@master web]# exportfs -r 重新输出所有的共享目录
  25. [root@master web]# exportfs -v
  26. /sc/web 192.168.2.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
  27. [root@master web]#
  28. [root@master web]# service nfs restart 重启nfs服务
  29. Redirecting to /bin/systemctl restart nfs.service
  30. 测试是否成功
  31. [root@web1 ~]# mkdir /sc
  32. [root@web1 ~]# mount 192.168.159.134:/web /sc
  33. [root@web1 ~]# df -Th
  34. 192.168.159.134:/web nfs4 17G 2.6G 15G 16% /sc
  35. 5.创建pv使用nfs服务器上的共享目录
  36. [root@master 55]# vim nfs-pv.yaml
  37. [root@master 55]# cat nfs-pv.yaml
  38. apiVersion: v1
  39. kind: PersistentVolume
  40. metadata:
  41. name: sc-nginx-pv
  42. labels:
  43. type: sc-nginx-pv
  44. spec:
  45. capacity:
  46. storage: 10Gi
  47. accessModes:
  48. - ReadWriteMany
  49. storageClassName: nfs #pv对应的名字
  50. nfs:
  51. path: "/sc/web" #nfs共享的目录
  52. server: 192.168.2.130 #nfs服务器的ip地址
  53. readOnly: false #访问模式
  54. [root@prometheus pv]# kubectl get pv
  55. NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
  56. sc-nginx-pv 10Gi RWX Retain Available nfs 32s
  57. 6.创建pvc使用pv
  58. [root@master 55]# vim pvc-nfs.yaml
  59. [root@master 55]# cat pvc-nfs.yaml
  60. apiVersion: v1
  61. kind: PersistentVolumeClaim
  62. metadata:
  63. name: sc-nginx-pvc
  64. spec:
  65. accessModes:
  66. - ReadWriteMany
  67. resources:
  68. requests:
  69. storage: 1Gi
  70. storageClassName: nfs #使用nfs类型的pv
  71. [root@prometheus pv]# kubectl apply -f pvc-nfs.yaml
  72. persistentvolumeclaim/sc-nginx-pvc created
  73. [root@prometheus pv]# kubectl get pvc
  74. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  75. sc-nginx-pvc Bound sc-nginx-pv 10Gi RWX nfs 4s
  76. 7.创建pod使用pvc
  77. [root@master 55]# cat pod-nfs.yaml
  78. apiVersion: v1
  79. kind: Pod
  80. metadata:
  81. name: sc-pv-pod-nfs
  82. spec:
  83. volumes:
  84. - name: sc-pv-storage-nfs
  85. persistentVolumeClaim:
  86. claimName: sc-nginx-pvc
  87. containers:
  88. - name: sc-pv-container-nfs
  89. image: nginx
  90. ports:
  91. - containerPort: 80
  92. name: "http-server"
  93. volumeMounts:
  94. - mountPath: "/usr/share/nginx/html"
  95. name: sc-pv-storage-nfs
  96. [root@prometheus pv]# kubectl apply -f pod.yaml
  97. pod/sc-pv-pod-nfs created
  98. [root@prometheus pv]# kubectl get pod
  99. sc-pv-pod-nfs 1/1 Running 0 28s
  100. 测试,结果发现内容为nfs1中储存的
  101. [root@prometheus pv]# curl 10.244.3.84
  102. wel come to qiuchao feizi feizi
  103. 修改nfs的内容,再次访问
  104. [root@prometheus pv]# curl 10.244.3.84
  105. wel come to qiuchao feizi feizi
  106. qiuchaochao

5.部署主从复制的Mysql集群,以及Redis集群提供数据库服务

  1. 主从复制实验
  2. 准备的机器为:web1,web3
  3. 因为web3开启了二进制日志,选web3为主服务器,web1为从
  4. 配置web3二进制日志
  5. [root@web3 backup]# cat /etc/my.cnf
  6. [mysqld_safe]
  7. [client]
  8. socket=/data/mysql/mysql.sock
  9. [mysqld]
  10. socket=/data/mysql/mysql.sock
  11. port = 3306
  12. open_files_limit = 8192
  13. innodb_buffer_pool_size = 512M
  14. character-set-server=utf8
  15. log_bin
  16. server_id = 1
  17. expire_logs_days = 3
  18. [mysql]
  19. auto-rehash
  20. prompt=\u@\d \R:\m mysql>
  21. 检查是否打开
  22. root@(none) 11:44 mysql>show variables like 'log_bin';
  23. +---------------+-------+
  24. | Variable_name | Value |
  25. +---------------+-------+
  26. | log_bin | ON |
  27. +---------------+-------+
  28. 1 row in set (0.02 sec)
  29. 保持主与从的数据一致性
  30. 在主服务器上备份数据
  31. [root@web3 backup]# mysqldump -uroot -p'Sanchuang123#' --all-databases > all_db.SAQL
  32. mysqldump: [Warning] Using a password on the command line interface can be insecure.
  33. [root@web3 backup]# ls
  34. all_db.SAQL check_pwd.sh my.cnf passwd qiuchao.sql
  35. 传到从服务器上/root目录
  36. [root@web3 backup]# scp all_db.SAQL root@192.168.159.131:/root
  37. all_db.SAQL 100% 885KB 66.2MB/s 00:00
  38. 在从服务器上
  39. [root@web1 ~]# mysql -uroot -p'Sanchuang123#' < all_db.SAQL
  40. mysql: [Warning] Using a password on the command line interface can be insecure.
  41. 主服务器清除二进制文件
  42. root@(none) 15:17 mysql>reset master;
  43. Query OK, 0 rows affected (0.01 sec)
  44. root@(none) 15:18 mysql>show master status;
  45. +-----------------+----------+--------------+------------------+-------------------+
  46. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  47. +-----------------+----------+--------------+------------------+-------------------+
  48. | web3-bin.000001 | 154 | | | |
  49. +-----------------+----------+--------------+------------------+-------------------+
  50. 1 row in set (0.00 sec)
  51. 主服务器新建一个授权用户给从服务器复制二进制日志
  52. grant replication slave on *.* to 'ren'@'192.168.92.%' identified by 'Sanchuang123#';
  53. Query OK, 0 rows affected, 1 warning (0.00 sec)
  54. 在从服务器上配置master info的信息
  55. root@(none) 15:16 mysql>CHANGE MASTER TO MASTER_HOST='192.168.92.134' ,
  56. MASTER_USER='ren',
  57. MASTER_PASSWORD='Sanchuang123#',
  58. MASTER_PORT=3306,
  59. MASTER_LOG_FILE='nginx-lb1-bin.000001',
  60. MASTER_LOG_POS=154;
  61. Query OK, 0 rows affected, 2 warnings (0.02 sec)
  62. 从服务器查看slave信息
  63. show slave status\G;
  64. Slave_IO_State:
  65. Master_Host: 192.168.159.133
  66. Master_User: renxj
  67. Master_Port: 3306
  68. Connect_Retry: 60
  69. Master_Log_File: web3-bin.000001
  70. Read_Master_Log_Pos: 154
  71. Relay_Log_File: web1-relay-bin.000001
  72. Relay_Log_Pos: 4
  73. Relay_Master_Log_File: web3-bin.000001
  74. Slave_IO_Running: No
  75. Slave_SQL_Running: No
  76. 从服务器启动slave
  77. root@(none) 15:31 mysql> start slave;
  78. root@(none) 15:31 mysql>show slave status\G;
  79. *************************** 1. row ***************************
  80. Slave_IO_State: Waiting for master to send event
  81. Master_Host: 192.168.159.133
  82. Master_User: renxj
  83. Master_Port: 3306
  84. Connect_Retry: 60
  85. Master_Log_File: web3-bin.000001
  86. Read_Master_Log_Pos: 450
  87. Relay_Log_File: web1-relay-bin.000002
  88. Relay_Log_Pos: 615
  89. Relay_Master_Log_File: web3-bin.000001
  90. Slave_IO_Running: Yes
  91. Slave_SQL_Running: Yes

6.安装promethues对所有集群(cpu,内存,网络带宽,web服务,数据库服务,磁盘IO等)进行监控包括k8s集群

  1. [root@k8s-master prometheus-grafana]# kubectl get pod -n monitor-sa
  2. NAME READY STATUS RESTARTS AGE
  3. monitoring-grafana-7bf44d7b64-9hsks 1/1 Running 1 (5h36m ago) 3d1h
  4. node-exporter-bz8x5 1/1 Running 6 (5h36m ago) 4d1h
  5. node-exporter-hrh2m 1/1 Running 3 (5h36m ago) 4d1h
  6. node-exporter-mf5d6 1/1 Running 4 (5h36m ago) 4d1h
  7. prometheus-server-6885985cfc-fr7k9 1/1 Running 1 (5h36m ago) 3d1h
  8. [root@k8s-master prometheus-grafana]# kubectl get service -n monitor-sa
  9. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  10. alertmanager ClusterIP 10.1.245.239 <none> 80/TCP 4d5h
  11. monitoring-grafana NodePort 10.1.92.54 <none> 80:30503/TCP 4d1h
  12. prometheus NodePort 10.1.240.51 <none> 9090:31066/TCP 4d23h

prometheus + Grafana效果图

 

7.使用ingress给web业务做负载均衡

  1. 使用yaml文件方式去安装部署ingress controller
  2. wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.7.0/deploy/static/provider/cloud/deploy.yaml
  3. [root@prometheus ingress]# ls
  4. deploy.yaml (新版配置文件)
  5. 在冯老师新加坡的服务器上去下载镜像,然后导出,传回国内使用
  6. [root@k8smaster 4-4]# ls
  7. ingress-controller-deploy.yaml
  8. kube-webhook-certgen-v1.1.0.tar.gz
  9. sc-ingress.yaml
  10. ingress-nginx-controllerv1.1.0.tar.gz
  11. nginx-deployment-nginx-svc-2.yaml
  12. sc-nginx-svc-1.yaml
  13. ingress-controller-deploy.yaml 是部署ingress controller使用的yaml文件
  14. ingress-nginx-controllerv1.1.0.tar.gz ingress-nginx-controller镜像
  15. kube-webhook-certgen-v1.1.0.tar.gz kube-webhook-certgen镜像
  16. sc-ingress.yaml 创建ingress的配置文件
  17. sc-nginx-svc-1.yaml 启动sc-nginx-svc服务和相关pod的yaml
  18. nginx-deployment-nginx-svc-2.yaml 启动sc-nginx-svc-2服务和相关pod的yaml
  19. 1.将镜像部署到所有的node节点上
  20. 2.导入镜像(所有机器)
  21. [root@web1 ~]# docker images|grep nginx-ingress
  22. registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller v1.1.0 ae1a7201ec95 16 months ago 285MB
  23. [root@web1 ~]# docker images|grep kube-webhook
  24. registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen v1.1.1 c41e9fcadf5a 18 months ago 47.7MB
  25. 3.使用ingress-controller-deploy.yaml 文件去启动ingress controller
  26. [root@prometheus 2]# kubectl apply -f ingress-controller-deploy.yaml
  27. namespace/ingress-nginx unchanged
  28. serviceaccount/ingress-nginx unchanged
  29. configmap/ingress-nginx-controller unchanged
  30. clusterrole.rbac.authorization.k8s.io/ingress-nginx unchanged
  31. clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx unchanged
  32. role.rbac.authorization.k8s.io/ingress-nginx unchanged
  33. rolebinding.rbac.authorization.k8s.io/ingress-nginx unchanged
  34. service/ingress-nginx-controller-admission unchanged
  35. service/ingress-nginx-controller unchanged
  36. deployment.apps/ingress-nginx-controller configured
  37. ingressclass.networking.k8s.io/nginx unchanged
  38. validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission configured
  39. serviceaccount/ingress-nginx-admission unchanged
  40. clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission unchanged
  41. clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission unchanged
  42. role.rbac.authorization.k8s.io/ingress-nginx-admission unchanged
  43. rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission unchanged
  44. job.batch/ingress-nginx-admission-create unchanged
  45. job.batch/ingress-nginx-admission-patch unchanged
  46. [root@prometheus 2]# kubectl get svc -n ingress-nginx
  47. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  48. ingress-nginx-controller NodePort 10.1.11.51 <none> 80:31493/TCP,443:30366/TCP 103s
  49. ingress-nginx-controller-admission ClusterIP 10.1.243.244 <none> 443/TCP 103s
  50. [root@prometheus 2]# kubectl get pod -n ingress-nginx
  51. NAME READY STATUS RESTARTS AGE
  52. ingress-nginx-admission-create-g4vcz 0/1 Completed 0 2m24s
  53. ingress-nginx-admission-patch-wqlh6 0/1 Completed 1 2m24s
  54. ingress-nginx-controller-7cd558c647-b5pzx 0/1 Pending 0 2m25s
  55. ingress-nginx-controller-7cd558c647-wl9n4 1/1 Running 0 2m25s
  56. [root@prometheus 2]# kubectl get service -n ingress-nginx
  57. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  58. ingress-nginx-controller NodePort 10.1.82.223 <none> 80:32222/TCP,443:31427/TCP 112s
  59. ingress-nginx-controller-admission ClusterIP 10.1.35.142 <none> 443/TCP 113s
  60. [root@prometheus 2]# cat sc-ingress.yaml
  61. apiVersion: networking.k8s.io/v1
  62. kind: Ingress
  63. metadata:
  64. name: sc-ingress
  65. annotations:
  66. kubernets.io/ingress.class: nginx
  67. spec:
  68. ingressClassName: nginx
  69. rules:
  70. - host: www.feng.com
  71. http:
  72. paths:
  73. - pathType: Prefix
  74. path: /
  75. backend:
  76. service:
  77. name: sc-nginx-svc 要绑定的服务
  78. port:
  79. number: 80
  80. - host: www.zhang.com
  81. http:
  82. paths:
  83. - pathType: Prefix
  84. path: /
  85. backend:
  86. service:
  87. name: sc-nginx-svc-2
  88. port:
  89. number: 80
  90. 创建pod和发布服务一
  91. [root@k8smaster 4-4]# cat sc-nginx-svc-1.yaml
  92. apiVersion: apps/v1
  93. kind: Deployment
  94. metadata:
  95. name: sc-nginx-deploy
  96. labels:
  97. app: sc-nginx-feng
  98. spec:
  99. replicas: 3
  100. selector:
  101. matchLabels:
  102. app: sc-nginx-feng
  103. template:
  104. metadata:
  105. labels:
  106. app: sc-nginx-feng
  107. spec:
  108. containers:
  109. - name: sc-nginx-feng
  110. image: nginx
  111. imagePullPolicy: IfNotPresent
  112. ports:
  113. - containerPort: 80
  114. ---
  115. apiVersion: v1
  116. kind: Service
  117. metadata:
  118. name: sc-nginx-svc
  119. labels:
  120. app: sc-nginx-svc
  121. spec:
  122. selector:
  123. app: sc-nginx-feng
  124. ports:
  125. - name: name-of-service-port
  126. protocol: TCP
  127. port: 80
  128. targetPort: 80
  129. [root@prometheus 2]# kubectl apply -f sc-nginx-svc-1.yaml
  130. deployment.apps/sc-nginx-deploy created
  131. service/sc-nginx-svc created
  132. [root@prometheus 2]# kubectl get svc
  133. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  134. kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 33m
  135. sc-nginx-svc ClusterIP 10.1.29.219 <none> 80/TCP 12s
  136. [root@prometheus 2]# kubectl apply -f nginx-deployment-nginx-svc-2.yaml
  137. deployment.apps/nginx-deployment created
  138. service/sc-nginx-svc-2 created
  139. [root@prometheus 2]# kubectl get svc
  140. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  141. kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 51m
  142. sc-nginx-svc ClusterIP 10.1.29.219 <none> 80/TCP 17m
  143. sc-nginx-svc-2 ClusterIP 10.1.183.100 <none> 80/TCP 7s
  144. [root@prometheus 2]# kubectl get ingress
  145. NAME CLASS HOSTS ADDRESS PORTS AGE
  146. sc-ingress nginx www.feng.com,www.zhang.com 192.168.92.140,192.168.92.142 80 20m
  147. [root@prometheus 2]# kubectl get pod -n ingress-nginx
  148. NAME READY STATUS RESTARTS AGE
  149. ingress-nginx-admission-create-7l4v5 0/1 Completed 0 13m
  150. ingress-nginx-admission-patch-bxpfq 0/1 Completed 1 13m
  151. ingress-nginx-controller-7cd558c647-jctwf 1/1 Running 0 13m
  152. ingress-nginx-controller-7cd558c647-sws4b 1/1 Running 0 4m29s
  153. 获取ingress controller对应的service暴露宿主机的端口,访问宿主机和相关端口,就可以验证ingress controller是否能进行负载均衡
  154. [root@prometheus 2]# kubectl get service -n ingress-nginx
  155. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  156. ingress-nginx-controller NodePort 10.1.236.56 <none> 80:30910/TCP,443:30609/TCP 26m
  157. ingress-nginx-controller-admission ClusterIP 10.1.187.223 <none> 443/TCP 26m
  158. 在宿主机上使用域名进行访问
  159. [root@nfs ~]# cat /etc/hosts
  160. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  161. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  162. 192.168.159.134 NFS
  163. 192.168.92.140 www.feng.com
  164. 192.168.92.142 www.zhang.com
  165. 因为我们是基于域名做的负载均衡的配置,所有必须要在浏览器里使用域名去访问,不能使用ip地址
  166. 同时ingress controller做负载均衡的时候是基于http协议的,7层负载均衡
  167. [root@nfs ~]# curl www.feng.com
  168. <!DOCTYPE html>
  169. <html>
  170. <head>
  171. <title>Welcome to nginx!</title>
  172. <style>
  173. html { color-scheme: light dark; }
  174. body { width: 35em; margin: 0 auto;
  175. font-family: Tahoma, Verdana, Arial, sans-serif; }
  176. </style>
  177. </head>
  178. <body>
  179. <h1>Welcome to nginx!</h1>
  180. <p>If you see this page, the nginx web server is successfully installed and
  181. working. Further configuration is required.</p>
  182. <p>For
  183. online documentation and support please refer to
  184. <a href="http://nginx.org/">nginx.org</a>.<br/>
  185. Commercial support is available at
  186. <a href="http://nginx.com/">nginx.com</a>.</p>
  187. <p><em>Thank you for using nginx.</em></p>
  188. </body>
  189. </html>
  190. 访问zhang.con
  191. [root@nfs ~]# curl www.zhang.com
  192. <html>
  193. <head><title>503 Service Temporarily Unavailable</title></head>
  194. <body>
  195. <center><h1>503 Service Temporarily Unavailable</h1></center>
  196. <hr><center>nginx</center>
  197. </body>
  198. </html>
  199. service2使用了pv+pvc+nfs
  200. [root@prometheus 2]# kubectl get pv
  201. NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
  202. sc-nginx-pv 10Gi RWX Retain Bound default/sc-nginx-pvc nfs 21h
  203. task-pv-volume 10Gi RWO Retain Bound default/task-pv-claim manual 25h
  204. 刷新一下nfs服务器,最终成功
  205. [root@nfs ~]# curl www.zhang.com
  206. wel come to qiuchao feizi feizi
  207. qiuchaochao

8.使用探针(liveless、readiness、startup)的httpGet和exec方法对web业务pod进行监控,一旦出现问题马上重启,增强业务pod的可靠性。

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx
  5. labels:
  6. app: nginx
  7. spec:
  8. replicas: 2
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - name: nginx
  19. image: nginx:latest # replace it with your exactly <image_name:tags>
  20. ports:
  21. - containerPort: 80
  22. resources:
  23. requests: ##必须设置,不然HPA无法运行。
  24. cpu: 500m
  25. livenessProbe:
  26. tcpSocket:
  27. port: 80
  28. initialDelaySeconds: 15
  29. periodSeconds: 20

9.对整个k8s集群和相关的服务器进行压力测试

使用ab压力测试

  1. 安装ab软件
  2. [root@lb conf]# yum install httpd-tools -y
  3. -n:表示请求数
  4. -c:表示并发数
  5. [root@lb conf]# ab -c 100 -n 1000 http://192.168.159.135/
  6. 并发数100,请求数1000访问负载均衡器
  7. 100人访问,访问1000次
  8. Requests per second: 3206.62 [#/sec] (mean)
  9. Time per request: 31.185 [ms] (mean)
  10. Time per request: 0.312 [ms] (mean, across all concurrent requests)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/578879
推荐阅读
相关标签
  

闽ICP备14008679号