• k8s 编写nginx资源文件


    k8s 编写nginx资源文件

    1. 要求

    • 通过explain定义deployment类型的控制器跑一个nginx的pod,要做端口映射,要能够在宿主机外面访问

    2. 编写用一个资源文件用来运行nginx pod

    [root@k8s-master manifest]# vim nginx-pod.yml 
    [root@k8s-master manifest]# kubectl apply -f nginx-pod.yml 
    deployment.apps/nginx-pod created
    [root@k8s-master manifest]# cat nginx-pod.yml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels: 
        abb: mushuang
        app: test
      name: nginx-pod
      namespace: dev
    spec:
      replicas: 3
      selector: 
        matchLabels:
          app: nginxlab
      template: 
        metadata:
          labels:
            app: nginxlab
          name: containerlab
          namespace: dev
        spec:
          containers: 
          - name: nginx 
            image: nginx:latest
            imagePullPolicy: IfNotPresent
    [root@k8s-master manifest]# 
    
    [root@k8s-master manifest]# kubectl get -f nginx-pod.yml 
    NAME        READY   UP-TO-DATE   AVAILABLE   AGE
    nginx-pod   3/3     3            3           34s
    [root@k8s-master manifest]# kubectl get pods -n dev 
    NAME                         READY   STATUS    RESTARTS   AGE
    nginx-pod-5c7b58d6bb-bltl9   1/1     Running   0          44s
    nginx-pod-5c7b58d6bb-p5hwr   1/1     Running   0          44s
    nginx-pod-5c7b58d6bb-rlzn4   1/1     Running   0          44s
    [root@k8s-master manifest]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    3. 创建service

    [root@k8s-master ~]# kubectl get svc
    NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
    kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   2d21h
    [root@k8s-master ~]# kubectl get pods -n dev
    NAME                         READY   STATUS    RESTARTS   AGE
    nginx-pod-5c7b58d6bb-bltl9   1/1     Running   0          2m43s
    nginx-pod-5c7b58d6bb-p5hwr   1/1     Running   0          2m43s
    nginx-pod-5c7b58d6bb-rlzn4   1/1     Running   0          2m43s
    [root@k8s-master ~]# 
    
        
    [root@k8s-master manifest]# cat nginx-svc.yml
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: nginxlab
      name: containerlab
      namespace: dev
    spec:
      ports:
      - name: svcport
        port: 80
        targetPort: 80
        protocol: TCP 
      selector: 
        app: nginxlab
      type: NodePort
    [root@k8s-master manifest]# 
    
        
    [root@k8s-master manifest]# kubectl apply -f nginx-svc.yml 
    service/containerlab created    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    3.1 查看并在集群中访问
    [root@k8s-master ~]# kubectl get pods -n dev
    NAME                         READY   STATUS    RESTARTS   AGE
    nginx-pod-5c7b58d6bb-bltl9   1/1     Running   0          13m
    nginx-pod-5c7b58d6bb-p5hwr   1/1     Running   0          13m
    nginx-pod-5c7b58d6bb-rlzn4   1/1     Running   0          13m
    [root@k8s-master ~]# kubectl get pods -n dev -o wide
    NAME                         READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
    nginx-pod-5c7b58d6bb-bltl9   1/1     Running   0          14m   10.244.2.39   k8s-node2   <none>           <none>
    nginx-pod-5c7b58d6bb-p5hwr   1/1     Running   0          14m   10.244.1.28   k8s-node1   <none>           <none>
    nginx-pod-5c7b58d6bb-rlzn4   1/1     Running   0          14m   10.244.2.40   k8s-node2   <none>           <none>
    [root@k8s-master ~]# 
    
    [root@k8s-master ~]# kubectl get svc -n dev
    NAME           TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
    containerlab   NodePort   10.99.32.163   <none>        80:30514/TCP   2m47s
    
    [root@k8s-master manifest]# kubectl get -f nginx-svc.yml 
    NAME           TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
    containerlab   NodePort   10.99.32.163   <none>        80:30514/TCP   43s
    [root@k8s-master manifest]# curl 10.99.32.163
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!
    <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!
    <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@k8s-master manifest]#
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    3.2 在浏览器中访问
    • http://192.168.232.128:30514/

    在这里插入图片描述

    3.3 查看deploy和svc标签
    [root@k8s-master manifest]# kubectl get -f nginx-pod.yml 
    NAME        READY   UP-TO-DATE   AVAILABLE   AGE
    nginx-pod   3/3     3            3           29m
    [root@k8s-master manifest]# kubectl get -f nginx-svc.yml 
    NAME           TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
    containerlab   NodePort   10.99.32.163   <none>        80:30514/TCP   17m
    [root@k8s-master manifest]# kubectl get -f nginx-pod.yml --show-labels
    NAME        READY   UP-TO-DATE   AVAILABLE   AGE   LABELS
    nginx-pod   3/3     3            3           29m   app=test
    [root@k8s-master manifest]# kubectl get -f nginx-svc.yml --show-labels
    NAME           TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE   LABELS
    containerlab   NodePort   10.99.32.163   <none>        80:30514/TCP   17m   app=nginxlab
    [root@k8s-master manifest]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    pandas reindex 方法
    【Spring事务的实现原理】
    CSS 基础知识 选择器
    three.js - matcap材质(MeshMatcapMaterial)
    C/C++中的new和delete的实现过程
    js的闭包例题
    每日五题-202111
    [LeetCode 1769]移动所有球到每个盒子所需的最小操作数
    第七章 防火墙
    2023烟台大学计算机考研信息汇总
  • 原文地址:https://blog.csdn.net/mushuangpanny/article/details/126779245