部署项目
一、先部署mariadb
二、在远程登录工具上进行登录测试,端口号为30117,用户为 root,密码为123
三、使用测试工具:
[root@k8s-master aaa]# kubectl exec -it pods/cluster-test0-58689d5d5d-7c49r -- bash
四、部署wordpress
[root@k8s-master aaa]# vim wordpress-con
apiVersion: v1
kind: ConfigMap
metadata:
name:wordpress-config
data:
NAME:"db"
USER:"wp"
PASSWORD:"123"
HOST:"mariadb-server"
[root@k8s-master aaa]# kubectl create -f wordpress-con
con/wordpress-con
[root@k8s-master aaa]# kubectl get cm
NAME DATA AGE
kube-root-ca.crt 1 12d
mariadb-cm 4 3d2h
mariadb-con4 50m
wordpress-cm 4 3d
wordpress-con4 38s
[root@k8s-master aaa]# vim wordpress-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-deployment
labels:
app: wordpress-deployment
spec:
replicas: 2
selector:
matchLabels:
app: wordpress-deployment
template:
metadata:
labels:
app: wordpress-deployment
spec:
containers:
- name: wp
image: docker.io/library/wordpress:latest
imagePullPolicy: Never
ports:
- name: wordpressport
containerPort: 80
envFrom:
- pre: "WORDPRESS_DB_"
con:
name: wordpress-con
[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-965670/1 ImagePullBackO0 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-deployment
ports:
- name: http
port: 80
targetPort: 80
nodePort: 32000
protocol: TCP
type: 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#53
Name: 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 distribution
Copyright (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_sc h e m a |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
MariaDB [(none)]> use db
Database changed
# 表是空的
MariaDB [db]> show table s ;
E m p t y s e t ( 0.0 0 s e c )
七 、 在 访 问 到 的 页 面 进 行 登 录
192.168.2.66:32000
八 、 d b 数 据 库 中 就 有 数 据 了
九、在远程登录工具中也可以看到数据
十、安装 Ingress Contorller
十一、下载附件再导入到服务器内,再进行安装
[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
con/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
validatingwebhookcon/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
后续论坛网站自行搭建