- Create a service to expose this app, the service type must be
NodePort , nodePort should be 30012
2.) Create a config map nginx-config for nginx.conf as we want to add some custom settings for nginx.conf.
a) Change default port 80 to 8091 in nginx.conf.
b) Change default document root /usr/share/nginx to /var/www/html in nginx.conf.
c) Update directory index to index index.html index.htm index.php in nginx.conf.
3.) Create a pod named nginx-phpfpm .
b) Create a shared volume shared-files that will be used by both containers (nginx and phpfpm) also it should be a emptyDir volume.
c) Map the ConfigMap we declared above as a volume for nginx container. Name the volume as nginx-config-volume , mount path should be /etc/nginx/nginx.conf and subPath should be nginx.conf
d) Nginx container should be named as nginx-container and it should use nginx:latest image. PhpFPM container should be named as php-fpm-container and it should use php:7.1-fpm image.
e) The shared volume shared-files should be mounted at /var/www/html location in both containers. Copy /opt/index.php from jump host to the nginx document root inside nginx container, once done you can access the app using App button on the top bar.
my yaml file:
# respective /var/www/html directories.
# Create a service named nginx-php-service to expose the app on nodePort.
apiVersion: v1
kind: Service
metadata:
name: nginx-phpfpm-service
spec:
type: NodePort
ports:
- port: 8080
targetPort: 80
nodePort: 30012
---
# First, create a ConfigMap whose contents are used
# as the nginx.conf file in the web server.
# This server uses /var/www/html as its
# root document directory. When the server gets a
# request for *.php, it will forward that request
# to our PHP-FPM container.
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
nginx.conf: |
events {
}
http {
log_format main
'remote_addr:$remote_addr\t'
'time_local:$time_local\t'
'method:$request_method\t'
'uri:$request_uri\t'
'host:$host\t'
'status:$status\t'
'bytes_sent:$body_bytes_sent\t'
'referer:$http_referer\t'
'useragent:$http_user_agent\t'
'forwardedfor:$http_x_forwarded_for\t'
'request_time:$request_time';
access_log /var/log/nginx/access.log main;
server {
listen 8091;
server_name localhost;
root /var/www/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}
}
---
# Create a pod containing the PHP-FPM application (my-php-app)
# and nginx, each mounting the `shared-files` volume to their
# respective /var/www/html directories.
apiVersion: v1
kind: Pod
metadata:
name: nginx-phpfpm
spec:
volumes:
# Create the shared files volume to be used in both pods.
- name: shared-files
emptyDir: {}
# Add the ConfigMap we declared above as a volume for the pod.
- name: nginx-config-volume
configMap:
name: nginx-config
containers:
# Our nginx container, which uses the configuration declared above,
# along with the files shared with the PHP-FPM app.
- name: nginx-container
image: nginx:latest
volumeMounts:
- name: shared-files
mountPath: /var/www/html
- name: nginx-config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
ports:
- containerPort: 80
# Our php-fpm application
- name: php-fpm-container
image: php:7.2-fpm
volumeMounts:
- name: shared-files
mountPath: /var/www/html
ports:
- containerPort: 80
When I try to access the app, it’s 502 Bad Gateway.
I think I misunderstood something on port, targetPort, nodePort, containerPort because I can’t expose the app as requested but I can access the index page into the pod.
Thanks for your help.
Because after changing the default port 80 to 8091. Nginx application will start to listen on 8091 instead of 80. A wrong port is mentioned in the pod and service YAML.
Regards,
Hi @Tej-Singh-Rana,
Thanks for your answer.
It was my first config, but obviously it wasnt working. Basically, it has to work with that config, but thing is strange… I think that maybe the 30012 is closed by a service or stuff like that so I’ll check.
If anyone of you have any idea, please let me know.
new_config_updated:
# Create a pod containing the php-based application and nginx, each mounting the 'shared-files' volume to their
# respective /var/www/html directories.
# Create a service named nginx-php-service to expose the app on nodePort.
apiVersion: v1
kind: Service
metadata:
name: nginx-phpfpm-service
spec:
type: NodePort
ports:
- port: 8091
targetPort: 8091
nodePort: 30012
---
# First, create a ConfigMap whose contents are used
# as the nginx.conf file in the web server.
# This server uses /var/www/html as its
# root document directory. When the server gets a
# request for *.php, it will forward that request
# to our PHP-FPM container.
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
nginx.conf: |
events {
}
http {
log_format main
'remote_addr:$remote_addr\t'
'time_local:$time_local\t'
'method:$request_method\t'
'uri:$request_uri\t'
'host:$host\t'
'status:$status\t'
'bytes_sent:$body_bytes_sent\t'
'referer:$http_referer\t'
'useragent:$http_user_agent\t'
'forwardedfor:$http_x_forwarded_for\t'
'request_time:$request_time';
access_log /var/log/nginx/access.log main;
server {
listen 8091;
server_name localhost;
root /var/www/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}
}
---
# Create a pod containing the PHP-FPM application (my-php-app)
# and nginx, each mounting the `shared-files` volume to their
# respective /var/www/html directories.
apiVersion: v1
kind: Pod
metadata:
name: nginx-phpfpm
spec:
volumes:
# Create the shared files volume to be used in both pods.
- name: shared-files
emptyDir: {}
# Add the ConfigMap we declared above as a volume for the pod.
- name: nginx-config-volume
configMap:
name: nginx-config
containers:
# Our nginx container, which uses the configuration declared above,
# along with the files shared with the PHP-FPM app.
- name: nginx-container
image: nginx:latest
volumeMounts:
- name: shared-files
mountPath: /var/www/html
- name: nginx-config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
ports:
- containerPort: 8091
# Our php-fpm application
- name: php-fpm-container
image: php:7.2-fpm
volumeMounts:
- name: shared-files
mountPath: /var/www/html
ports:
- containerPort: 8091
Also, when I try to curl the node IP adress of the pod with the nodePort (i.e. curl 172.17.0.2:30012), I got no response, so I think the problem still in the port mapping on the yaml file.
Name: nginx-phpfpm
Namespace: default
Priority: 0
Node: kodekloud-control-plane/172.17.0.2
Start Time: Mon, 04 Jul 2022 21:49:19 +0000
Labels: <none>
Annotations: <none>
Status: Running
IP: 10.244.0.6
IPs:
IP: 10.244.0.6
Containers:
nginx-container:
Container ID: containerd://a2d6270524f6930a8439aba9bd3b906e20bd6da201407674a62df46d6eb30aee
Image: nginx:latest
Image ID: docker.io/library/nginx@sha256:10f14ffa93f8dedf1057897b745e5ac72ac5655c299dade0aa434c71557697ea
Port: 8091/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 04 Jul 2022 21:49:21 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/etc/nginx/nginx.conf from nginx-config-volume (rw,path="nginx.conf")
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xxv48 (ro)
/var/www/html from shared-files (rw)
php-fpm-container:
Container ID: containerd://f6c0fba3844559c2919813e4d5bd483b4b2bfbdb50d1537fe079937bd8e76096
Image: php:7.2-fpm
Image ID: docker.io/library/php@sha256:9c84ae47fddb97b94d1d2e289635b7306142a5336bc4ece0a393458c5e0d2cef
Port: 8091/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 04 Jul 2022 21:49:21 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xxv48 (ro)
/var/www/html from shared-files (rw)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
shared-files:
Type: EmptyDir (a temporary directory that shares a pod's lifetime)
Medium:
SizeLimit: <unset>
nginx-config-volume:
Type: ConfigMap (a volume populated by a ConfigMap)
Name: nginx-config
Optional: false
default-token-xxv48:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xxv48
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 9m8s default-scheduler Successfully assigned default/nginx-phpfpm to kodekloud-control-plane
Normal Pulling 9m7s kubelet Pulling image "nginx:latest"
Normal Pulled 9m7s kubelet Successfully pulled image "nginx:latest" in 366.995419ms
Normal Created 9m7s kubelet Created container nginx-container
Normal Started 9m6s kubelet Started container nginx-container
Normal Pulled 9m6s kubelet Container image "php:7.2-fpm" already present on machine
Normal Created 9m6s kubelet Created container php-fpm-container
Normal Started 9m6s kubelet Started container php-fpm-container
thor@jump_host ~$ curl 172.17.0.2:30012
I finally fixed it. Indeed, my service can’t join my pods only with port. So, I had to specify labels.
Hi @mndiayepro97 and @Tej-Singh-Rana,
I am having similar issue as well. Can anyone post a reference yaml file with the final fixes for us to compare?