kubernetes deployment MySql and Tomcat application latest tutorial

Please note that before you complete the deployment of MySql and Tomcat applications through this tutorial, make sure your environment has successfully installed kubernetes and docker, otherwise please move centos 8 uses kubeadm to install kubernetes 1.8.2 Install kubernetes and docker.

Original address quoted in this article https://www.blog-china.cn/blog/liuzaiqingshan/home/254/1591608565959 

Change docker image warehouse to domestic Alibaba cloud warehouse

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://1ui7q4j1.mirror.aliyuncs.com"]
}
EOF

Note: the Alibaba cloud warehouse used here is my own generated warehouse. If you want to use your own Alibaba cloud warehouse, please visit the Alibaba cloud official website to obtain it. This process requires you to restart the machine or the docker service

docker pulls Mysql image

docker queries the pull MySql image version

docker search mysql

The following will be returned

NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation...   9592                [OK]
mariadb                           MariaDB is a community-developed fork of MyS...   3486                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create...   702                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   76
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr...   69
centurylink/mysql                 Image containing mysql. Optimized to be link...   61                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r...   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      39                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se...   35
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup...   30                                      [OK]
prom/mysqld-exporter                                                              28                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe...   25
databack/mysql-backup             Back up mysql databases to... anywhere!         20
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   19
circleci/mysql                    MySQL is a widely used, open-source relation...   19
mysql/mysql-router                MySQL Router provides transparent routing be...   16
arey/mysql-client                 Run a MySQL client from a docker container      14                                      [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image...   6
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas...   6                                       [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  5                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici...   3
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta...   1
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl...   0                                       [OK]
monasca/mysql-init                A minimal decoupled init container for mysql    0

docker pulls mysql image

I will pull the image of MySql version 5.7

docker pull mysql:5.7

If the pull is successful, the following information will be returned

Digest: sha256:d16d9ef7a4ecb29efcd1ba46d5a82bda3c28bd18c0f1e3b86ba54816211e1ac4
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker pulls tomcat image

docker view the pull tomcat image

docker search tomcat

This will return the following

NAME                          DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
tomcat                        Apache Tomcat is an open source implementati...   2746                [OK]
tomee                         Apache TomEE is an all-Apache Java EE certif...   79                  [OK]
dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base...   54                                      [OK]
bitnami/tomcat                Bitnami Tomcat Docker Image                     34                                      [OK]
kubeguide/tomcat-app          Tomcat image for Chapter 1                      28
consol/tomcat-7.0             Tomcat 7.0.57, 8080, "admin/admin"              17                                      [OK]
cloudesire/tomcat             Tomcat server, 6/7/8                            15                                      [OK]
aallam/tomcat-mysql           Debian, Oracle JDK, Tomcat & MySQL              13                                      [OK]
arm32v7/tomcat                Apache Tomcat is an open source implementati...   10
rightctrl/tomcat              CentOS , Oracle Java, tomcat application ssl...   6                                       [OK]
maluuba/tomcat7-java8         Tomcat7 with java8.                             5
unidata/tomcat-docker         Security-hardened Tomcat Docker container.      4                                       [OK]
amd64/tomcat                  Apache Tomcat is an open source implementati...   2
jelastic/tomcat               An image of the Tomcat Java application serv...   2
arm64v8/tomcat                Apache Tomcat is an open source implementati...   2
ppc64le/tomcat                Apache Tomcat is an open source implementati...   1
camptocamp/tomcat-logback     Docker image for tomcat with logback integra...   1                                       [OK]
99taxis/tomcat7               Tomcat7                                         1                                       [OK]
oobsri/tomcat8                Testing CI Jobs with different names.           1
i386/tomcat                   Apache Tomcat is an open source implementati...   1
secoresearch/tomcat-varnish   Tomcat and Varnish 5.0                          0                                       [OK]
picoded/tomcat7               tomcat7 with jre8 and MANAGER_USER / MANAGER...   0                                       [OK]
cfje/tomcat-resource          Tomcat Concourse Resource                       0
appsvc/tomcat                                                                 0
s390x/tomcat                  Apache Tomcat is an open source implementati...   0

docker pulls Tomcat image

I will pull the image of Tomcat version 8

docker pull tomcat:8

Pull succeeded, return the following information

Digest: sha256:bb59f86923e098ad54f485853e8f8859e2243cfceae762cccc2e5460ab006237
Status: Downloaded newer image for tomcat:8
docker.io/library/tomcat:8

kubernetes deploy tomcat

kubernetes creates Tomcat rc

First create Tomcat on your server- rc.yaml File and copy in the following

apiVersion: v1      #The version describing the RC object is v1
kind: ReplicationController    #I am now declaring the RC object
metadata:     #In metadata, the description information of this RC object
  name: myweb-tomcat      #This RC object is named myweb-tomcat-1 in the gpf-first namespace. The naming in the same namespace must be different
  namespace: gpf-first  #Use GPF first namespace
spec:     #spec is the concrete description of RC object
  replicas: 1    #I'm going to create five copies, pod, of course
  selector:      #Selectors for selecting objects
    app: myweb    #I chose the pod labeled app: myweb
  template:     #Template, which is used to describe the created pod template
    metadata:   #Metadata for pod template description
      labels:      #Label the following for selector to choose from
        app: myweb   #Label the pod template as app: myweb
    spec:             #Specific description of pod template
      containers:         #Here is the container to be put into the pod template
      - image: docker.io/tomcat:8    #Select mirror
        name: myweb         #Container name
        imagePullPolicy: Never  #Do not use remote mirror
        resources:           #The size of the resource assigned to the container
          limits:
            cpu: "0.5"   #CPU size
            memory: 1Gi  #Memory size
        ports:         #Container port number
        - containerPort: 8080         
        env:          #Set the environment variable for the container, and you can connect mysql with our tomcat
        - name: MYSQL_SERVICE_HOST
          value: 'mysql'
        - name: MYSQL_SERVICE_PORT
          value: '3306'

Execute the following command in the directory of the above file on the server

kubectl create -f tomcat-rc.yaml

This will successfully create a pod for tomcat

Create Tomcat on the server- service.yaml (service discovery), and copy in the following

apiVersion: v1
kind: Service   #Object is service
metadata:
  name: myweb #The name of this object in the namespace
  namespace: gpf-first  #Use GPF first namespace
spec:
  ports:
  - name: myweb-svc  #Port name. Service must specify the port name
    #The key point lies in the difference between the three ports. The container has a port, the Service has a port, and the node has a real port number. Here we associate these three ports
    port: 8080 #Port number of the Service
    targetPort: 8080  #Port number of container exposed
    nodePort: 80  #The real port number of the node
  selector:
    app: myweb   #Service selected pod with tag app: myweb
  type: NodePort

Content no longer explained

Creating service discovery and load balancing for tomcat

kubectl create -f tomcat-service.yaml

This step will create a pod application for tomcat with 80 ports exposed outside the cluster

At this time, you will be able to access your tomcat app through the following url

http://Your physical / virtual ip

Note that it's the ip of the physical / virtual machine, not the svc

1. Note that if your host reports the following error

The Service "myweb" is invalid: spec.ports[0].nodePort: Invalid value: 80: provided port is not in the valid range. The range of valid ports is 30000-32767

This is because the default port range of a node is 30000-32767. You can modify the port range so that the node allows this port

vim /etc/kubernetes/manifests/kube-apiserver.yaml

Find the line -- service cluster IP range, and add the following to the next line

- --service-node-port-range=1-65535

Remember to restart

2. If the following error is reported

node(s) had taints that the pod didn't tolerate.

Please execute the following command

kubectl taint nodes --all node-role.kubernetes.io/master-

kubernetes deploy MySql

kubernetes creates Mysql rc

First create MySQL on your server- rc.yaml File and copy in the following

apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb-mysql
  namespace: gpf-first
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
        containers:
        - image: docker.io/mysql:5.7
          imagePullPolicy: Never  #Do not use remote mirror
          name: mysql
          resources:
            limits:
              cpu: "0.3"
              memory: 0.8Gi
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: "root#dba1"

Execute the following command in the directory of the above file on the server

kubectl create -f mysql-rc.yaml
This will successfully create a mysql pod

Create MySQL on the server- service.yaml (service discovery), and copy in the following

apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: gpf-first
spec:
  ports:
  - name: mysql-svc
    port: 3306
    targetPort: 3306
    nodePort: 31101
  selector:
    app: mysql
  type: NodePort

This step will create a Mysql application pod that exposes 3306 ports in the cluster

At this point, you will be able to use the ip+3306 port of the svc of mysql to access when you deploy the application in tomat

Tags: MySQL Tomcat Docker Kubernetes

Posted on Mon, 08 Jun 2020 22:46:02 -0400 by wee493