Giter Site home page Giter Site logo

spring-boot-kubernetes's Introduction

spring-boot-kubernetes

Create Dockerfile

FROM openjdk:8-jdk-alpine
MAINTAINER Phea Soy
VOLUME [ "/tmp" ]
COPY  target/spring-boot-kubernetes-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 9977
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Build image

$ docker build -t spring-boot-k8s:v1 .

Install minikube for single cluster

Start minikube

$ minikube start

Set docker to use minikube daemon

$ eval $(minikube docker-env)

Create Deployment

$ kubectl run spring-boot-k8s --image=spring-boot-k8s:v1 --port=9977

Get POD

$ kubectl get pod

Watch pod status

$ kubectl get pod -w

Switch

Create namespace

apiVersion: v1
kind: Namespace
metadata: 
  name: example-k8s
  • Create create-namespace.yml
$ kubectl create -f k8s/create-namespace.yml

Get namespace

$ kubectl get ns

Switch namespace

$ kubectl config set-context minikube --namespace example-k8s
$ kubens example-k8s

Describe namespace

$  kubectl describe ns example-k8s

Delete namespace

$ kubectl delete namespaces example-k8s

Create ResourceQuota

  • Create create-resource-quota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
  name: resource-quota-mem-cpu-example
  namespace: example-k8s
spec:
  hard: 
    requests.cpu: "1"
    requests.memory: 1Gi
    limits.cpu: "2"
    limits.memory: 4Gi
$ kubectl create -f k8s/create-resource-quota.yaml 
  • Get the resource
$ kubectl describe ns example-k8s
  • ===>
Name:		example-k8s
Labels:		app=spring-boot
Annotations:	<none>
Status:		Active

Resource Quotas
 Name:			resource-quota-mem-cpu-example
 Resource		Used	Hard
 --------		---	---
 limits.cpu		0	2
 limits.memory		0	4Gi
 requests.cpu		0	1
 requests.memory	0	1Gi

Create Deployment

  • Create create-deployment.yaml file
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name:   spring-boot-k8s-deployment
  labels:
    app: spring-boot-k8s
  namespace: example-k8s
spec:
  replicas: 1
  selector:
    matchLabels:
      app: spring-boot-k8s
  template:
    metadata:
      labels:
        app: spring-boot-k8s
    spec:
      containers:
      - name: spring-boot-k8s
        image: spring-boot-k8s:v1
        ports:
        - containerPort: 9977
        resources:
          limits:
            memory: "1Gi"
            cpu: "500m"
          requests: 
            memory: "256Mi"
            cpu: "200m"
        livenessProbe:
          httpGet:
            path: /actuator/health/liveness
            port: 9977
          initialDelaySeconds: 60
          periodSeconds: 5
        readinessProbe:
          httpGet:
            path: /actuator/health/readiness
            port: 9977
          initialDelaySeconds: 60
          timeoutSeconds: 5
$ kubectl create -f k8s/create-deployment.yaml

Scale deployment

$ kubectl scale deployment spring-boot-k8s-deployment --replicas=2

Update deployment

 kubectl set image deployment/spring-boot-k8s-deployment spring-boot-k8s=spring-boot-k8s:v5 --record

Expose service for deployment

$ kubectl expose deployment spring-boot-k8s-deployment --name=spring-boot-service --type=NodePort

Get the URL of the expose service

$ minikube service spring-boot-service -n example-k8s --url

Access the application

$ curl http://192.168.64.2:32352/k8s/sidara
  • Response
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 62
Date: Sun, 01 Sep 2019 05:48:36 GMT
Hi sidara- I am a SpringBoot Application run inside Kubernetes

Delete service

$ kubectl delete service spring-boot-k8s

Create service access outside the cluster

  • Create create-service.yaml file
apiVersion: v1
kind: Service
metadata:
  labels:
    app: spring-boot-k8s-service
  name: spring-boot-k8s-service
  namespace: example-k8s
spec:
  ports:
  - nodePort: 30001 # Port access outside the cluster
    port: 9977 # Port access inside the cluster
    protocol: TCP
    targetPort: 9977 # Port forward to inside the port which container running
  selector:
    app: spring-boot-k8s
  type: NodePort
$ kubectl create -f k8s/create-service.yaml

Access The Application

$  curl http://$(minikube ip):30001/k8s/dara

Create ConfigMap

$ kubectl create configmap spring-boot-k8s --from-file k8s/user.properties 

spring-boot-kubernetes's People

Contributors

pheasoy avatar rsanzana avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.