1

The bottom line is that I need to replace the pod with php code in the kubernetes cluster with a docker container using telepresence, this pod contains 2 containers: php and nginx. I do everything according to the instructions (https://www.telepresence.io/tutorials/php) but it seems kubernetes does not see my container.

I run the cluster locally on minikube version 1.16.0, kubernetes version 1.20.0. I am using the NodePort service to access the application. After swap the pod, I get a 404 error.

This is how I find the external address: addr = kubectl get nodes -o jsonpath='{ $.items[*].status.addresses[?(@.type=="InternalIP")].address }'

Target browser address: addr + :30000

Spoof command: telepresence --swap-deployment telepresence-php --docker-run --rm -v$(pwd)/web:/var/www/html telepresence-dev

Kubernetes php + nginx pod settings:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: telepresence-php
spec:
  replicas: 1
  selector:
    matchLabels:
      svc: telepresence-php
  template:
    metadata:
      labels:
        svc: telepresence-php
    spec:
      containers:
      - name: telepresence-php
        image: mavellol/telepresence-php
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: shared-files
          mountPath: /var/www/html
        lifecycle:
          postStart:
            exec:
              command: ["/bin/sh", "-c", "cp -r /app/. /var/www/html"]
      - name: telepresence-nginx
        image: mavellol/telepresence-nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: shared-files
          mountPath: /var/www/html
      volumes:
      - name: shared-files
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: telepresence-np
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30000
  selector:
    svc: telepresence-php

Kubernetes mysql pod settings

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: telepresence-db
spec:
  replicas: 1
  selector:
    matchLabels:
      svc: telepresence-db
  template:
    metadata:
      labels:
        svc: telepresence-db
    spec:
      containers:
      - name: telepresence-db
        image: mavellol/telepresence-db
        ports:
        - name: db-port
          containerPort: 3306
      volumes:
      - name: telepresence-mysql-data
        persistentVolumeClaim:
          claimName: telepresence-mysql-pvc
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: telepresence-mysql
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
    - ReadOnlyMany
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /tmp/telepresence-mysql
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: telepresence-mysql-pvc
spec:
  resources:
    requests:
      storage: 1Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: ""
---
apiVersion: v1
kind: Service
metadata:
  name: telepresence-db
spec:
  ports:
  - name: db-port
    port: 3306
    targetPort: db-port
  selector:
    svc: telepresence-db

docker container with which I substitute the php + nginx pod.

FROM nanoninja/php-fpm:latest

RUN apt-get update && apt-get install nginx -y
ADD ./etc/nginx/default.conf /etc/nginx/conf.d/default.conf
RUN rm /etc/nginx/sites-enabled/default

CMD nginx && php-fpm

Nginx default.conf settings:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name localhost;

    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;

    set $virtualdir "";
    set $realdir "";

    if ($request_uri ~ ^/([^/]*)/.*$ ) {
        set $virtualdir /$1;
    }

    if (-d "$document_root$virtualdir") {
        set $realdir "${virtualdir}";
    }

    location / {
        try_files $uri $uri/ $realdir/index.php?$args;
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass localhost:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

Help if it's not difficult. The demo project is here https://github.com/Mavellol/kuber-nginx-php-mysql

  • Please elaborate your question, post here your Kubernetes YAMLs. Which bottom line, from what file? Please provide your pod YAML. What error you got? What Kubernetes version are you using? It's local or cloud? Please provide steps to replicate your scenario. – PjoterS Feb 15 '21 at 08:28
  • In your `telepresence-php` you have `containerPort: 9000` and service have `targetPort: 80`. Did you try to change service `targetPort` to `9000` or deployment `containerPort` to 80? – PjoterS Feb 16 '21 at 11:50
  • Yes, I did. Did not help. – Malcev Pavel Feb 16 '21 at 15:18
  • Well, as you are using Minikube, you need to access using address from command `$ minikube ip`. So it should looks like `$ curl :NodePort`. Please let me know what answer you are getting then – PjoterS Feb 18 '21 at 13:39
  • @PjoterS `minikube ip` returns the same address as `kubectl get nodes -o jsonpath='{ $.items[*].status.addresses[?(@.type=="InternalIP")].address }'` – Malcev Pavel Feb 18 '21 at 16:27

0 Answers0