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