Skip to content

Instantly share code, notes, and snippets.

@mikksoone
Last active January 19, 2021 08:57
Show Gist options
  • Save mikksoone/8c0c1ef2892f829b482048948b8293b4 to your computer and use it in GitHub Desktop.
Save mikksoone/8c0c1ef2892f829b482048948b8293b4 to your computer and use it in GitHub Desktop.
start.spring.io app dockerize + k8s workshop
gcloud auth login
gcloud container clusters get-credentials k8s-ws-6 --zone europe-west1-b --project k8s-ws-7
gcloud auth configure-docker
kubectl version
kubectl get nodes
kubectl create namespace myname
1. Go to this webpage: https://start.spring.io
2. Choose these options:
- Project: Gradle Project
- Language: Java
- Spring Boot: 2.4.1
- Project metadata:
-- defaults
-- Java: 11
3. Dependecies -> Add dependencies: Spring Web, Spring Boot Actuator
4. Generate (download)
5. Unzip to a folder in your computer
# Build-time image that is discarded
FROM gradle:6.7.1-jdk11 AS build
WORKDIR /app
COPY src /app/src
COPY build.gradle /app
RUN gradle clean build bootJar
RUN ls -l /app/build/libs
FROM adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim
COPY --from=build /app/build/libs/app-0.0.1-SNAPSHOT.jar demo.jar
# We add curl just for testing purposes
RUN apk --no-cache add curl
ENV JAVA_OPTS=""
CMD exec java $JAVA_OPTS -jar demo.jar
EXPOSE 8080
#!/bin/sh -e
# How to build the docker image
# NB! Change myname to your name
docker build -t myname:latest .
# How to locally run the docker
docker run --name myname -p 8080:8080 myname:latest
# Open browser localhost:8080/actuator/health
# Tag the image to be able to push it
docker tag myname:latest eu.gcr.io/k8s-ws-7/myname:1
# Send the docker image to docker repository
docker push eu.gcr.io/k8s-ws-7/myname:1
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- image: eu.gcr.io/k8s-ws-7/mikk1:3
resources:
limits:
cpu: 1000m
memory: 1000Mi
requests:
cpu: 100m
memory: 600Mi
name: demo
env:
- name: SPRING_PROFILES_ACTIVE
value: STAGING
- name: JAVA_OPTS
value: -Xmx256m -Xms256m
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /actuator/health
port: 8080
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 100
kubectl get nodes
kubectl get namespaces
# Set kubectl against certain namespace (default ns is the default, but we want to deploy to your own ns)
kubectl config set-context $(kubectl config current-context) --namespace=myname
kubectl get pods
# This will tell the kubernetes to create the manifest
# NB! Replace image to your own. But mine will work as well.
kubectl apply -f deployment.yaml
kubectl get pods
kubectl describe pod podname
kubectl logs podname
kubectl delete pod podname
kubectl get pods
kubectl edit deployment demo
#set replicas: 2
kubectl get pods
apiVersion: v1
kind: Service
metadata:
name: demo
spec:
type: NodePort
ports:
- port: 80
targetPort: 8080
protocol: TCP
name: http
selector:
app: demo
kubectl apply -f service.yaml
# Check which services you have now
kubectl get svc
# Check the details of that service
kubectl describe svc demo
# Check the ip of your pod
kubectl get pods -o wide
# "log in" to the running container
kubectl exec -it podname -- /bin/sh
# How to access your running java app inside the container
curl localhost:8080/actuator/health
# How service is accessing your pod, note the port of 8080 (theoretical, usually you don't need the pod ip at all)
curl podip:8080/actuator/health
# How to access your java app via service ip (not via DNS)
curl svc-cluster-ip/actuator/health
# How to access a service in your own namespace (DNS)
curl demo/actuator/health
# How to access a service in any namespace (DNS)
curl demo.mikk.svc.cluster.local/actuator/health
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: demo
spec:
rules:
- http:
paths:
- path: /*
backend:
serviceName: demo
servicePort: http
kubectl apply -f ingress.yaml
kubectl get ingress
kubectl describe ingress demo
# open browser or curl with ip/actuator/health
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: demo
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: demo
minReplicas: 1
maxReplicas: 3
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 100
kubectl apply -f autoscaler.yaml
kubectl top pods
# Check deployment resources.requests. Autoscaler works on comparing actual (from kubectl top pods) usage to request.
# Run load test to check how autoscaler will scale your pod
# Check the content in zalando files, change target url to your ingress public ip
bash <(curl -s https://raw.githubusercontent.com/zalando-incubator/docker-locust/master/local.sh) deploy --target=http://34.102.186.215/actuator/health --locust-file=https://raw.githubusercontent.com/zalando-incubator/docker-locust/master/example/simple.py --slaves=4 --mode=manual
# Open browser at localhost:8089 to run the load test
test=1
props=2
kubectl create configmap demo --from-file=some.conf
kubectl get configmaps
kubectl describe configmap demo
# Edit your previous deployment.yaml and volumeMounts to the end
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 100
volumeMounts:
- mountPath: /conf
name: conf
volumes:
- name: conf
configMap:
name: demo
items:
- key: some.conf
path: some.conf
kubectl apply -f deployment.yaml
kubectl get pods
# Check for new pods
kubectl exec -it podname -- /bin/sh
ls /conf
cat /conf/some.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment