Sunday, February 21, 2021

JBoss Wildfly Kubernetes Operator

 HOWTO

 

GitHub repos


Build JBoss Wildfly Docker image GitHub repo https://github.com/dveselka/java-ee-8/tree/main/java-ee8-minimal Dockerfile
$ more Dockerfile
FROM jboss/wildfly

RUN /opt/jboss/wildfly/bin/add-user.sh admin Admin#123 --silent

COPY ./target/java-ee8-minimal.war /opt/jboss/wildfly/standalone/deployments/

CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0"]

Build Docker image

mvn clean package && docker build -t dveselka/java-ee8-minimal .
....

Sending build context to Docker daemon  111.6kB
Step 1/4 : FROM jboss/wildfly
latest: Pulling from jboss/wildfly
75f829a71a1c: Pull complete 
7b11f246b3d3: Pull complete 
b765648c2a58: Pull complete 
506aff4a9c5a: Pull complete 
a7ae73eec52a: Pull complete 
Digest: sha256:c0be1b7462a7ee9d07be5e319f7f2b10a93e5b43d51532ed012d2869e0224cf0
Status: Downloaded newer image for jboss/wildfly:latest
 ---> 13b4b5beba73
Step 2/4 : RUN /opt/jboss/wildfly/bin/add-user.sh admin Admin#123 --silent
 ---> Running in ecb1651c51fc
Removing intermediate container ecb1651c51fc
 ---> 74f6e50296e9
Step 3/4 : COPY ./target/java-ee8-minimal.war /opt/jboss/wildfly/standalone/deployments/
 ---> dd07d8a883fc
Step 4/4 : CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0"]
 ---> Running in d9575f15ea43
Removing intermediate container d9575f15ea43
 ---> 01e0043e9e6f
Successfully built 01e0043e9e6f
Successfully tagged dveselka/java-ee8-minimal:latest

Docker image
[dave@dave java-ee8-minimal]$ docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
dveselka/java-ee8-minimal       latest              01e0043e9e6f        3 minutes ago       714MB

Push Docker image to DockerHub
$ docker push   dveselka/java-ee8-minimal
The push refers to repository [docker.io/dveselka/java-ee8-minimal]
4a24521ab9d6: Pushed 
348cd5508b6f: Pushed 
9ef61a25476f: Mounted from jboss/wildfly 
869989761eb2: Mounted from jboss/wildfly 
3fbe1e874b0d: Mounted from jboss/wildfly 
115463be137a: Mounted from jboss/wildfly 
613be09ab3c0: Mounted from jboss/wildfly 
latest: digest: sha256:d416275117d084e0cf557f6cf1ad82dc0aba7bf67921e4f19703e9a48701b2ec size: 1790

Run in k8s
$ kubectl run java-ee8-minimal --image=dveselka/java-ee8-minimal
Get k8s pods
 kubectl get pods
NAME               READY   STATUS             RESTARTS   AGE
java-ee8-minimal   0/1     ImagePullBackOff   0          12s
Minikube running in Docker
$ docker ps
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS              PORTS                                                                                                                                  NAMES
5f6397acb7e1        gcr.io/k8s-minikube/kicbase:v0.0.18   "/usr/local/bin/entr…"   21 hours ago        Up 22 minutes       127.0.0.1:32772->22/tcp, 127.0.0.1:32771->2376/tcp, 127.0.0.1:32770->5000/tcp, 127.0.0.1:32769->8443/tcp, 127.0.0.1:32768->32443/tcp   minikube

Start k8s dashboard
$ kubectl cluster-info 
Kubernetes control plane is running at https://172.17.0.2:8443
KubeDNS is running at https://172.17.0.2:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[dave@dave java-ee8-minimal]$ minikube dashboard
🔌  Enabling dashboard ...
    ▪ Using image kubernetesui/dashboard:v2.1.0
    ▪ Using image kubernetesui/metrics-scraper:v1.0.4
🤔  Verifying dashboard health ...
🚀  Launching proxy ...
🤔  Verifying proxy health ...
🎉  Opening http://127.0.0.1:43609/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...

Forward k8s port to host
$ kubectl port-forward java-ee8-minimal 9990:9990
Forwarding from 127.0.0.1:9990 -> 9990
Forwarding from [::1]:9990 -> 9990
Handling connection for 9990

Check connection to app
$ wget http://localhost:8080/
--2021-03-14 17:28:48--  http://localhost:8080/
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1504 (1.5K) [text/html]
Saving to: ‘index.html’

Call application resource
 curl http://localhost:8080/java-ee8-minimal/resources/ping
Enjoy Jakarta EE with MicroProfile 2+
Minikube dashboard with pod
Create deployment on k8s cluster
$ kubectl apply -f app.yml 
deployment.apps/java-ee8-minimal created
[dave@dave java-ee8-minimal]$ kubectl get pods
NAME                                READY   STATUS              RESTARTS   AGE
java-ee8-minimal-76cd57679d-l5smd   0/1     ContainerCreating   0          3s
java-ee8-minimal-76cd57679d-swvcd   0/1     ContainerCreating   0          3s

k8s deployment https://github.com/dveselka/java-ee-8/blob/main/java-ee8-minimal/app.yml
 more app.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-ee8-minimal
spec:
  selector:
    matchLabels:
      app: java-ee8
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: java-ee8
    spec:
      containers:
      - name: java-ee8-minimal
        image: dveselka/java-ee8-minimal
        ports:
        - containerPort: 8080

Check pods
[dave@dave java-ee8-minimal]$ kubectl get pods -l app=java-ee8 -o wide
NAME                                READY   STATUS    RESTARTS   AGE     IP           NODE       NOMINATED NODE   READINESS GATES
java-ee8-minimal-76cd57679d-l5smd   1/1     Running   0          3m35s   172.18.0.5   minikube   <none>           <none>
java-ee8-minimal-76cd57679d-swvcd   1/1     Running   0          3m35s   172.18.0.6   minikube   <none>           <none>

Export port - service https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/
dave@dave java-ee8-minimal]$ kubectl expose deployment/java-ee8-minimal
service/java-ee8-minimal exposed
[dave@dave java-ee8-minimal]$ curl 172.18.0.5:8080
curl: (7) Failed to connect to 172.18.0.5 port 8080: No route to host
[dave@dave java-ee8-minimal]$ kubectl get pods -l app=java-ee8 -o wide |grep podIP
[dave@dave java-ee8-minimal]$ kubectl get pods -l app=java-ee8 -o wide 
NAME                                READY   STATUS    RESTARTS   AGE     IP           NODE       NOMINATED NODE   READINESS GATES
java-ee8-minimal-76cd57679d-l5smd   1/1     Running   0          5m28s   172.18.0.5   minikube   <none>           <none>
java-ee8-minimal-76cd57679d-swvcd   1/1     Running   0          5m28s   172.18.0.6   minikube   <none>           <none>
[dave@dave java-ee8-minimal]$ kubectl get svc java-ee8-minimal
NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
java-ee8-minimal   ClusterIP   10.103.186.17   <none>        8080/TCP   35s
[dave@dave java-ee8-minimal]$ kubectl get svc java-ee8-minimal
NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
java-ee8-minimal   ClusterIP   10.103.186.17   <none>        8080/TCP   40s
[dave@dave java-ee8-minimal]$ kubectl get ep java-ee8-minimal
NAME               ENDPOINTS                         AGE
java-ee8-minimal   172.18.0.5:8080,172.18.0.6:8080   61s
[dave@dave java-ee8-minimal]$ curl 172.18.0.5:8080/java-ee8-minimal/resources/ping
curl: (7) Failed to connect to 172.18.0.5 port 8080: No route to host
[dave@dave java-ee8-minimal]$ kubectl exec my-nginx-3800858182-jr4a2 -- printenv | grep SERVICE^C
[dave@dave java-ee8-minimal]$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
java-ee8-minimal-76cd57679d-l5smd   1/1     Running   0          7m18s
java-ee8-minimal-76cd57679d-swvcd   1/1     Running   0          7m18s
[dave@dave java-ee8-minimal]$ kubectl exec java-ee8-minimal-76cd57679d-l5smd -- printenv | grep SERVICE
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.96.0.1
[dave@dave java-ee8-minimal]$ kubectl get services kube-dns --namespace=kube-system
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   26h
[dave@dave java-ee8-minimal]$ kubectl run curl --image=radial/busyboxplus:curl -i --tty
If you don't see a command prompt, try pressing enter.
[ root@curl:/ ]$ nslookup  java-ee8-minimal
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

nslookup: can't resolve 'java-ee8-minimal'
[ root@curl:/ ]$ curl 172.18.0.5:8080/java-ee8-minimal/resources/ping

Add ingress https://kubernetes.io/docs/concepts/services-networking/ingress/ https://github.com/dveselka/java-ee-8/blob/main/java-ee8-minimal/ingress.yaml
$ kubectl apply -f ingress.yaml 
ingress.networking.k8s.io/java-ee8-ingress configured
[dave@dave java-ee8-minimal]$ kubectl describe ingress  java-ee8-ingress
Name:             java-ee8-ingress
Namespace:        default
Address:          
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host        Path  Backends
  ----        ----  --------
  *           
              /java-ee8-minimal   java-ee8-minimal:8080 (172.18.0.5:8080,172.18.0.6:8080)
Annotations:  nginx.ingress.kubernetes.io/rewrite-target: /
Events:       <none>

SSH into container inside k8s cluster

[dave@dave java-ee8-minimal]$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
curl                                1/1     Running   1          3h49m
java-ee8-minimal-76cd57679d-l5smd   1/1     Running   0          3h58m
java-ee8-minimal-76cd57679d-swvcd   1/1     Running   0          3h58m
[dave@dave java-ee8-minimal]$ kubectl exec --stdin --tty java-ee8-minimal-76cd57679d-l5smd  -- /bin/bash
[jboss@java-ee8-minimal-76cd57679d-l5smd ~]$ curl localhost:8080/java-ee8-minimal/resources/ping
Enjoy Jakarta EE with MicroProfile 2+!
Add JBOss Wildfly admin user
[jboss@java-ee8-minimal-76cd57679d-l5smd ~]$ cd wildfly/
[jboss@java-ee8-minimal-76cd57679d-l5smd wildfly]$ ls
LICENSE.txt  README.txt  appclient  bin  copyright.txt  docs  domain  jboss-modules.jar  modules  standalone  welcome-content
[jboss@java-ee8-minimal-76cd57679d-l5smd wildfly]$ cd bin
[jboss@java-ee8-minimal-76cd57679d-l5smd bin]$ ./add-user.sh

What type of user do you wish to add? 
 a) Management User (mgmt-users.properties) 
 b) Application User (application-users.properties)
(a): 

Access Wildfly admin console Forward admin port
$ kubectl port-forward  java-ee8-minimal-76cd57679d-l5smd 9990:9990
Forwarding from 127.0.0.1:9990 -> 9990
Forwarding from [::1]:9990 -> 9990
Handling connection for 9990

Access console via browser using
http://localhost:9990/console
Access JBoss using minikube service
$ minikube service java-ee8-service
|-----------|------------------|-------------|-------------------------|
| NAMESPACE |       NAME       | TARGET PORT |           URL           |
|-----------|------------------|-------------|-------------------------|
| default   | java-ee8-service |        8080 | http://172.17.0.2:30919 |
|-----------|------------------|-------------|-------------------------|
🎉  Opening service default/java-ee8-service in default browser...

Instance running at http://172.17.0.2:30919/
$ wget -O - http://172.17.0.2:30919 
--2021-03-20 09:33:06--  http://172.17.0.2:30919/
Connecting to 172.17.0.2:30919... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1504 (1.5K) [text/html]
Saving to: ‘STDOUT’

-                                         0%[                                                                                ]       0  --.-KB/s               <!DOCTYPE html>

<html>
<head>
    <!-- proper charset -->
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />

    <title>Welcome to WildFly</title>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link rel="StyleSheet" href="wildfly.css" type="text/css">
</head>

<body>
<div class="wrapper">
    <div class="content">
        <div class="logo">
                <img src="wildfly_logo.png" alt="WildFly" border="0" />
        </div>
        <h1>Welcome to WildFly</h1>

        <h3>Your WildFly instance is running.</h3>

        <p><a href="https://docs.wildfly.org">Documentation</a> | <a href="https://github.com/wildfly/quickstart">Quickstarts</a> | <a href="/console">Administration
            Console</a> </p>

        <p><a href="https://wildfly.org">WildFly Project</a> |
            <a href="https://community.jboss.org/en/wildfly">User Forum</a> |
            <a href="https://issues.jboss.org/browse/WFLY">Report an issue</a></p>
        <p class="logos"><a href="https://www.jboss.org"><img src="jbosscommunity_logo_hori_white.png" alt="JBoss and JBoss Community" width=
                "195" height="37" border="0"></a></p>

        <p class="note">To replace this page simply deploy your own war with / as its context path.<br />
            To disable it, remove the "welcome-content" handler for location / in the undertow subsystem.</p>
    </div>
</div>
</body>
</html>
-                                       100%[===============================================================================>]   1.47K  --.-KB/s    in 0s      

2021-03-20 09:33:06 (36.7 MB/s) - written to stdout [1504/1504]

No comments:

Post a Comment