Right. I have now been through this and here is the solution.
#1
Create configmap
thor@jump_host ~$ echo 'variables_order = "EGPCS"' > php.ini
thor@jump_host ~$ k create cm php-config --from-file php.ini
#2/#3
thor@jump_host ~$ k create deployment lamp-wp --image webdevops/php-apache:alpine-3-php7 --dry-run=client -o yaml > lamp-wp.yaml
thor@jump_host ~$ vi lamp-wp.yaml
Now we have to be careful how we mount php.ini. We are putting it into a directory that already exists and contains files and folders, so if we use a simple mount we are going to knock out all the other stuff.
We need to use a subPath
mount, AND extra syntax in the volume declaration.
While we are here, also put in the mysql container.
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: lamp-wp
name: lamp-wp
spec:
replicas: 1
selector:
matchLabels:
app: lamp-wp
template:
metadata:
creationTimestamp: null
labels:
app: lamp-wp
spec:
volumes:
- name: ini
configMap:
name: php-config
items: #<- This extra syntax to make subPath work properly
- key: php.ini
path: php.ini
containers:
- image: webdevops/php-apache:alpine-3-php7
name: httpd-php-container
volumeMounts:
- name: ini
mountPath: /opt/docker/etc/php/php.ini
subPath: php.ini
- image: mysql:5.6
name: mysql-container
#4
CAREFUL - There is a curveball in this one! We cannot use just anything for MYSQL_HOST
or the database will not be able to be connected to. Before creating the secret see task #7
thor@jump_host ~$ k create secret generic mysql-config --from-literal MYSQL_ROOT_PASSWORD=paswd --from-literal MYSQL_DATABASE=lampdb --from-literal MYSQL_USER=thor --from-literal MYSQL_PASSWORD=mjolnir123 --from-literal MYSQL_HOST=mysql-service
#5
Mount the secret’s environment variables. Note that I have used an alias/anchor combination here both for less typing and to avoid errors in duplicating the variables to both containers. This should now be the final edit to the deployment YAML
thor@jump_host ~$ vi lamp-wp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: lamp-wp
name: lamp-wp
spec:
replicas: 1
selector:
matchLabels:
app: lamp-wp
template:
metadata:
creationTimestamp: null
labels:
app: lamp-wp
spec:
volumes:
- name: ini
configMap:
name: php-config
items: #<- This extra syntax to make subPath work properly
- key: php.ini
path: php.ini
containers:
- image: webdevops/php-apache:alpine-3-php7
name: httpd-php-container
env: &environment #<- ANCHOR
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-config
key: MYSQL_ROOT_PASSWORD
- name: MYSQL_DATABASE
valueFrom:
secretKeyRef:
name: mysql-config
key: MYSQL_DATABASE
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: mysql-config
key: MYSQL_USER
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-config
key: MYSQL_PASSWORD
- name: MYSQL_HOST
valueFrom:
secretKeyRef:
name: mysql-config
key: MYSQL_HOST
volumeMounts:
- name: ini
mountPath: /opt/docker/etc/php/php.ini
subPath: php.ini
- image: mysql:5.6
name: mysql-container
env: *environment #<- ALIAS (all environment beneath ANCHOR will be repeated here)
#6
Create nodeport service. First run up the deployment that we have so far so we can use imperative commands to create the services
Spin up the deployment - it should run fine.
thor@jump_host ~$ k create -f lamp-wp.yaml
Create YAML file for nodePort
service
thor@jump_host ~$ k expose deployment lamp-wp --port 80 --target-port 80 --type NodePort --name lamp-service --dry-run=client -o yaml > lamp-service.yaml
Edit in the correct nodeport
thor@jump_host ~$ vi lamp-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: lamp-wp
name: lamp-service
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 30008 #<- Add this
selector:
app: lamp-wp
type: NodePort
Create service
k create -f lamp-service.yaml
#7
Create mysql-service
thor@jump_host ~$ k expose deployment lamp-wp --port 3306 --target-port 3306 --type ClusterIP --name mysql-service
service/mysql-service exposed
No need to create a YAML file for this service, since we don’t need to make additional edits to a ClusterIP service.
#8
Sort out index.php and place in the container
#8a
thor@jump_host ~$ cp /tmp/index.php .
thor@jump_host ~$ vi index.php
Edit it to look like this
<?php
$dbname = getenv('MYSQL_DATABASE');
$dbuser = getenv('MYSQL_USER');
$dbpass = getenv('MYSQL_PASSWORD');
$dbhost = getenv('MYSQL_HOST');
$connect = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($test_query);
if ($result->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Now copy it into container
thor@jump_host ~$ k get po
NAME READY STATUS RESTARTS AGE
lamp-wp-78cfc8c984-vsjc5 2/2 Running 0 18s
Note pod name. We will need it for the next command
k cp -c httpd-php-container index.php lamp-wp-78cfc8c984-vsjc5:/app/index.php
#8b Test it.