SpringBoot e-commerce project mall (40K + STAR) address: github.com/macrozheng/…

Abstract

Until now, we’ve been using the command line to manage K8S, which, while cool for programmers, was sometimes cumbersome to use. Today we will introduce a K8S visual management tool Rancher, using it can greatly reduce our management of K8S workload, hope to help you!

Rancher profile

Rancher is a container management platform for companies that use containers. Rancher simplifies the process of using K8S, allowing developers to run IT anywhere, meeting IT requirements specifications, and empowering DevOps teams.

Docker installation

There are several ways to install Rancher, but using Docker is easily the easiest! Do not install Docker friends can first install.

  • The installationyum-utils:
yum install -y yum-utils device-mapper-persistent-data lvm2
Copy the code
  • Add docker repository location to yum source:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Copy the code
  • Install the Docker:
yum install docker-ce
Copy the code
  • Start the Docker:
systemctl start docker
Copy the code

Rancher installation

With Docker installed, we are ready to install Rancher. The Rancher already has K8S built in and no additional installation is required. Just like we installed Minikube, the K8S comes right in.

  • First download the Rancher image;
Docker pull rancher/rancher: v2.5 - headCopy the code
  • Once the download is complete, run the Rancher container. Rancher is a bit slow to run and may need to wait a few minutes:
Docker run -p 80:80-p 443:443 --name rancher \ --privileged \ --restart= solved-stopped \ -d rancher/rancher:v2.5-headCopy the code
  • After running, you can access Rancher’s home page. For the first time, you need to set the administrator account password and access the address: https://192.168.5.46

  • Set up Rancher’sServer URL, an address that can be accessed by other nodes if we want to install other nodes.

Rancher use

Let’s start with a simple Rancher.

  • On the home page we can view all the clusters directly. Currently we only have clusters with Rancher installed;

  • To view the cluster status, click the cluster name or click the button in the upper right cornerkubectlCommand;

  • By clicking the Dashboard button, we can view the Dashboard of the cluster, which is much richer in terms of Deployment, Service and Pod information.

Rancher of actual combat

We used to use the command line to operate K8S, this time we use a graphical interface to try. Again, deploy the SpringBoot application as an example, but first deploy MySQL.

Deploy MySQL

  • First we start withyamlCreate Deployment in the form ofDeployments-> Create -> Edit with YAML file;

  • Deployment ofyamlThe content is as followsnamespace: defaultThis line, otherwise it won’t create;
apiVersion: apps/v1
kind: Deployment
metadata:
  Specify the name of the Deployment
  name: mysql-deployment
  Specify the size of the Deployment
  namespace: default
  Tag to specify Deployment
  labels:
    app: mysql
spec:
  # specify the number of Pod copies to create
  replicas: 1
  How to find pods to manage
  selector:
    # manage TAB app for mysql Pod
    matchLabels:
      app: mysql
  # specify the template to create Pod
  template:
    metadata:
      # tag Pod with app:mysql
      labels:
        app: mysql
    # Template specification for Pod
    spec:
      containers:
        - name: mysql
          # specify the container image
          image: Mysql: 5.7
          Specify an open port
          ports:
            - containerPort: 3306
          Set environment variables
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: root
          Use a storage volume
          volumeMounts:
            Mount the storage volume to the internal path of the container
            - mountPath: /var/log/mysql
              name: log-volume
            - mountPath: /var/lib/mysql
              name: data-volume
            - mountPath: /etc/mysql
              name: conf-volume
      # Define a storage volume
      volumes:
        - name: log-volume
          # hostPath Specifies the path of the volume on the host
          hostPath:
            path: /home/docker/mydata/mysql/log
            # create directory when directory does not exist
            type: DirectoryOrCreate
        - name: data-volume
          hostPath:
            path: /home/docker/mydata/mysql/data
            type: DirectoryOrCreate
        - name: conf-volume
          hostPath:
            path: /home/docker/mydata/mysql/conf
            type: DirectoryOrCreate
Copy the code
  • In fact, we can also configure the Deployment properties via the page, if you have ayamlThe configuration is not familiar, you can modify the properties in the page and compare, for examplehostPath.typeThis property, you know what it is;

  • After theyamlCreate a Service in the form ofServices-> Create -> Node ports -> Edit with YAML file;

  • The Service ofyamlAs follows,namespaceAttributes can’t be less;
apiVersion: v1
kind: Service
metadata:
  # define space
  namespace: default
  Other PODS can be accessed by the service name as a domain name
  name: mysql-service
spec:
  Specify the service type and expose the service through a static port on Node
  type: NodePort
  # manage TAB app for mysql Pod
  selector:
    app: mysql
  ports:
    - name: http
      protocol: TCP
      port: 3306
      targetPort: 3306
      Static port on Node
      nodePort: 30306
Copy the code
  • After the deployment is complete, create the Mall database and import related tables. The table address is github.com/macrozheng/…

  • An easy way to import a database is to create a connection through Navicat and configure an SSH channel.

  • The next step is to get the IP address where the Rancher container is running (the address we used to use Minikube in Minikube);
[root@linux-local ~]# docker inspect rancher |grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3"."IPAddress": "172.17.0.3".Copy the code
  • You can then access the database in Rancher just as you would on a Linux server, simply adding Rancher’s IP and database port.

The SpringBoot application is deployed

  • In order toyamlCreate a Deployment for the SpringBoot application in the form ofDeployments-> Create -> Edit with YAML file, the configuration information is as follows:
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: default
  name: mall-tiny-fabric-deployment
  labels:
    app: mall-tiny-fabric
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mall-tiny-fabric
  template:
    metadata:
      labels:
        app: mall-tiny-fabric
    spec:
      containers:
        - name: mall-tiny-fabric
          Docker Hub specifies the image address
          image: Macrodocker/mall - tiny - fabric: 0.0.1 - the SNAPSHOT
          ports:
            - containerPort: 8080
          env:
            Set database connection address
            - name: spring.datasource.url
              value: jdbc:mysql://mysql-service:3306/mall? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
            # specify log file path
            - name: logging.path
              value: /var/logs
          volumeMounts:
            - mountPath: /var/logs
              name: log-volume
      volumes:
        - name: log-volume
          hostPath:
            path: /home/docker/mydata/app/mall-tiny-fabric/logs
            type: DirectoryOrCreate
Copy the code
  • In order toyamlCreate a Service in the form ofServices-> Create -> Node ports -> Edit with YAML file, the configuration information is as follows:
apiVersion: v1
kind: Service
metadata:
  namespace: default
  name: mall-tiny-fabric-service
spec:
  type: NodePort
  selector:
    app: mall-tiny-fabric
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 8080
      Static port on Node
      nodePort: 30180
Copy the code
  • Once created successfully, in the Deployments TAB, we can see that the instance is ready.

External access application

Still use the Nginx reverse proxy to access the SpringBoot application.

  • The Rancher service is already occupied80Port, the Nginx service can only be replaced with a new port, which runs on2080Port;
docker run -p 2080:2080 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/ nginx \ - v/mydata/nginx/conf: / etc/nginx \ - d nginx: 1.10Copy the code
  • After creating the Nginx container, add the configuration filemall-tiny-rancher.confThat will bemall-tiny.macrozheng.comDomain name access reverse proxy to the SpringBoot application in K8S;
server { listen 2080; server_name mall-tiny.macrozheng.com; {proxy_set_header Host $Host :$server_port; Proxy_pass http://172.17.0.3:30180; # change the proxy server address to index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }}Copy the code
  • Then modify the local host file for accessing Linux servers and add the following records.
192.168.5.46 mall-tiny.macrozheng.com
Copy the code
  • After can be directly on the phone access K8S SpringBoot application, access to the address: mall-tiny.macrozheng.com: 2080 / swagger – UI….

conclusion

Using Rancher to visually manage K8S is simple, making deployment and administration of K8S much easier. Deployment can be completed with a Docker command, and the visual interface can view various states of application running. K8S scripts are easy to execute and can be done even under graphical interface Settings that do not write scripts. To sum up: It smells good!

The resources

Rancher official document: docs. The Rancher. Cn/rancher2 /

Project source code address

Github.com/macrozheng/…

In this paper, making github.com/macrozheng/… Already included, welcome everyone Star!