Deploy java project within 4 k8s

Keywords: Linux Java Tomcat MySQL Docker

One is to create nfs (maste node operation, two node nodes also need to be installed)

1 First install an nfs server and configure the shared directory.

[yx@tidb-tidb-02 ~]$ cat /etc/exports
/home/yx/hnf *(rw,no_root_squash)
 //Then start nfs

2 Then create a dynamic feed of NFS PV on master, requiring three files, class.yaml deployment.yaml rbac.yaml

These three files are downloaded online https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client/deploy
rbac is to give nfs access to apiserver, where changes are needed to the nfsip address in deploynment.yaml and the directory shared by nfs

kubectl create -f rbac.yaml
kubectl create -f class.yaml
kubectl create -f deployment.yaml #The first time you create it, you are prompted to exist, you don't know why, you delete it, you can create it again

Final check to see if the creation was successful

[yx@tidb-tidb-03 nfs]$ kubectl get pods 
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-89bb89db6-gwdhn 1/1 Running 0 4m46s

2. Build your own dockerhub private warehouse, or register a public warehouse. The following operations operate on the node

1 Pull project code

1 Pull substitution code, via git
git clone https://github.com/lizhenliang/tomcat-java-demo.git
 2 Compile to enter the project directory
 yum install maven install commands required for compilation
mvn clean package
 It takes a long time for the first time

3 Mirror Packaging
 The docker login username and password need to be logged in first. That is the username and password you used when you registered.
sudo docker build -t huningfei/tomcat .   
sudo docker push huningfei/tomcat:latest #uploaded to dockerhub's public repository

3 Create a yaml file for a java project (above master)

1 Create namespace

namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: test

2 Create deployment to create secret authentication first, otherwise you cannot download the image

Because my image was uploaded to the public repository of dockerhub, if it is private, it needs to be followed by--docker-server=dockerhub-ip address

Authentication:

kubectl create secret docker-registry  registry-pull-secret --docker-username=huningfei --docker-password=password --docker-email=huningfei@126.com -n test
## Check to see if the creation was successful

[yx@tidb-tidb-03 java-demo]$ kubectl get secret -n test
NAME                   TYPE                                  DATA   AGE
default-token-t2xwp    kubernetes.io/service-account-token   3      3m4s
registry-pull-secret   kubernetes.io/dockerconfigjson        1      34s

deployment.yaml creates the specified pod

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: tomcat-java-demo
  namespace: test
spec:
  replicas: 3
  selector:
    matchLabels:
      project: www
      app: java-demo
  template:
    metadata:
      labels:
        project: www
        app: java-demo
    spec:
      imagePullSecrets:
      - name: registry-pull-secret
      containers:
      - name: tomcat
        image: huningfei/tomcat:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        resources:
          requests:
            cpu: 0.5
            memory: 1Gi
          limits:
            cpu: 1
            memory: 2Gi
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20

Create deployment

kubectl create -f deployment.yaml   #Establish

[yx@tidb-tidb-03 java-demo]$ kubectl get pods -n test  #Check to see if the creation was successful
NAME                                READY   STATUS    RESTARTS   AGE
tomcat-java-demo-755456cdd6-49rcc   0/1     Running   0          61s
tomcat-java-demo-755456cdd6-khvsc   0/1     Running   0          61s
tomcat-java-demo-755456cdd6-n67dt   0/1     Running   0          61s

Found three tomct containers created successfully

3 Create a service to map ports out

kubectl create -f service.yaml

apiVersion: v1
kind: Service
metadata:
  name: tomcat-java-demo
  namespace: test
spec:
  selector:
    project: www
    app: java-demo
  ports:
  - name: web
    port: 80
    targetPort: 8080

4 Create ingress in order to use domain name access (this file was created by deploying ingress mandatory.yaml in advance)

kubectl create -f ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-java-demo 
  namespace: test
spec:
  rules:
    - host: java.ctnrs.com
      http:
        paths:
        - path: /
          backend:
            serviceName: tomcat-java-demo 
            servicePort: 80

5 Final view of pod and svc

kubectl get pod,svc -n test
NAME                                    READY   STATUS    RESTARTS   AGE
pod/tomcat-java-demo-755456cdd6-49rcc   1/1     Running   0          6m13s
pod/tomcat-java-demo-755456cdd6-khvsc   1/1     Running   0          6m13s
pod/tomcat-java-demo-755456cdd6-n67dt   1/1     Running   0          6m13s

NAME                       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/tomcat-java-demo   ClusterIP   10.0.0.98    <none>        80/TCP    48s

#See ingress on that node
[yx@tidb-tidb-03 java-demo]$ kubectl get pods -n ingress-nginx -o wide
NAME                                        READY   STATUS    RESTARTS   AGE    IP               NODE             NOMINATED NODE
nginx-ingress-controller-7d8dc989d6-sfqcd   1/1     Running   0          2d5h   192.168.18.105   192.168.18.105   <none>

6 Create mysql, database does not need to be accessed by external network, so ip is not generated

mysql.yaml

kubectl create -f mysql.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: test
spec:
  ports:
  - port: 3306 
    name: mysql 
  clusterIP: None
  selector:
    app: mysql-public

---

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: db
  namespace: test
spec:
  serviceName: "mysql"
  template:
    metadata:
      labels:
        app: mysql-public 
    spec:
      containers:
      - name: mysql
        image: mysql:5.7 
        env: 
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
        - name: MYSQL_DATABASE
          value: test
        ports: 
        - containerPort: 3306
        volumeMounts:
        - mountPath: "/var/lib/mysql"
          name: mysql-data
  volumeClaimTemplates:
  - metadata:
      name: mysql-data 
    spec:
      accessModes: ["ReadWriteMany"]
      storageClassName: "managed-nfs-storage"
      resources:
        requests:
          storage: 2Gi 

7 View pv

kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                  STORAGECLASS          REASON   AGE
pvc-73253e56-b425-11e9-b2ea-000c29bfd03f   2Gi        RWX            Delete           Bound    test/mysql-data-db-0   managed-nfs-storage            26m

8 Enter the database and import the table structure

kubectl exec -it db-0 bash -n test

Then import the SQL file into the database, scp-r/home/yx/tomcat-java-demo/db/tables_ly_tomcat.sql onto the maste
 Then use kubeclt to copy directly into the container
 Kubectl cp/tmp/tables_ly_tomcat.sql db-0:/ -n test #db-0 is the name of the pod
 Finally, when you log in to the database, the source can be imported

9 Change the ip of the database, which is the name of mysql pod

vim tomcat-java-demo/src/main/resources/application.yml

Followed by the name of the pod, then the name of the service (serviceName), and finally the namespace namespace

10 The mirror needs to be rebuilt after the change

 mvn clean package
 Then sudo docker build -t huningfei/tomcat:1.0.
Push sudo docker push huningfei/tomcat:1.0 can also specify a different version, such as 2.0

Then you can go to dockhub to see it

11 Scroll update pod

If the mirror name changes, you need to change deployment.yaml first

If not, direct kubectl apply -f deployment.yaml 
Then check to see if the update is scrolling
kubectl get pods -n test
 The following scenario shows that after a scrolling update, the bottom boot completes, one is deleted

12 Last bound domain name, browser view java.ctnrs.com

#View Domain Name
[yx@tidb-tidb-03 java-demo]$ kubectl get ingress -n test 
NAME               HOSTS            ADDRESS   PORTS   AGE
tomcat-java-demo   java.ctnrs.com             80      139m

Final access to the domain name, test if the function is normal

Posted by tom2oo8 on Sat, 10 Aug 2019 11:22:46 -0700