[CKA-Exercises]Application Lifecycle Management

281 阅读2分钟
原文链接: blog.kii.la

Application Lifecycle Management(8%) [应用程序生命周期管理 占比 8%].

kubernetes.io > Documentation > Reference > kubectl CLI >kubectl Cheat Sheet

kubernetes.io > Documentation > Tasks > Run Applications > Perform Rolling Update Using a Replication Controller

kubernetes.io > Documentation > Concepts > Configuration > Secrets

kubernetes.io > Documentation > Tasks > Configure Pod and Containers > Configure a Pod to Use a ConfigMap

kubernetes.io > Documentation > Tasks> Inject Data into Applications > Define Environment Variables for a Container

kubernetes.io > Documentation > Concepts > Cluster Administration > Managing Resources

kubernetes.io > Documentation > Reference > Command line tools Reference > kube scheduler

Understand Deployments and how to perform rolling updates and rollbacks [理解部署以及如何执行滚动更新和回滚]

show

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 注意: 滚动更新是默认的 Deployment 策略

$ kubectl rollout status deployment/myapp-deployment
$ kubectl rollout history deployment/myapp-deployment


# 您可能希望将 Deployment 容器的镜像更改为一个新的版本, 例如将 `image:nginx` 更改为 `image:nginx:1,7.1`, 然后运行
$ kubectl apply -f deployment.yaml

# 您还可以使用命令行来发布更新, 例如:
$ kubectl set image deployment/myapp-deployment nginx=nginx:1.7.1

# 但是这样不会更新原始的 Deployment YAML
# 在滚动更新期间检查副本集

$ kubectl get replicasets

# 如果在应用程序部署期间数显错误, 运行 `rollout` 来撤销更新
$ kubectl rollout undo deployment/myapp-deployment

# 如果希望更改滚动更新策略以重新创建,请 `edit` Deployment.
$ kubectl edit deployment myapp-deployment

Know various ways to configure applications [理解配置应用程序的各种方法]

show

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: apps/v1
kind: DaemonSet
metadata:
    name: sleep-pod
spec:
    selector:
        matchLabels:
            app: sleep-app
    template:
        metadata:
            labels:
                app: sleep-app
        spec:
            containers:
              - name: nginx-container
                image: nginx
                command: ["sleep"]
                args: ["10"]

使用 env 键值对定义 Pod

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
apiVersion: apps/v1
kind: DaemonSet
metadata:
    name: sleep-pod
spec:
    containers:
      - name: nginx-container
        image: nginx
        ports:
            - containerPort: 8080
        env:
            - name: app_type
            - restapi

该 Pod 类似于如下的 Docker 命令

1
$ docker run -e app_type=restapi simple-color-container

您也可以使用 ConfigMap 作为键值对, 将 env 变量注入到 Pod 的定义中.

1
2
3
4
5
6
7
apiVersion: v1
kind: ConfigMap
metadata:
    name: app-config
data:
    App_color: bulue
    App_mode: prod

或者

1
2
3
$ kubectl create configmap \ 
    app-config --from-literal=app_color=blue \ 
    --from-litteral=app_type=prod

或者

1
2
$ kubectl create configmap \ 
    app-config --from-file=app_config.properties
1
2
$ kubectl get configmaps
$ kubectl describe configmaps
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apiVersion: apps/v1
kind: DaemonSet
metadata:
    name: sleep-pod
spec:
    containers:
      - name: nginx-container
        image: nginx
        ports:
            - containerPort: 8080
        envFrom:
            - configMapRef:
              name: app-config
# ConfigMap 中的单个环境变量
        env:
            - name: app_type
              valueFrom: 
                name: app-config
                key: app_color
# ConfigMap 中的卷环境变量
    volumes:
        - name: app_config_vol
          configMap:
            name: app-config
        

如果希望 DB 主机, 用户, 密码等环境变量传递给 web 应用程序, 建议使用 Kubernetes Secret.

1
2
3
4
$ kubectl create secret generic \
    app-secret --from-literal=DB_host=mysql \ 
    --from-literal=DB_user=root \
    --from-literal=DB_passwd=mysql

或者

1
2
$ kubectl create secret generic \
    app-secret --from-file=app_secret.properties

以编码格式存储 Secret



$ echo -n 'mysql' | base64 (对用户和密码重复此操作)


apiVersion: v1
kind: Secret
metadata:
    name: app-secret
data:
    DB_host: mysql 
    DB_user: root
    DB_password: passwd


$ kubectl get scretes
$ kubectl get secret app-secret -o yaml (此处将显示编码后的值)
$ kubectl desc secrets
$ echo -n 'codedValue' | base64 --decode (如果你想解码这个 Secret 的值)

创建一个 Pod 来使用 Secret



apiVersion: apps/v1
kind: DaemonSet
metadata:
    name: sleep-pod
spec:
    containers:
    - name: nginx-container
      image: nginx
      ports:
        - containerPort:8080
      envFrom:
      - secretRef:
          name: app-secret

Know how to scale Applications [了解如何扩展应用程序]



$ kubectl scale deployments/kubernetes-bootcamp --replicas=4
$ kubectl edit (replicas object) 
    --replicas in Pod

Understand the primitives necessary to Crete a self-healing Application [理解创建自修复应用程序所需的基本类型]

Kubernetes 通过副本集和副本控制器支持自修复应用程序. 副本控制器有助于确保在 Pod 中的应用程序崩溃时自动创建 Pod. 它有助于确保在任何时候都运行足够的优越性副本集.

Kubernetes 提供了额外的支持来检查 Pods 中的应用程序的健康状况, 并通过 LivenessReadiness Probes 来采取必要的行动.