Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Get to know Kubernetes (K8s): start with an example of a stand-alone deployment to implement Java Web applications

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This paper installs Kubernetes through yum, and deploys tomcat+mysql to realize Jave Web application. This application is that the JSP page accesses the Mysql database through JDBC. As long as the program connects to the database correctly, it will automatically complete the creation of the corresponding Table and the preparation of the initialization data. When we access the application through a browser, a page of a table is displayed and the data comes from the database.

This application needs to launch two containers: the Web App container and the MySQL container, and the Web App container needs to access the MySQL container. Now let's take a look at how Java Web applications are implemented through Kubernetes.

Before we continue reading, we need to have a basic understanding of Kubernetes, and we need to understand its basic concepts such as its principle, core architecture, core components and objects, and the relationships between components. You can refer to my previous blog post, "getting to know Kubernetes (K8s): theoretical basis", https://blog.51cto.com/andyxu/2308937.

System environment

Operating system: Centos 7.564 bit

IP address: 192.168.2.238

First, install and deploy Kubernetes (K8s)

1. Disable the firewall service that comes with Centos

Note: there will be a lot of network communication between Kubernetes clusters. It is recommended to turn off the firewall service in a secure internal network environment.

[root@andyxu-test ~] # systemctl disable firewalld [root@andyxu-test ~] # systemctl stop firewalld

2. Install etcd and Kubernetes software (Docker software will be installed automatically)

[root@andyxu-test ~] # yum-y install etcd kubernetes

Note: the version of kubernetes installed in yum is 1.5.2

3. Generate rhsm certificate file

[root@andyxu-test ~] # wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm[root@andyxu-test ~] # rpm2cpio python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm | cpio-iv-- to-stdout. / etc/rhsm/ca/redhat-uep.pem | tee / etc/rhsm/ca/redhat-uep.pem

Note: when creating the container, you need to download the pod-infrastructure:latest image from the redhat site. If you do not have this certificate file, an error will be reported, and Pod will always display the ContainerCreating status.

4. Modify the configuration files of docker and kube-apiserver

The docker configuration file is / etc/sysconfig/docker, and modify the contents of the OPTIONS to

OPTIONS='--selinux-enabled=false-insecure-registry gcr.io'

The kube-apiserver configuration file is / etc/kubernetes/apiserver. Modify the contents of KUBE_ADMISSION_CONTROL and delete the ServiceAccount in the-- admission-control parameter.

KUBE_ADMISSION_CONTROL= "--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"

5. Start all services sequentially

[root@andyxu-test ~] # systemctl start etcd [root@andyxu-test ~] # systemctl start docker [root@andyxu-test ~] # systemctl start kube-apiserver [root@andyxu-test ~] # systemctl start kube-controller-manager [root@andyxu-test ~] # systemctl start kube-scheduler [root@andyxu-test ~] # systemctl start kubelet [root@andyxu-test ~] # systemctl start kube- proxy II, create and configure mysql container

1. Create a Deployment definition file for mysql

The mysql-dep.yaml file is as follows:

ApiVersion: version of extensions/v1beta1 # apiserver kind: Deployment # replica controller deployment, name of management pod and RSmetadata: name: mysql # deployment Globally unique spec: replicas: 1 # Pod copy expected quantity selector: matchLabels: # defines the label of RS app: mysql # the Pod that matches the target has this label strategy: # define the upgrade policy type: RollingUpdate # Rolling upgrade Step-by-step replacement policy template: # create a copy of Pod based on this template (instance) metadata: labels: app: mysql # Pod copy label Corresponding to the definition part of the container in Selector spec: containers: # Pod of RS-name: name of mysql # container image: docker image volumeMounts corresponding to mysql:5.7 # container: # definition of the mount point in the container-name: time-zone # name of the mount point in the container mountPath: / etc/localtime # path to the mount point in the container Can be a file or directory-name: mysql-data mountPath: / var/lib/mysql # the data directory of mysql in the container-name: mysql-logs mountPath: / var/log/mysql # the log directory of mysql in the container ports:-containerPort: 3306 # exposed by the container The port number env: # the environment capacity written to the container-name: MYSQL_ROOT_PASSWORD # defines a variable of mysql's root password value: "123456" volumes: # Local mount To the volume definition section in the container-name: time-zone # data volume name It needs to be the same as the name of the mount point in the container hostPath: path: / etc/localtime # mount the path to the container, and mount the localtime file to the container. Allows the container to use the local time zone-name: mysql-data hostPath: path: / data/mysql/data # directory where mysql data is stored locally-name: mysql-logs hostPath: path: / data/mysql/logs # directory locally stored in mysql logs apiVersion: defines which version of apiserver to use You can check which versions of apiserver are available through the kubectl api-versions command Kind: used to indicate the type of this resource object. For example, the value here is "Deployment", indicating that this is a definition of deployment;spec:RS-related attributes. Spec.selector is the Pod Label selector of RS, that is, to monitor and manage Pod instances with these tags to ensure that there are always and only replicas Pod instances running on the current cluster. Setting replicas=1 here means that only one Mysql Pod instance can be run. Spec.strategy: define the upgrade scheme for Pod. Recreate means to delete all existing Pod and re-create new ones. RollingUpdate means rolling upgrade and gradual replacement policy. Additional parameters are supported when rolling upgrade, such as setting the maximum number of unavailable Pod, minimum upgrade interval, and so on. Spec.template: when the number of Pod running in the cluster is less than replicas, RS will generate a new Pod instance according to the Pod template defined in spec.template. Spec.template.metadata.labels specifies the label of the Pod. It is important to note that the labels here must match the previous spec.selector. Spec.template.spec.containers: the definition of the container, including the name of the container, the docker image used, the mounted data volume, the port number of the service, variables, and so on. Spec.template.spec.volumes: the definition part of the local data volume that needs to be mounted to the container. The name of the data volume should be the same as the name of the mount point in the container. Path defines the local data volume path.

2. Create deployment, RS, Pod and container

During the creation process, you need to download the image first, which will take a long time. You can grab the girl next to you with a cup of tea. Haha, please wait patiently.

[root@andyxu-test ~] # kubectl create-f mysql-dep.yamldeployment "mysql" created

3. Check the operation of the created deployment

[root@andyxu-test] # kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEmysql 1 1 1 8s

Note: all 1s indicate normal operation.

4. Check the operation of ReplicaSet (RS)

[root@andyxu-test] # kubectl get rsNAME DESIRED CURRENT READY AGEmysql-3238461207 1 1 16m

Note: all 1s indicate normal operation.

5. Check the operation of Pod

[root@andyxu-test ~] # kubectl get podNAME READY STATUS RESTARTS AGEmysql-3238461207-vvwt8 1amp 1 Running 0 56m

Note: the value of READY is 1max 1, and the value of STATUS is Running, which indicates that it is running normally.

Since the creation of the Pod takes some time, the status of the STATUS will be ContainerCreating before the container is created, indicating that the container is being created, and all you have to do is wait. After the Pod is created, the status of the STATUS will be Running, and you can check the operation of the container through the docker ps command.

6. Check the operation of the container

[root@andyxu-test ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5252cd76009a mysql:5.7 "docker-entrypoint..." 55 minutes Ago Up 55 minutes k8s_mysql.23f88726_mysql-3238461207-vvwt8_default_72d7bff7-d81c-11e8-a729-000c29dabb02_6b15dcfcf026e79ddad9 registry.access.redhat.com/rhel7/pod-infrastructure:latest "/ usr/bin/pod" 55 minutes ago Up 55 minutes k8s_POD.1d520ba5_mysql-3238461207-vvwt8_default_72d7bff7-d81c-11e8-a729-000c29dabb02_668a091e

7. Check the time of the container in Pod and check whether the time is the same as the local time

[root@andyxu-test ~] # kubectl exec mysql-3238461207-vvwt8 dateThu Oct 25 15:06:15 CST 2018

Note: exec is followed by the name of pod

8. Create a service definition file for mysql

The mysql-svc.yaml file is as follows:

ApiVersion: v1kind: Service # represents the name of Kubernetes Servicemetadata: name: mysql # Service spec: ports:-port: 3306 # Service port number selector: app: mysql # Service corresponding to the Pod tag metadata.name:Service service name spec.ports:Service service port number provided by the corresponding container spec.selector: determine which Pod copies (instances) correspond to this Service

9. Create a Service

[root@andyxu-test ~] # kubectl create-f mysql-svc.yaml service "mysql" created

10. Check the operation of Service

[root@andyxu-test ~] # kubectl get svcNAME CLUSTER-IP EXTERNAL-IP PORT (S) AGEkubernetes 10.254.0.1 443/TCP 4hmysql 10.254.144.64 3306/TCP 57s

Kubernetes assigns a Cluster IP to Service, which is a virtual IP address, and then other newly created Pod in the cluster can connect to and access the mysql service through this Cluster IP+ port number.

Create and configure tomcat containers

1. Create a Deployment definition file for tomcat

The myweb-dep.yaml file is as follows:

ApiVersion: extensions/v1beta1kind: Deploymentmetadata: name: mywebspec: replicas: 1 selector: matchLabels: app: myweb strategy: type: RollingUpdate template: metadata: labels: app: mywebspec: containers:-name: myweb image: kubeguide/tomcat-app:v1 volumeMounts:-name: time-zone mountPath: / etc/localtime-name: tomcat-logs MountPath: / usr/local/tomcat/logs ports:-containerPort: 8080 env:-name: MYSQL_SERVICE_HOST value: '10.254.144.64' # here is the Cluster IP-name: MYSQL_SERVICE_PORT value: '3306' volumes:-name: time-zone hostPath for mysql service: Path: / etc/localtime-name: tomcat-logs hostPath: path: / data/tomcat/logs

2. Create deployment, RS, Pod and container of tomcat

[root@andyxu-test ~] # kubectl create-f myweb-dep.yaml deployment "myweb" created

The creation process takes a long time. Please wait patiently. If the STATUS status of pod is Running, the creation is successful.

3. Create the Service definition file of tomcat

The myweb-svc.yaml file is as follows:

ApiVersion: v1kind: Servicemetadata: name: mywebspec: type: NodePort ports:-port: 8080 nodePort: 30001 selector: app: myweb

This Service enables the public network access mode of NodePort mode. The port is 30001. This port is mapped to port 8080 of the tomcat container.

4. Create Service

[root@andyxu-test ~] # kubectl create-f myweb-svc.yaml service "myweb" created

5. Check the operation of Service

[root@andyxu-test ~] # kubectl get svcNAME CLUSTER-IP EXTERNAL-IP PORT (S) AGEkubernetes 10.254.0.1 443/TCP 5hmysql 10.254.144.64 3306/TCP 24mmyweb 10.254.246.56 8080:30001/TCP 39s

6. You can use curl command to test whether the tomcat service can be accessed properly.

[root@andyxu-test] # curl http://192.168.2.238:30001 IV. Visit the web page through the browser

1. If port 30001 is not available, restart and shut down firewalld Firewall

[root@andyxu-test ~] # systemctl start firewalld [root@andyxu-test ~] # systemctl stop firewalld

Note: because kubernetes will add some policies to iptables, you need to turn off the firewall again to turn off these policies.

2. Access http://192.168.2.238:30001/demo/ through browser

Click "Add..." to add a record and submit

Once submitted, the data is written to the mysql database.

3. Log in to MySQL database for verification

[root@andyxu-test ~] # docker exec-it 5252cd76009a / bin/bashroot@mysql-3238461207-vvwt8:/# mysql- uroot-p123456mysql > use HPE_APPmysql > select * from T_USERS

We can continue to study this example, such as:

Study the format of RS, Service and other files. Be familiar with subcommands of kubectl. Manually stop the container corresponding to a Service and see what happens. Modify the Deployment file, change the number of pod copies, recreate, and observe the results.

This example comes from the Kubernetes authoritative Guide (2nd Edition). I made some changes, as well as error handling, and used Deployment to create it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report