1. 编辑 nginx-deployment 文件

$ vim nginx-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx  # 应用名称
  name: nginx-deployment1   # deployment名称
  namespace: test  # 命名空间
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.23.1
        ports:
        - containerPort: 80
        name: nginx
      tolerations:
      - key: "key"
        operator: "Equal"
        value: "nginx"
        effect: "NoSchedule"

2. 创建nginx-deployment

$ kubectl create namespace test
$ kubectl create -f nginx-deployment.yaml

3. 编辑nginx-nodeport文件

$ vim nginx-nodeport.yaml 
apiVersion: v1
kind: Service
metadata:
  labels:
   app: nginx  # 应用名称
  name: nginx-deployment1  # deployment名称
  namespace: test  # 命名空间
spec:
  ports:
  - port: 42  # 本服务端口(随便一个)
    name: nginx-service80to31090  # 本服务名称(随便一个)
    protocol: TCP  # 网络协议TCP
    targetPort: 80  # 原服务端口
    nodePort: 31090  # 主机端口
  selector:
    app: nginx  # 应用名称
  type: NodePort  # 网络类型

4. 创建nginx-nodeport

$ kubectl apply -f nginx-nodeport.yaml 

5. 访问测试

$ curl 127.0.0.1:31090
<!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>
文章作者: hzbb
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 运维小记
云原生 Kubernetes K8S Nginx
喜欢就支持一下吧
打赏
微信 微信
支付宝 支付宝