k8s集群PHP环境使用
<h2>一、环境介绍</h2><p><span style="font-size: 18px">k8s版本: 1.15.2</span></p>
<p><span style="font-size: 18px">存储: 阿里云NAS</span></p>
<p><span style="font-size: 18px">测试代码: wordpress</span></p>
<p> </p>
<h2>二、下载wordpress和创建好数据库等</h2>
<p> </p>
<h3><span style="font-size: 18px">1、下载wordpress</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">wget</span> https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">cn.wordpress.org/latest-zh_CN.zip</span></pre>
</div>
<h3><span style="font-size: 18px">2、创建数据库(数据库我使用yum下载的,数据库尽量不要部署在k8s集群中)</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">create database wordpress DEFAULT CHARACTER SET utf8;
grant all on wordpress.</span>* to <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">wordpress</span><span style="color: rgba(128, 0, 0, 1)">'</span>@<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">%</span><span style="color: rgba(128, 0, 0, 1)">'</span> identified by <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">123456</span><span style="color: rgba(128, 0, 0, 1)">'</span>;</pre>
</div>
<p> </p>
<h3><span style="font-size: 18px">3、把wordpress代码放入到NAS存储中</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">mkdir</span> /data -<span style="color: rgba(0, 0, 0, 1)">p
</span><span style="color: rgba(0, 0, 255, 1)">mount</span> -t nfs -o vers=<span style="color: rgba(128, 0, 128, 1)">4</span>,minorversion=<span style="color: rgba(128, 0, 128, 1)">0</span>,noresvport 12XXXXXXXxx.cn-hongkong.nas.aliyuncs.com:/ /<span style="color: rgba(0, 0, 0, 1)">data
</span><span style="color: rgba(0, 0, 255, 1)">mv</span> wordpress /data/</pre>
</div>
<p> </p>
<h2>三、写dockerfile和构建镜像(我这自己写的nginx镜像,挂载配置或者使用secret的方法也能更改配置)</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">mkdir</span> -<span style="color: rgba(0, 0, 0, 1)">p Dockerfile
# <span style="color: rgba(0, 0, 255, 1)">cat</span><span style="color: rgba(0, 0, 0, 1)"> default.conf
server {
listen </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">;
server_name localhost;
location </span>/<span style="color: rgba(0, 0, 0, 1)"> {
root </span>/usr/share/nginx/<span style="color: rgba(0, 0, 0, 1)">html;
indexindex.html index.htm;
}
error_page </span><span style="color: rgba(128, 0, 128, 1)">500</span> <span style="color: rgba(128, 0, 128, 1)">502</span> <span style="color: rgba(128, 0, 128, 1)">503</span> <span style="color: rgba(128, 0, 128, 1)">504</span>/<span style="color: rgba(0, 0, 0, 1)">50x.html;
location </span>= /<span style="color: rgba(0, 0, 0, 1)">50x.html {
root </span>/usr/share/nginx/<span style="color: rgba(0, 0, 0, 1)">html;
}
location </span>~<span style="color: rgba(0, 0, 0, 1)"> \.php$ {
root </span>/var/www/<span style="color: rgba(0, 0, 0, 1)">html;
fastcgi_pass php</span>-svc.default.svc.cluster.local:<span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)">;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# <span style="color: rgba(0, 0, 255, 1)">cat</span><span style="color: rgba(0, 0, 0, 1)"> Dockerfile
FROM nginx:</span><span style="color: rgba(128, 0, 128, 1)">1.15</span>.<span style="color: rgba(128, 0, 128, 1)">4</span>-<span style="color: rgba(0, 0, 0, 1)">alpine
LABEL maintainer</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zhang 1232@qq.com</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
COPY default.conf </span>/etc/nginx/conf.d/<span style="color: rgba(0, 0, 0, 1)">
EXPOSE </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">
CMD [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-g</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">daemon off;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">]
#构建镜像(没有镜像仓库的话,要把镜像导入到其它服务器上)
# docker build -t mynginx:<span style="color: rgba(128, 0, 128, 1)">2.0</span> ./<span style="color: rgba(0, 0, 0, 1)">
#查看镜像
# docker images|<span style="color: rgba(0, 0, 255, 1)">grep</span><span style="color: rgba(0, 0, 0, 1)"> mynginx
mynginx </span><span style="color: rgba(128, 0, 128, 1)">2.0</span> 2fd9a2724422 <span style="color: rgba(128, 0, 128, 1)">2</span> hours ago <span style="color: rgba(128, 0, 128, 1)">17</span>.7MB</pre>
</div>
<p> </p>
<h2>四、配置nginx和php</h2>
<h3><span style="font-size: 18px">1、创建与导入php的svc和deploy</span></h3>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">cat</span><span style="color: rgba(0, 0, 0, 1)">php.yaml
apiVersion: v1
kind: Service
metadata:
name: php</span>-<span style="color: rgba(0, 0, 0, 1)">svc
spec:
selector:
name: php
ports:
</span>- port: <span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)">
name: http</span>-<span style="color: rgba(0, 0, 0, 1)">php
targetPort: </span><span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)">
protocol: TCP
</span>---<span style="color: rgba(0, 0, 0, 1)">
apiVersion: apps</span>/<span style="color: rgba(0, 0, 0, 1)">v1
kind: Deployment
metadata:
name: my</span>-php-<span style="color: rgba(0, 0, 0, 1)">deploy
spec:
replicas: </span><span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">
selector:
matchLabels:
name: php
template:
metadata:
labels:
name: php
spec:
containers:
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: php
image: php:</span><span style="color: rgba(128, 0, 128, 1)">7.2</span>-<span style="color: rgba(0, 0, 0, 1)">fpm
ports:
</span>- name: http-<span style="color: rgba(0, 0, 0, 1)">php
containerPort: </span><span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)">
volumeMounts:
</span>- name: php-<span style="color: rgba(0, 0, 0, 1)">code
mountPath: </span>/var/www/html/<span style="color: rgba(0, 0, 0, 1)">
volumes:
</span>- name: php-<span style="color: rgba(0, 0, 0, 1)">code
nfs:
path: </span>/wordpress/<span style="color: rgba(0, 0, 0, 1)">
server: 12xxxxxxxxx.cn</span>-hongkong.nas.aliyuncs.com</pre>
</div>
<p> </p>
<h3><span style="font-size: 18px">2、创建与导入nginx的svc和deploy</span></h3>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">cat</span> nginx-<span style="color: rgba(0, 0, 0, 1)">deploy.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx</span>-<span style="color: rgba(0, 0, 0, 1)">svc
spec:
selector:
name: nginx
ports:
</span>- port: <span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">
name: http
targetPort: </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">
protocol: TCP
</span>---<span style="color: rgba(0, 0, 0, 1)">
apiVersion: apps</span>/<span style="color: rgba(0, 0, 0, 1)">v1
kind: Deployment
metadata:
name: my</span>-nginx-<span style="color: rgba(0, 0, 0, 1)">deploy
spec:
replicas: </span><span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">
selector:
matchLabels:
name: nginx
template:
metadata:
labels:
name: nginx
spec:
containers:
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: nginx
image: mynginx:</span><span style="color: rgba(128, 0, 128, 1)">2.0</span><span style="color: rgba(0, 0, 0, 1)">
imagePullPolicy: IfNotPresent
ports:
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: http
containerPort: </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">
volumeMounts:
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: html
mountPath: </span>/usr/share/nginx/html/<span style="color: rgba(0, 0, 0, 1)">
volumes:
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: html
nfs:
path: </span>/wordpress/<span style="color: rgba(0, 0, 0, 1)">
server: 124xxxxxxxxxxxxx</span>-hongkong.nas.aliyuncs.com</pre>
</div>
<p> </p>
<h3><span style="font-size: 18px">3、查看</span></h3>
<div class="cnblogs_code">
<pre># kubectl get svc
NAME TYPE CLUSTER</span>-IP EXTERNAL-<span style="color: rgba(0, 0, 0, 1)">IP PORT(S) AGE
kubernetes ClusterIP </span><span style="color: rgba(128, 0, 128, 1)">10.96</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span> <none> <span style="color: rgba(128, 0, 128, 1)">443</span>/<span style="color: rgba(0, 0, 0, 1)">TCP 4h15m
nginx</span>-svc ClusterIP <span style="color: rgba(128, 0, 128, 1)">10.101</span>.<span style="color: rgba(128, 0, 128, 1)">81.167</span> <none> <span style="color: rgba(128, 0, 128, 1)">80</span>/<span style="color: rgba(0, 0, 0, 1)">TCP 99m
php</span>-svc ClusterIP <span style="color: rgba(128, 0, 128, 1)">10.111</span>.<span style="color: rgba(128, 0, 128, 1)">89.228</span> <none> <span style="color: rgba(128, 0, 128, 1)">9000</span>/<span style="color: rgba(0, 0, 0, 1)">TCP 99m
# kubectl get deploy
NAME READY UP</span>-TO-<span style="color: rgba(0, 0, 0, 1)">DATE AVAILABLE AGE
my</span>-nginx-deploy <span style="color: rgba(128, 0, 128, 1)">3</span>/<span style="color: rgba(128, 0, 128, 1)">3</span> <span style="color: rgba(128, 0, 128, 1)">3</span> <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)"> 99m
my</span>-php-deploy <span style="color: rgba(128, 0, 128, 1)">3</span>/<span style="color: rgba(128, 0, 128, 1)">3</span> <span style="color: rgba(128, 0, 128, 1)">3</span> <span style="color: rgba(128, 0, 128, 1)">3</span> 100m</pre>
</div>
<h3><span style="font-size: 18px">4、创建Ingress访问</span></h3>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">cat</span> wordpress-<span style="color: rgba(0, 0, 0, 1)">ingress.yaml
apiVersion: extensions</span>/<span style="color: rgba(0, 0, 0, 1)">v1beta1
kind: Ingress
metadata:
name: ingress</span>-<span style="color: rgba(0, 0, 0, 1)">nginx
spec:
rules:
</span>-<span style="color: rgba(0, 0, 0, 1)"> host: haha.zhang.com
http:
paths:
</span>-<span style="color: rgba(0, 0, 0, 1)"> backend:
serviceName: nginx</span>-<span style="color: rgba(0, 0, 0, 1)">svc
servicePort: </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">
# kubectlapply -f wordpress-<span style="color: rgba(0, 0, 0, 1)">ingress.yaml
ingress.extensions</span>/ingress-nginx created</pre>
</div>
<h3><span style="font-size: 18px">5、访问测试</span></h3>
<p><span style="font-size: 18px"><img src="https://img2018.cnblogs.com/blog/1242171/201908/1242171-20190815142705025-1727925037.png" alt=""></span></p>
<p> </p>
<h3> </h3>
<h2>五、PHP扩展安装</h2>
<p><span style="font-size: 18px">php容器中的ini扩展文件路径:/usr/local/etc/php/conf.d/</span></p>
<h3><span style="font-size: 18px">1、进入php容器中</span></h3>
<div class="cnblogs_code">
<pre># docker run -it--name php-gdphp:<span style="color: rgba(128, 0, 128, 1)">7.2</span>-fpm bash</pre>
</div>
<h3><span style="font-size: 18px">2、下载依赖</span></h3>
<div class="cnblogs_code">
<pre>apt-get update && apt-get <span style="color: rgba(0, 0, 255, 1)">install</span>libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng-dev -y</pre>
</div>
<h3><span style="font-size: 18px">3、安装扩展(例如GD)</span></h3>
<div class="cnblogs_code">
<pre>docker-php-ext-<span style="color: rgba(0, 0, 0, 1)">configure gd
#安装
docker</span>-php-ext-<span style="color: rgba(0, 0, 255, 1)">install</span><span style="color: rgba(0, 0, 0, 1)"> gd
#启用
docker</span>-php-ext-enable gd</pre>
</div>
<h3><span style="font-size: 18px">4、将容器保存为新的镜像</span></h3>
<div class="cnblogs_code">
<pre># docker commit -p php-gd php-gd:<span style="color: rgba(128, 0, 128, 1)">1.0</span><span style="color: rgba(0, 0, 0, 1)">
sha256:c562ad539630b3c5eb6888f0b7bac937d9d3af1d39de118106c5e6ca30a02ebd
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php</span>-gd <span style="color: rgba(128, 0, 128, 1)">1.0</span> c562ad539630 <span style="color: rgba(128, 0, 128, 1)">4</span> seconds ago 426MB</pre>
</div>
<h3><span style="font-size: 18px">5、运行新php容器测试</span></h3>
<div class="cnblogs_code">
<pre># docker run -it --<span style="color: rgba(0, 0, 255, 1)">rm</span>php-gd:<span style="color: rgba(128, 0, 128, 1)">1.0</span><span style="color: rgba(0, 0, 0, 1)"> bash
root@9e7fa8e57db7:</span>/var/www/html# <span style="color: rgba(0, 0, 255, 1)">ls</span> /usr/local/etc/php/conf.d/docker-php-ext-<span style="color: rgba(0, 0, 0, 1)">gd.ini
</span>/usr/local/etc/php/conf.d/docker-php-ext-<span style="color: rgba(0, 0, 0, 1)">gd.ini
root@9e7fa8e57db7:</span>/var/www/html# <span style="color: rgba(0, 0, 255, 1)">cat</span>/usr/local/etc/php/conf.d/docker-php-ext-<span style="color: rgba(0, 0, 0, 1)">gd.ini
extension</span>=<span style="color: rgba(0, 0, 0, 1)">gd.so
root@9e7fa8e57db7:</span>/var/www/html# php -m|<span style="color: rgba(0, 0, 255, 1)">grep</span><span style="color: rgba(0, 0, 0, 1)"> gd
gd</span></pre>
</div>
<p> </p>
<h3><span style="font-size: 18px">6、redis之类的扩展安装</span></h3>
<h3><span style="font-size: 18px"> </span></h3>
<div class="cnblogs_code">
<pre>curl -L -o redis-<span style="color: rgba(128, 0, 128, 1)">4.1</span>.<span style="color: rgba(128, 0, 128, 1)">1</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">github.com/phpredis/phpredis/archive/4.1.1.tar.gz</span>
<span style="color: rgba(0, 0, 255, 1)">tar</span> xf redis-<span style="color: rgba(128, 0, 128, 1)">4.1</span>.<span style="color: rgba(128, 0, 128, 1)">1</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span><span style="color: rgba(0, 0, 0, 1)">.gz
</span><span style="color: rgba(0, 0, 255, 1)">rm</span> redis-<span style="color: rgba(128, 0, 128, 1)">4.1</span>.<span style="color: rgba(128, 0, 128, 1)">1</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span><span style="color: rgba(0, 0, 0, 1)">.gz
</span><span style="color: rgba(0, 0, 255, 1)">mv</span> phpredis-<span style="color: rgba(128, 0, 128, 1)">4.1</span>.<span style="color: rgba(128, 0, 128, 1)">1</span> /usr/src/php/ext/<span style="color: rgba(0, 0, 0, 1)">redis
docker</span>-php-ext-<span style="color: rgba(0, 0, 0, 1)">configureredis
docker</span>-php-ext-<span style="color: rgba(0, 0, 255, 1)">install</span><span style="color: rgba(0, 0, 0, 1)"> redis
docker</span>-php-ext-enable redis</pre>
</div>
<p>##查看</p>
<div class="cnblogs_code">
<pre>root@9d5d4e093dbd:/var/www/html# php -m|<span style="color: rgba(0, 0, 255, 1)">grep</span><span style="color: rgba(0, 0, 0, 1)"> redis
redis</span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/zhangb8042/p/11357736.html
頁:
[1]