content/guides/java/deploy.md
In this section, you'll learn how to use Docker Desktop to deploy your application to a fully-featured Kubernetes environment on your development machine. This lets you test and debug your workloads on Kubernetes locally before deploying.
In your spring-petclinic directory, create a file named
docker-java-kubernetes.yaml. Open the file in an IDE or text editor and add
the following contents. Replace DOCKER_USERNAME/REPO_NAME with your Docker
username and the name of the repository that you created in Configure CI/CD for
your Java application.
apiVersion: apps/v1
kind: Deployment
metadata:
name: docker-java-demo
namespace: default
spec:
replicas: 1
selector:
matchLabels:
service: server
template:
metadata:
labels:
service: server
spec:
containers:
- name: server-service
image: DOCKER_USERNAME/REPO_NAME
imagePullPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: service-entrypoint
namespace: default
spec:
type: NodePort
selector:
service: server
ports:
- port: 8080
targetPort: 8080
nodePort: 30001
In this Kubernetes YAML file, there are two objects, separated by the ---:
template, has just one container in it. The
container is created from the image built by GitHub Actions in Configure CI/CD for
your Java application.To learn more about Kubernetes objects, see the Kubernetes documentation.
In a terminal, navigate to spring-petclinic and deploy your application to
Kubernetes.
$ kubectl apply -f docker-java-kubernetes.yaml
You should see output that looks like the following, indicating your Kubernetes objects were created successfully.
deployment.apps/docker-java-demo created
service/service-entrypoint created
Make sure everything worked by listing your deployments.
$ kubectl get deployments
Your deployment should be listed as follows:
NAME READY UP-TO-DATE AVAILABLE AGE
docker-java-demo 1/1 1 1 15s
This indicates all one of the pods you asked for in your YAML are up and running. Do the same check for your services.
$ kubectl get services
You should get output like the following.
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h
service-entrypoint NodePort 10.99.128.230 <none> 8080:30001/TCP 75s
In addition to the default kubernetes service, you can see your service-entrypoint service, accepting traffic on port 30001/TCP.
In a terminal, curl the service. Note that a database wasn't deployed in this example.
$ curl --request GET \
--url http://localhost:30001/actuator/health \
--header 'content-type: application/json'
You should get output like the following.
{"status":"UP","groups":["liveness","readiness"]}
Run the following command to tear down your application.
$ kubectl delete -f docker-java-kubernetes.yaml
In this section, you learned how to use Docker Desktop to deploy your application to a fully-featured Kubernetes environment on your development machine.
Related information: