Kubernetes Spring Boot Example commands
You can use the sample project which I have in here.
git clone https://github.com/TechPrimers/spring-boot-lazy-init-example.git
Command to create docker image using Google JIB plugin
./mvnw com.google.cloud.tools:jib-maven-plugin:build -Dimage=gcr.io/$GOOGLE_CLOUD_PROJECT/spring-boot-example:v1
Command to run the docker image which we created in the previous step
docker run -ti --rm -p 8080:8080 gcr.io/$GOOGLE_CLOUD_PROJECT/spring-boot-example:v1
Command to login to the K8s cluster from Cloud Shell
gcloud container clusters get-credentials techprimer-cluster-1 --zone us-central1-a
kubectl get pods
kubectl get deployments
kubectl get services
kubectl run spring-boot-example --image=gcr.io/$GOOGLE_CLOUD_PROJECT/spring-boot-example:v1 --port=8080
kubectl expose deployment spring-boot-example --type=LoadBalancer
kubectl scale deployment spring-boot-example --replicas=3
Link to Brandon Potter's YML builder - https://static.brandonpotter.com/kubernetes/DeploymentBuilder.html
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: spring-boot-example
spec:
replicas: 3
template:
metadata:
labels:
app: spring-boot-example
spec:
containers:
- name: spring-boot-example
image: 'gcr.io/fleet-resolver-237016/spring-boot-example:v1'
ports:
- containerPort: 8080
apiVersion: v1
kind: Service
metadata:
name: spring-boot-example
labels:
name: spring-boot-example
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: spring-boot-example
type: LoadBalancer
kubectl apply -f deployment.yml
kubectl apply -f service.yml
kubectl logs <POD_NAME>
kubectl get pods
kube.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: spring-boot-example
spec:
replicas: 3
strategy:
type: Recreate
template:
metadata:
labels:
app: spring-boot-example
spec:
containers:
- name: spring-boot-example
image: 'gcr.io/fleet-resolver-237016/spring-boot-example:v1'
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: spring-boot-example
labels:
name: spring-boot-example
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: spring-boot-example
type: LoadBalancer
kube.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: spring-boot-example
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
template:
metadata:
labels:
app: spring-boot-example
spec:
containers:
- name: spring-boot-example
image: 'gcr.io/fleet-resolver-237016/spring-boot-example:v1'
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /lazy
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
name: spring-boot-example
labels:
name: spring-boot-example
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: spring-boot-example
type: LoadBalancer
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: spring-boot-example-v1
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
template:
metadata:
labels:
app: spring-boot-example
version: "v1"
spec:
containers:
- name: spring-boot-example
image: 'gcr.io/fleet-resolver-237016/spring-boot-example:v1'
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /lazy
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
apiVersion: v1
kind: Service
metadata:
name: spring-boot-example
labels:
name: spring-boot-example
version: "v1"
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: spring-boot-example
version: "v1"
type: LoadBalancer
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: spring-boot-example-v2
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
template:
metadata:
labels:
app: spring-boot-example
version: "v2"
spec:
containers:
- name: spring-boot-example
image: 'gcr.io/fleet-resolver-237016/spring-boot-example:v2'
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /lazy
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
apiVersion: v1
kind: Service
metadata:
name: spring-boot-example-green
labels:
name: spring-boot-example-green
version: "v2"
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: spring-boot-example
version: "v2"
type: LoadBalancer
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: spring-boot-example-v2
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
template:
metadata:
labels:
app: spring-boot-example
version: "v2"
spec:
containers:
- name: spring-boot-example
image: 'gcr.io/fleet-resolver-237016/spring-boot-example:v2'
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /lazy
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
apiVersion: v1
kind: Service
metadata:
name: spring-boot-example
labels:
name: spring-boot-example
version: "v2"
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: spring-boot-example
version: "v2"
type: LoadBalancer
kubectl apply -f deployment-blue-v1.yml
kubectl apply -f service-blue-v1.yml
kubectl apply -f deployment-green-v2.yml
kubectl apply -f service-green-v2.yml
kubectl apply -f deployment-blue-v2.yml
kubectl apply -f service-blue-v2.yml
kubectl delete deployment.apps/spring-boot-example-v1 service/spring-boot-example-green
Using Tags in Kubernetes
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: spring-boot-example-v1
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
template:
metadata:
labels:
app: spring-boot-example
version: "v1"
spec:
containers:
- name: spring-boot-example
image: 'gcr.io/fleet-resolver-237016/spring-boot-example:v1'
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /lazy
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
name: spring-boot-example
labels:
name: spring-boot-example
version: "v1"
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: spring-boot-example
version: "v1"
type: LoadBalancer
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: spring-boot-example-v2
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
template:
metadata:
labels:
app: spring-boot-example
version: "v2"
spec:
containers:
- name: spring-boot-example
image: 'gcr.io/fleet-resolver-237016/spring-boot-example:v2'
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /lazy
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
apiVersion: v1
kind: Service
metadata:
name: spring-boot-example
labels:
name: spring-boot-example
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: spring-boot-example
type: LoadBalancer
apiVersion: v1
kind: Service
metadata:
name: spring-boot-example
labels:
name: spring-boot-example
version: "v2"
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: spring-boot-example
version: "v2"
type: LoadBalancer
kubectl apply -f kube-v1.yml
kubectl apply -f deployment-v2.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: spring-boot-example-v3
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
template:
metadata:
labels:
app: spring-boot-example
version: "v1"
spec:
containers:
- name: spring-boot-example
image: 'gcr.io/fleet-resolver-237016/spring-boot-example:v3'
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /v3/lazy
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
name: spring-boot-example-v3
labels:
name: spring-boot-example
version: "v3"
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: spring-boot-example
version: "v3"
type: LoadBalancer
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: sb-ingress
spec:
rules:
- http:
paths:
- path: /lazy/*
backend:
serviceName: spring-boot-example
servicePort: 8080
- path: /v3/lazy
backend:
serviceName: spring-boot-example-v3
servicePort: 8080
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: sb-ingress
spec:
backend:
serviceName: spring-boot-example-v3
servicePort: 8080
kubectl apply -f kube-v3.yml
kubectl apply -f ingress.yml
kubectl apply -f ingress-default.yml