K8S 创建资源的两种方式

K8S 的资源有 PodServiceVolumeNamespaceReplicaSetDeploymentStatefulSetDaemonSetJob等等

kubectl run 创建方式

类似于 docker run 创建容器

kubectl run nginx-deployment --image=nginx:1.8.1 --replicas=2

yaml 配置文件创建方式

类似于 docker-compose 批量创建容器

kubectl apply -f nginx.yml
kubectl create -f nginx-rc.yml

示例: 部署 springboot 项目

root@k8s-master:~/rc# cat demo-rc.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: demo
spec:
  # 节点数,设置为多个可以实现负载均衡效果
  replicas: 1
  selector:
    app: demo
  template:
    metadata:
      labels:
        app: demo
    spec:
      containers:
      - name: demo
        #镜像名
        image: testspringboot
        #本地有镜像就不会去仓库拉取
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080

apply vs create

比较项 apply create
资源创建 支持 支持
资源更新 支持
重复创建 支持

其他类似的命令:kubectl replace、kubectl edit 和 kubectl patch
为避免造成不必要的困扰,我们会尽量只使用 kubectl apply,
此命令已经能够应对超过 90% 的场景,事半功倍。

你可能感兴趣的:(K8S 创建资源的两种方式)