-
-
[原创]学习Kubernetes笔记——部署web站点环境(PHP+Nginx)
-
发表于: 2022-12-31 14:46 20753
-
进入k8s集群主节点:10.10.30.201
因为官方的php-fpm
镜像没有MySQL插件,所以需要自己制作一个有插件的镜像
创建一个Dockerfile
文件
生成镜像
因为K8S在创建容器的时候,是从docker hub上拉取镜像的,所以需要将镜像上传到docker仓库
这样就可以从docker hub上拉取自己制作好的添加了mysql插件的php-fpm镜像了
web环境为:php-fpm+nginx
使用2个镜像:ai3666191/php-fpm:ameng
和nginx
编写web.yaml
文件
执行yaml文件
在nfs创建一个测试文件
查看pod运行状态及svc暴露端口
浏览器访问节点IP:端口号
访问,这里的访问地址为10.10.30.201:31041
出现PHP页面及web环境搭建成功。
Jenkins将站点源码从gitlab拉取到NFS服务器挂载的站点路径并赋予源码文件相应权限后
访问10.10.30.201:31041
出现如下界面
接下来配置数据库
因为同属集群内,Nginx与MySQL可以使用CLUSTER-IP
相互通信,所以数据库服务地址填写集群内mysql的CLUSTER-IP
如果是集群外的Nginx,地址应该填10.10.30.201:30336
等进度条走完就可以访问站点了
FROM php:
7.4
-
fpm
RUN apt
-
get update && apt
-
get install
-
y \
libfreetype6
-
dev \
libjpeg62
-
turbo
-
dev \
libpng
-
dev \
&& docker
-
php
-
source extract \
&& docker
-
php
-
ext
-
configure gd
-
-
with
-
freetype
-
-
with
-
jpeg \
&& docker
-
php
-
ext
-
install
-
j$(nproc) gd \
&& docker
-
php
-
ext
-
install pdo_mysql \
&& docker
-
php
-
ext
-
install mysqli
FROM php:
7.4
-
fpm
RUN apt
-
get update && apt
-
get install
-
y \
libfreetype6
-
dev \
libjpeg62
-
turbo
-
dev \
libpng
-
dev \
&& docker
-
php
-
source extract \
&& docker
-
php
-
ext
-
configure gd
-
-
with
-
freetype
-
-
with
-
jpeg \
&& docker
-
php
-
ext
-
install
-
j$(nproc) gd \
&& docker
-
php
-
ext
-
install pdo_mysql \
&& docker
-
php
-
ext
-
install mysqli
docker build
-
t ai3666191
/
php
-
fpm:ameng .
docker build
-
t ai3666191
/
php
-
fpm:ameng .
[root@m1 yaml]
# docker login
Login with your Docker
ID
to push
and
pull images
from
Docker Hub. If you don't have a Docker
ID
, head over to https:
/
/
hub.docker.com to create one.
Username: ai3666191
Password:
WARNING! Your password will be stored unencrypted
in
/
root
/
.docker
/
config.json.
Configure a credential helper to remove this warning. See
https:
/
/
docs.docker.com
/
engine
/
reference
/
commandline
/
login
/
#credentials-store
Login Succeeded
[root@m1 yaml]
# docker push ai3666191/php-fpm:ameng
The push refers to repository [docker.io
/
ai3666191
/
php
-
fpm]
338933207958
: Pushed
5e65a6c61859
: Pushed
d78098596d78: Pushed
7c314756ee72
: Pushed
89982c6135ad
: Pushed
91fd2792fa74
: Pushed
08cc615b0242
: Pushed
44148371c697
: Pushed
797a7c0590e0
: Pushed
f60117696410: Pushed
ec4a38999118: Pushed
ameng: digest: sha256:
9767e38c793bdb77b96d2f9bdc921feb319e7b61504f3d2144628ebdd6a4b640
size:
2622
[root@m1 yaml]
# docker login
Login with your Docker
ID
to push
and
pull images
from
Docker Hub. If you don't have a Docker
ID
, head over to https:
/
/
hub.docker.com to create one.
Username: ai3666191
Password:
WARNING! Your password will be stored unencrypted
in
/
root
/
.docker
/
config.json.
Configure a credential helper to remove this warning. See
https:
/
/
docs.docker.com
/
engine
/
reference
/
commandline
/
login
/
#credentials-store
Login Succeeded
[root@m1 yaml]
# docker push ai3666191/php-fpm:ameng
The push refers to repository [docker.io
/
ai3666191
/
php
-
fpm]
338933207958
: Pushed
5e65a6c61859
: Pushed
d78098596d78: Pushed
7c314756ee72
: Pushed
89982c6135ad
: Pushed
91fd2792fa74
: Pushed
08cc615b0242
: Pushed
44148371c697
: Pushed
797a7c0590e0
: Pushed
f60117696410: Pushed
ec4a38999118: Pushed
ameng: digest: sha256:
9767e38c793bdb77b96d2f9bdc921feb319e7b61504f3d2144628ebdd6a4b640
size:
2622
kind: Service
# 对象类型
apiVersion: v1
# api 版本
metadata:
# 元数据
name: php
-
fpm
-
nginx
#Service 服务名
spec:
type
: NodePort
# 类型为nodeport
selector:
#标签选择器
app: php
-
fpm
-
nginx
ports:
#端口信息
-
port:
80
# 容器端口80
protocol: TCP
#tcp类型
targetPort:
80
# Service 将 nginx 容器的 80 端口暴露出来
-
-
-
kind: ConfigMap
# 对象类型
apiVersion: v1
# api 版本
metadata:
# 元数据
name: nginx
-
config
# 对象名称
data:
# key-value 数据集合
nginx.conf: |
# 将 nginx config 配置写入 ConfigMap 中,经典的 php-fpm 代理设置,这里就不再多说了
user nginx;
worker_processes auto;
error_log
/
var
/
log
/
nginx
/
error.log notice;
pid
/
var
/
run
/
nginx.pid;
events {
worker_connections
1024
;
}
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;
keepalive_timeout
65
;
server {
listen
80
default_server;
listen [::]:
80
default_server;
root
/
var
/
www
/
html;
index index.php index.html;
server_name _;
if
(
-
f $request_filename
/
index.html) {
rewrite (.
*
) $
1
/
index.html
break
;
}
if
(
-
f $request_filename
/
index.php) {
rewrite (.
*
) $
1
/
index.php;
}
if
(!
-
f $request_filename) {
rewrite (.
*
)
/
index.php;
}
location
/
{
try_files $uri $uri
/
=
404
;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass
127.0
.
0.1
:
9000
;
}
}
include
/
etc
/
nginx
/
conf.d
/
*
.conf;
}
-
-
-
kind: StatefulSet
# 对象类型
apiVersion: apps
/
v1
# api 版本
metadata:
# 元数据
name: php
-
fpm
-
nginx
# StatefulSet 对象名称
spec:
# StatefulSet 对象规约
selector:
# 选择器
matchLabels:
# 标签匹配
app: php
-
fpm
-
nginx
replicas:
3
# 副本数量
template:
# 模版
metadata:
# Pod 对象的元数据
labels:
# Pod 对象的标签
app: php
-
fpm
-
nginx
spec:
# Pod 对象规约
containers:
# 这里设置了两个容器
-
name: php
-
fpm
# 第一个容器名称
image: ai3666191
/
php
-
fpm:ameng
# 容器镜像
imagePullPolicy: IfNotPresent
#镜像拉取策略
livenessProbe:
# 存活探测
initialDelaySeconds:
5
# 容器启动后要等待多少秒后才启动存活和就绪探测器
periodSeconds:
10
# 每多少秒执行一次存活探测
tcpSocket:
# 监测tcp端口
port:
9000
#监测端口
readinessProbe:
# 就绪探测
initialDelaySeconds:
5
# 容器启动后要等待多少秒后才启动存活和就绪探测器
periodSeconds:
10
# 每多少秒执行一次存活探测
tcpSocket:
# 监测tcp端口
port:
9000
#监测端口
resources:
# 资源约束
requests:
# 最小限制
memory:
"64Mi"
# 内存最新64M
cpu:
"250m"
# CPU最大使用0.25核
limits:
# 最大限制
memory:
"128Mi"
# 内存最新128M
cpu:
"500m"
# CPU最大使用0.5核
ports:
-
containerPort:
9000
# php-fpm 端口
volumeMounts:
# 挂载数据卷
-
mountPath:
/
var
/
www
/
html
# 挂载两个容器共享的 volume
name: nginx
-
www
lifecycle:
# 生命周期
postStart:
# 当容器处于 postStart 阶段时,执行一下命令
exec
:
command: [
"/bin/sh"
,
"-c"
,
"echo startup..."
]
# 将 /app/index.php 复制到挂载的 volume
preStop:
exec
:
command:
-
sh
-
'-c'
-
sleep
5
&& kill
-
SIGQUIT
1
# 优雅退出
-
name: nginx
# 第二个容器名称
image: nginx
# 容器镜像
imagePullPolicy: IfNotPresent
livenessProbe:
# 存活探测
initialDelaySeconds:
5
# 容器启动后要等待多少秒后才启动存活和就绪探测器
periodSeconds:
10
# 每多少秒执行一次存活探测
httpGet:
# 以httpGet方式进行探测
path:
/
# 探测路径
port:
80
# 探测端口
readinessProbe:
# 就绪探测
initialDelaySeconds:
5
# 容器启动后要等待多少秒后才启动存活和就绪探测器
periodSeconds:
10
# 每多少秒执行一次存活探测
httpGet:
# 以httpGet方式进行探测
path:
/
# 探测路径
port:
80
# 探测端口
resources:
# 资源约束
requests:
# 最小限制
memory:
"64Mi"
# 内存最新64M
cpu:
"250m"
# CPU最大使用0.25核
limits:
# 最大限制
memory:
"128Mi"
# 内存最新128M
cpu:
"500m"
# CPU最大使用0.5核
ports:
-
containerPort:
80
# nginx 端口
volumeMounts:
# nginx 容器挂载了两个 volume,一个是与 php-fpm 容器共享的 volume,另外一个是配置了 nginx.conf 的 volume
-
mountPath:
/
var
/
www
/
html
# 挂载两个容器共享的 volume
name: nginx
-
www
-
mountPath:
/
etc
/
nginx
/
nginx.conf
# 挂载配置了 nginx.conf 的 volume
subPath: nginx.conf
name: nginx
-
config
lifecycle:
preStop:
exec
:
command:
-
sh
-
'-c'
-
sleep
5
&&
/
usr
/
sbin
/
nginx
-
s quit
# 优雅退出
volumes:
-
name: nginx
-
www
# 网站文件通过nfs挂载
nfs:
path:
/
k8s
/
html
/
server: nfs
-
name: nginx
-
config
configMap:
# configMap
name: nginx
-
config
kind: Service
# 对象类型
apiVersion: v1
# api 版本
metadata:
# 元数据
name: php
-
fpm
-
nginx
#Service 服务名
spec:
type
: NodePort
# 类型为nodeport
selector:
#标签选择器
app: php
-
fpm
-
nginx
ports:
#端口信息
-
port:
80
# 容器端口80
protocol: TCP
#tcp类型
targetPort:
80
# Service 将 nginx 容器的 80 端口暴露出来
-
-
-
kind: ConfigMap
# 对象类型
apiVersion: v1
# api 版本
metadata:
# 元数据
name: nginx
-
config
# 对象名称
data:
# key-value 数据集合
nginx.conf: |
# 将 nginx config 配置写入 ConfigMap 中,经典的 php-fpm 代理设置,这里就不再多说了
user nginx;
worker_processes auto;
error_log
/
var
/
log
/
nginx
/
error.log notice;
pid
/
var
/
run
/
nginx.pid;
events {
worker_connections
1024
;
}
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;
keepalive_timeout
65
;
server {
listen
80
default_server;
listen [::]:
80
default_server;
root
/
var
/
www
/
html;
index index.php index.html;
server_name _;
if
(
-
f $request_filename
/
index.html) {
rewrite (.
*
) $
1
/
index.html
break
;
}
if
(
-
f $request_filename
/
index.php) {
rewrite (.
*
) $
1
/
index.php;
}
if
(!
-
f $request_filename) {
rewrite (.
*
)
/
index.php;
}
location
/
{
try_files $uri $uri
/
=
404
;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass
127.0
.
0.1
:
9000
;
}
}
include
/
etc
/
nginx
/
conf.d
/
*
.conf;
}
-
-
-
kind: StatefulSet
# 对象类型
apiVersion: apps
/
v1
# api 版本
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)
最后于 2022-12-31 15:28
被阿蒙i编辑
,原因:
赞赏记录
参与人
雪币
留言
时间
伟叔叔
为你点赞~
2023-3-18 00:02
一笑人间万事
为你点赞~
2023-1-9 09:41
99JW99
为你点赞~
2022-12-31 17:01
阿蒙i
为你点赞~
2022-12-31 16:49
赞赏
他的文章
看原图
赞赏
雪币:
留言: