k8s

步骤

1.先创建 namespace

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

2.创建 deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: nginx
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        volumeMounts:
        - name: config-volume
          mountPath: /etc/nginx/conf
        - name: log-volume
          mountPath: /var/log/nginx
      volumes:
      - name: config-volume
        configMap:
          name: nginx-config
      - name: log-volume
        emptyDir: {}

replicas 我们设置了 1 代表只启动 1 个副本,当然我们可以更改数量做水平扩容和缩容
我们挂载了 nginx 的配置文件 nginx 的日志和 nginx 的 html 目录

[root@node1 ~]# ls /etc/nginx/
conf  html  logs

可以看到 node1 节点下目录已经有了
当然现在还没让配置文件生效,所以请 apply 容器启动(runing)之后再去检查

3.创建 service

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
  namespace: nginx
spec:
  ports:
  - port: 9000
    name: nginx-service80
    protocol: TCP
    targetPort: 80
    nodePort: 31090
  selector:
    app: nginx
  type: NodePort

我们采用的是 NodePort 模式
这边我们讲下什么是 NodePort

NodePort 是 Kubernetes 中一种 Service 类型,它允许外部流量通过节点的特定端口访问集群中的 Service。当您创建一个 NodePort 类型的 Service 时,Kubernetes 会在每个工作节点上打开一个端口,从而允许外部流量访问 Service。
具体而言,当您创建一个 NodePort 类型的 Service 时,Kubernetes 会为该 Service 分配一个随机端口(范围在 30000-32767 之间),并将此端口映射到 Service 的端口。然后,集群中每个工作节点都会监听分配的端口,并将来自外部流量的请求转发到 Service 中对应的 Pod。
NodePort 类型的 Service 通常用于需要从集群外部直接访问 Service 的情况,例如在开发环境中进行调试或测试时。但需要注意的是,使用 NodePort 暴露 Service 可能会带来一些安全风险,因此在生产环境中可能会选择其他更安全的方式来暴露 Service,例如使用 Ingress 或 LoadBalancer 类型的 Service。

总之,NodePort 类型的 Service 允许外部流量通过节点的特定端口访问集群中的 Service,是 Kubernetes 中一种常用的 Service 类型之一

我们暴露的端口是 31090 这是你在外面访问 nginx 服务的端口
targetPort 指的是容器内 nginx 服务所在的端口

4.创建 configmap

还记得我们之前的 deployment 写过 nginx 的配置文件是通过 configmap 来挂载的吗,所以我们需要创建 configmap 来配置 nginx

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
  namespace: nginx
data:
  nginx.conf: |
    user  nginx;
    worker_processes  1;
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    events {
        worker_connections  1024;
    }
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            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.conf: | 这行下面之间写 nginx 的配置即可
metadata 下的 name 指定下 configmap 的名称,
请注意这里的名称必须和 deployment 中的名称保持一致

测试

我们服务下 node1 下的 nginx 服务也就是 31090 端口
看下 nginx 服务的状态

[root@master nginx]# kubectl get pod -n nginx -owide
NAME                                READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
nginx-deployment-797d846549-jkchq   1/1     Running   0          23m   192.168.3.127   node1              

看到 pod 正常在运行

我的 node1 的 ip 是 192.168.3.47
现在我们访问下 http://192.168.3.47:31090

pidLA81.md.png

可以看到成功显示出了 nginx 服务的默认页面

当然这只是 k8s 部署 nginx 最基本的,后面还有反向代理,LVS 等等...

  • alipay_img
  • wechat_img
此作者没有提供个人介绍
最后更新于 2024-11-17