9.24-k8s服务发布

server/2024/9/29 3:19:56/

Ingress

使用域名发布 K8S 服务

部署项目

一、先部署mariadb

[root@k8s-master ~]# mkdir aaa
[root@k8s-master ~]# cd aaa/
[root@k8s-master aaa]# # 先部署mariadb
[root@k8s-master aaa]# # configmap
[root@k8s-master aaa]# vim mariadb-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: mariadb-configmap
data:USER: "wp"PASSWORD: "123"ROOT_PASSWORD: "123"DATABASE: "db"[root@k8s-master aaa]# kubectl create -f mariadb-configmap.yaml 
configmap/mariadb-configmap created
[root@k8s-master aaa]# # deployment
[root@k8s-master aaa]# vim mariadb.deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: mariadb-deploymentlabels:app: mariadb-deployment
spec:replicas: 1selector:matchLabels:app: mariadb-deploymenttemplate:metadata:labels:app: mariadb-deploymentspec:containers:-       name: mariadbimage: docker.io/library/mariadb:latestimagePullPolicy: Neverports:-       name: mariadbportcontainerPort: 3306envFrom:-       prefix: "MARIADB_"configMapRef:name: mariadb-configmap[root@k8s-master aaa]# kubectl create -f mariadb.deployment.yaml 
deployment.apps/mariadb-deployment created
[root@k8s-master aaa]# kubectl get pod
NAME                                  READY   STATUS             RESTARTS      AGE
cluster-test0-58689d5d5d-7c49r        1/1     Running            4 (16m ago)   3d2h
haha-96567ff6f-r2mh5                  0/1     ImagePullBackOff   0             3d2h
mariadb-deployment-5bf6d9f98c-9mddb   1/1     Running            0             22s
wordpress-7695bd58f4-42hx2            1/1     Running            1 (16m ago)   2d23h
wordpress-7695bd58f4-dqp8q            1/1     Running            1 (16m ago)   2d23h
wordpress-7695bd58f4-v8j7l            1/1     Running            1 (16m ago)   2d23h
[root@k8s-master aaa]# kubectl get po -o wide
NAME                                  READY   STATUS             RESTARTS      AGE     IP              NODE         NOMINATED NODE   READINESS GATES
cluster-test0-58689d5d5d-7c49r        1/1     Running            4 (20m ago)   3d2h    172.16.58.193   k8s-node02   <none>           <none>
haha-96567ff6f-r2mh5                  0/1     ImagePullBackOff   0             3d2h    172.16.85.234   k8s-node01   <none>           <none>
mariadb-deployment-5bf6d9f98c-9mddb   1/1     Running            0             4m34s   172.16.85.237   k8s-node01   <none>           <none>
wordpress-7695bd58f4-42hx2            1/1     Running            1 (20m ago)   2d23h   172.16.58.255   k8s-node02   <none>           <none>
wordpress-7695bd58f4-dqp8q            1/1     Running            1 (20m ago)   2d23h   172.16.85.233   k8s-node01   <none>           <none>
wordpress-7695bd58f4-v8j7l            1/1     Running            1 (20m ago)   2d23h   172.16.85.235   k8s-node01   <none>           <none>[root@k8s-master aaa]# mysql -h 172.16.85.237 -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 11.5.2-MariaDB-ubu2404 mariadb.org binary distributionCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> exit
Bye[root@k8s-master aaa]# vim mariadb-service.yaml
[root@k8s-master aaa]# kubectl create -f mariadb-service.yaml 
service/mariadb-service created
[root@k8s-master aaa]# kubectl get svc
NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes        ClusterIP   10.96.0.1       <none>        443/TCP          12d
mariadb-service   NodePort    10.96.148.212   <none>        3306:30117/TCP   15s
[root@k8s-master aaa]# kubectl get pod
NAME                                  READY   STATUS             RESTARTS      AGE
cluster-test0-58689d5d5d-7c49r        1/1     Running            4 (33m ago)   3d2h
haha-96567ff6f-r2mh5                  0/1     ImagePullBackOff   0             3d2h
mariadb-deployment-5bf6d9f98c-9mddb   1/1     Running            0             17m
wordpress-7695bd58f4-42hx2            1/1     Running            1 (33m ago)   2d23h
wordpress-7695bd58f4-dqp8q            1/1     Running            1 (33m ago)   2d23h
wordpress-7695bd58f4-v8j7l            1/1     Running            1 (33m ago)   2d23h

二、在远程登录工具上进行登录测试,端口号为30117,用户为root,密码为123

三、使用测试工具:

[root@k8s-master aaa]# kubectl exec -it pods/cluster-test0-58689d5d5d-7c49r -- bash

四、部署wordpress

[root@k8s-master aaa]# vim wordpress-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: wordpress-config
data:NAME: "db"USER: "wp"PASSWORD: "123"HOST: "mariadb-service"[root@k8s-master aaa]# kubectl create -f wordpress-configmap.yaml 
configmap/wordpress-config created
[root@k8s-master aaa]# kubectl get cm
NAME                DATA   AGE
kube-root-ca.crt    1      12d
mariadb-cm          4      3d2h
mariadb-configmap   4      50m
wordpress-cm        4      3d
wordpress-config    4      38s
[root@k8s-master aaa]# vim wordpress-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: wordpress-deploymentlabels:app: wordpress-deployment
spec:replicas: 2selector:matchLabels:app: wordpress-deploymenttemplate:metadata:labels:app: wordpress-deploymentspec:containers:-       name: wpimage: docker.io/library/wordpress:latestimagePullPolicy: Neverports:-       name: wordpressportcontainerPort: 80envFrom:-        prefix: "WORDPRESS_DB_"configMapRef:name: wordpress-config[root@k8s-master aaa]# kubectl create -f wordpress-deployment.yaml 
deployment.apps/wordpress-deployment created
[root@k8s-master aaa]# kubectl get pod
NAME                                    READY   STATUS             RESTARTS        AGE
cluster-test0-58689d5d5d-7c49r          1/1     Running            5 (9m35s ago)   3d3h
haha-96567ff6f-r2mh5                    0/1     ImagePullBackOff   0               3d3h
mariadb-deployment-5bf6d9f98c-9mddb     1/1     Running            0               54m
wordpress-7695bd58f4-42hx2              1/1     Running            1 (70m ago)     3d
wordpress-7695bd58f4-dqp8q              1/1     Running            1 (70m ago)     3d
wordpress-7695bd58f4-v8j7l              1/1     Running            1 (70m ago)     3d
wordpress-deployment-555685954b-52lbs   1/1     Running            0               15s
wordpress-deployment-555685954b-d8qqz   1/1     Running            0               15s
[root@k8s-master aaa]# vim wordpress-service.yaml
apiVersion: v1
kind: Service
metadata:name: wordpress-deployment
spec:selector:app: wordpress-deploymentports:-       name: httpport: 80targetPort: 80nodePort: 32000protocol: TCPtype: NodePort[root@k8s-master aaa]# kubectl create -f wordpress-service.yaml 
service/wordpress-deployment created
[root@k8s-master aaa]# kubectl get svc
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes             ClusterIP   10.96.0.1       <none>        443/TCP          12d
mariadb-service        NodePort    10.96.148.212   <none>        3306:30117/TCP   46m
wordpress-deployment   NodePort    10.96.26.205    <none>        80:32000/TCP     1s

五、浏览器访问本机地址: 192.168.2.66:32000

六、查看db数据中的表

表中是空的

# 使用测试工具测试
[root@k8s-master aaa]# kubectl exec -it cluster-test0-58689d5d5d-7c49r -- bash
(01:10 cluster-test0-58689d5d5d-7c49r:/) nslookup mariadb-service
Server:		10.96.0.10
Address:	10.96.0.10#53Name:	mariadb-service.default.svc.cluster.local
Address: 10.96.148.212(01:10 cluster-test0-58689d5d5d-7c49r:/) exit
exit
您在 /var/spool/mail/root 中有新邮件# 查看节点
[root@k8s-master aaa]# kubectl get po
NAME                                    READY   STATUS    RESTARTS      AGE
cluster-test0-58689d5d5d-7c49r          1/1     Running   6 (21m ago)   3d18h
mariadb-deployment-5bf6d9f98c-9mddb     1/1     Running   1 (21m ago)   16h
wordpress-7695bd58f4-42hx2              1/1     Running   2 (21m ago)   3d16h
wordpress-7695bd58f4-dqp8q              1/1     Running   2 (21m ago)   3d16h
wordpress-7695bd58f4-v8j7l              1/1     Running   2 (21m ago)   3d16h
wordpress-deployment-555685954b-52lbs   1/1     Running   1 (21m ago)   15h
wordpress-deployment-555685954b-d8qqz   1/1     Running   1 (21m ago)   15h# 查看结点的信息
[root@k8s-master aaa]# kubectl get po -o wide
NAME                                    READY   STATUS    RESTARTS      AGE     IP              NODE         NOMINATED NODE   READINESS GATES
cluster-test0-58689d5d5d-7c49r          1/1     Running   6 (22m ago)   3d18h   172.16.58.200   k8s-node02   <none>           <none>
mariadb-deployment-5bf6d9f98c-9mddb     1/1     Running   1 (22m ago)   16h     172.16.85.240   k8s-node01   <none>           <none>
wordpress-7695bd58f4-42hx2              1/1     Running   2 (22m ago)   3d16h   172.16.58.201   k8s-node02   <none>           <none>
wordpress-7695bd58f4-dqp8q              1/1     Running   2 (22m ago)   3d16h   172.16.85.243   k8s-node01   <none>           <none>
wordpress-7695bd58f4-v8j7l              1/1     Running   2 (22m ago)   3d16h   172.16.85.242   k8s-node01   <none>           <none>
wordpress-deployment-555685954b-52lbs   1/1     Running   1 (22m ago)   15h     172.16.58.198   k8s-node02   <none>           <none>
wordpress-deployment-555685954b-d8qqz   1/1     Running   1 (22m ago)   15h     172.16.85.241   k8s-node01   <none>           <none># 登录数据库
[root@k8s-master aaa]# mysql -h 172.16.85.240 -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 11.5.2-MariaDB-ubu2404 mariadb.org binary distributionCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db                 |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)MariaDB [(none)]> use db
Database changed# 表是空的
MariaDB [db]> show tables;
Empty set (0.00 sec)

七、在访问到的页面进行登录

192.168.2.66:32000

八、db数据库中就有数据了

MariaDB [db]> show tables;
+-----------------------+
| Tables_in_db          |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.00 sec)MariaDB [db]> select * wp_users;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'wp_users' at line 1
MariaDB [db]> select * from wp_users;
+----+------------+------------------------------------+---------------+------------+---------------------------+---------------------+---------------------+-------------+--------------+
| ID | user_login | user_pass                          | user_nicename | user_email | user_url                  | user_registered     | user_activation_key | user_status | display_name |
+----+------------+------------------------------------+---------------+------------+---------------------------+---------------------+---------------------+-------------+--------------+
|  1 | haha       | $P$B9wSyumd047LAk6T9sM5oO7G8IhnsS. | haha          | 123@qq.com | http://192.168.2.66:32000 | 2024-09-24 01:15:54 |                     |           0 | haha         |
+----+------------+------------------------------------+---------------+------------+---------------------------+---------------------+---------------------+-------------+--------------+
1 row in set (0.00 sec)MariaDB [db]> exit
Bye

九、在远程登录工具中也可以看到数据

十、安装 Ingress Contorller

注册 · 语雀 (yuque.com)](注册 · 语雀)

十一、下载附件再导入到服务器内,再进行安装

[root@k8s-master ~]# vim ingress.yaml
[root@k8s-master ~]# kubectl create -f ingress.yaml 
namespace/ingress-nginx created
serviceaccount/ingress-nginx created
serviceaccount/ingress-nginx-admission created
role.rbac.authorization.k8s.io/ingress-nginx created
role.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrole.rbac.authorization.k8s.io/ingress-nginx created
clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission created
rolebinding.rbac.authorization.k8s.io/ingress-nginx created
rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
configmap/ingress-nginx-controller created
service/ingress-nginx-controller created
service/ingress-nginx-controller-admission created
deployment.apps/ingress-nginx-controller created
job.batch/ingress-nginx-admission-create created
job.batch/ingress-nginx-admission-patch created
ingressclass.networking.k8s.io/nginx created
validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission created
[root@k8s-master ~]# kubectl get po -n ingress-nginx 
NAME                                        READY   STATUS      RESTARTS   AGE
ingress-nginx-admission-create-6hj4c        0/1     Completed   0          71s
ingress-nginx-admission-patch-bt7mj         0/1     Completed   0          71s
ingress-nginx-controller-674f66cf96-lhg8z   1/1     Running     0          72s
[root@k8s-master ~]# kubectl describe  pod -n ingress-nginx ingress-nginx-controller-674f66cf96-lhg8z 
[root@k8s-master ~]# kubectl get service -A
NAMESPACE              NAME                                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
default                kubernetes                           ClusterIP   10.96.0.1       <none>        443/TCP                      12d
default                mariadb-service                      NodePort    10.96.148.212   <none>        3306:30117/TCP               17h
default                wordpress-deployment                 NodePort    10.96.26.205    <none>        80:32000/TCP                 16h
ingress-nginx          ingress-nginx-controller             NodePort    10.96.124.77    <none>        80:32540/TCP,443:32218/TCP   4m7s
ingress-nginx          ingress-nginx-controller-admission   ClusterIP   10.96.175.242   <none>        443/TCP                      4m7s
kube-system            kube-dns                             ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP       12d
kube-system            metrics-server                       ClusterIP   10.96.212.31    <none>        443/TCP                      11d
kubernetes-dashboard   dashboard-metrics-scraper            ClusterIP   10.96.51.222    <none>        8000/TCP                     11d
kubernetes-dashboard   kubernetes-dashboard                 NodePort    10.96.242.161   <none>        443:30754/TCP                11d
[root@k8s-master ~]# cd pods/
# 创建ingress
[root@k8s-master pods]# vim test0054.yaml
[root@k8s-master pods]# kubectl create -f test0054.yaml 
ingress.networking.k8s.io/nginx-ingress created
[root@k8s-master pods]# kubectl get service -A
NAMESPACE              NAME                                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
default                kubernetes                           ClusterIP   10.96.0.1       <none>        443/TCP                      12d
default                mariadb-service                      NodePort    10.96.148.212   <none>        3306:30117/TCP               21h
default                wordpress-service                    NodePort    10.96.126.255   <none>        80:32000/TCP                 114m
ingress-nginx          ingress-nginx-controller             NodePort    10.96.124.77    <none>        80:32540/TCP,443:32218/TCP   3h42m
ingress-nginx          ingress-nginx-controller-admission   ClusterIP   10.96.175.242   <none>        443/TCP                      3h42m
kube-system            kube-dns                             ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP       12d
kube-system            metrics-server                       ClusterIP   10.96.212.31    <none>        443/TCP                      12d
kubernetes-dashboard   dashboard-metrics-scraper            ClusterIP   10.96.51.222    <none>        8000/TCP                     12d
kubernetes-dashboard   kubernetes-dashboard                 NodePort    10.96.242.161   <none>        443:30754/TCP                12d

十二、访问测试

(1)修改hosts文件

(2)测试ip

访问 IP+Ingress 映射的端口是无法进入后端服务器的

(3)只有访问先前定义的域名+端口才可访问到后端服务器

本次实战域名服务器为:wp-web.com:30080

后续论坛网站自行搭建


http://www.ppmy.cn/server/123293.html

相关文章

速盾:高防cdn防御的时候会封ip吗?

高防CDN&#xff08;Content Delivery Network&#xff09;是一种用于提供网站或应用程序防护和内容分发的服务。它通过将内容分发到全球多个服务器上&#xff0c;帮助用户提高网站的访问速度和可用性&#xff0c;并提供强大的防护功能&#xff0c;包括防DDoS攻击、Web应用程序…

《凡人歌》中的IT职业启示录

《凡人歌》是由中央电视台、正午阳光、爱奇艺出品&#xff0c;简川訸执导&#xff0c;纪静蓉编剧&#xff0c;侯鸿亮任制片&#xff0c;殷桃、王骁领衔主演&#xff0c;章若楠、秦俊杰、张哲华、陈昊宇主演的都市话题剧 &#xff0c;改编自纪静蓉的小说《我不是废柴》。该剧于2…

Linux编译安装Mysql笔记

1.Mysql介绍 MySQL是一个广泛使用的开源关系型数据库管理系统&#xff08;RDBMS&#xff09;&#xff0c;它基于SQL&#xff08;Structured Query Language&#xff09;进行操作。MySQL是由瑞典MySQL AB公司开发的&#xff0c;后来被Sun Microsystems收购&#xff0c;最终成为…

C语言自定义类型:联合体

目录 前言一、联合体1.1 联合体类型的声明1.2 联合体的特点1.3 相同成员的结构体和联合体对比1.4 联合体大小的计算1.5 联合体的⼀个练习 总结 前言 前面我讲到C语言中的自定义结构——结构体&#xff0c;其实C语言中的自定义结构不只有结构体&#xff0c;还有枚举和联合体&am…

实战C++手写线程池

课程总目录 文章目录 一、项目必备基础概念1.1 并发和并行1.2 多线程的优势1.3 线程的消耗1.4 线程池的优势1.5 线程池的两种模式:fixed模式和cached模式1.6 线程同步之线程互斥1.7 线程同步之线程通信1.7.1 条件变量1.7.2 信号量1.8 项目设计图浏览二、线程池代码展示三、线程…

PyQt5 statusbar 放图片并设置图片大小和左右间距

在 PyQt5 中&#xff0c;状态栏&#xff08;QStatusBar&#xff09;通常用于显示窗口的状态信息或提示。虽然 PyQt5 的 QStatusBar 没有直接提供设置图片作为状态栏项&#xff08;QStatusBarItem&#xff09;的 API&#xff0c;但你可以通过添加一个 QWidget&#xff08;如 QLa…

长列表加载性能优化

一、长列表优化概述 列表是应用开发中最常见的一类开发场景&#xff0c;它可以将杂乱的信息整理成有规律、易于理解和操作的形式&#xff0c;便于用户查找和获取所需要的信息。应用程序中常见的列表场景有新闻列表、购物车列表、各类排行榜等。随着信息数据的累积&#xff0c;特…

WPF入门教学十八 动画入门

WPF&#xff08;Windows Presentation Foundation&#xff09;是微软推出的一种用于创建Windows客户端应用程序的用户界面框架。WPF 提供了丰富的动画支持&#xff0c;可以通过XAML或者代码来实现各种动画效果。以下是一个简单的WPF动画入门教学&#xff0c;我们将使用XAML来创…