当前位置:   article > 正文

【Kubernetes项目部署】k8s集群+高可用、负载均衡+防火墙

【Kubernetes项目部署】k8s集群+高可用、负载均衡+防火墙

项目架构图

(1)部署 kubernetes 集群

详见:http://t.csdnimg.cn/RLveS

(2)

在 Kubernetes 环境中,通过yaml文件的方式,创建2个Nginx Pod分别放置在两个不同的节点上;
Pod使用hostPath类型的存储卷挂载,两个节点本地目录共享使用 /data,2个Pod副本测试页面自定义,但要不同,以做区分

编辑nginx.yaml 文件

  1. mkdir /opt/k8s-shiyan
  2. cd /opt/k8s-shiyan/
  3. vim nginx.yaml
  4. apiVersion: v1
  5. kind: Pod
  6. metadata:
  7. name: nginx01
  8. labels:
  9. app: nginx
  10. spec:
  11. #调度到指定的节点
  12. nodeName: node01
  13. #容器名和镜像
  14. containers:
  15. - name: nginx-container01
  16. image: nginx:latest
  17. #将指定的卷挂载到指定的目录
  18. volumeMounts:
  19. - name: data-volume
  20. mountPath: /usr/share/nginx/html
  21. #创建并定义挂载卷的卷名和路径,类型为目录
  22. volumes:
  23. - name: data-volume
  24. hostPath:
  25. path: /data
  26. type: Directory
  27. ---
  28. apiVersion: v1
  29. kind: Pod
  30. metadata:
  31. name: nginx02
  32. labels:
  33. app: nginx
  34. spec:
  35. nodeName: node02
  36. containers:
  37. - name: nginx-container02
  38. image: nginx:latest
  39. volumeMounts:
  40. - name: data-volume
  41. mountPath: /usr/share/nginx/html
  42. volumes:
  43. - name: data-volume
  44. hostPath:
  45. path: /data
  46. type: Directory

node节点创建/data 目录

执行nginx.yaml 创建资源

  1. kubectl apply -f nginx.yaml
  2. kubectl get pod -o wide

检验测试挂载情况

  1. kubectl describe pod nginx01
  2. kubectl describe pod nginx02

  1. #在两个pod中添加文件
  2. kubectl get pod
  3. kubectl exec -it nginx01 /bin/bash
  4. echo "web01" > /usr/share/nginx/html/index.html
  5. exit
  6. kubectl exec -it nginx02 /bin/bash
  7. echo "web02" > /usr/share/nginx/html/index.html
  8. exit

#到两个node节点查看

ls /data/

(3)

编写service对应的yaml文件,使用NodePort类型和TCP 30000端口将Nginx服务发布出去

编辑nginx-svc.yaml

  1. vim nginx-svc.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: nginx-svc
  6. spec:
  7. #允许外部流量通过该 NodePort 访问 Service
  8. type: NodePort
  9. ports:
  10. #端口协议
  11. - protocol: TCP
  12. #Service 暴露的端口为 80
  13. port: 80
  14. #将流量转发到 Pod 的端口 80
  15. targetPort: 80
  16. #将外部流量映射到节点的 30000 端口
  17. nodePort: 30000
  18. #将该 Service 与具有标签 app: nginx 的 Pod 进行关联
  19. selector:
  20. app: nginx

创建service资源

  1. #创建service资源
  2. kubectl apply -f nginx-svc.yaml
  3. kubectl get svc

访问测试

  1. curl 10.103.25.72
  2. curl 192.168.67.30:30000

(4)

负载均衡区域配置Keepalived+Nginx,实现负载均衡高可用,通过VIP 192.168.10.100和自定义的端口号即可访问K8S发布出来的服务

  1. cat > /etc/yum.repos.d/nginx.repo << 'EOF'
  2. [nginx]
  3. name=nginx repo
  4. baseurl=http://nginx.org/packages/centos/7/$basearch/
  5. gpgcheck=0
  6. EOF
  7. yum -y install nginx

配置负载均衡和高可用服务器
  1. systemctl stop firewalld.service
  2. setenforce 0

配置nginx.conf文件

  1. user nginx;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log notice;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. #在http模块中添加upstream和server模块
  10. upstream k8s {
  11. server 192.168.67.12:30000;
  12. server 192.168.67.13:30000;
  13. }
  14. server {
  15. #监听30000,当访问30000端口时,去调用下面的location
  16. listen 30000;
  17. location / {
  18. proxy_pass http://k8s;
  19. }
  20. }
  21. include /etc/nginx/mime.types;
  22. default_type application/octet-stream;
  23. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  24. '$status $body_bytes_sent "$http_referer" '
  25. '"$http_user_agent" "$http_x_forwarded_for"';
  26. access_log /var/log/nginx/access.log main;
  27. sendfile on;
  28. #tcp_nopush on;
  29. keepalive_timeout 65;
  30. #gzip on;
  31. include /etc/nginx/conf.d/*.conf;
  32. }
  1. #检查、启动nginx,设置开机自启并过滤查看
  2. nginx -t
  3. systemctl restart nginx
  4. systemctl enable nginx
  5. netstat -natp | grep nginx

配置keepalived.conf文件

  1. vim /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. notification_email {
  5. acassen@firewall.loc
  6. }
  7. notification_email_from
  8. smtp_server 127.0.0.1
  9. smtp_connect_timeout 30
  10. router_id 192.168.67.21
  11. }
  12. vrrp_script check_nginx {
  13. script "/etc/keepalived/nginx_check.sh"
  14. interval 2
  15. weight -30
  16. fall 3
  17. rise 2
  18. timeout 2
  19. }
  20. vrrp_instance NGINX {
  21. state MASTER
  22. interface ens33
  23. virtual_router_id 10
  24. priority 100
  25. advert_int 1
  26. authentication {
  27. auth_type PASS
  28. auth_pass 123
  29. }
  30. virtual_ipaddress {
  31. 192.168.67.100
  32. }
  33. track_script {
  34. check_nginx
  35. }
  36. }

interval 2 表示检查的间隔为 2 秒;

weight -30 表示权重为 -30;

fall 3 表示在连续 3 次检查失败后认为服务不可用;

rise 2 表示在连续 2 次检查成功后认为服务恢复正常;

timeout 2 表示脚本执行的超时时间为 2 秒

  1. #监控Nginx服务,确保在Nginx服务出现问题时,Keepalived不会将流量路由到这个不健康的节点上
  2. vim /etc/keepalived/nginx_check.sh
  3. killall -0 nginx
  4. #该命令实际上并不会杀死任何进程,而是用来检查是否存在名为 nginx 的进程,并验证进程是否仍在运行
  5. #如果命令成功执行并且没有报错,说明存在名为 nginx 的进程在运行;如果命令执行失败或者没有找到对应的进程,那么可能 nginx 进程并未在运行
  6. #使用信号0来检查进程的存在性是一种常见的技巧,因为它不会对进程产生影响,只是用来做检查
  1. #!/bin/bash
  2. # used to realise the keepalived detection to nginx
  3. NUM=`ps -ef| grep nginx | grep -v "grep"| grep -v "check"|wc -l`
  4. echo $NUM
  5. if [ $NUM -ne 2 ];then
  6. systemctl stop keepalived
  7. fi

systemctl restart keepalived.service

浏览器访问虚拟IP

http://192.168.67.100

模拟故障

  1. systemctl stop nginx
  2. hostname -I

故障恢复

  1. systemctl start nginx
  2. ip a

(5)

iptables防火墙服务器,设置双网卡,并且配置SNAT和DNAT转换实现外网客户端可以通过12.0.0.1访问内网的Web服务

添加网卡

点击【虚拟机】,选择【设置】;

点击【添加】,选择【网络适配器】,点击【完成】;

点击【确定】;

启动虚拟机

  1. #修改主机名
  2. hostnamectl set-hostname iptables
  3. su
  4. #关闭防火墙
  5. systemctl stop firewalld.service
  6. systemctl enable firewalld.service
  7. setenforce 0

ifconfig

添加ens36网卡

  1. cd /etc/sysconfig/network-scripts/
  2. ls
  3. cp ifcfg-ens33 ifcfg-ens36
  4. vim ifcfg-ens36
  5. #修改为如下内容
  6. TYPE=Ethernet
  7. DEVICE=ens36
  8. ONBOOT=yes
  9. BOOTPROTO=static
  10. IPADDR=12.0.0.1
  11. NETMASK=255.255.255.0
  12. GATEWAY=12.0.0.1

重启网络

  1. systemctl restart network
  2. vim /etc/sysctl.conf
  3. #末尾添加
  4. net.ipv4.ip_forward = 1
  5. sysctl -p

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

闽ICP备14008679号