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

Methods for Kubernetes to deploy and configure Deploymen, network mapping, and replica sets

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "Kubernetes deployment and configuration of Deploymen, network mapping, replica set", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Kubernetes deployment and configuration of Deploymen, network mapping, replica set" article.

Deployment

Deployment is a self-repairing mechanism provided by Kubernetes to solve the problem of machine fault maintenance.

When we deploy the application using docker alone, we can use the-- restart=always parameter to restart the application after it has hung up, for example:

Docker run-itd-- restart=always-p 666V 80 nginx:latest

However, this method can only restart the container and does not have the ability to recover from machine failure.

Kubernetes Deployment is a configuration that instructs Kubernetes to create and update your deployed application instance. After creating Deployment, Kubernetes master dispatches the application to each node in the cluster. Kubernetes Deployment provides a different approach to application management.

There are two ways to create a Deployment, one is to create it directly with the command, and the other is through yaml, which we will introduce later.

Create Deployment

Let's deploy a Nginx application.

Kubectl create deployment nginx-image=nginx:latest

Execute docker ps on the worker node, and you can see:

Root@instance-2:~# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESfe7433f906a0 nginx "/ docker-entrypoint." 7 seconds ago Up 6 seconds k8s_nginx_nginx-55649fd747-wdrjj_default_ea41dcc4-94fe-47f9-a804-5b5b1df703e9_0

Get all deployment:

Kubectl get deploymentsNAME READY UP-TO-DATE AVAILABLE AGEnginx 1/1 1 1 2m24s

More detailed information can be obtained using kubectl describe deployment nginx.

Using kubectl get events, you can get a detailed event record of the process from creating a Deployment to deploying a container.

Successfully assigned default/nginx-55649fd747-wdrjj to instance-2Pulling image "nginx:latest" Successfully pulled image "nginx:latest" in 8.917597859sCreated container nginxStarted container nginxCreated pod: nginx-55649fd747-wdrjjScaled up replica set nginx-55649fd747 to 1

We can also use the yaml file to create a Deployment:

Kubectl apply-f https://k8s.io/examples/controllers/nginx-deployment.yaml Export yaml

Either way, we can export the yaml file from the Deployment we have created and export it using-o yaml (- o json export json).

Kubectl get deployment nginx-o yaml# save to file # kubectl get deployment nginx-o yaml > mynginx.yaml

Then the terminal will print:

ApiVersion: apps/v1kind: Deploymentmetadata: annotations: deployment.kubernetes.io/revision: "1" creationTimestamp: "2021-04-21T00:37:13Z" generation: 1 labels: app: nginx name: nginx namespace: default... ...

We can try to export yaml to a mynginx.yaml file, and then we delete the Deployment.

Kubectl delete deployment ngin

Then use the exported mynginx.yaml to create another Deployment.

Kubectl apply-f mynginx.yamlkubectl apply/create

When we create a deployment, the kubectl create effect is the same as the kubectl apply effect, but apply also has the update function.

Kubectl apply will find the tripartite difference between the previous configuration, the input provided, and the current configuration of the resource to determine how to modify the resource. The kubectl apply command will compare the pushed version with the previous version and apply your changes, but will not automatically overwrite any properties that you have not specified to change.

In addition, kubectl replace and kubectl edit,kubectl replace are destructive updates / replacements, which can easily lead to problems; kubectl edit can update deployment.

According to Kubernetes's official documentation, resources should always be created using kubectl apply or kubectl create-- save-config.

Here's the difference between creating a deployment.

If you create it using create, the command format:

Kubectl create deployment {name of deployment}-- image= {image name}

If you create it using the apply command, you need to specify some information in yaml:

Kind: Deployment... ... medatada: name:nginx... ... Spec: containers:-image: nginx:latest

Then execute the kubectl apply-f xxx.yaml file.

One is kubectl create deployment; the other is kubectl apply-f, which specifies kind: Deployment in yaml.

Sometimes we don't know if our create command or yaml is correct, so we can use the-- dry-run=client,-- dry-run=client parameter to preview without actually submitting.

Kubectl create deployment testnginx-image=nginx:latest-dry-run=client

In some K8s authentication, we don't have time to write yaml little by little, but we need to customize it. We can use-- dry-run=client-o yaml at this time, which can either disable Deployment or export yaml files.

Kubectl create deployment testnginx-image=nginx:latest-dry-run=client-o yaml

In addition to deployment, other kubernetes objects can also use this method in the format kubectl {object} {parameter}-- dry-run=client-o yaml.

Kubernetes objects / resources, such as deployment, job, role, namespace, etc.

Another point is that it doesn't matter whether you take s or not when you kubectl get xxx, for example, kubectl get nodes / kubectl get node is the same.

In general, however, semantically, we can use kubectl get nodes when we get all objects, and kubectl get node nginx when we get specific objects. Similar, kubectl describe nodes, kubectl describe node nginx. In fact, it is the same whether you add s or not.

Network port mapping and updating Deployment

For docker, we can use docker...-p 6666 deployment 80 when we want to map ports, so what do we do when deploying container applications for deployment?

We can take a look at the https://k8s.io/examples/controllers/nginx-deployment.yaml file

Spec: containers:-name: nginx image: nginx:1.14.2 ports:-containerPort: 80

Instead of using the yaml file directly here, let's continue to use the previous yaml file. Let's deploy a nginx first.

Kubectl apply-f mynginx.yaml

Then modify the mynginx.yaml file, find image: nginx:latest, and add the port mapping at the end.

Spec: containers:-image: nginx:latest imagePullPolicy: Always name: nginx ports:-containerPort: 80 protocol: TCP... ... Note: three lines ports:-containerPort: 80 protocol: TCP have been added to it.

Then delete two rows of fields:

ResourceVersion: "109967" uid: e66201e3-a740-4c1c-85f5-a849db40a0fd

Because these two fields limit the version and uid, you can directly manipulate the deployment of nginx when you replace or update them.

View deployment, pod:

Kubectl get deployment,podNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/nginx 1 Running 1 1 5m44sNAME READY STATUS RESTARTS AGEpod/nginx-55649fd747-9vfrx 1 Running 0 5m44s

Then we create service.

Kubectl expose deployment nginx

Or specify a port:

Kubectl expose deployment nginx-port=80-target-port=8000

View service:

Kubectl get servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGEkubernetes ClusterIP 10.96.0.1 443/TCP 24hnginx ClusterIP 10.101.245.225 80/TCP 5m57s

View the port:

Kubectl get epNAME ENDPOINTS AGEkubernetes 10.170.0.2:6443 25hnginx 192.168.56.24:80 17m

View Pod information:

Kubectl describe pod nginx | grep Node:Node: instance-2/10.170.0.4

Because the application / pod deployed by deployment will not be on master, different Node cannot be accessed.

Using the ip queried by kubectl get services or kubectl get ep, we can access it directly on the worker node.

For example, the ip queried by the author can be accessed in this way in worker.

Curl 192.168.56.24:80curl 10.101.245.225:80ReplicaSet

We execute the kubectl get deployments command and output:

NAME READY UP-TO-DATE AVAILABLE AGEnginx 1/1 1 1 38m

NAME lists the name of the Deployment in the cluster.

READY displays the number of copies available for the application. The mode displayed is "ready / expected".

UP-TO-DATE shows the number of copies that have been updated to achieve the desired state.

AVAILABLE displays the number of copies of the application available to the user.

AGE shows how long the application is running.

Copy

Because in containerized applications, according to the methodology and core ideas of cloud native 12 factors, an Processes should be stateless, and any persistent data should be stored in the back-end service. Therefore, An image, launch N docker containers with ports 801, 802, 803... they are all the same, which container we access, the final services provided are the same.

However, if we put these containers into different Node, and then through K8s, we can distribute traffic among multiple instances, that is, load balancing.

In Deployment, you can set the number of copies by specifying the .spec.replicas field of the yaml file or by using the command parameter-- replicas=.

ReplicaSet

"the purpose of ReplicaSet is to maintain a stable set of Pod replicas that are running at all times. Therefore, it is usually used to ensure the availability of a given number of identical Pod."

Interested readers can read the document: https://kubernetes.io/zh/docs/concepts/workloads/controllers/replicaset/

Earlier, we have created the nginx, and then we modify the number of copies in this deployment.

Kubectl scale deployment nginx-replicas=3

Then wait a few seconds and then execute kubectl get deployments to see the results.

NAME READY UP-TO-DATE AVAILABLE AGEnginx 3/3 3 3 3h25m

Execute kubectl get pod-o wide to output pod information of the information.

NAME READY STATUS ESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-581 1 Running 0 3h21m 192.168.56.24 instance-2 nginx-582 1 Running 0 3m30s 192.168.56.25 instance-2 nginx-583 1 Running 0 3m30s 192.168.56.26 instance-2 # Note The author deleted part of the name of Name.

You can see that these pod are all assigned to the instance-2 node, because I only have one worker node server, and if I create a few more node servers, K8s will be automatically assigned to different nodes. As you can see from the output of what, each pod has its own ip address.

When we delete pod using kubectl delete xxx, Deployment automatically maintains three replica sets, so the new pod is automatically enabled.

Perform kubectl get ep to see the ports exposed by different pod.

NAME ENDPOINTS AGEkubernetes 10.170.0.2 Kubernetes 6443 28hnginx 192.168.56.24 3h25m 80192.168.56.25 3h25m above is the content of this article on how to deploy and configure Deploymen, network mapping and replica sets in Kubernetes I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report