VOOZH about

URL: https://www.javacodegeeks.com/2015/08/kubernetes-application-package-multiple-resources-together.html

⇱ Kubernetes Application – Package Multiple Resources Together - Java Code Geeks


Deploying an application in Kubernetes require to create multiple resources such as Pods, Services, Replication Controllers, and others. Typically each resource is define in a configuration file and created using kubectl script. But if multiple resources need to be created then you need to invoke kubectl multiple times. So if you need to create the following resources:

  • MySQL Pod
  • MySQL Service
  • WildFly Replication Controller

Then the commands would look like:

kubectl.sh create -f ~/workspaces/kubernetes-java-sample/app-mysql-pod.yaml
kubectl.sh create -f ~/workspaces/kubernetes-java-sample/app-mysql-service.yaml
kubectl.sh create -f ~/workspaces/kubernetes-java-sample/app-wildfly-rc.yaml

Or for convenience, wrap these invocations in a shell script. But that is not very intuitive! There is a better, and more natural and intuitive way.

Kubernetes allow multiple resources to be specified in a single configuration file. This allows to create a “Kubernetes Application” that can consists of multiple resources easily.

Previous section showed how to deploy the Java EE application using multiple configuration files. This application can be delpoyed using a single configuration file as well.

An application, as discussed above, consisting of MySQL Pod, MySQL Service, and WildFly Replication Controller can be created using the following configuration file:

apiVersion: v1
kind: Pod
metadata:
 name: mysql-pod
 labels:
 name: mysql-pod
 context: docker-k8s-lab
spec:
 containers:
 -
 name: mysql
 image: mysql:latest
 env:
 -
 name: "MYSQL_USER"
 value: "mysql"
 -
 name: "MYSQL_PASSWORD"
 value: "mysql"
 -
 name: "MYSQL_DATABASE"
 value: "sample"
 -
 name: "MYSQL_ROOT_PASSWORD"
 value: "supersecret"
 ports:
 -
 containerPort: 3306
----
apiVersion: v1
kind: Service
metadata:
 name: mysql-service
 labels:
 name: mysql-pod
 context: docker-k8s-lab
spec:
 ports:
 # the port that this service should serve on
 - port: 3306
 # label keys and values that must match in order to receive traffic for this service
 selector:
 name: mysql-pod
 context: docker-k8s-lab
----
apiVersion: v1
kind: ReplicationController
metadata:
 name: wildfly-rc
 labels:
 name: wildfly
 context: docker-k8s-lab
spec:
 replicas: 1
 template:
 metadata:
 labels:
 name: wildfly
 spec:
 containers:
 - name: wildfly-rc-pod
 image: arungupta/wildfly-mysql-javaee7:k8s
 ports:
 - containerPort: 8080

Notice that each section, one each for MySQL Pod, MySQL Service, and WildFly Replication Controller, is separated by ----.

Such an application can be created as:

kubectl.sh create -f ~/workspaces/kubernetes-java-sample/app.yaml
pods/mysql-pod
services/mysql-service
replicationcontrollers/wildfly-rc
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Arun Gupta
Arun Gupta
August 26th, 2015Last Updated: August 25th, 2015
0 93 2 minutes read

Arun Gupta

Arun is a technology enthusiast, avid runner, author of a best-selling book, globe trotter, a community guy, Java Champion, JavaOne Rockstar, JUG Leader, Minecraft Modder, Devoxx4Kids-er, and a Red Hatter.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz