在k8s中部署一个nginx来验证功能, 并使用nodoPort暴露端口

部署服务

kubectl apply -f demo_nginx_deployment.yaml
# nginx 服务
apiVersion: v1
kind: Service
metadata:
  name: demo-nginx
  labels:
    app: demo-nginx
spec:
  type: NodePort
  ports:
  # node port 要求端口大于30000
  - nodePort: 30013
    port: 80
    targetPort: 80
    protocol: TCP
  selector:
    app: demo-nginx


---
# nginx 部署 deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-nginx
  labels:
    app: demo-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-nginx
  template:
    metadata:
      labels:
        app: demo-nginx
    spec:
      containers:
      - name: demo-nginx
        image: docker.io/nginx:1.22.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: "1Gi"
            cpu: "200m"

查看状态

kubectl get deployment,service,pod

输出以下内容

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/demo-nginx   1/1     1            1           9s

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/demo-nginx   NodePort    10.107.196.123   <none>        80:30013/TCP   9s
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        39m

NAME                              READY   STATUS    RESTARTS   AGE
pod/demo-nginx-6b8fb69c68-zqpdl   1/1     Running   0          9s

查看日志是否正常

kubectl logs -f --tail=100 demo-nginx-6b8fb69c68-zqpdl
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/01/13 08:48:11 [notice] 1#1: using the "epoll" event method
2024/01/13 08:48:11 [notice] 1#1: nginx/1.22.0
2024/01/13 08:48:11 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2024/01/13 08:48:11 [notice] 1#1: OS: Linux 3.10.0-1160.105.1.el7.x86_64
2024/01/13 08:48:11 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/01/13 08:48:11 [notice] 1#1: start worker processes
2024/01/13 08:48:11 [notice] 1#1: start worker process 31
2024/01/13 08:48:11 [notice] 1#1: start worker process 32
2024/01/13 08:48:11 [notice] 1#1: start worker process 33
2024/01/13 08:48:11 [notice] 1#1: start worker process 34

进入容器内验证是否正常

kubectl exec -it pod/demo-nginx-6b8fb69c68-zqpdl -- /bin/bash
# 在容器内查看nginx工作目录
# root@demo-nginx-6b8fb69c68-zqpdl:/# ls -l /etc/nginx/


drwxr-xr-x 1 root root   26 Jan 13 08:48 conf.d
-rw-r--r-- 1 root root 1007 May 23  2022 fastcgi_params
-rw-r--r-- 1 root root 5349 May 23  2022 mime.types
lrwxrwxrwx 1 root root   22 May 24  2022 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root  648 May 24  2022 nginx.conf
-rw-r--r-- 1 root root  636 May 23  2022 scgi_params
-rw-r--r-- 1 root root  664 May 23  2022 uwsgi_params

验证nginx服务是否可用

curl -v 192.168.21.223:30013

返回一下内容

* About to connect() to 192.168.21.223 port 30013 (#0)
*   Trying 192.168.21.223...
* Connected to 192.168.21.223 (192.168.21.223) port 30013 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.21.223:30013
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.22.0
< Date: Sat, 13 Jan 2024 08:53:08 GMT
< Content-Type: text/html
< Content-Length: 615
< Last-Modified: Mon, 23 May 2022 23:59:19 GMT
< Connection: keep-alive
< ETag: "628c1fd7-267"
< Accept-Ranges: bytes
<
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<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!</h1>
<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>
* Connection #0 to host 192.168.21.223 left intact

转载请注明出处来源