k8s部署nginx的三种方式小结

 更新时间:2023年11月15日 09:14:22   作者:转战容器化  
本文主要介绍了k8s部署nginx的三种方式小结,主要包括直接部署、使用数据卷部署、使用ConfigMap部署,具有一定的参考价值,感兴趣的可以了解一下

使用kubernetes来部署nginx服务,nginx一般是作为服务的入口,其在kubernetes的部署方式也大致相似,我将其分为三种----直接部署、使用数据卷部署、使用ConfigMap部署。个人建议使用ConfigMap部署。

直接部署

这种方式就是直接将nginx复制到容器内部,将其制作为镜像,之后进行部署,优点吗?不知道。缺点在每次更新配置文件时,需要重新制作经镜像

部署步骤

前提:需要有自己的nginx配置文件

  • 拉去nginx官方镜像,建议选择稳定版(stable)
$ docker pull nginx:1.22.0
  • 编写Dockerfile
FROM nginx:1.22.0

# 删除官方nginx镜像默认的配置
RUN rm -rf /etc/nginx/conf.d/default.conf

# 将nginx.conf(自己的nginx配置)添加到默认配置目录下
# 注意:nginx.conf需要与Dockerfile在同一目录
ADD ./nginx.conf /etc/nginx/conf.d/
  • 构建自己的nginx镜像
# 在Dockerfile所在目录执行,v1.0.0是新构建nginx镜像的tag
$ docker build -t nginx:v1.0.0 .
  • 编写nginx-service-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-servie
  name: nginx-service
	# 命名空间,没有可以删除,默认是default
  namespace: hello-world
spec:
  ports:
	# 对外暴露的端口
  - nodePort: 30013
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx-pod
  # NodePort类型可以对外暴露端口
  type: NodePort

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-deploy
  name: nginx-deploy
  namespace: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
      namespace: hello-world
    spec:
      containers:
		# 镜像名称
      - image: nginx:v1.0.0
        name: nginx
        ports:
        - containerPort: 80
        resources: {}
  • 执行yaml文件
$ kubectl apply -f nginx-service-deployment.yaml
  • 通过nodeIp+nodePort进行访问

使用数据卷部署

这种方式是通过将nginx的配置文件以数据卷的形式挂载出来,修改nginx配置文件,只需要修改挂载出来的文件,同时删除pod即可。缺点是需要数据卷做支撑,如nfs等,如果使用pv、pvc,还需要配置pv、pvc文件,在集群模式下不要使用host进行挂载,测试时可以使用;优点是部署好后改动小。

部署步骤

前提:需要有nginx的配置文件,并且配置好nfs共享

  • 配置nfs共享,将nginx配置文件共享出来,略
    注意:挂载的方式只支持文件夹挂载不支持文件挂载,不仅是在nfs配置中,容器的配置中也是一样的
  • 编写nginx-service-deployment.yaml,示例不适用pv、pvc
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-service
  name: nginx-service
  # 命名空间,没有可以删除,默认是default
  namespace: hello-world
spec:
  ports:
	# 对外暴露的端口
  - nodePort: 30013
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx-pod
  # NodePort类型可以对外暴露端口
  type: NodePort

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-deploy
  name: nginx-deploy
  namespace: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-pod
  strategy: {}
  template:
    metadata:
      labels:
        app: nginx-pod
      namespace: hello-world
    spec:
      containers:
      - image: nginx:1.22.0
        name: nginx
        ports:
        - containerPort: 80
        resources: {}
        volumeMounts:
        - name: nginx-config
          mountPath: "/etc/nginx/conf.d/"
      volumes:
      - name: nginx-config
        nfs:
		  # 共享的目录
          path: "/opt/nginx/"
	      server: xxx.xxx.xxx.xxx
  • 执行yaml文件
$ kubectl apply -f nginx-service-deployment.yaml
  • 通过nodeIp+nodePort进行访问

使用ConfigMap进行部署

这种方式是通过ConfigMap的方式将nginx的配置文件挂载出来,修改nginx的配置文件时,只需要修改ConfigMap,同时删除就的pod即可。缺点是配置文件时只读文件,如果对文件有特殊要求的不行;优点是改动小,操作简单。

部署步骤

  • 编写nginx-configmap.yaml
    这里写了一个简单的例子,访问/hello-world/进行跳转
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
  namespace: hello-world
data:
  default.conf: |
    server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

  • 编写nginx-service-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-service
  name: nginx-service
	# 命名空间,没有可以删除,默认是default
  namespace: hello-world
spec:
  ports:
	# 对外暴露的端口
  - nodePort: 30013
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx-pod
	# NodePort类型可以对外暴露端口
  type: NodePort

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-deploy
  name: nginx-deploy
  namespace: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-pod
  strategy: {}
  template:
    metadata:
      labels:
        app: nginx-pod
      namespace: hello-world
    spec:
      containers:
      - image: nginx:1.22.0
        name: nginx
        ports:
        - containerPort: 80
        resources: {}
        volumeMounts:
        - name: nginx-config
          mountPath: "/etc/nginx/conf.d/"
          readOnly: true
      volumes:
      - name: nginx-config
        configMap:
          name: nginx-configmap
  • 执行yaml文件
$ kubectl apply -f nginx-configmap.yaml
$ kubectl apply -f nginx-service-deployment.yaml
  • 通过nodeIp+nodePort进行访问

到此这篇关于k8s部署nginx的三种方式小结的文章就介绍到这了,更多相关k8s部署nginx内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Nginx中root与alias区别讲解

    Nginx中root与alias区别讲解

    这篇文章主要介绍了Nginx中root与alias区别 ,通过两个小例子来学习root和alias的区别,对Nginx中root与alias区别感兴趣的朋友跟随小编一起看看吧
    2022-08-08
  • Nginx中的root&alias文件路径及索引目录配置详解

    Nginx中的root&alias文件路径及索引目录配置详解

    这篇文章主要介绍了Nginx中的root&alias文件路径及索引目录配置,顺带讲解了root和alias命令的用法,需要的朋友可以参考下
    2016-01-01
  • nginx基于tcp做负载均衡的方法

    nginx基于tcp做负载均衡的方法

    这篇文章主要介绍了nginx基于tcp做负载均衡的方法,需要的朋友可以参考下
    2014-08-08
  • LNMP 解决Access Denied错误详细介绍

    LNMP 解决Access Denied错误详细介绍

    这篇文章主要介绍了LNMP 解决Access Denied错误详细介绍的相关资料,需要的朋友可以参考下
    2016-10-10
  • 超实用的Nginx常见配置合集分享

    超实用的Nginx常见配置合集分享

    这篇文章主要为大家详细介绍了超实用的Nginx常见配置合集,文中的示例代码讲解详细,对我们学习或工作有一定的参考价值,感兴趣的可以了解一下
    2022-07-07
  • 详解Nginx服务器和iOS的HTTPS安全通信

    详解Nginx服务器和iOS的HTTPS安全通信

    这篇文章主要介绍了详解Nginx服务器和iOS的HTTPS安全通信的相关资料,需要的朋友可以参考下
    2017-06-06
  • 解决nginx代理 url重写的问题

    解决nginx代理 url重写的问题

    这篇文章主要介绍了解决nginx代理 url重写的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • CentOS6使用nginx搭建web网站服务的方法

    CentOS6使用nginx搭建web网站服务的方法

    这篇文章主要介绍了CentOS6使用nginx搭建web网站服务的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Nginx禁止ip访问或非法域名访问

    Nginx禁止ip访问或非法域名访问

    这篇文章主要介绍了Nginx禁止ip访问或非法域名访问,需要的朋友可以参考下
    2022-04-04
  • Nginx的gzip指令使用小结

    Nginx的gzip指令使用小结

    GZIP就是将文件压缩传输,图片、视频、大文件不建议使用压缩,压缩需要占用你的服务器资源,压缩完效果也不大,今天通过本文给大家如何用好Nginx的gzip指令,感兴趣的朋友一起看看吧
    2022-05-05

最新评论