当前位置: 首页 > news >正文

k8s小型实验模拟

在这里插入图片描述
(1)Kubernetes 区域可采用 Kubeadm 方式进行安装。(5分)
(2)要求在 Kubernetes 环境中,通过yaml文件的方式,创建2个Nginx Pod分别放置在两个不同的节点上,Pod使用hostPath类型的存储卷挂载,节点本地目录共享使用 /data,2个Pod副本测试页面二者要不同,以做区分,测试页面可自己定义。(20分)
(3)编写service对应的yaml文件,使用NodePort类型和TCP 30000端口将Nginx服务发布出去。(10分)
(4)负载均衡区域配置Keepalived+Nginx,实现负载均衡高可用,通过VIP 192.168.10.100和自定义的端口号即可访问K8S发布出来的服务。(20分)
(5)iptables防火墙服务器,设置双网卡,并且配置SNAT和DNAT转换实现外网客户端可以通过12.0.0.1访问内网的Web服务。(10分)
注:编写实验报告,包括实验步骤、实验配置、结果验证截图等。

一 实验环境

192.168.217.99 master01

192.1687.217.66 node01

192.168.217.77 node02

192.168.217.22 ng01 (主负载均衡器)

192.168.217.44 ng02 (备负载均衡器)

192.168.217.55 / 12.0.0.1 iptables (iptables 网关)

12.0.0.12 客户端

二 安装k8s

(1)Kubernetes 区域可采用 Kubeadm 方式进行安装。(5分)

[root@master01 data]#kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   

三 启动两个nginx 实例 pod

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

1,启动第一个 nginx pod

 [root@master01 shiyan]#cat pod2.yaml 
apiVersion: v1  
kind: Pod 
metadata:name: nginx02
spec:containers:- name: nginximage: nginx:1.18.0imagePullPolicy: IfNotPresentports:- name: httpcontainerPort: 80volumeMounts:                       #定义如何在容器内部挂载存储卷- name: web                         #指定要挂载的存储卷的名称,volumes.name字段定义的名称mountPath: /usr/share/nginx/html  #容器内部的目录路径,存储卷将被挂载到这个路径上readOnly: false                   #false表示容器可以读写该存储卷volumes:                              #定义宿主机上的目录文件为Pod中可用的存储卷,- name: web                           #自定义存储卷的名称hostPath:                           #指定存储卷类型为hostPath,path: /data/            #指定宿主机上可以挂载到pod中的目录或文件type: DirectoryOrCreate        

2, 启动第二个nginx pod

[root@master01 shiyan]#cat pod.yaml 
apiVersion: v1  
kind: Pod 
metadata:name: nginx01
spec:containers:- name: nginximage: nginx:1.18.0imagePullPolicy: IfNotPresentports:- name: httpcontainerPort: 80volumeMounts:                       #定义如何在容器内部挂载存储卷- name: web                         #指定要挂载的存储卷的名称,volumes.name字段定义的名称mountPath: /usr/share/nginx/html  #容器内部的目录路径,存储卷将被挂载到这个路径上readOnly: false                   #false表示容器可以读写该存储卷volumes:                              #定义宿主机上的目录文件为Pod中可用的存储卷,- name: web                           #自定义存储卷的名称hostPath:                           #指定存储卷类型为hostPath,path: /data/            #指定宿主机上可以挂载到pod中的目录或文件type: DirectoryOrCreate        

3, nginx01 做页面

[root@master01 data]#kubectl exec -it nginx01  /bin/bashroot@nginx01:/# cd /usr/share/nginx/html/
root@nginx01:/usr/share/nginx/html# echo "this is nginx01"  > index.html

4, nginx02 做页面

[root@master01 data]#kubectl exec -it nginx02  /bin/bashroot@nginx02:/# cd /usr/share/nginx/html/root@nginx02:/usr/share/nginx/html# echo "this is nginx02" > index.html

5, 查看状态

[root@master01 shiyan]#kubectl get pod  -owide
NAME      READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
nginx01   1/1     Running   0          17m   10.244.1.4   node01   <none>           <none>
nginx02   1/1     Running   0          16m   10.244.2.3   node02   <none>           <none>
[root@master01 shiyan]#curl 10.244.2.3
this is nginx02
[root@master01 shiyan]#curl 10.244.1.4
this is nginx01
[root@master01 shiyan]#

四 对外发布

1,用NodePort模式 对外发布

[root@master01 data]#cat nginx.yaml 
apiVersion: v1
kind: Service
metadata:creationTimestamp: nullname: nginx01
spec:ports:- port: 80protocol: TCPtargetPort: 80nodePort: 30000selector:app: nginxtype: NodePort
status:loadBalancer: {}

2, 查看svc

[root@master01 data]#kubectl get svc,pod -owide
NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        21d   <none>
service/nginx01      NodePort    10.96.82.239   <none>        80:30000/TCP   22s   app=nginxNAME          READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
pod/nginx01   1/1     Running   0          44m   10.244.1.4   node01   <none>           <none>
pod/nginx02   1/1     Running   0          43m   10.244.2.3   node02   <none>           <none>

3, 将pod 和svc 通过标签选择器绑定

[root@master01 data]#kubectl get pod --show-labels
NAME      READY   STATUS    RESTARTS   AGE   LABELS
nginx01   1/1     Running   0          48m   <none>
nginx02   1/1     Running   0          47m   <none>
[root@master01 data]#kubectl label pod nginx01 app=nginx
pod/nginx01 labeled
[root@master01 data]#kubectl label pod nginx02 app=nginx
pod/nginx02 labeled
[root@master01 data]#kubectl get pod --show-labels
NAME      READY   STATUS    RESTARTS   AGE   LABELS
nginx01   1/1     Running   0          52m   app=nginx
nginx02   1/1     Running   0          51m   app=nginx

4, 查看效果

[root@master01 data]#curl 192.168.217.66:30000
this is nginx02
[root@master01 data]#curl 192.168.217.66:30000
this is nginx01

五 做负载均衡 和高可用

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

1, nginx 配置文件

将两个node 的对外发布的ip+ 端口 转为指定的30010端口


user  nginx;
worker_processes  auto;error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}stream {log_format  main  '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';access_log  /var/log/nginx/k8s-access.log  main;upstream k8s-apiserver {server 192.168.217.66:30000;server 192.168.217.77:30000;}server {listen 30010;proxy_pass k8s-apiserver;}
}http {include       /etc/nginx/mime.types;default_type  application/octet-stream;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;include /etc/nginx/conf.d/*.conf;
}
~                                                

2, keepalived 配置文件


! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_01vrrp_skip_check_adv_addrvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_script check_down {script "/etc/keepalived/ng.sh"interval 1weight -30fall 3rise 2timeout 2
}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.217.100}track_script {check_down
}}

! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_02vrrp_skip_check_adv_addrvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_script check_down {script "/etc/keepalived/ng.sh"interval 1weight -30fall 3rise 2timeout 2
}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 51priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.217.100}track_script {check_down
}}

3, 检测nginx 脚本

脚本

#!/bin/bash# 检查Nginx进程是否在运行
NGINX_STATUS=$(ps aux | grep '[n]ginx: worker process' | wc -l)# 设置阈值,判断Nginx是否至少有一个工作进程在运行
THRESHOLD=1if [ "$NGINX_STATUS" -ge "$THRESHOLD" ]; thenecho "OK - Nginx is running"exit 0  # 表示服务正常
elseecho "CRITICAL - Nginx is not running"exit 1  # 表示服务有问题
fi
[root@ng02 keepalived]#chmod +x ng.sh 

4, 虚拟ip 飘效果

效果

[root@ng01 keepalived]#systemctl stop  nginx
[root@ng01 keepalived]#hostname -I
192.168.217.22 192.168.217.100 
[root@ng01 keepalived]#hostname -I
192.168.217.22 192.168.217.100 
[root@ng01 keepalived]#hostname -I
192.168.217.22 192.168.217.100 
[root@ng01 keepalived]#hostname -I
192.168.217.22 

5, 效果

访问vip加指定端口 可看到内容

[root@localhost ~]# curl 192.168.217.100:30010
this is nginx02
[root@localhost ~]# curl 192.168.217.100:30010
this is nginx01
[root@localhost ~]# curl 192.168.217.100:30010
this is nginx02
[root@localhost ~]# curl 192.168.217.100:30010
this is nginx01

::

六 iptables网关服务器

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

1,设置双网卡

ens33:

[root@iptables net]#cd /etc/sysconfig/network-scripts/
[root@iptables network-scripts]#cat ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=ens33
UUID=c770d08d-12a0-4e69-9a6c-a5457b33d89c
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.217.55
NETMASK=255.255.255.0
GATEWAY=192.168.217.2
DNS1=8.8.8.8
DNS2=114.114.114.114

ens36

[root@iptables network-scripts]#cat ifcfg-ens36
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=ens36
DEVICE=ens36
ONBOOT=yes
IPADDR=12.0.0.1
NETMASK=255.255.255.0
GATEWAY=12.0.0.1
DNS1=8.8.8.8
DNS2=114.114.114.114

重启网卡

[root@iptables network-scripts]#systemctl restart network

2, 开启路由转发

[root@iptables network-scripts]#cat /etc/sysctl.conf 
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_forward = 1

3, 做iptables 策略

[root@iptables network-scripts]#iptables -t nat -A PREROUTING -i ens36 -d 12.0.0.1 -p tcp --dport 80 -j DNAT --to 192.168.217.100:30010 

查看策略

[root@iptables network-scripts]#iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 163 packets, 14674 bytes)pkts bytes target     prot opt in     out     source               destination         14   840 DNAT       tcp  --  ens36  *       0.0.0.0/0            12.0.0.1             tcp dpt:80 to:192.168.217.100:30010Chain INPUT (policy ACCEPT 28 packets, 4183 bytes)pkts bytes target     prot opt in     out     source               destination         Chain OUTPUT (policy ACCEPT 143 packets, 10701 bytes)pkts bytes target     prot opt in     out     source               destination         Chain POSTROUTING (policy ACCEPT 234 packets, 17504 bytes)pkts bytes target     prot opt in     out     source               destination         

4, 改两个nginx负载均衡器的 网关 为iptables网关服务器的ens33

ng02:

[root@ng02 keepalived]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.217.55  0.0.0.0         UG    100    0        0 ens33
192.168.217.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

ng01:

[root@ng01 keepalived]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.217.55  0.0.0.0         UG    100    0        0 ens33
192.168.217.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

重启网络

5, 准备客户端

改客户端网络 网卡指向12.0.0.1

[root@client network-scripts]#cat ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=12.0.0.12
NETMASK=255.255.255.0
GATEWAY=12.0.0.1
DNS1=8.8.8.8
DNS2=114.114.114.114

重启网络

6, 实验效果

客户端访问12.0.0.1 可以看到后面k8s 中的nginx pod 的页面

[root@client network-scripts]# 
```![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=C%3A%5CUsers%5C%E5%90%B4%E4%BA%91%E9%9D%92%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20240607165533541.png&pos_id=img-OUapC2sJ-1717752224463)92.168.217.55  0.0.0.0         UG    100    0        0 ens33
192.168.217.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

重启网络

5, 准备客户端

改客户端网络 网卡指向12.0.0.1

[root@client network-scripts]#cat ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=12.0.0.12
NETMASK=255.255.255.0
GATEWAY=12.0.0.1
DNS1=8.8.8.8
DNS2=114.114.114.114

重启网络

6, 实验效果

客户端访问12.0.0.1 可以看到后面k8s 中的nginx pod 的页面

在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • mac环境基于llama3和metaGPT自动开发2048游戏
  • HTML静态网页成品作业(HTML+CSS)—— 金宝贝儿童教育机构介绍网页(2个页面)
  • 箭头函数 this
  • 纷享销客安全体系:安全运维运营
  • mysqldump常用备份数据库命令
  • 开发自动发消息插件需要用到的源代码!
  • 5.3 数据模型设计总结
  • OceanBase 4.3 特性解析:列存技术
  • 实现k8s网络互通
  • 物联网8大协议介绍及对比
  • NFT 智能合约实战-快速开始(1)NFT发展历史 | NFT合约标准(ERC-721、ERC-1155和ERC-998)介绍
  • 消息中间件比较:Redis,Kafka和RabbitMQ
  • 排序---归并排序(简单优化前后比较)
  • 网球运动目标检测跟踪
  • 本周MoonBit新增Wasm1引用计数支持、语法即将添加错误恢复机制
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • 【162天】黑马程序员27天视频学习笔记【Day02-上】
  • JavaScript实现分页效果
  • js如何打印object对象
  • Linux快速复制或删除大量小文件
  • tab.js分享及浏览器兼容性问题汇总
  • 翻译:Hystrix - How To Use
  • 译米田引理
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • 策略 : 一文教你成为人工智能(AI)领域专家
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • #100天计划# 2013年9月29日
  • #565. 查找之大编号
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • $().each和$.each的区别
  • (2)nginx 安装、启停
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (poj1.2.1)1970(筛选法模拟)
  • (二)延时任务篇——通过redis的key监听,实现延迟任务实战
  • (离散数学)逻辑连接词
  • (六)什么是Vite——热更新时vite、webpack做了什么
  • (十一)图像的罗伯特梯度锐化
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (一) 初入MySQL 【认识和部署】
  • (一)Java算法:二分查找
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • (转)真正的中国天气api接口xml,json(求加精) ...
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • ******之网络***——物理***
  • *1 计算机基础和操作系统基础及几大协议
  • .NET 8.0 中有哪些新的变化?
  • .NET COER+CONSUL微服务项目在CENTOS环境下的部署实践
  • .net 反编译_.net反编译的相关问题
  • .net6 core Worker Service项目,使用Exchange Web Services (EWS) 分页获取电子邮件收件箱列表,邮件信息字段
  • .Net7 环境安装配置
  • .NET企业级应用架构设计系列之开场白
  • .Net转Java自学之路—SpringMVC框架篇六(异常处理)
  • @serverendpoint注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • [145] 二叉树的后序遍历 js
  • [2016.7 test.5] T1