千家信息网

PHP环境中如何使用k8s集群

发表于:2024-11-20 作者:千家信息网编辑
千家信息网最后更新 2024年11月20日,本篇文章为大家展示了PHP环境中如何使用k8s集群,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1、下载wordpresswget https://cn.wo
千家信息网最后更新 2024年11月20日PHP环境中如何使用k8s集群

本篇文章为大家展示了PHP环境中如何使用k8s集群,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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 -pmount -t nfs -o vers=4,minorversion=0,noresvport 12XXXXXXXxx.cn-hongkong.nas.aliyuncs.com:/    /datamv 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-alpineLABEL maintainer="zhang 1232@qq.com"COPY default.conf /etc/nginx/conf.d/EXPOSE 80CMD ["nginx", "-g", "daemon off;"]#构建镜像(没有镜像仓库的话,要把镜像导入到其它服务器上)[root@k8s-m Dockerfile]# docker build -t mynginx:2.0 ./#查看镜像[root@k8s-m Dockerfile]# docker images|grep mynginxmynginx                              2.0                 2fd9a2724422        2 hours ago         17.7MB复制代码

四、配置nginx和php

1、创建与导入php的svc和deploy

[root@k8s-m ~]# cat  php.yamlapiVersion: v1kind: Servicemetadata:  name: php-svcspec:  selector:    name: php  ports:  - port: 9000    name: http-php    targetPort: 9000    protocol: TCP ---apiVersion: apps/v1kind: Deploymentmetadata:  name: my-php-deployspec:  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.yamlapiVersion: v1kind: Servicemetadata:  name: nginx-svcspec:  selector:     name: nginx  ports:  - port: 80     name: http     targetPort: 80     protocol: TCP  ---apiVersion: apps/v1kind: Deploymentmetadata:  name: my-nginx-deployspec:  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)    AGEkubernetes   ClusterIP   10.96.0.1       none        443/TCP    4h25mnginx-svc    ClusterIP   10.101.81.167   none        80/TCP     99mphp-svc      ClusterIP   10.111.89.228   none        9000/TCP   99m[root@k8s-m ~]# kubectl get deploy NAME              READY   UP-TO-DATE   AVAILABLE   AGEmy-nginx-deploy   3/3     3            3           99mmy-php-deploy     3/3     3            3           100m复制代码

4、创建Ingress访问

[root@k8s-m ~]# cat wordpress-ingress.yaml apiVersion: extensions/v1beta1kind: Ingressmetadata:  name: ingress-nginxspec:  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.0sha256:c562ad539630b3c5eb6888f0b7bac937d9d3af1d39de118106c5e6ca30a02ebd[root@node1 ~]# docker imagesREPOSITORY                                  TAG                 IMAGE ID            CREATED             SIZEphp-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.iniroot@9e7fa8e57db7:/var/www/html# cat  /usr/local/etc/php/conf.d/docker-php-ext-gd.ini extension=gd.soroot@9e7fa8e57db7:/var/www/html# php -m|grep gdgd复制代码

6、redis之类的扩展安装

curl -L -o redis-4.1.1.tar.gz   https://github.com/phpredis/phpredis/archive/4.1.1.tar.gztar xf redis-4.1.1.tar.gz rm redis-4.1.1.tar.gz mv phpredis-4.1.1    /usr/src/php/ext/redisdocker-php-ext-configure  redisdocker-php-ext-install redisdocker-php-ext-enable redis复制代码

##查看

root@9d5d4e093dbd:/var/www/html# php -m|grep redisredis

上述内容就是PHP环境中如何使用k8s集群,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

0