SoFunction
Updated on 2025-03-10

Kubernetes deploys instances and configures Deployment, Network Mapping, Replica Sets

Deployment

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

When we use docker to deploy the application separately, in order to restart the application after it is hung, we can use--restart=always Parameters, for example:

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

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

Kubernetes Deployment is a configuration that directs Kubernetes how to create and update the application instances you deploy. After creating the Deployment, the Kubernetes master will schedule the application to various nodes in the cluster. Kubernetes Deployment provides a unique approach to application management.

There are two ways to create a Deployment. One is to create it directly using commands, and the other is to use yaml. We will introduce these two ways to create it later.

Create a Deployment

Let's deploy an Nginx application.

kubectl create deployment nginx --image=nginx:latest

Execute on the worker nodedocker ps, you can see:

root@instance-2:~# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
fe7433f906a0   nginx     "/docker-entrypoint.…"   7 seconds ago    Up 6 seconds              k8s_nginx_nginx-55649fd747-wdrjj_default_ea41dcc4-94fe-47f9-a804-5b5b1df703e9_0

Get all deployments:

kubectl get deployments
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           2m24s

usekubectl describe deployment nginxMore detailed information can be obtained.

usekubectl get eventsA detailed event record of the process of creating a Deployment to the deployment container can be obtained.

Successfully assigned default/nginx-55649fd747-wdrjj to instance-2
Pulling image "nginx:latest"
Successfully pulled image "nginx:latest" in 8.917597859s
Created container nginx
Started container nginx
Created pod: nginx-55649fd747-wdrjj
Scaled up replica set nginx-55649fd747 to 1

We can also create a Deployment using the yaml file:

kubectl apply -f /examples/controllers/

Export yaml

No matter which deployment method we can export yaml files from the already created Deployment and use-o yamlYou can export (-o jsonExport json).

kubectl get deployment nginx -o yaml
# Save to file# kubectl get deployment nginx -o yaml > 

Then the terminal will print:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    /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 file, and then we delete this Deployment.

kubectl delete deployment ngin

Then use the export to create a Deployment.

kubectl apply -f 

kubectl apply/create

When we create a deployment,kubectl createandkubectl apply The effect is the same, butapplyIt also has the function of updating.

kubectl applyThe three-party differences will be found between the previous configuration, the provided input, and the current configuration of the resource to determine how to modify the resource.kubectl applyThe command will compare the pushed version with the previous version and apply the changes you made, but will not automatically overwrite any properties you did not specify the changes.

There is alsokubectl replace 、kubectl editkubectl replaceIt is a destructive update/replacement, which can easily lead to problems;kubectl editYou can update deployment.

According to the official Kubernetes documentation, it should always be usedkubectl applyorkubectl create --save-configCreate a resource.

Let’s talk about the difference between creating deployments.

If created using create, the command format:

kubectl create deployment {deploymentName} --image={Mirror name}

If created using the apply command, some information needs to be specified in yaml:

kind: Deployment
... ...
medatada:
    name:nginx
... ...
    spec:
      containers:
      - image: nginx:latest

Then executekubectl apply -f document.

One iskubectl create deployment;The other iskubectl apply -f, specified in yamlkind: Deployment

Sometimes we don't know if our creation command or yaml is correct, and we can use--dry-run=client ,--dry-run=clientParameters are used to preview without actually submitting.

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

In some k8s certifications, we don’t have time to write yaml a little bit, but we need to customize it, so we can use it at this time--dry-run=client -o yaml, it can not take effect, and it can also 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, the format iskubectl {Object} {Parameter} --dry-run=client -o yaml

kubernetes objects/resources, including deployment, job, role, namespace, etc.

There is another place to talk about,kubectl get xxxWhen, take it or notsIt doesn't matter, for examplekubectl get nodes / kubectl get nodeIt's all the same.

However, generally semantically, when we obtain all objects, we can usekubectl get nodes, when obtaining specific objects, you can usekubectl get node nginx. akin,kubectl describe nodes 、kubectl describe node nginx. In fact, add or notsAll the same.

Network Port Mapping and Update Deployment

For docker, when we want to map ports, we can usedocker ... -p 6666:80 , so how do we deal with deployment container applications?

We can take a look/examples/controllers/document,

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

Here we do not use this yaml file directly, and continue to use the previous yaml file. We first deploy a nginx.

kubectl apply -f 

Then modify the file and findimage: nginx:latest, add a port map after it.

    spec:
      containers:
      - image: nginx:latest
        imagePullPolicy: Always
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
... ...
Note:Added in it
        ports:
        - containerPort: 80
          protocol: TCP
          These three lines。

Then delete two lines of fields:

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

Because these two fields limit the version and uid, you can directly operate on nginx's deployment when replacing or updating.

Check deployment, pod:

kubectl get deployment,pod
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
/nginx   1/1     1            1           5m44s

NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-55649fd747-9vfrx   1/1     Running   0          5m44s

Then we create the service.

kubectl expose deployment nginx

Or specify the port:

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

Check out the service:

kubectl get services
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   24h
nginx        ClusterIP   10.101.245.225   <none>        80/TCP    5m57s

View Port:

kubectl get ep
NAME         ENDPOINTS          AGE
kubernetes   10.170.0.2:6443    25h
nginx        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 the master, different Nodes cannot be accessed.

usekubectl get servicesorkubectl get epWe can access the query ip directly on the worker node.

For example, the ip I query can be accessed in the worker in this way.

curl 192.168.56.24:80
curl 10.101.245.225:80

ReplicaSet

We executekubectl get deploymentsCommand, output:

NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           38m
  • NAMELists the name of the Deployment in the cluster.
  • READYDisplays the number of available copies of the application. The displayed mode is "Ready Number/Desired Number".
  • UP-TO-DATEDisplays the number of replicas that have been updated to achieve the expected state.
  • AVAILABLEDisplays the number of copies that the application can be used by the user.
  • AGEShows the time the application is running.

Copy

Because in containerized applications, according to the methodology and core ideas of 12 factors in cloud native, aProcessesIt should be stateless, and any persistent data must be stored in the backend service. Therefore, A mirrors, starts N docker containers, with ports 801, 802, 803... They are actually the same. Which container we access, the service we ultimately provide is the same.

However, if we put these containers into different Nodes and then pass k8s, we can allocate traffic between multiple instances, that is, load balancing.

In Deployment, you can specify the yaml file's.Field or command parameters--replicas=Set the number of copies.

ReplicaSet

“The purpose of ReplicaSet is to maintain a stable collection of Pod replicas that are running at any time. Therefore, it is often used to ensure the availability of a given number of exactly the same Pods.”

Interested readers can read the documentation:/zh/docs/concepts/workloads/controllers/replicaset/

In the previous step, we have created nginx , and next we modify the number of replicas in this deployment .

kubectl scale deployment nginx --replicas=3

Then wait a few seconds before executingkubectl get deploymentsView the results.

NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   3/3     3            3           3h15m

implementkubectl get pod -o widePod information that can output information.

NAME       READY   STATUS   ESTARTS   AGE     IP              NODE     NOMINATED NODE   READINESS GATES
nginx-581   1/1     Running   0     3h11m   192.168.56.24   instance-2   &lt;none&gt;           &lt;none&gt;
nginx-582   1/1     Running   0     3m30s   192.168.56.25   instance-2   &lt;none&gt;           &lt;none&gt;
nginx-583   1/1     Running   0     3m30s   192.168.56.26   instance-2   &lt;none&gt;           &lt;none&gt;
# Note,The author deleted itNamePartial name of

You can see that these pods are allocated ininstance-2On this node, because I only have one worker node server, if I create several more node servers, k8s will be automatically assigned to different nodes. You can also see the output of something, each pod has its own ip address.

When we usekubectl delete xxxWhen deleting a pod, Deployment will automatically maintain three replica sets, so the new pod will be automatically enabled.

implementkubectl get epYou can see the ports exposed by different pods.

NAME         ENDPOINTS                                            AGE
kubernetes   10.170.0.2:6443                                      28h
nginx        192.168.56.24:80,192.168.56.25:80,192.168.56.26:80   3h15m

This is the end of this article about deploying Kubernetes instances and configuring deployment, network mapping, and replica sets. I hope it will be helpful to everyone's learning and I hope everyone will support me more.