Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k8s 使用 Deployment 部署应用 #39

Open
lqshow opened this issue Aug 25, 2018 · 0 comments
Open

k8s 使用 Deployment 部署应用 #39

lqshow opened this issue Aug 25, 2018 · 0 comments
Labels

Comments

@lqshow
Copy link
Owner

lqshow commented Aug 25, 2018

Pod 中的容器有可能会因为各种原因死掉,Deployment controller 会通过动态创建和销毁 Pod 来保证应用整体的健壮性。

1. 通过 Yaml 配置文件创建

在实际开发中我们通常使用配置文件的形式来部署应用。

创建 Deployment

Deployment 的 yaml 定义如下

apiVersion: extensions/v1beta1
kind: Deployment

metadata:
  name: webapp-deploy
  labels:
    app: webapp

spec:
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:   # create pods using pod definition in this template
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: katacoda/docker-http-server:latest
        ports:
        - containerPort: 80

执行命令生成 Deployment

kubectl create -f webapp-deploy.yml --record

查看 Deployment 信息

➜  lab2 kubectl get deploy -o wide
NAME            DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES                               SELECTOR
webapp-deploy   2         2         2            0           10s       webapp       katacoda/docker-http-server:latest   app=webapp

查看 Replica set 信息

Replica set 确保任何时候都有定量的 Pod

➜  lab2 kubectl get rs
NAME                      DESIRED   CURRENT   READY     AGE
webapp-deploy-b8569996f   2         2         2         17m

看下 Pod 的信息

➜  lab2 kubectl get pods -o wide -l app=webapp --show-labels
NAME                            READY     STATUS    RESTARTS   AGE       IP          NODE                 LABELS
webapp-deploy-b8569996f-26fr9   1/1       Running   0          18m       10.1.0.72   docker-for-desktop   app=webapp,pod-template-hash=641255529
webapp-deploy-b8569996f-bvh7t   1/1       Running   0          18m       10.1.0.71   docker-for-desktop   app=webapp,pod-template-hash=641255529

创建服务

Service 的 yaml 定义如下

apiVersion: v1
kind: Service

metadata:
  name: webapp-svc
  labels:
    app: webapp

spec:
  ports:
  - port: 80
    nodePort: 30080

  selector:
    app: webapp

  type: NodePort

执行命令生成 Service

kubectl create -f webapp-svc.yml

看下 Service 的信息

➜  lab2 kubectl get svc -o wide -l app=webapp
NAME         TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE       SELECTOR
webapp-svc   NodePort   10.98.48.85   <none>        80:30080/TCP   27s       app=webapp

由于现在 Deployment 存在2个 Pod,所以访问的时候会定位到不同的 Pod下

deploy

2. 通过 Kubectl 创建

创建 Deployment

使用kubectl run命令创建 Deployment 来管理Pod

kubectl run webapp-deploy --image=katacoda/docker-http-server:latest --replicas=2 --port=80 --labels="app=webapp"

创建 Service

kubectl expose deployment webapp-deploy --port=80 --target-port=80  --type=LoadBalancer

总结

  1. Deployment 创建后,会同时创建 ReplicaSet。ReplicaSet在后台创建pod。
  2. 删除 Deployment 的同时会删除 Pod,除非指定参数--cascade=false
  3. 如果在删除 Deployment 之前直接删除 Pod,Deployment 将会重新创建该 Pod
@lqshow lqshow added the k8s label Aug 25, 2018
@lqshow lqshow changed the title k8s 使用 Deployement 部署应用 k8s 使用 Deployment 部署应用 Aug 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant