Commit a03a96d2 authored by Sarun Mungthanya's avatar Sarun Mungthanya
Browse files

testnaja

parent 5022030b
FROM php:8.2-apache FROM php:8.3-apache
COPY . /var/www/html
RUN echo "<?php echo 'Version 1.0'; ?>" > /var/www/html/version.txt # เปิดใช้งาน MySQLi และ PDO MySQL
RUN docker-php-ext-install mysqli && \
docker-php-ext-enable mysqli && \
apt-get update && apt-get install -y libonig-dev && \
docker-php-ext-install mbstring && \
apt-get clean && rm -rf /var/lib/apt/lists/*
# Set the working directory
WORKDIR /var/www/html
# Copy application files to the container
COPY . /var/www/html/
# Grant appropriate permissions
RUN chown -R www-data:www-data /var/www/html && \
chmod -R 755 /var/www/html
# Enable Apache mod_rewrite
RUN a2enmod rewrite
# Expose port 80
EXPOSE 80 EXPOSE 80
# Set the entrypoint to Apache
CMD ["apache2-foreground"]
\ No newline at end of file
version: "3.8" version: '3.8'
services: services:
php-app: php:
build: . build: .
container_name: php-container
ports: ports:
- "8080:80" - "8080:80"
volumes:
- .:/var/www/html
depends_on:
- db
# db:
# image: mysql:8.0
# container_name: mysql-container
# restart: always
# environment:
# MYSQL_ROOT_PASSWORD: password
# MYSQL_DATABASE: shipping2024
# MYSQL_USER: root
# MYSQL_PASSWORD: password
# ports:
# - "3306:3306"
<?php
$db_host = "10.6.208.107";
$db_user = "nbdev";
$db_pass = "P@ssw0rd";
$db_name = "aybhshipping20200601";
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
// ตรวจสอบการเชื่อมต่อ
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// ดึงข้อมูล
$sql = "SELECT * FROM tab_user limit 10";
$result = $conn->query($sql);
$data = array();
if ($result->num_rows > 0) {
// ส่งค่ากลับเป็น JSON
while ($row = $result->fetch_assoc()) {
// แปลงค่าในแต่ละ field ที่เป็นสตริงจาก TIS-620 เป็น UTF-8
foreach ($row as $key => $value) {
if (is_string($value)) {
$row[$key] = iconv("TIS-620", "UTF-8", $value);
}
}
$data[] = $row;
}
echo json_encode($data, JSON_UNESCAPED_UNICODE);
} else {
echo "0 results";
}
$conn->close();
?>
\ No newline at end of file
<?php <?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST') {
shell_exec('kubectl rollout restart deployment php-app'); // Call a function to trigger the deployment update, for example, through a Kubernetes API or CI/CD
echo "Update requested!"; triggerUpdate();
exit;
} }
?>
function triggerUpdate()
{
// $apiServer = "http://localhost:8001"; // ค่า API Server Local
$apiServer = "https://kubernetes.default.svc"; // ค่า API Server ภายใน Cluster
$namespace = "my-app";
$deployment = "php-app";
$url = "$apiServer/apis/apps/v1/namespaces/$namespace/deployments/$deployment";
// โหลด Token จาก Service Account (Kubernetes ให้มาใน Pod)
$token = file_get_contents("/var/run/secrets/kubernetes.io/serviceaccount/token");
// JSON สำหรับสั่งให้ Deployment Restart
$data = json_encode([
"spec" => [
"template" => [
"metadata" => [
"annotations" => [
"kubectl.kubernetes.io/restartedAt" => date("c")
]
]
]
]
]);
// ตั้งค่า HTTP Request
$options = [
"http" => [
"header" => "Authorization: Bearer " . $token . "\r\n" .
"Content-Type: application/strategic-merge-patch+json\r\n", // แก้ไข Content-Type
"method" => "PATCH",
"content" => $data,
"ignore_errors" => true
],
"ssl" => ["verify_peer" => false, "verify_peer_name" => false]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$statusFile = "/var/tmp/update_status.txt";
if ($result !== false) {
file_put_contents($statusFile, "Update completed successfully");
} else {
file_put_contents($statusFile, "Update failed");
}
echo "Deployment update triggered! Response: " . $result;
}
echo '<p>PHP Version: ' . phpversion() . '</p>';
?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>PHP App</title> <title>PHP App</title>
<script>
let count = 0;
function incrementCounter() {
count++;
document.getElementById('counter').textContent = count;
}
// Start incrementing the counter every second
setInterval(incrementCounter, 1000);
// Function to fetch data from the database every second
function fetchData() {
fetch('fetch_data.php')
.then(response => response.json())
.then(data => {
console.log('Data:', data); // หรือคุณสามารถอัปเดตข้อมูลไปยัง DOM
document.getElementById('status').textContent = 'Data fetched successfully: ' + JSON.stringify(data);
})
.catch(error => {
console.error('Error fetching data:', error);
document.getElementById('status').textContent = 'Failed to fetch data';
});
}
</script>
</head> </head>
<body> <body>
<h1>Welcome to PHP App</h1> <h1>Welcome to PHP App</h1>
<p>Current Version: <?php echo file_get_contents("version.txt"); ?></p> <p>Current Version: 14</p>
<p id="counter">0</p> <!-- Display the counter -->
<form method="POST"> <form method="POST">
<button type="submit">Update App</button> <button type="submit">Update App</button>
</form> </form>
<p id="status">Checking status...</p>
<button type="button" onclick="fetchData()">fetchData</button>
</body> </body>
</html>
</html>
\ No newline at end of file
...@@ -5,11 +5,12 @@ metadata: ...@@ -5,11 +5,12 @@ metadata:
labels: labels:
app: php-app app: php-app
spec: spec:
replicas: 3 replicas: 3 # ใช้หลาย Replica เพื่อให้รองรับการอัปเดตแบบ Rolling
strategy: strategy:
type: RollingUpdate
rollingUpdate: rollingUpdate:
maxUnavailable: 1 maxUnavailable: 1 # ไม่ปิดมากกว่า 1 Pod ในแต่ละครั้ง # ไม่ปิดมากกว่า 1 Pod ในแต่ละครั้ง
maxSurge: 1 maxSurge: 1 # สร้าง Pod ใหม่ก่อนลบ Pod เก่า
selector: selector:
matchLabels: matchLabels:
app: php-app app: php-app
...@@ -18,14 +19,35 @@ spec: ...@@ -18,14 +19,35 @@ spec:
labels: labels:
app: php-app app: php-app
spec: spec:
imagePullSecrets:
- name: gitlab-registry-secret
containers: containers:
- name: php-app - name: php-app
image: myrepo/php-app:latest image: mobydick.netbay.co.th/sarun.netbay/kubernatestest:latest
imagePullPolicy: Always
ports: ports:
- containerPort: 80 - containerPort: 80
volumeMounts:
- name: php-ini-volume
mountPath: /usr/local/etc/php/php.ini
subPath: php.ini
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 10"] # รอให้ Request เสร็จก่อนปิด Pod
livenessProbe: livenessProbe:
httpGet: httpGet:
path: / path: /
port: 80 port: 80
initialDelaySeconds: 3 initialDelaySeconds: 3
periodSeconds: 5 periodSeconds: 5
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 3
periodSeconds: 5
volumes:
- name: php-ini-volume
configMap:
name: php-ini-config
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: php-app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: php-app-service
port:
number: 80
...@@ -9,4 +9,4 @@ spec: ...@@ -9,4 +9,4 @@ spec:
- protocol: TCP - protocol: TCP
port: 80 port: 80
targetPort: 80 targetPort: 80
type: LoadBalancer type: ClusterIP
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
\ No newline at end of file
This diff is collapsed.
<?php
phpinfo();
exit;
$statusFile = "/var/tmp/update_status.txt";
if (file_exists($statusFile)) {
echo file_get_contents($statusFile);
} else {
echo "No update in progress";
}
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment