Overview
In this demonstration we will create a sequence for:
- secret definition
- mysql pod
- mysql service
- deployment defionition
- deployment service
Create credential file
1 2 3 4 5 6 7 8 9 10 |
apiVersion: v1 kind: Secret metadata: name: helloworld-secrets type: Opaque data: username: aGVsbG93b3JsZA== password: cGFzc3dvcmQ= rootPassword: cm9vdHBhc3N3b3Jk database: aGVsbG93b3JsZA== |
Create mysql pod with this definition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# *********************** # mysql pod # *********************** cat database.yml ... apiVersion: v1 kind: Pod metadata: name: database labels: app: database spec: containers: - name: mysql image: mysql:5.7 ports: - name: mysql-port containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: helloworld-secrets key: rootPassword - name: MYSQL_USER valueFrom: secretKeyRef: name: helloworld-secrets key: username - name: MYSQL_PASSWORD valueFrom: secretKeyRef: name: helloworld-secrets key: password - name: MYSQL_DATABASE valueFrom: secretKeyRef: name: helloworld-secrets key: database ... :wq! |
On the top of this deployment.yml pod the service definition is necessary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
cat database-service.yml ... apiVersion: v1 kind: Service metadata: name: database-service spec: ports: - port: 3306 protocol: TCP selector: app: database type: NodePort ... :wq! |
Deployment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
cat helloworld-db.yml ... apiVersion: extensions/v1beta1 kind: Deployment metadata: name: helloworld-deployment spec: replicas: 3 template: metadata: labels: app: helloworld-db spec: containers: - name: k8s-demo image: wardviaene/k8s-demo command: ["node", "index-db.js"] ports: - name: nodejs-port containerPort: 3000 env: - name: MYSQL_HOST value: database-service - name: MYSQL_USER value: root - name: MYSQL_PASSWORD valueFrom: secretKeyRef: name: helloworld-secrets key: rootPassword - name: MYSQL_DATABASE valueFrom: secretKeyRef: name: helloworld-secrets key: database ... :wq! |
Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
helloworld-db-service.yml ... apiVersion: v1 kind: Service metadata: name: helloworld-db-service spec: ports: - port: 3000 protocol: TCP selector: app: helloworld-db type: NodePort ... :wq! |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# *********************************** # get all pods available # # *********************************** kubectl get pods NAME READY STATUS RESTARTS AGE database 1/1 Running 0 4h helloworld-deployment-54dfb95d8c-8m6fk 1/1 Running 0 9m helloworld-deployment-54dfb95d8c-s8rdf 1/1 Running 0 9m helloworld-deployment-54dfb95d8c-x5xqk 1/1 Running 0 9m # *********************************** # login to database - to "database" # pod # *********************************** kubectl exec database -it -- mysql -u root -p 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> mysql> mysql> show databases ; +--------------------+ | Database | +--------------------+ | information_schema | | helloworld | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) |