nginx-php k8s 配置方式
配置方式一 POD emptyDir:
vim nginx-php-emptyDir-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-php
spec:
containers:
- name: my-nginx
image: nginx:1.15.11
ports:
- containerPort: 80
hostPort: 8080
volumeMounts:
- name: app-www
mountPath: /usr/share/nginx/html
- name: my-php
image: php:7.3-fpm
ports:
- containerPort: 9000
volumeMounts:
- name: app-www
mountPath: /var/www/html
volumes:
- name: app-www
emptyDir: {}
在pod所在的节点创建测试的PHP文件和Nginx配置文件
# 创建data文件夹 mkdir data vi index.php # index.php内容如下 echo "this is php"; vi index.html # 内容如下
# 创建默认的default.conf文件
vi default.conf
# 内容如下 注意我们这里fastcgi_pass用了localhost
server {
listen 80 default;
index index.php index.html index.htm;
server_name localhost;
root /usr/share/nginx/html;
location ~ \.php {
include fastcgi_params;
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
}
}
注意:上述fastcgi_pass的值是 localhost:9000
把测试的文件拷贝到容器中
# 查看nginx容器的名字 docker ps docker cp ./index.php [nginx容器的名称]:/usr/share/nginx/html docker cp ./default.conf [nginx容器的名称]:/etc/nginx/conf.d
docker restart [nginx容器的名称]
验证:
# 外部访问php文件 curl nginxIP:8080/index.php # 分别在nginx 和 php 中执行 curl -i http://127.0.0.1 # 访问成功,证明一个pod中的所有容器网络是共享的,可以用localhost访问到 # 查看php容器中的/var/www/html是否有我们刚上传的index.php文件 docker exec -it 你的php容器的名字 /bin/bash # 进入容器后执行 cd /var/www/html ls # 我们发现了我们刚上传到此目录的index.php文件, # 所以nginx的/usr/share/nginx/html
目录和PHP的/var/www/html目录通过volume:emptyDir已经完成了共享
配置方式二 POD hostPath: 重启所有 pod的方式之一:
kubectl replace --force -f nginx-php-hostPath-pod.yaml
vim nginx-php-hostPath-pod.yaml
apiVersion: v1 kind: Pod metadata: name: nginx-php spec: containers: - name: my-nginx image: nginx:1.15.11 ports: - containerPort: 80 hostPort: 8080 volumeMounts: - name: app-www mountPath: /usr/share/nginx/html - name: app-conf mountPath: /etc/nginx/conf.d - name: my-php image: php:7.3-fpm ports: - containerPort: 9000 volumeMounts: - name: app-www mountPath: /var/www/html volumes: - name: app-www hostPath: path: /root/nginx-php/www-data - name: app-conf hostPath: path: /root/nginx-php/conf
需要在指定机器上指定配置文件:
需将配置文件放到指定的目录: 类似docker-compose
/root/nginx-php/www-data
/root/nginx-php/conf
使用 kubectl get pod -o wide 查看状态:

如果出现 ContainerCreating 可以使用以下命令查看详情
kubectl describe pod nginx-php
[192.168.1.104] root ➜ ~/nginx-php nginx-php kubectl describe pod nginx-php
Name: nginx-php
Namespace: default
Node: worker2/192.168.1.106
Start Time: Sun, 30 Jan 2022 14:55:54 +0800
Labels: <none>
Status: Pending
IP:
Controllers: <none>
Containers:
my-nginx:
Container ID:
Image: nginx:1.15.11
Image ID:
Port: 80/TCP
State: Waiting
Reason: ContainerCreating
Ready: False
Restart Count: 0
Volume Mounts:
/etc/nginx/conf.d from app-conf (rw)
/usr/share/nginx/html from app-www (rw)
Environment Variables: <none>
my-php:
Container ID:
Image: php:8.1-fpm
Image ID:
Port: 9000/TCP
State: Waiting
Reason: ContainerCreating
Ready: False
Restart Count: 0
Volume Mounts:
/var/www/html from app-www (rw)
Environment Variables: <none>
Conditions:
Type Status
Initialized True
Ready False
PodScheduled True
Volumes:
app-www:
Type: HostPath (bare host directory volume)
Path: /root/nginx-php/www-data
app-conf:
Type: HostPath (bare host directory volume)
Path: /root/nginx-php/conf
QoS Class: BestEffort
Tolerations: <none>
Events:
FirstSeen LastSeen Count From SubObjectPath Type Reason Message
--------- -------- ----- ---- ------------- -------- ------ -------
5m 5m 1 {default-scheduler } Normal Scheduled Successfully assigned nginx-php to worker2
5m 5m 2 {kubelet worker2} Warning MissingClusterDNS kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
5m 5m 1 {kubelet worker2} spec.containers{my-nginx} Normal Pulled Container image "nginx:1.15.11" already present on machine
5m 5m 1 {kubelet worker2} spec.containers{my-nginx} Normal Created Created container with docker id 745f8ed540d9; Security:[seccomp=unconfined]
5m 5m 1 {kubelet worker2} spec.containers{my-nginx} Normal Started Started container with docker id 745f8ed540d9
5m 5m 1 {kubelet worker2} spec.containers{my-php} Normal Pulling pulling image "php:8.1-fpm"
这里的原因是: pulling image "php:8.1-fpm"
5m 5m 1 {kubelet worker2} spec.containers{my-php} Normal Pulling pulling image "php:8.1-fpm"
配置方式三 service + deployment:deployment yaml:
apiVersion: extensions/v1beta1 kind: Deployment #指定资源类型 metadata: name: nginx-deployment labels: app: nginx-deployment spec: replicas: 2 selector: matchLabels: app: nginx-deployment template: metadata: labels: app: nginx-deployment spec: containers: - name: my-nginx image: nginx:alpine #定义镜像,这个地方指定版本alpine ports: - containerPort: 80 volumeMounts: - name: app-www mountPath: /usr/share/nginx/html - name: app-conf mountPath: /etc/nginx/conf.d - name: my-php image: php:7.3.33-fpm ports: - containerPort: 9000 volumeMounts: - name: app-www mountPath: /var/www/html volumes: - name: app-www hostPath: path: /root/nginx-php/www-data - name: app-conf hostPath: path: /root/nginx-php/conf
service.yaml
apiVersion: v1 kind: Service metadata: name: my-service labels: app: my-service spec: selector: app: nginx-deployment ports: - protocol: TCP port: 80 targetPort: 80 nodePort: 30088 #节点端口,外部通过节点ip加端口可访问 type: NodePort
nginx 和 php 的配置文件参考 配置方式二
使用命令查看所有状态:
kubectl get all
or
kubectl get all -o wide
