POD with mysql and wordpress

9/9/2019

I'am tryng to learn K8S and I have followed some tutorial but I have an Issue. How can I connect my wordpress to my mysql db ?

A random secret is generated by mysql for root and first connection. I have used this manifest but I have error Error establishing a database connection

Can you tell how to connect to my mysql server to recover the random secret or any solution to understand and resolve my issue please ?

Thanks a lot

apiVersion: v1
kind: Pod
metadata:
  name: wp-pod
spec:
  containers:
  - name: wordpress-ct
    image: wordpress
    env:
    - name: WORDPRESS_DB_PASSWORD
      value: mysqlpwd
    - name: WORDPRESS_DB_HOST
      value: 127.0.0.1
  - name: mysql-ct
    image: mysql
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: mysqlpwd
    volumeMounts:
    - name: data
      mountPath: /var/lib/mysql
  volumes:
  - name: data
    emptyDir: {}



kubectl get all
NAME             READY   STATUS    RESTARTS   AGE
pod/debug        1/1     Running   13         37h
pod/new-wp-pod   2/2     Running   0          7s
pod/www          1/1     Running   0          37h

-

kubectl port-forward pod/new-wp-pod 8080:80

-

Brower error http://localhost:8080/

error Error establishing a database 
connection

-

root@new-wp-pod:/var/www/html# ls
index.php        wp-admin              wp-config.php  wp-links-opml.php  wp-settings.php
license.txt      wp-blog-header.php    wp-content     wp-load.php        wp-signup.php
readme.html      wp-comments-post.php  wp-cron.php    wp-login.php       wp-trackback.php
wp-activate.php  wp-config-sample.php  wp-includes    wp-mail.php        xmlrpc.php
root@new-wp-pod:/var/www/html# cat wp-config.php
<?php
/**
 * The base configuration for WordPress
 *
 * The wp-config.php creation script uses this file during the
 * installation. You don't have to use the web site, you can
 * copy this file to "wp-config.php" and fill in the values.
 *
 * This file contains the following configurations:
 *
 * * MySQL settings
 * * Secret keys
 * * Database table prefix
 * * ABSPATH
 *
 * @link https://codex.wordpress.org/Editing_wp-config.php
 *
 * @package WordPress
 */

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress');

/** MySQL database username */
define( 'DB_USER', 'root');

/** MySQL database password */
define( 'DB_PASSWORD', 'mysqlpwd');

/** MySQL hostname */
define( 'DB_HOST', '127.0.0.1');

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '');

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define( 'AUTH_KEY',         '8791830dd9fc85ced9dd9ff993363f1bae2add6f');
define( 'SECURE_AUTH_KEY',  '71a37b78a9e54ff5256cccc843645afaf4d4baac');
define( 'LOGGED_IN_KEY',    '108ec9ab01eb7280bf5e48d8982e115e06560dc5');
define( 'NONCE_KEY',        '7bc243bf7ed23389bf96dce94b41018bb139aeec');
define( 'AUTH_SALT',        '55e44dc4f33ead449ba738f7d01f8d79297b74f6');
define( 'SECURE_AUTH_SALT', '7930012aeff11ba89585a09de8237f8bf80b6c93');
define( 'LOGGED_IN_SALT',   '2698989a788b7b94b3f211077bf2c77ce3c51f60');
define( 'NONCE_SALT',       'acd8bda316b0f92e664b0c451163af562945ae48');

/**#@-*/

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wp_';

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the Codex.
 *
 * @link https://codex.wordpress.org/Debugging_in_WordPress
 */
define( 'WP_DEBUG', false );

// If we're behind a proxy server and using HTTPS, we need to alert Wordpress of that fact
// see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
        $_SERVER['HTTPS'] = 'on';
}

/* That's all, stop editing! Happy publishing. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
        define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}

/** Sets up WordPress vars and included files. */
require_once( ABSPATH . 'wp-settings.php' );



     kubectl describe po/wp-pod
Name:         wp-pod
Namespace:    default
Priority:     0
Node:         k8snode1/192.168.1.16
Start Time:   Mon, 09 Sep 2019 02:04:14 -0400
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.244.1.16
Containers:
  wordpress-ct:
    Container ID:   docker://b20c92ac7c67cdb707c0623f080aa1e406d32ff762e292e866fac1a93a4aa8c4
    Image:          wordpress
    Image ID:       docker-pullable://wordpress@sha256:2d1ced84d8bfa187d69479cec36fc57824a9d22125050ffd55fe7dbc92bd473c
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 09 Sep 2019 02:04:17 -0400
    Ready:          True
    Restart Count:  0
    Environment:
      WORDPRESS_DB_PASSWORD:  mysqlpwd
      WORDPRESS_DB_HOST:      127.0.0.1
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-j9sjf (ro)
  mysql-ct:
    Container ID:   docker://f0b4a418c5feb95ad277d434bf8f0a93d9ccd5fbff4236e5feac2dda06465dc9
    Image:          mysql
    Image ID:       docker-pullable://mysql@sha256:01cf53f2538aa805bda591d83f107c394adca8d31f98eacd3654e282dada3193
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 09 Sep 2019 02:04:19 -0400
    Ready:          True
    Restart Count:  0
    Environment:
      MYSQL_ROOT_PASSWORD:  mysqlpwd
    Mounts:
      /var/lib/mysql from data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-j9sjf (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  data:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:
    SizeLimit:  <unset>
  default-token-j9sjf:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-j9sjf
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  2m52s  default-scheduler  Successfully assigned default/wp-pod to k8snode1
  Normal  Pulling    2m51s  kubelet, k8snode1  Pulling image "wordpress"
  Normal  Pulled     2m49s  kubelet, k8snode1  Successfully pulled image "wordpress"
  Normal  Created    2m49s  kubelet, k8snode1  Created container wordpress-ct
  Normal  Started    2m49s  kubelet, k8snode1  Started container wordpress-ct
  Normal  Pulling    2m49s  kubelet, k8snode1  Pulling image "mysql"
  Normal  Pulled     2m48s  kubelet, k8snode1  Successfully pulled image "mysql"
  Normal  Created    2m47s  kubelet, k8snode1  Created container mysql-ct
  Normal  Started    2m47s  kubelet, k8snode1  Started container mysql-ct

-

kubectl logs -f new-wp-pod -c mysql-ct
Error from server (NotFound): pods "new-wp-pod" not found
[rbo@K8SMaster ~]$ kubectl logs -f new-wp-pod -c ^C
[rbo@K8SMaster ~]$ kubectl logs -f wp-pod -c mysql-ct
Initializing database
2019-09-09T06:04:19.288516Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-09-09T06:04:19.288579Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.17) initializing of server in progress as process 28
2019-09-09T06:04:20.767998Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2019-09-09T06:04:21.694368Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (mysqld 8.0.17) initializing of server has completed
Database initialized
MySQL init process in progress...
2019-09-09T06:04:23.309354Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-09-09T06:04:23.309459Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17) starting as process 80
2019-09-09T06:04:23.659349Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-09-09T06:04:23.661058Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2019-09-09T06:04:23.674473Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
2019-09-09T06:04:23.834817Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.

2019-09-09T06:04:28.044068Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17)  MySQL Community Server - GPL.

MySQL init process done. Ready for start up.

2019-09-09T06:04:28.367849Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-09-09T06:04:28.367929Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17) starting as process 1
2019-09-09T06:04:28.671834Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-09-09T06:04:28.673563Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2019-09-09T06:04:28.687554Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2019-09-09T06:04:28.876157Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
-- rab
kubernetes

1 Answer

9/9/2019

your yaml seems correct and I have tried one myself to check if it works and it does. Can you run "kubectl describe pod new-wp-pod..." and also "kubectl logs -f new-wp-pod -c mysql-ct"? I am thinking your mysql pod is not running.

If you want to debug further you could add another container and do a "telnet localhost 3306" to check mysql is running. Also a netstat -tlpn could help.

      - name: "container-1"
        image: alpine
        args:
          - "sleep"
          - "1000"

However, given that you want to run two containers I would recommend using different deployments and allow them to be resolved via kubernetes services. The following two yamls should do the job.

---
apiVersion: v1
kind: Service
metadata:
  labels:
    mojix.service: mysql
  name: mysql
spec:
  ports:
  - name: "3306"
    port: 3306
    targetPort: 3306
  selector:
    mojix.service: mysql
status:
  loadBalancer: {}

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    mojix.service: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      mojix.service: mysql
  template:
    metadata:
      creationTimestamp: null
      labels:
        mojix.service: mysql
    spec:
      containers:
      - name: "mysql"
        image: mysql
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: "root"
          - name: MYSQL_DB
            value: "test"
      hostname: mysql
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    mojix.service: wordpress
spec:
  ports:
  - name: "8000"
    port: 8000
    targetPort: 8000
  selector:
    mojix.service: wordpress
status:
  loadBalancer: {}

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    mojix.service: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      mojix.service: wordpress
  template:
    metadata:
      creationTimestamp: null
      labels:
        mojix.service: wordpress
    spec:
      containers:
      - name: "wordpress-ct"
        image: wordpress
        env:
          - name: WORDPRESS_DB_PASSWORD
            value: "root"
          - name: WORDPRESS_DB_HOST
            value: "mysql"
      restartPolicy: Always
-- Rodrigo Loza
Source: StackOverflow