k8s部署nginx - Go语言中文社区

k8s部署nginx


k8s启动nginx服务

本次需要启动的是一个通过nginx实现的静态文件服务器。

创建命名空间脚本create_shjujubu_namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: shujubu
  labels:
    name: shujubu

执行文件创建 shujubu命名空间:

kubectl create -f  create_shjujubu_namespace.yaml

需要的镜像列表:

nginx                                                                      1.17                62c261073ecf        3 months ago        109MB

定义pod文件 nginx-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: sjb-nginx
  namespace: shujubu
  labels:
    name: sjb-nginx
spec:
  containers:
  - name: nginx
    image: ccr.ccs.tencentyun.com/shujubu/nginx:1.17
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80
    volumeMounts:
    - mountPath: /etc/nginx/nginx.conf
      name: config
    - mountPath: /etc/nginx/conf.d/default.conf
      name: default
    - mountPath: /var/log/nginx/
      name: log
    - mountPath: /usr/share/nginx/shzr/area
      name: shzr
    - mountPath: /usr/share/nginx/zgbs/mujun
      name: mujun
    - mountPath: /usr/share/nginx/zgbs/area
      name: zgbs

  restartPolicy: Never

  volumes:                                                                                                                                                                                           
  - name: config
    hostPath:
      path: /home/users/qix/sjb_bs_source/nginx/nginx.conf
  - name: default
    hostPath:
      path: /home/users/qix/sjb_bs_source/nginx/conf.d/default.conf
  - name: log
    hostPath:
      path: /home/users/qix/sjb_bs_source/nginx/log/
  - name: shzr
    hostPath:
      path: /home/users/qix/sjb_bs_source/shzr/area/
  - name: mujun
    hostPath:
      path: /home/users/qix/sjb_bs_source/zgbs/mujun/
  - name: zgbs
    hostPath:
      path: /home/users/qix/sjb_bs_source/zgbs/area/

上面的pod中只定义了一个容器,并在容器中挂载了nginx的配置文件、日志文件及我们要访问的资源文件。

定义service文件 nginx-np.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    name: sjb-nginx
  type: NodePort
  ports:
    - protocol: TCP
      port: 10008
      targetPort: 80
      nodePort: 30008

注意,service中的selector中的配置要与pod中的labels保持一致。

并附上nginx.conf中的部分配置
user  root;
worker_processes  1;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    gzip  on;
    gzip_min_length 1024K;   # 大于1M的压缩
    gzip_comp_level 5;    # 压缩级别 1-10 数字越大压缩的越好
    gzip_types text/plain application/x-javascript text/css application/xml application/json text/javascript application/x-httpd-php image/jpeg image/gif image/png;   # 压缩的文件类型

    server {
        listen       80;
        server_name  192.168.111.36:80;
        location / {
            root /usr/share/nginx/html;
            index index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
        location ^~ /shzr/area/ {
            root /usr/share/nginx/;
            autoindex on;
        }
        location ^~ /zgbs/mujun/ {
            root /usr/share/nginx/;
            autoindex on;
        }
        location ^~ /zgbs/area/ {
            root /usr/share/nginx/;
            autoindex on;
        }
    }
}

启动pod命令:

kubectl create -f nginx-pod.yaml -n shujubu

启动service命令:

kubectl create -f nginx-np.yaml -n shujubu

大功告成!访问http://IP:30008/查看nginx服务是否正常:

在这里插入图片描述
访问其中一个静态文件:
在这里插入图片描述

O了!

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/wsxx1020/article/details/101520808
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢