k8s部署nginx

创建namespace

nginx-namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: ns-nginx

创建Deployment

nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: ns-nginx
spec:
  selector:
    matchLabels:
      app: nginx-app
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - name: nginx
        image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:stable
        ports:
        - containerPort: 80

创建Service

nginx-service.yaml

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

启动

kubectl apply -f nginx-namespace.yaml
kubectl apply -f .

查看

kubectl get pod -n ns-nginx
kubectl get svc -n ns-nginx

访问

http://10.0.2.12:32080/

10.0.2.12为宿主机ip,32080为tomcat Service的port。

posted @ 2024-10-22 12:17  BUG弄潮儿  阅读(3)  评论(0编辑  收藏  举报