Building Images & Running Containers
End to End
Find a base image: docker.io, quay.io, gcr.io, registry.redhat.io
Craft your Dockerfile
Configure docker:
eval $(minikube --profile myprofile decoder-env)
Build your image:
docker build -t liulx/myimage:v1 .
a. Test via:
- `docker run -it -p 8080:8080 --name myboot liulx/myimage:v1` - `docker run -d -p 8080:8080 --name my boot liulx/myboot:v1` - `curl $(minikube --profile myprofile ip):8080`
b. If remote repo, do
docker tag
anddocker push
c.
docker stop containername
to stop testingkubectl apply -f myDeployment.yaml
kubectl apply -f myService.yaml
Expose a URL via your kubernetes distribution’s load-balancer
docker build
1 | docker build -t something/animagename:tag |
The .
indicates where to find the Dockerfile and assets to be included in the build process.
You can alse explicitly identify the Dockerfile:
docker build -t somestring/animagename:tag -f somedirectory/Dockerfile_Production .
docker build -t somestring/animagename:tag -f somedirectory/Dockerfile_Testing .
docker build -f src/main/docker/Dockerfile.native -t mystuff/myimage:v1 .
Builiding Images
Options Include:
docker build
thenkubectl run
orkubectl create -f deploy.yml
- Jib - Maven/Gradle plugin by google
- Shift maven plugin by Red hat
- s2i - source to image
- Tekton - pipeline-based image building
- No docker: red hat’s podman, Google’s kaniko, Uber’s makisu
- Buildpacks - similar to Heroku & Cloud Foundry
Namespaces
1 | - `kubectl create namespace demo` |
Pods
1 | cat <<EOF | kubectl apply -f - |
ReplicaSets
1 | cat <<EOF | kubectl apply -f - |
See what it produced:
1 | $ kubectl get pods --show-labels |
Deployments
1 | cat <<EOF | kubectl apply -f - |
Run curl inside Pod: kubectl exec -it pod name -- curl localhost:8080
Service
1 | cat <<EOF | kubectl apply -f - |
curl those Endpoints behind that service:
1 | $ IP=$(minikube --profile 9steps ip) |
Scale
Let’s scale up the application to 3 replicas, there are several possible ways to achieve this result.
You can edit the myboot-deployment.yml, updating replicas to 3
1 | $ kubectl replace -f kubefiles/myboot-deployment.yml |
Or use the kubectl scale command
1 | $ kubectl scale --replicas=3 deployment/myboot |
Or you might patch it
1 | $ kubectl patch deployment/myboot -p '{"spec":{"replicas":3}}' |
Or use the kubectl edit command which essentially gives you “vi” for editing the deployment yaml
1 | $ kubectl edit deployment/myboot |
Note: You can use VS Code as your editor with export KUBE_EDITOR=”code -w”
You can then use “kubectl get pods” to see the pods that have been created
1 | $ kubectl get pods |
Note: 3 pods might push you out of your memory limits for your VM. Check your memory usage with:
1 | $ minishift -p 9steps ssh |
Update
Update MyRESTController.java
1 | greeting = environment.getProperty("GREETING","Bonjour"); |
Compile & Build the fat jar
1 | mvn clean package |
You can test with “java -jar target/boot-demo-0.0.1.jar” and “curl localhost:8080”. Ideally, you would have unit tests executed with “mvn test” as well.
Build the new docker image with a v2 tag
1 | $ docker build -t 9stepsawesome/myboot:v2 . |
Rollout the update
1 | $ kubectl set image deployment/myboot myboot=9stepsawesome/myboot:v2 |
And if you were running your polling curl command, you might see
1 | Aloha from Spring Boot! 346 on myboot-79bfc988c8-ttz5r |
We will be working on those errors when we address Liveness and Readiness probes in Step 7
For now, undo the update, going back to v1
1 | $ kubectl rollout undo deployment/myboot |
Scale to 1
1 | $ kubectl scale --replicas=1 deployment/myboot |
Hit the sysresources endpoint
1 | $ curl $(minikube -p 9steps ip):$(kubectl get service/myboot -o jsonpath="{.spec.ports[*].nodePort}")/sysresources |
Note: you can use echo to see what this URL really looks like on your machine
1 | $ echo $(minikube -p 9steps ip):$(kubectl get service/myboot -o jsonpath="{.spec.ports[*].nodePort}")/sysresources |
The results of the “sysresources” call should be about 1/4 memory and all the cores that were configured for the VM. You can double check this with the following command:
1 | $ minikube config view |
Now, let’s apply resource contraints via Kubernetes & deployment yaml. Look at myboot-deployment-resources.yml
1 | $ kubectl replace -f kubefiles/myboot-deployment-resources.yml |
Curl sysresources again
1 | $ curl $(minikube -p 9steps ip):$(kubectl get service/myboot -o jsonpath="{.spec.ports[*].nodePort}")/sysresources |
In another terminal window, watch the pods
1 | $ kubectl get pods -w |
and now curl the consume endpoint
1 | $ curl $(minikube -p 9steps ip):$(kubectl get service/myboot -o jsonpath="{.spec.ports[*].nodePort}")/consume |
and you should see a OOMKilled
1 | $ kubectl get pods -w |
And you can also see the OOMKilled with the kubectl describe command
1 | $ kubectl describe pod -l app=myboot |
JIB
1 | <plugin> |
Build Docker Image and run:
1 | eval $(minishift docker-env) |