From Scratch: Deploying Nginx Applications Using Kubernetes

[root@icv-k8s-node-1 ~]# kubectl create deployment javaedge-nginx --image=nginx:1.24.0
deployment.apps/javaedge-nginx created
[root@icv-k8s-node-1 ~]# kubectl get all
NAME                                  READY   STATUS    RESTARTS   AGE
pod/javaedge-nginx-6996b98cc4-f28r2   1/1     Running   0          29s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   17h

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/javaedge-nginx   1/1     1            1           29s

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/javaedge-nginx-6996b98cc4   1         1         1       29s
[root@icv-k8s-node-1 ~]# kubectl get all -o wide
NAME                                  READY   STATUS    RESTARTS   AGE   IP             NODE             NOMINATED NODE   READINESS GATES
pod/javaedge-nginx-6996b98cc4-f28r2   1/1     Running   0          15m   10.244.26.65   icv-k8s-node-2   <none>           <none>

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   17h   <none>

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/javaedge-nginx   1/1     1            1           15m   nginx        nginx:1.24.0   app=javaedge-nginx

NAME                                        DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
replicaset.apps/javaedge-nginx-6996b98cc4   1         1         1       15m   nginx        nginx:1.24.0   app=javaedge-nginx,pod-template-hash=6996b98cc4
[root@icv-k8s-node-1 ~]# 

It can be seen that this nginx is not created on the master node, but on the slave node.

1 port exposed

Port 8088 of the pod is mapped as port 8080 of the container. To provide access to the outside world, the pod needs to implement the service service. Only with the
service can it be accessed. A service can be thought of as managing a set of pods. NodePort: expose the service to the outside world

[root@icv-k8s-node-1 ~]# kubectl expose deployment javaedge-nginx  --port=90 --target-port=80 --type=NodePort
service/javaedge-nginx exposed

[root@icv-k8s-node-1 ~]# kubectl get all
NAME                                  READY   STATUS    RESTARTS   AGE
pod/javaedge-nginx-6996b98cc4-f28r2   1/1     Running   0          29m

NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/javaedge-nginx   NodePort    10.99.205.238   <none>        90:32708/TCP   3m48s
service/kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP        18h

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/javaedge-nginx   1/1     1            1           29m

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/javaedge-nginx-6996b98cc4   1         1         1       29m
[root@icv-k8s-node-1 ~]# 
  • 90:32708/TCPin 90is the port of Service
  • 3270832708It is the port of NodePort, indicating that the Service is mapped to the port on the Node

Therefore, the externally exposed port is 32708not 90. In the k8s cluster, <NodeIP>:<NodePort>the Service can be accessed through .

PlantUML Diagram

A new Nodecomponent has been added to represent a Node in a Kubernetes cluster. The port (ie ) of Nodeis running on the . The port of will be mapped to this port on the Node. Therefore, external clients can access the Service through , and is the IP address of the Node, yes .ServiceNodePortNodePort: 32708ServiceNodePort<NodeIP>:<NodePort><NodeIP><NodePort>NodePort: 32708

Browser access:

Shut down one of the nodes. Check the status of the node. The node that was shut down just now is not ready. But check the pod and nginx pod again, which realizes the disaster recovery problem of the middleware. Once there is a downtime, it can be restored and restarted.

Delete service:

kubectl delete service/javaedge-nginx

Delete deployments:

kubectl delete deployment/deployment_name

2 Dynamic scaling (elastic scaling)

expansion

[root@icv-k8s-node-1 ~]# kubectl get allNAME                                  READY   STATUS    RESTARTS   AGE
pod/javaedge-nginx-6996b98cc4-gncwc   1/1     Running   0          13m

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   21h

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/javaedge-nginx   1/1     1            1           3h37m

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/javaedge-nginx-6996b98cc4   1         1         1       3h37m
[root@icv-k8s-node-1 ~]# kubectl scale --replicas=2 deployment javaedge-nginx
deployment.apps/javaedge-nginx scaled
[root@icv-k8s-node-1 ~]# kubectl get all
NAME                                  READY   STATUS    RESTARTS   AGE
pod/javaedge-nginx-6996b98cc4-gncwc   1/1     Running   0          13m
pod/javaedge-nginx-6996b98cc4-n4qkk   1/1     Running   0          8s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   21h

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/javaedge-nginx   2/2     2            2           3h37m

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/javaedge-nginx-6996b98cc4   2         2         2       3h37m
[root@icv-k8s-node-1 ~]#

When there is a sudden high concurrency, it can be temporarily expanded.

Now that the flow is stable, we

Shrinkage

[root@icv-k8s-node-1 ~]# kubectl scale --replicas=1 deployment javaedge-nginx
deployment.apps/javaedge-nginx scaled
[root@icv-k8s-node-1 ~]# kubectl get all
NAME                                  READY   STATUS    RESTARTS   AGE
pod/javaedge-nginx-6996b98cc4-gncwc   1/1     Running   0          16m

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   21h

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/javaedge-nginx   1/1     1            1           3h40m

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/javaedge-nginx-6996b98cc4   1         1         1       3h40m
[root@icv-k8s-node-1 ~]#

It can be seen that pod/javaedge-nginx-6996b98cc4-n4qkk was killed

delete container

kubectl get all
[root@icv-k8s-node-1 ~]# kubectl get all
NAME                                  READY   STATUS    RESTARTS   AGE
pod/javaedge-nginx-6996b98cc4-gncwc   1/1     Running   0          21m

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   21h

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/javaedge-nginx   1/1     1            1           3h45m

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/javaedge-nginx-6996b98cc4   1         1         1       3h45m
## 删除部署信息,pod也会随之删除
[root@icv-k8s-node-1 ~]# kubectl delete deployment.apps/javaedge-nginx
deployment.apps "javaedge-nginx" deleted
[root@icv-k8s-node-1 ~]# kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   21h
[root@icv-k8s-node-1 ~]# 
## 删除service信息
kubectl delete service/javaedge-nginx

3 load balancing

The node type was set before, and now the cluster type can be used to achieve load balancing port: cluster port target-port: internal nginx
default port ClusterIP: cluster mode, k8s will automatically assign a cluster ip.

New deployment

[root@icv-k8s-node-1 ~]# kubectl create deployment edge-nginx --image=nginx:1.24.0
deployment.apps/edge-nginx created
[root@icv-k8s-node-1 ~]# kubectl get all
NAME                                  READY   STATUS    RESTARTS   AGE
pod/edge-nginx-6d57745bc8-pgmdk       1/1     Running   0          37s
pod/javaedge-nginx-6996b98cc4-m84s5   1/1     Running   0          7m49s

NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/javaedge-nginx   NodePort    10.109.194.31   <none>        90:31104/TCP   6m50s
service/kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP        21h

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/edge-nginx       1/1     1            1           37s
deployment.apps/javaedge-nginx   1/1     1            1           7m49s

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/edge-nginx-6d57745bc8       1         1         1       37s
replicaset.apps/javaedge-nginx-6996b98cc4   1         1         1       7m49s
[root@icv-k8s-node-1 ~]# 

port exposure

[root@icv-k8s-node-1 ~]# kubectl expose deployment edge-nginx  --port=88 --target-port=80 --type=ClusterIP
service/edge-nginx exposed
[root@icv-k8s-node-1 ~]# kubectl get all
NAME                                  READY   STATUS    RESTARTS   AGE
pod/edge-nginx-6d57745bc8-pgmdk       1/1     Running   0          77s
pod/javaedge-nginx-6996b98cc4-m84s5   1/1     Running   0          8m29s

NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/edge-nginx       ClusterIP   10.106.187.202   <none>        88/TCP         13s
service/javaedge-nginx   NodePort    10.109.194.31    <none>        90:31104/TCP   7m30s
service/kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP        21h

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/edge-nginx       1/1     1            1           77s
deployment.apps/javaedge-nginx   1/1     1            1           8m29s

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/edge-nginx-6d57745bc8       1         1         1       77s
replicaset.apps/javaedge-nginx-6996b98cc4   1         1         1       8m29s
[root@icv-k8s-node-1 ~]# 

So, you can't name it casually.

expansion

[root@icv-k8s-node-1 ~]# kubectl scale --replicas=3 deployment edge-nginx
deployment.apps/edge-nginx scaled
[root@icv-k8s-node-1 ~]# kubectl get all
NAME                                  READY   STATUS    RESTARTS   AGE
pod/edge-nginx-6d57745bc8-dm998       1/1     Running   0          9s
pod/edge-nginx-6d57745bc8-pgmdk       1/1     Running   0          3m57s
pod/edge-nginx-6d57745bc8-zsw9v       1/1     Running   0          9s
pod/javaedge-nginx-6996b98cc4-m84s5   1/1     Running   0          11m

NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/edge-nginx       ClusterIP   10.106.187.202   <none>        88/TCP         2m53s
service/javaedge-nginx   NodePort    10.109.194.31    <none>        90:31104/TCP   10m
service/kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP        21h

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/edge-nginx       3/3     3            3           3m57s
deployment.apps/javaedge-nginx   1/1     1            1           11m

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/edge-nginx-6d57745bc8       3         3         3       3m57s
replicaset.apps/javaedge-nginx-6996b98cc4   1         1         1       11m
[root@icv-k8s-node-1 ~]# curl http://10.106.187.202:88/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html {
    
     color-scheme: light dark; }
body {
    
     width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@icv-k8s-node-1 ~]# 

nginx returns the same, how to test the realization of load balancing? Let's enter the container through k8s (instead of docker exec -it of docker), and the pod is the smallest unit of k8s, so just enter the name of the pod:

[root@icv-k8s-node-1 ~]# kubectl exec -it edge-nginx-6d57745bc8-dm998 -- /bin/bash
root@edge-nginx-6d57745bc8-pgmdk:/# cd /usr/share/nginx/html
root@edge-nginx-6d57745bc8-dm998:/usr/share/nginx/html# apt-get update
root@edge-nginx-6d57745bc8-dm998:/usr/share/nginx/html# apt-get install
root@edge-nginx-6d57745bc8-zsw9v:/usr/share/nginx/html# vim index.html

ctrl+d to exit the current pod, and then do the same for the other two copies.

Revisit the cluster ip:

curl http://10.106.187.202:88/

You will find that different JavaEdge 00x is printed.

Note: If the capacity is dynamically expanded, the new nginx node will automatically join the pod and enter the effect of load balancing

4 yml description file

When we deployed and operated the nginx container earlier, it was all done by typing commands. In fact, it was very cumbersome and would fail if we were not careful.

We can deploy the container through the yaml configuration file, no need to write the command line. yaml is actually an object description file. We
declare the container to be implemented as an object and specify its related attributes.

For example:

  • deploy deployment as yaml
  • build pod as yaml
  • Implement load balancing as yaml
  • Build service as yaml
  • ……It will be all right

The content in the file can be realized by running: kubectl apply -f xxx.yaml.

apiVersion: apps/v1 ## 同k8s集群版本⼀致,通过kubectl api-versions查看
kind: Deployment ## 本配置⽂件的类型
metadata: ## 元数据,配置的⼀些基本属性和信息
  name: nginx-deployment ## 当前 Deployment 的命名
  labels: ## 标签,定义⼀个或多个资源,类似于 docker t
    app: nginx ## 设置key为app,value为nginx的标签
spec: ## 当前 Deployment 的具体描述,在k8s中的配置
  replicas: 2 ## 指定将要部署的应⽤副本数
  selector: ## 标签选择器,同上labels
    matchLabels: ## 匹配并选择包含标签app:nginx的相关资源
      app: nginx
  template: ## 使⽤Pod模板
    metadata: ## Pod元数据
      labels: ## Pod标签选择,选择包含[app:nginx]标签的Po
        app: nginx
    spec: ## 定义Pod中的容器详细描述
      containers: ## 同docker中的容器

      - name: my-nginx ## 容器名称
        image: nginx:1.24.0 ## 容器所使用的镜像名称及版本号
[root@icv-k8s-node-1 home]# vim ngx-k8s_deployment.yaml
[root@icv-k8s-node-1 home]# kubectl apply -f ngx-k8s_deployment.yaml
deployment.apps/nginx-deployment created
[root@icv-k8s-node-1 home]# 

think now

4.1 What should I do if I shrink to one?

modify yaml

Change the number of copies to 1:

insert image description here

apply

[root@icv-k8s-node-1 home]# kubectl apply -f ngx-k8s_deployment.yaml
deployment.apps/nginx-deployment configured
[root@icv-k8s-node-1 home]# 

Now the number of pods and the number of replicas have become 1:

[root@icv-k8s-node-1 home]# kubectl expose deployment nginx-deployment  --port=88 --target-port=80 --type=NodePort --dry-run -o yaml
W0630 15:27:30.699552    3888 helpers.go:692] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx-deployment
spec:
  ports:
  - port: 88
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
status:
  loadBalancer: {
    
    }
[root@icv-k8s-node-1 home]# 

For this, create a new yaml:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx-deployment
spec:
  ports:
  - port: 89
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
status:
  loadBalancer: {
    
    }
[root@icv-k8s-node-1 home]#  kubectl apply -f k8s_ngx_expose.yaml
service/nginx-deployment created
[root@icv-k8s-node-1 home]# 

According to the port access is also no problem:

Generate corresponding yaml based on existing pod

[root@icv-k8s-node-1 home]# kubectl get pod edge-nginx-6d57745bc8-dm998 -o yaml > k8s_ngx_pod.yaml
[root@icv-k8s-node-1 home]# vim k8s_ngx_pod.yaml
[root@icv-k8s-node-1 home]# 

If you want to create a deployment first, and then create a service, you can also write it in a yaml file, as long as the two parts are separated by "—":

Guess you like

Origin blog.csdn.net/qq_33589510/article/details/131478541