Kubernetes Environment Variable

Semua container yang dideploy didalam cluster kubernetes perlu dikonfigurasi terlebih dahulu. Bisa saja dengan kasar (hardcoded) merubah konfigurasi dockerfiles dan melakukan build ulang setiap ada konfigurasi baru, tetapi itu tidak efisien. Maka munculah konsep Environment Variable dimana kita bisa masukan variable pada script manifest

Berikut script manifest untuk membuat deployment mysql database :

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql-database
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: mysql-database
    spec:
     containers:
      - name: mysql-database
        image: docker pull mysql/mysql-server
        ports:
        - containerPort: 3306

dari sumbernya mysql-server, ada environment variable yang dapat digunakan seperti MYSQL_DATABASE,MYSQL_USER, MYSQL_PASSWORD,MYSQL_ROOT_PASSWORD. maka hasil akhirnya akan seperti ini

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql-database
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: mysql-database
    spec:
     containers:
      - name: mysql-database
        image: mysql/mysql-server
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: myrootpassword
        - name: MYSQL_DATABASE
          value: database1
        - name: MYSQL_USER
          value: myuser
        - name: MYSQL_PASSWORD
          value: mydatabasepassword

Lalu buat service untuk mengexpose mysql

kind: Service
apiVersion: v1
metadata:
  name: mysql-server
spec:
  ports:
  - port: 3306
    protocol: TCP
  selector:
    name: mysql-database

Lakukan Deploy

ubuntu@master:~$ kubectl create -f environment.yaml 
deployment.extensions "mysql-database" created
ubuntu@master:~$ kubectl get pod
NAME                              READY     STATUS              RESTARTS   AGE
mysql-database-75f6f66c6f-789h6   0/1       ContainerCreating   0          6s
ubuntu@master:~$ kubectl get pod
NAME                              READY     STATUS    RESTARTS   AGE
mysql-database-75f6f66c6f-789h6   1/1       Running   0          24s
ubuntu@master:~$ kubectl create -f svc-environment.yaml 
service "mysql-server" created
ubuntu@master:~$ kubectl get svc
NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes     ClusterIP   10.96.0.1       <none>        443/TCP    7d
mysql-server   ClusterIP   10.100.102.99   <none>        3306/TCP   4s

Untuk melakukan testing terhadap database, gunakan script python

import mysql.connector
from mysql.connector import Error
 
 
def connect():
    """ Connect to MySQL database """
    try:
        conn = mysql.connector.connect(host='10.100.102.99',
                                       database='database1',
                                       user='myuser',
                                       password='mydatabasepassword')
        if conn.is_connected():
            print('Connected to MySQL database')
 
    except Error as e:
        print(e)
 
    finally:
        conn.close()
 
 
if __name__ == '__main__':
    connect()

coba jalankan :

ubuntu@master:~/python$ cat checkdb.py
import mysql.connector
from mysql.connector import Error
 
 
def connect():
    """ Connect to MySQL database """
    try:
        conn = mysql.connector.connect(host='10.100.102.99',
                                       database='database1',
                                       user='myuser',
                                       password='mydatabasepassword')
        if conn.is_connected():
            print('Connected to MySQL database')
 
    except Error as e:
        print(e)
 
    finally:
        conn.close()
 
 
if __name__ == '__main__':
    connect()
ubuntu@master:~/python$ python checkdb.py 
Connected to MySQL database

Jika module belum terinstall

pip install mysql-connector-python

bisa juga cek secara langsung dengan masuk kedalam container

ubuntu@master:~$ kubectl get pod
NAME                              READY     STATUS    RESTARTS   AGE
mysql-database-75f6f66c6f-789h6   1/1       Running   0          15m
ubuntu@master:~$ kubectl exec mysql-database-75f6f66c6f-789h6 -it -- bash
bash-4.2# mysql -u root -p
Enter password: myrootpassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| database1          |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql>

 

 

Comments are closed.