K8S--安装Nginx

原文网址:K8S--安装Nginx-CSDN博客

简介

本文介绍K8S安装Nginx的方法。

1.创建Nginx目录及配置文件

复制代码
mkdir -p /work/devops/k8s/app/nginx/{config,html}

在config目录下创建nginx.conf配置文件,内容如下:

bash 复制代码
# events必须要有
events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    #日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" $http_x_forwarded_for';
    #日志的路径
    access_log  /var/log/nginx/access.log  main;

    sendfile  on;

    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;

    server{
        # 监听的端口
        listen 80;
        server_name _;
        root /usr/share/nginx/html/;
        index index.html;
    }
}

在html目录下创建index.html,内容如下:

bash 复制代码
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>K8S搭建nginx</title>
</head>
<body>
  <h1>Hello World!</h1>
</body>
</html>

2.编写K8S配置文件

此配置文件用于创建Namespace、Deployment、Service。

  • Namespace:命名空间,用于区分应用。
  • Deployment:用于生成pod。
  • Service:用于将pod暴露到集群中,供其他pod或者外部访问。

到/work/devops/k8s/app/nginx/路径下,创建名为k8s.yaml的配置文件

bash 复制代码
# 创建命名空间,Deployment和Service中要引用。
apiVersion: v1
kind: Namespace
metadata:
  name: middle
  labels:
    name: middle
---
# 创建Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: middle
  labels:
    app: nginx
spec:
  # 副本的数量
  replicas: 1
  selector:
    # 选择app=nginx的Pod
    matchLabels:
      app: nginx
  # 选择或创建的Pod的模板
  template:
    metadata:
      labels:
        app: nginx
    spec:
      # 容器的路径
      containers:
        - image: nginx:latest
          name: nginx-latest
          ports:
            - containerPort: 80
              name: nginx-80
          # 容器内的路径
          volumeMounts:
            - name: config
              mountPath: /etc/nginx/nginx.conf
            - name: config-include
              mountPath: /etc/nginx/conf.d/
            - name: log
              mountPath: /var/log/nginx/
            - name: html
              mountPath: /usr/share/nginx/html/
            - name: cert
              mountPath: /etc/nginx/cert/
      # 主机的路径
      volumes:
        - name: config #和volumeMounts中的内容要对应
          hostPath:
            path: /work/devops/k8s/app/nginx/config/nginx.conf
            type: File
        - name: config-include #和volumeMounts中的内容要对应
          hostPath:
            path: /work/devops/k8s/app/nginx/config/conf.d/
            type: DirectoryOrCreate
        - name: log #和volumeMounts中的内容要对应
          hostPath:
            path: /work/devops/k8s/app/nginx/logs/
            type: DirectoryOrCreate
        - name: html #和volumeMounts中的内容要对应
          hostPath:
            path: /work/devops/k8s/app/nginx/html/
            type: DirectoryOrCreate
        - name: cert #和volumeMounts中的内容要对应
          hostPath:
            path: /work/devops/k8s/app/nginx/cert/
            type: DirectoryOrCreate
---
# 创建Service
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: middle
  labels:
    app: nginx
spec:
  ports:
    - name: nginx-port
      port: 8000   # Service监听的端口
      targetPort: 80  # nginx自身暴露的端口。对应Deployment的containerPort
      # 对外的端口号
      nodePort: 30003
  selector:
    app: nginx
  # NodePort类型可以对外暴露端口
  type: NodePort

3.启动Nginx

到/work/devops/k8s/app/nginx/路径下,执行此命令:

bash 复制代码
kubectl apply -f k8s.yaml

结果:

4.查看启动结果

法1:通过dashboard查看

法2:通过命令查看

bash 复制代码
kubectl get pods -A

结果:

bash 复制代码
kubectl get services -A

结果

4.访问Nginx

测试1:外部访问NodePort

访问:ip:nodePort端口号

测试2:访问集群

bash 复制代码
curl 10.100.95.158:8000

结果:

测试3:访问容器内部

先进入容器内部,再访问容器端口。

1.找到pod的名字

bash 复制代码
kubectl get pods -A

结果:

2.进入容器内部

bash 复制代码
kubectl exec nginx-deployment-557c7b98b6-8l5r5 -n middle -it -- bash

结果:

3.访问Nginx

bash 复制代码
curl localhost:80

结果

备注:可以通过Ctrl+D退出。

5.删除Nginx

到/work/devops/k8s/app/nginx/路径下,执行此命令:

bash 复制代码
kubectl delete -f k8s.yaml

结果:

相关推荐
ShirleyWang0122 天前
让headlamp控制台能访问
linux·服务器·python·k8s·k3s
spider_xcxc2 天前
K8s 部署学习笔记
docker·容器·kubernetes·云计算·k8s
ShirleyWang0122 天前
Day02 K3s NGF(Nginx Gateway Fabric)单 Worker 环境网关更新与故障处置 SOP
linux·服务器·python·k8s·k3s
ShirleyWang0122 天前
DAY01 K3s 私有化部署排障复盘与 SOP
linux·服务器·k8s·k3s·企业部署
XUHUOJUN6 天前
AKS 不是安装在 Windows Server 上,而是运行在 Windows Server 之上的 Azure 平台能力
windows·架构·k8s·azure local·azure stack
Geek-Chow6 天前
Connecting kubectl to a Private EKS Cluster Over an Internal Domain
kubernetes·k8s·aws
spider_xcxc8 天前
Docker Compose 容器通信详解:同一个 Compose 文件才能互通吗?
docker·容器·k8s·容器化·容器网络
欢醉13 天前
k8s调用服务异常cannot allocate memory
kubernetes·k8s
万里侯16 天前
GitOps 漂移检测:集群里改了配置,Git 还以为一切正常
微服务·容器·k8s
spider_xcxc17 天前
云上网络基石(阿里云):深入解读阿里云VPC及其应用生态
网络·阿里云·云计算·k8s·运维开发·虚拟化