k8s创建容器的两种办法

1, 命令行创建容器

1.1 kubectl run 通过命令行参数创建容器

#从镜像仓库拉取
kubectl run mycentos007 --image=centos-test-telnet:latest
#从本地拉取
kubectl run mycentos007 --image=centos-test-telnet:latest --image-pull-policy=Never

  

1.2 kubectl create 命令创建容器

命令行只能从镜像仓库拉取镜像,暂时没找到本地拉取的参数

kubectl create deployment nginx --image=centos-test-telnet:latest

 

不执行命令,只输出yaml文件

kubectl create deployment nginx --image=centos-test-telnet:latest --dry-run=client -oyaml

  

修改输出的yml文件中的拉取策略,然后就可以拉取本地镜像了

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: centos-test-telnet:latest
        imagePullPolicy: IfNotPresent
        name: centos-test-telnet
        resources: {}
status: {}
View Code

 创建node和server的yml文件样例

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
      - image: centos-test-telnet:latest
        imagePullPolicy: IfNotPresent
        name: centos-test-telnet
        resources: {}
status: {}
---
apiVersion: v1
kind: Service
metadata:
 name: web-app-entrypoint
 namespace: default
spec:
 type: NodePort
 selector:
   app: web
 ports:
 - port: 80
   targetPort: 80
   nodePort: 30002
View Code

 

2 通过yml文件创建容器

2.1 使用 kubectl create -f  **.yml 来创建容器

kubectl create -f nginx.yml

2.2 yml文件详解

https://luanlengli.github.io/2019/07/02/Kubernetes-deployment%E6%A8%A1%E6%9D%BF.html

  

posted @ 2024-02-25 14:30  solomon123  阅读(244)  评论(0编辑  收藏  举报