Monday, April 27, 2020

Append all file content in cmd window

We want to merge all content of file in one specific folder in cmd window use the following example
e.g cd to folder contains files and run the command below.

for %f in (*.sql) do type "%f" >> c:\Test\output.txt

Monday, March 30, 2020

How to start jvm monitor remote

Start jvm: jvisualvm.exe -cp:a C:\work\wildfly10\bin\client\jboss-client.jar


Then enter username/password

Friday, February 21, 2020

How to use kubectl proxy to access service in k8s

1. Run kubectl proxy with port
kubectl proxy --port 8080

2. Then we can access service via api in local example
http://localhost:8080/api/v1/namespaces/devgcp/services/luz-person:8080/proxy/luz_person/api/version


Friday, December 20, 2019

Change the default PGDATA directory on Windows

In case we need to change the default postgres data folder just because we have an existing postgres data folder. So that we can do like the following to archive it.

1. First we have to stop postgres service in window
2. Open regedit by typing in window + R.
3. Look into the directory link and edit it to the corresponding postgres data folder
Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\postgresql-x64-9.5

Tuesday, November 12, 2019

Azure Deploy Ingress Controller For Micro Fontend

Install helm chart
https://docs.microsoft.com/bs-cyrl-ba/azure/aks/kubernetes-helm

Create a file named helm-rbac.yaml and copy in the following YAML:
apiVersion: v1 kind: ServiceAccount metadata: name: tiller namespace: kube-system --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: tiller roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: tiller namespace: kube-system

Create the service account and role binding with the kubectl apply command:
kubectl apply -f helm-rbac.yaml

Sunday, November 10, 2019

Azure build deploy to k8s complete in azure cloud shell

CREATE RESOURCE

# List all location for create resource
az account list-locations

# Create resource
az group create --name myDevResource --location southeastasia

#Clean up resource
This command line will clean up everything under this resource like k8s, database, container register...
az group delete --name myDevResource

Friday, October 4, 2019

How to setup Jenkin Container for building docker image and deploy application to Google Cloud

I Start jenkin original

1. Follow this link and then you can start your jenkin container
https://jenkins.io/doc/book/installing/

docker run -u root --rm -p 9090:8080 -p 50000:50000 -v D:\Jenkins-Data:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkinsci/blueocean

II Custom jenkin
At this custom I will add kubectl into custom image that for using kubectl later to create deployment and expose service in Google Cloud.

1. Dockerfile
FROM jenkinsci/blueocean

User root

COPY --from=lachlanevenson/k8s-kubectl:v1.10.3 /usr/local/bin/kubectl /usr/local/bin/kubectl

EXPOSE 8080 50000

Build container

docker build -t myjenkin .

Run

docker run -u root --rm -p 9090:8080 -p 50000:50000 -v D:\Jenkins-Data:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock myjenkin

3. Run jenkin
3.1. Access url localhost:9090 from browser and you set it up like a normal

Sunday, September 22, 2019

Mysql export/import database

You want to export database run the following command
mysqldump -u [user] -p [dbName] > D:\output.sql

In case you want to import data to database from sql file run the following command
mysql -u [user] -p [dbName]  < D:\output.sql

Git: Create new branch in local

For example we are in branch B and now we have to commit some code to new branch C
Instead of going to gitserver like bitbucket or gitlab to create new remote branch C and then check out from local and then switch to new branch to work.

We can use the command below to create new branch C while you are in branch B.
git checkout -b [branchC]
Then you can do your job and commit like usual.

Thursday, September 5, 2019

How to install portable MySql

1. Download mysql version 5.7.57 zip or what ever
2. Extract to folder for example D:\MySql57
3. Open terminal and run command D:\MySql57\bin>mysqld --initialize for initialize folder data.
4. Create file init.txt in folder D:\MySql57 with content ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
5. Now we can start mysql by command: D:\MySql57\bin>mysqld --init-file=D:\MySql57\init.txt --console
Now we can download mysql client tool to connect to Mysql server and enjoy.

Friday, August 23, 2019

Create user with all permission on database in postgres

Sometime we want to create a user and role in postgresql that he can create a new database, schema and so on so we can the like this below.

CREATE USER user1 WITH PASSWORD 'userPassword';
CREATE DATABASE dbUser1 OWNER user1;
GRANT ALL PRIVILEGES ON DATABASE dbUser1 TO user1;
GRANT ALL ON DATABASE dbUser1 TO user1;
ALTER ROLE user1 WITH LOGIN SUPERUSER INHERIT CREATEDB CREATEROLE NOREPLICATION;

Postgres psql command usage

+ Login to postgres server
psql -U [username: postgres] -d [dbname: postgtes] -h [IP server: 127.0.0.1] -p [5432]

+ List all databases
[postgres$] \l

+ Connect to database
[postgres$] \c [db name]

+ Show all table within table
[postgres$] \dt

+ Show all user
[postgres$] \du

+ Show connection info
[postgres$] \coninfo


Monday, August 12, 2019

Gcloud create deployment by yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-deployment
spec:
  replicas: 1
  selector:
    matchLabels:              # tells what pods the deployment will apply to, refer to line number 11 for pod template
      app: dev-backend
  template:                   # actually, this is the pod template, describes a pod that is launched
    metadata:
      labels:
        app: dev-backend # this should match with the matchLabels value above, otherwise, this kind of pod will not be controlled by the above deployment
    spec:
      containers:
      - name: backend-container
        image: asia.gcr.io/mhdanh-hello-kubernete/api-backend
        # Add opening port
        ports:                                      #+
        - containerPort: 8080                       #+
          name: http                                #+
        - containerPort: 9990                       #+
          name: http-admin                          #+
---
kind: Service                     #+
apiVersion: v1                    #+
metadata:                         #+
  name: backend-service  #+
spec:                             #+
  selector:                       #+
    app: dev-backend         #+
  ports:                          #+
  - protocol: TCP                 #+
    targetPort: 8080              #+
    port: 8080                    #+
    name: http                    #+
  - protocol: TCP                 #+
    targetPort: 9990              #+
    port: 9990                    #+
    name: http-admin              #+
  type: LoadBalancer              #+
  loadBalancerIP: 35.240.212.150    #++ with the static IP address

Gcloud common commands

+  Login with google cloud platform
gcloud auth login

+  List all projects
gcloud projects list

+  Set project that working on
gcloud config set project [my-project]

+ List all current config include current project in gcloud cli
gcloud config list

+ Access to specific pod in kubernetes
kubectl exec -i -t hellow-world-57499c4b79-ktkn5 /bin/bash

+ Deploy image
kubectl run staticapi --image=eu.gcr.io/[project-id]/staticapi --port 8080

+ Expose deployment to service
kubectl expose deployment staticapi --type=LoadBalancer --name=static-api-service --port=8080 --target-port=8080 --load-balancer-ip=34.65.189.62
// --load-balancer-ip=34.65.189.62 is static ip other gcp will assign ephemeral IP

+ Access pod for checking log
kubectl exec -it nginx-1-bc4ffd99c-jtwxc -- /bin/bash

+ Get Pods with namespace
kubectl get pods --namespace [namespace name]

+ View log of pod
kubectl log [pod name] -f --namespace [namespace name]

+ Delete pod
kubectl delete [pod name] --namespace [namespace name]

How to deploy image in google registry to google kubernetes engine

If you already push you image with google registry you can deploy it easily by access that image and deploy to GKE

If you prefer to use command line I can show you like below

1. List all images in google registry
gcloud container images list --repository=eu.gcr.io/myproject

2. Install kubernetes
gcloud components install kubectl

3. Deploy image to GKE
kubectl run hello-server --image gcr.io/google-samples/hello-app:1.0 --port 8080




4. Kubernetes service
[Get] kubectl get services

[Delete] kubectl delete service hello-web
gcloud components install kubectl
gcloud components install kubectl
gcloud components install kubectl

Docker file for build example postgresql database

Here is the docker file
FROM library/postgres:9.5
# echo version
RUN postgres --version

ADD init.sql /docker-entrypoint-initdb.d/

#Set 'postgres' account's password
ENV POSTGRES_PASSWORD=postgres

EXPOSE 5432

CMD ["postgres"]

Below is the file init.sql where we can defined script to init data
CREATE USER admin WITH PASSWORD 'admin' SUPERUSER CREATEDB CREATEROLE;

CREATE DATABASE myDB;
GRANT ALL PRIVILEGES ON DATABASE myDB TO admin;
GRANT ALL PRIVILEGES ON DATABASE myDB TO postgres;

Tuesday, August 6, 2019

Host 'localhost' is not allowed to connect to this MariaDB server


Open file my.ini in mysql/bin

Find my.ini and open.
Simply add single line:
skip-grant-tables
after [mysqld] group.
Example:
...
[mysqld]
skip-grant-tables
port=3306
socket=/tmp/mysql.sock

== then restart and run phpadmin again

https://forum.laragon.org/topic/801/host-localhost-is-not-allowed-to-connect-to-this-mariadb-server

Friday, July 19, 2019

How to push and pull image in google cloud

Regarding this page we can do that with https://cloud.google.com/container-registry/docs/pushing-and-pulling Just some important step to archive this.

1. Install gcloud
- Download and install gcloud sdk GoogleCloudSDKInstaller.exe

2. Config gcloud and docker
- Run command gcloud auth configure-docker
Note: When you see error like below run this command for fixed that gcloud components install docker-credential-gcr

WARNING: `docker-credential-gcloud` not in system PATH.
gcloud's Docker credential helper can be configured but it will not work until this is corrected.
WARNING: `docker` not in system PATH.
`docker` and `docker-credential-gcloud` need to be in the same PATH in order to work correctly together.
gcloud's Docker credential helper can be configured but it will not work until this is corrected.

3. Now create docker image and tag it

[tag image] docker tag helloImag eu.gcr.io/google-project/helloworld

[push image] docker push eu.gcr.io/google-project/helloworld

Wednesday, September 5, 2018

JPA: OneToOne Lazy Load

Mặc định thì lazy load cho trường hợp là OneToOne sẽ không hoạt động trong jpa hoặc là ta sẽ convert qua OneToMany hoặc là ta xài theo cách bên dười là dùng LazyToOne của Hibernate.

1. use @LazyToOne(LazyToOneOption.NO_PROXY)
@OneToOne(cascade = {CascadeType.PERSIST}, mappedBy = "post", fetch = FetchType.LAZY, optional=false)
@LazyToOne(LazyToOneOption.NO_PROXY) 
private PostDetailEntity detail;
1.1 Must define one more property in persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
 version="2.1">
 <persistence-unit name="mydb" transaction-type="JTA">
  <jta-data-source>java:jboss/datasources/mydb</jta-data-source>
  ...
  <properties>
   ...
   <property name="hibernate.ejb.use_class_enhancer" value="true"/>
   ...
  </properties>
 </persistence-unit>
</persistence>
2. Reference
https://stackoverflow.com/questions/45107084/hibernate-lazy-loading-with-lazytoonelazytooneoption-no-proxy

http://justonjava.blogspot.com/2010/09/lazy-one-to-one-and-one-to-many.html