---
title: "K8S 创建pod，service, deployment"
author: "mukeliang728"
date: "2021-11-05T14:58:29Z"
tags: []
categories: ["开源软件"]
slug: "k8s-pod-service-deployment"
draft: false
lastmod: "2021-11-05T15:20:40Z"
id: 2521383
url: "https://mukeliang.iteye.com/blog/2521383"
---

- **编写 Nginx Pod 的 yaml**
  
  ```
  apiVersion: v1
  kind: Pod
  metadata:
    name: my-nginx
  spec:
    containers:
    - image: nginx
      name: my-nginx
  ```
- 创建 Nginx Pod
  
  ```
  kubectl apply -f nginx.yaml  
  ```
- 查看 Pod 情况
  
  ```
  kubectl get po
  kubectl describ pod my-nginx
  ```
- **创建 NodePort 类型的 service**
  
  ```
  apiVersion: v1
  kind: Service
  metadata:
    labels:
      app: nginx
    name: nginx-deployment
  spec:
    ports:
    - port: 80
      name: nginx-service80
      protocol: TCP
      targetPort: 80
      nodePort: 30001  
    selector:
      app: nginx
    type: NodePort 
  ```
  
  ## **创建 Deployment**
  
  ```
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: nginx-deployment
    labels:
      app: nginx
  spec:
    replicas: 3
    selector:
      matchLabels:
        app: nginx
    template:
      metadata:
        labels:
          app: nginx
      spec:
        containers:
        - name: nginx
          image: nginx:1.14.2
          ports:
          - containerPort: 80
  
  ```