一.搭建nginx+php
1.站点配置文件
1.1创建nginx-configmap.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | [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 应用该配置文件
1 | #kubectl create -f nginx-configmap.yaml |
2.nginx
2.1创建nginx-deployment.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | [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 应用该配置文件
1 | #kubectl create -f nginx-configmap.yaml |
ps:
因为使用了NodePort类型,现在你可以在外部通过任意节点服务器ip+端口访问到nginx服务
你可以可以通过pod上的ip来访问到nginx服务
3.php
3.1 php-deployment.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | [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 应用该配置文件
1 | #kubectl create -f php-configmap.yaml |
二.创建mysql服务
1.一般我们将数据库放在单独的物理服务器上或另一个集群上,我们创建一个mysql-service.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 | [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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [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程序文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [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 "连接成功<br/>" ; $dbh = null ; } catch (PDOException $e) { die ( "Error!: " . $e->getMessage() . "<br/>" ); } |
2.运行该文件,提示
Error!: could not find driver
这是因为用到了pdo,但php-fpm2镜像并没有这个扩展
3.安装php-pdo扩展(更好的办法是你应该制作一个运行php环境的容器)
1 | kubectl exec -it php-deployment-3540934081-75sqv -- /usr/local/bin/docker-php-ext-install pdo_mysql |
4.重启节点上对应的php 容器
1 | docker restart 4cf7949cfc30 |
ps:
实际操作中在容器内无法连同service,重启了节点的docker才可以.
四.访问
在任意节点上或master上访问
1 | #curl http://192.168.2.18:31746/b.php |
应该输出连接成功
所有评论(0)