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 的页面

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/850390.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java | Leetcode Java题解之第139题单词拆分

题目&#xff1a; 题解&#xff1a; public class Solution {public boolean wordBreak(String s, List<String> wordDict) {Set<String> wordDictSet new HashSet(wordDict);boolean[] dp new boolean[s.length() 1];dp[0] true;for (int i 1; i < s.len…

JimuReport 积木报表 v1.7.52 版本发布,免费的低代码报表

项目介绍 一款免费的数据可视化报表工具&#xff0c;含报表和大屏设计&#xff0c;像搭建积木一样在线设计报表&#xff01;功能涵盖&#xff0c;数据报表、打印设计、图表报表、大屏设计等&#xff01; Web 版报表设计器&#xff0c;类似于excel操作风格&#xff0c;通过拖拽完…

智能变电站网络报文记录及故障录波分析装置

是基于Intel X86、PowerPC、FPGA等技术的高度集成化的硬件平台&#xff0c;采用了高性能CPU无风扇散热、网络数据采集、高速数据压缩存储加密等多种技术&#xff0c;实现了高性能计算、多端口同步高速数据采集、数据实时分析、大容量数据存储等功能。 ● 在满足工业标准的同时&…

数据结构 -- 树状数组

前言 树状数组或二叉索引树&#xff08;Binary Indexed Tree&#xff09;&#xff0c;又以其发明者命名为 Fenwick 树。其初衷是解决数据压缩里的累积频率的计算问题&#xff0c;现多用于高效计算数列的前缀和、区间和。它可以以 O(logn) 的时间得到任意前缀和。并同时支持在 …

Kali Linux 2024.2 释出

渗透测试发行版 Kali Linux 释出了最新的 2024.2。 主要新特性包括&#xff1a;桌面环境更新到 GNOME 46&#xff0c;Xfce 环境加入 HiDPI 模式&#xff0c;更新了网络侦察工具 AutoRecon&#xff0c;监视 Linux 进程的命令行工具 pspy&#xff0c;提取和显示 CVE 信息的 Splo…

项目验收总体计划书(实际项目验收原件参考Word)

测试目标&#xff1a;确保项目的需求分析说明书中的所有功能需求都已实现&#xff0c;且能正常运行&#xff1b;确保项目的业务流程符合用户和产品设计要求&#xff1b;确保项目的界面美观、风格一致、易学习、易操作、易理解。 软件全套文档过去进主页。 一、 前言 &#xff0…

Unity 编辑器扩展,获取目录下所有的预制件

先看演示效果 实现方案 1创建几个用于测试的cube 2&#xff0c;创建一个Editor脚本 3&#xff0c;编写脚本内容 附上源码 using UnityEditor; using UnityEngine;public class GetPrefeb : EditorWindow {private string folderPath "Assets/Resources"; // 指定预…

[FSCTF 2023]Tea_apk

得到密文和密钥 import base64 from ctypes import c_uint32import libnumDELTA 0x9E3779B9def decrypt(v, n, k):rounds 6 int(52 / n)sum c_uint32(rounds * DELTA)y v[0].valuewhile rounds > 0:e (sum.value >> 2) & 3p n - 1while p > 0:z v[p …

Django 连接mysql数据库配置

1&#xff0c;提前创建注册的app1应用 Test/Test/settings.py python manage.py startapp app1 2&#xff0c;配置mysql数据库连接 Test/Test/settings.py DATABASES {default: {ENGINE: django.db.backends.mysql,# 数据库名字NAME: db1,# 连接mysql数据库用户名USER: ro…

Python 基于阿里云的OSS对象存储服务实现本地文件上云框架

Python 基于阿里云的OSS对象存储服务实现将文件上云框架 文章目录 Python 基于阿里云的OSS对象存储服务实现将文件上云框架一、前言二、阿里云配置1、获取用户AKEY和AKeySecret2、创建Bucket 三、Python 阿里云oss上云框架1、安装oss2依赖库2、阿里云oss python 一、前言 未来…

2024年CKA模拟系统制作 | step-by-step | 1、基础环境准备

目录 一、软件环境 二、虚拟网络环境准备 1、编辑虚拟网络 2、网络设置 三、新建虚拟主机 1、新建目录 2、新建虚拟主机 四、系统安装 1、装载系统镜像 2、开启虚拟机 3、选择语言 4、键盘选择 5、网络配置 6、代理设置 7、设置软件源 8、存储设置 9、名称设置 …

摆脱Jenkins - 使用google cloudbuild 部署 java service 到 compute engine VM

在之前 介绍 cloud build 的文章中 初探 Google 云原生的CICD - CloudBuild 已经介绍过&#xff0c; 用cloud build 去部署1个 spring boot service 到 cloud run 是很简单的&#xff0c; 因为部署cloud run 无非就是用gcloud 去部署1个 GAR 上的docker image 到cloud run 容…

42【Aseprite 作图】梅花盆栽——拆解

1 花盆 是高度比较低的盆&#xff0c;只有一个下2&#xff1b;上面分两个 1 2 盆边缘颜色深&#xff0c;上面靠近外面的颜色浅&#xff0c;正面颜色稍微深一点&#xff0c;画两条竖的浅色线&#xff0c;作为装饰 2 花盆中的沙石 沙子颜色深一点&#xff0c;中间有浅一点的线…

[office] excel工作表数据分级显示 #其他#笔记

excel工作表数据分级显示 如下图1所示的工作表数据&#xff0c;我们按东区、西区、南区、北区来建立分级显示。 图1 这里先利用“创建组”命令建立分级显示。选取单元格区域A3:E5&#xff0c;单击功能区“数据”选项卡“分级显示”组中的“创建组——创建组…”命令&#xff…

【c语言】qsort函数及泛型冒泡排序的模拟实现

&#x1f31f;&#x1f31f;作者主页&#xff1a;ephemerals__ &#x1f31f;&#x1f31f;所属专栏&#xff1a;C语言 目录 一、qsort函数 1.回调函数 2.qsort函数 3.void* 指针 二、泛型冒泡排序的模拟实现 1.比较函数的编写 2.交换函数的编写 3.冒泡排序的编写 4…

JWT 快速入门

什么是 JWT JSON Web Token&#xff08;JWT&#xff09;是目前最流行的跨域身份验证解决方案 JSON Web Token Introduction - jwt.ioLearn about JSON Web Tokens, what are they, how they work, when and why you should use them.https://jwt.io/introduction 一、常见会…

APP单页分发源码下载安卓苹果自动识别apk描述文件免签自动安装

下载地址&#xff1a;APP单页分发源码下载安卓苹果自动识别apk描述文件免签自动安装

MySQL数据库操作基础(增删查改)

数据库操作基础(增删查改) 1.插入 1.1往数据表中插入一条数据 insert into 表名 values (值,值,值...)此处列出的这些值的数目和类型 要和表的相对应 1.2指定列插入 insert into 表名(列名) values (值);1.3一次插入多个记录 insert into 表名 values (值),(值)...一次插入…

wordpress主题导航主题v4.16.2哈哈版

1.下载授权接口源码onenav-auth-api-v2.zip &#xff0c;在宝塔新建一个网站&#xff0c;域名为 auth.iotheme.cn&#xff0c;设置wordpress伪静态&#xff0c;申请ssl证书。将上面源码解压后上传到此网站根目录。 2. 在宝塔根目录etc下 hosts 中添加 127.0.0.1 auth.iotheme.…

《Brave New Words 》1.1 抛弃瓶子

Part I: Rise of the AI Tutor 第一部分&#xff1a;AI 导师的崛起 A great teacher can teach calculus with a paper clip and literature in an empty field. Technology is just another tool, not a destination. —Unknown 一位伟大的教师可以用回形针教微积分&#xff0…