Kubernetes-Kubernetes deploys "containerized applications" (2)

Before reading this article, please refer to

Kubernetes - Detailed explanation of Kubernetes; installation and deployment - MinggeQingchun's blog - CSDN blog

1. Kubernetes deploys "containerized applications" (testing kubernetes clusters)

1. Containerized application

In layman's terms, it is to deploy a program in Docker, and this Docker application is a containerized application.

For example: Deploying a SpringBoot in Docker, this Docker+SpringBoot together is a containerized application

The core idea of ​​Docker  is how to integrate applications into containers and actually run them in containers.

The process of integrating applications into containers and running them is called "Containerizing" (Containerizing), sometimes also called "Dockerizing" (Dockerizing).

Containers are born for applications. Specifically, containers can simplify the process of building, deploying, and running applications.

2. Docker deployment application

(1) Write application code

(2) Create a Dockerfile that includes a description of the current application, dependencies, and how to run the application

(3) Execute the docker image build command on the Dockerfile

(4) Wait for Docker to build the application into the Docker image

Once the application is containerized (that is, the application is packaged as a Docker image), it can be delivered as an image and run as a container

3. k8s deployment application

(1) Write application code

(2) Create a Dockerfile that includes a description of the current application, dependencies, and how to run the application

(3) Mirror the application through K8S

1. Deploy an Nginx in the Kubernetes cluster

1. Pull an nginx mirror on the Internet

kubectl create deployment nginx --image=nginx

2. Expose a port 80 to the outside world

kubectl expose deployment nginx --port=80 --type=NodePort

3. Check the external port

kubectl get pod,svc

4. Access address: http://NodeIP:Port

2. Deploy a Tomcat in the Kubernetes cluster

kubectl create deployment tomcat --image=tomcat
 
kubectl expose deployment tomcat --port=8080 --type=NodePort
 
kubectl get pod,svc

Access address: http://NodeIP:Port

kubectl get related commands

// Get node and service version information

kubectl get node(s)    

// All services in all namespaces

kubectl get service(s)  

// list deployments

kubectl get deployment (deploy)

// List all pods in all namespaces    

kubectl get pod(s)    

//delete service

kubectl delete service nginx

//Delete nginx controller

kubectl delete deployment nginx

//delete pod

kubectl delete pod nginx-6799fc88d8-zc48m (pod name)

//kubectl command help

kubectl --help

3. Kubernetes cluster deployment SpringBoot application

1. Write the application, pack it into jar or war, and upload it to Linux (for example, the blogger put it in the /opt/app/k8s path)

2. Write a Dockerfile file to customize the JDK image

(1) Dockerfile

FROM java:8
MAINTAINER zm
EXPOSE 8081

ADD springboot-1-hello.jar springboot-1-hello.jar

# Dockerfile时区设置
RUN echo 'Asia/Shanghai' >/etc/timezone

ENTRYPOINT ["java","-jar","springboot-1-hello.jar"]

(2) Build and run the image; write scripts here

Note:

After Windows writes a script and uploads it to Linux, an error will be reported: xx.sh: line 2: $'\r': command not found , the solution is described in the following stepping pit module, please refer to

#!/bin/sh

cd /opt/app/k8s

docker rm -f springboot-1-hello
docker rmi springboot-1-hello:latest
docker build -t springboot-1-hello .
docker run -d -p 8081:8081 -e "SPRING_PROFILES_ACTIVE=test" -v /etc/localtime:/etc/localtime -v /data/logs/app/springboot-1-hello:/logs -v /data/files/app/springboot-1-hello:/files -v /etc/hosts:/etc/hosts -v /etc/hostname:/etc/hostname --restart=always --privileged=true --name springboot-1-hello springboot-1-hello:latest

3. docker images view image 

Note:

This image needs to be deployed on the master master node machine and all node slave node machines, otherwise an error  ErrImageNeverPull will be reported ! ! ! 

Container image "springboot-1-hello" is not present with pull policy of Never
Error: ErrImageNeverPull

4. Run the test dry to generate a yaml or json file

// kubectl get  deployment will not create a deployment; it will only output configuration content in yaml format

kubectl create deployment deployment name --image=custom project image name --dry-run -o yaml

// kubectl get  deployment will not create a deployment; it will only output configuration content in json format

kubectl create deployment deployment name --image=custom project image name --dry-run -o json

// Only the configuration file of deploy.yaml will be generated

kubectl create deployment deployment name --image=custom project image name --dry-run -o yaml > yaml file to be generated

// Only the configuration file of deploy.json will be generated

kubectl create deployment deployment name --image=custom project image name --dry-run -o json > json file to be generated

--dry-run means dry run test, will not actually run
-o means test output

as follows:

kubectl create deployment springboot-k8s --image=springboot-1-hello --dry-run -o yaml

 kubectl create deployment springboot-k8s --image=springboot-1-hello --dry-run -o json

kubectl create deployment springboot-k8s --image=springboot-1-hello --dry-run -o yaml > deploy.yaml

或者

kubectl create deployment springboot-k8s --image=springboot-1-hello --dry-run -o json> deploy.json

The content of the deploy.yaml or deploy.json file we generated (in the same directory as the jar package) is consistent with the content of the direct output (you can also manually create the file to write the content)

Viewed through the kubectl get deploy command, the deployment is not really running

5. Deployment

Note:

The image in the deploy.yaml file needs to be pulled from the local, otherwise an error will be reported and the failure to pull the local image will be reported (the kubernetes ( K8S) cluster cannot normally pull the image file in the self-built image warehouse, the default is to pull the image from the central warehouse )

Failed to pull image "springboot-1-hello": rpc error: code = Unknown desc = Error response from daemon: pull access denied for springboot-1-hello, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
  Normal   BackOff    47m (x7 over 50m)    kubelet            Back-off pulling image "springboot-1-hello"
  Warning  Failed     25m (x9 over 50m)    kubelet            Error: ErrImagePull
  Warning  Failed     62s (x191 over 50m)  kubelet            Error: ImagePullBackOff
//检查是否创建了deployments任务
kubectl get deployments

kubectl get pods

//查看pod详细信息
kubectl describe pods pod名称
//查看pod日志
kubectl logs pod名称

Events:
  Type     Reason             Age                From               Message
  ----     ------             ----               ----               -------
  Normal   Scheduled          91s                default-scheduler  Successfully assigned default/springboot-k8s-648db45cd5-42f2j to k8snode
  Warning  ErrImageNeverPull  11s (x8 over 89s)  kubelet            Container image "springboot-1-hello" is not present with pull policy of Never
  Warning  Failed             11s (x8 over 89s)  kubelet            Error: ErrImageNeverPull

Modify the deploy.yaml file, and change the image pull policy imagePullPolicy to Never (the image is pulled from the local, and the default is to pull the image from the central warehouse)

spec:
      containers:
      - image: springboot-1-hello
        name: springboot-1-hello
        imagePullPolicy: Never

The usage of  imagePullPolicy is summarized as follows

name strategy effect
Never Only use local images
Always download mirror every time
IfNotPresent The local image is used first, and there is no need to download it locally

Re-execute  the kubectl apply -f deploy.yaml command

//Check if the deployments task is created: kubectl get deployments

kubectl get pods

//View pod details: kubectl describe pods pod name

//View pod logs: kubectl logs pod name

Error from server (BadRequest): container "springboot-1-hello" in pod "springboot-k8s-648db45cd5-42f2j" is waiting to start: ErrImageNeverPull

Note:

Kubernetes (K8S) cluster deployment with one master and multiple slaves

In order to use local mirroring in this mode, two conditions need to be met:

1. ImagePullPolicy is set to IfNotPresent (if not locally, it will be pulled from the remote warehouse) or Never (only pulled from the local)

2. The slave node must have this image. To be precise, which node is scheduled, the image must exist on that node, otherwise ErrImageNeverPull will be reported

(1) Yml file deployment

kubectl apply -f deploy.yaml  (yaml是资源清单)

等价于

kubectl create deployment springboot-k8s --image=springboot-1-hello

Expose the service port:

kubectl expose deployment springboot-k8s --port=8081 --type=NodePort

At this point you can view the service: kubectl get service

(2) Command mode deployment

kubectl create deployment springboot-k8s --image=springboot-1-hello

Enter the following address in the browser to access, both master and slave node IPs are available 

http://192.168.133.129:8081/springboot-1-hello/hellospringboot

 

Two, step on the pit 

1. K8S executes kubectl xx xx and reports Unable to connect to the server: net/http: TLS handshake timeout

https://blog.csdn.net/MinggeQingchun/article/details/126423450

When executing the kubectl get nodes command to view the Node node, it took 3 to 5 minutes to return the result, and an error was reported. The error message is as follows:

Unable to connect to the server: net/http: TLS handshake timeout

It may be that the memory allocation of the VM virtual machine is too small, or the memory of the master node is too small, because the swap partition is turned off

Therefore, increase the memory of the VM virtual machine from 2G to 4G, restart the virtual machine, and execute kubectl get nodes.

2. xx.sh: line 2: $'\r': command not found (Windows writes Docker to run the script to start the container)

https://blog.csdn.net/MinggeQingchun/article/details/126422635

When we write a script on the Windows system and upload it to Linux, an error will be reported when the script is executed

xx.sh: line 2: $'\r': command not found

Ubuntu workaround:

1、sudo apt-get install tofrodos

2. fromdos sh script file name

Centos solution:

1、yum -y install dos2unix

2. dos2unix sh script file name

3. Master node Ready; Node node NotReady

kubectl get nodes

master node host

Node node host 

Check out the kubelet startup log

journalctl -f -u kubelet

From the last two logs, we can judge that the node node has stopped being proxied

The master startup sequence of k8s in the host is: systemd > kubelet > container components > 
kubernetes

We can judge from systemd to kubelet, because kubelet is an application process, so we check the status of kubelet (master, node master and slave are all executed)

systemctl status  kubelet

We can see from the command line that the kubelet is not loaded (node ​​node slave)

So we use the command to restart and start the service to start it (node ​​node slave)

systemctl restart kubelet
systemctl status  kubelet

At this time, go to check the master node host 

 

4. The SpringBoot application needs to deploy Docker images on the master and all Node node machines

The SpringBoot application needs to deploy the Docker image on the master and all Node node machines. If it is only deployed on the master master node, and the Node node machine is not deployed, otherwise an error ErrImageNeverPull will be  reported  ! ! !

Container image "springboot-1-hello" is not present with pull policy of Never
Error: ErrImageNeverPull

Kubernetes (K8S) cluster deployment with one master and multiple slaves

In order to use local mirroring in this mode, two conditions need to be met:

1. ImagePullPolicy is set to IfNotPresent (if not locally, it will be pulled from the remote warehouse) or Never (only pulled from the local)

2. The slave node must have this image. To be precise, which node is scheduled, the image must exist on that node, otherwise ErrImageNeverPull will be reported

Guess you like

Origin blog.csdn.net/MinggeQingchun/article/details/126420188