日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

k8s php mysql_在k8s上部署第一个php应用

發布時間:2023/12/31 数据库 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 k8s php mysql_在k8s上部署第一个php应用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一.搭建nginx+php

1.站點配置文件

1.1創建nginx-configmap.yaml

[root@master k8s]# cat nginx-configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: nginx-config data: default.conf: | server { listen 80; server_name localhost; root /usr/share/nginx/html; access_log /var/log/nginx/host_access.log; error_log /var/log/nginx/host_error.log debug; location / { root /usr/share/nginx/html; index index.html index.htm index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass 10.254.235.214:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

ps:

10.254.235.214: 是下文中配置的php-service服務的集群ip

1.2 應用該配置文件

#kubectl create -f nginx-configmap.yaml

2.nginx

2.1創建nginx-deployment.yaml

[root@master k8s]# cat nginx-deployment.yaml apiVersion: v1 kind: Service metadata: name: nginx-service spec: type: NodePort selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 --- apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deployment spec: selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:alpine volumeMounts: - name: nginx-config mountPath: /etc/nginx/conf.d - name: web-root mountPath: /usr/share/nginx/html volumes: - name: nginx-config configMap: name: nginx-config - name: web-root nfs: server: 192.168.2.17 path: /data/nfs

ps:請提前搭建好nfs環境,nfs中的server為局域網ip

2.2 應用該配置文件

#kubectl create -f nginx-configmap.yaml

ps:

因為使用了NodePort類型,現在你可以在外部通過任意節點服務器ip+端口訪問到nginx服務

你可以可以通過pod上的ip來訪問到nginx服務

3.php

3.1 php-deployment.yaml

[root@master k8s]# cat php-deployment.yaml apiVersion: v1 kind: Service metadata: name: php-service spec: clusterIP: 10.254.235.214 selector: app: php ports: - name: php port: 9000 targetPort: 9000 --- apiVersion: extensions/v1beta1 kind: Deployment metadata: name: php-deployment spec: replicas: 1 selector: matchLabels: app: php template: metadata: labels: app: php spec: containers: - name: php image: php:7.2-fpm volumeMounts: - name: web-root mountPath: /usr/share/nginx/html volumes: - name: web-root nfs: server: 192.168.2.17 path: /data/nfs

3.2 應用該配置文件

#kubectl create -f php-configmap.yaml

二.創建mysql服務

1.一般我們將數據庫放在單獨的物理服務器上或另一個集群上,我們創建一個mysql-service.yaml

[root@master]# cat mysql-service.yaml apiVersion: v1 kind: Service metadata: name: mysql-service namespace: default spec: ports: - protocol: TCP port: 3306 targetPort: 3306 [root@master]#kubectl create -f mysql-service.yaml

2.創建同名的endpoint,這樣子service可以使用endpoint

[root@master]# cat mysql-endpoint.yaml apiVersion: v1 kind: Endpoints metadata: name: mysql-service namespace: default subsets: - addresses: - ip: 192.168.2.10 ports: - port: 3306 protocol: TCP [root@master]#kubectl create -f mysql-endpoint.yaml

三.應用程序文件

1.一個簡單的php程序文件:

[root@master]#cat b.php <?php $dbms='mysql'; $host= getenv('MYSQL_SERVICE_SERVICE_HOST'); // 獲得環境變量,對應php-service;可以進入pod容器內使用env查看 $dbName='test'; $user='test'; $pass='1983512gx'; $dsn="$dbms:host=$host;dbname=$dbName"; try { $dbh = new PDO($dsn, $user, $pass); echo "連接成功
"; $dbh = null; } catch (PDOException $e) { die ("Error!: " . $e->getMessage() . "
"); }

2.運行該文件,提示

Error!: could not find driver

這是因為用到了pdo,但php-fpm2鏡像并沒有這個擴展

3.安裝php-pdo擴展(更好的辦法是你應該制作一個運行php環境的容器)

kubectl exec -it php-deployment-3540934081-75sqv -- /usr/local/bin/docker-php-ext-install pdo_mysql

4.重啟節點上對應的php 容器

docker restart 4cf7949cfc30

ps:

實際操作中在容器內無法連同service,重啟了節點的docker才可以.

四.訪問

在任意節點上或master上訪問

#curl http://192.168.2.18:31746/b.php

應該輸出連接成功

總結

以上是生活随笔為你收集整理的k8s php mysql_在k8s上部署第一个php应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。