Kubernetes Application – Package Multiple Resources Together
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
- Complete details about how to setup Kubernetes and run this application are available at github.com/arun-gupta/kubernetes-java-sample/#kubernetes-application.
- More details about creating a Kubernetes application with multiple resources can be found in #12104.
- You can learn about how to create Kubernetes resources for a Java application, or otherwise, at github.com/arun-gupta/kubernetes-java-sample/.
|Reference:||Kubernetes Application – Package Multiple Resources Together from our JCG partner Arun Gupta at the Miles to go 2.0 … blog.|