0% found this document useful (0 votes)
348 views69 pages

Kubernetes CKA 0400 Application Lifecycle Management

The document discusses Kubernetes deployment concepts like rolling updates, rollbacks, and commands used to manage deployments. It shows how a deployment can be configured to use a rolling update strategy to gradually rollout a new version of an application. The kubectl rollout and history commands are used to monitor and rollback deployments. Common deployment management commands like create, apply, set image, status, and undo are also demonstrated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
348 views69 pages

Kubernetes CKA 0400 Application Lifecycle Management

The document discusses Kubernetes deployment concepts like rolling updates, rollbacks, and commands used to manage deployments. It shows how a deployment can be configured to use a rolling update strategy to gradually rollout a new version of an application. The kubectl rollout and history commands are used to monitor and rollback deployments. Common deployment management commands like create, apply, set image, status, and undo are also demonstrated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

65

66

Rolling
Updates &
Rollbacks
Rollout and Versioning

Revision 1
nginx:1.7.0 nginx:1.7.0 nginx:1.7.0 nginx:1.7.0 nginx:1.7.0 nginx:1.7.0 nginx:1.7.0 nginx:1.7.0 nginx:1.7.0

Revision 2

nginx:1.7.1 nginx:1.7.1 nginx:1.7.1 nginx:1.7.1 nginx:1.7.1 nginx:1.7.1 nginx:1.7.1 nginx:1.7.1 nginx:1.7.1


Rollout Command
> kubectl rollout status deployment/myapp-deployment
Waiting for rollout to finish: 0 of 10 updated replicas are available...
Waiting for rollout to finish: 1 of 10 updated replicas are available...
Waiting for rollout to finish: 2 of 10 updated replicas are available...
Waiting for rollout to finish: 3 of 10 updated replicas are available...
Waiting for rollout to finish: 4 of 10 updated replicas are available...
Waiting for rollout to finish: 5 of 10 updated replicas are available...
Waiting for rollout to finish: 6 of 10 updated replicas are available...
Waiting for rollout to finish: 7 of 10 updated replicas are available...
Waiting for rollout to finish: 8 of 10 updated replicas are available...
Waiting for rollout to finish: 9 of 10 updated replicas are available...
deployment "myapp-deployment" successfully rolled out

> kubectl rollout history deployment/myapp-deployment


deployments "myapp-deployment"
REVISION CHANGE-CAUSE
1 <none>
2 kubectl apply --filename=deployment-definition.yml --record=true
Deployment Strategy

nginx:1.7.0 nginx:1.7.0 nginx:1.7.0 nginx:1.7.0 nginx:1.7.0 nginx:1.7.1 nginx:1.7.1 nginx:1.7.1 nginx:1.7.1 nginx:1.7.1
Recreate
Application
Down

Rolling nginx:1.7.1
nginx:1.7.0 nginx:1.7.1 nginx:1.7.0 nginx:1.7.1 nginx:1.7.0 nginx:1.7.1 nginx:1.7.0
Update nginx:1.7.0 nginx:1.7.1
deployment-definition.yml
apiVersion: apps/v1

Kubectl apply kind: Deployment


metadata:
name: myapp-deployment
labels:
app: myapp
> kubectl apply –f deployment-definition.yml type: front-end
deployment "myapp-deployment" configured spec:
template:
metadata:
> kubectl set image deployment/myapp-deployment \ name: myapp-pod
nginx=nginx:1.9.1 labels:
deployment "myapp-deployment" image is updated app: myapp
type: front-end
spec:
containers:
- name: nginx-container
image: nginx:1.7.1
replicas: 3
selector:
matchLabels:
type: front-end
Recreate RollingUpdate
Upgrades

POD POD POD POD POD POD POD POD POD POD

Replica Set - 1 Replica Set - 2

Deployment

> kubectl get replicasets


NAME DESIRED CURRENT READY AGE
myapp-deployment-67c749c58c 0 0 0 22m
myapp-deployment-7d57dbdb8d 5 5 5 20m
Rollback
> kubectl get replicasets > kubectl get replicasets
NAME DESIRED CURRENT READY AGE NAME DESIRED CURRENT READY AGE
myapp-deployment-67c749c58c 0 0 0 22m myapp-deployment-67c749c58c 5 5 5 22m
myapp-deployment-7d57dbdb8d 5 5 5 20m myapp-deployment-7d57dbdb8d 0 0 0 20m

POD POD POD POD POD POD POD POD POD POD

Replica Set - 1 Replica Set - 2

Deployment

> kubectl rollout undo deployment/myapp-deployment


deployment “myapp-deployment” rolled back
kubectl run
> kubectl run nginx --image=nginx
deployment "nginx" created
Summarize Commands

Create > kubectl create –f deployment-definition.yml

Get > kubectl get deployments

> kubectl apply –f deployment-definition.yml


Update
> kubectl set image deployment/myapp-deployment nginx=nginx:1.9.1

> kubectl rollout status deployment/myapp-deployment


Status
> kubectl rollout history deployment/myapp-deployment

Rollback > kubectl rollout undo deployment/myapp-deployment


76

COMMANDS
&
ARGUMENTS
docker run ubuntu

docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS

docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS


45aacca36850 ubuntu "/bin/bash" 43 seconds ago Exited (0) 41 seconds ago
??
docker run ubuntu [COMMAND]

docker run ubuntu sleep 5

5
4
3
2
1
FROM Ubuntu

CMD sleep 5

CMD command param1 CMD sleep 5

CMD [“command”, “param1”] CMD [“sleep”, “5”] CMD [“sleep 5”]

docker build –t ubuntu-sleeper .


5
4
3
2
1
docker run ubuntu-sleeper
FROM Ubuntu
docker run ubuntu-sleeper sleep 10
CMD sleep 5

Command at Startup: sleep 10

FROM Ubuntu
docker run ubuntu-sleeper 10
10
ENTRYPOINT [“sleep”]
sleep

Command at Startup:

docker run ubuntu-sleeper


sleep: missing operand
Try 'sleep --help' for more information.

Command at Startup:
FROM Ubuntu docker run ubuntu-sleeper
sleep: missing operand
ENTRYPOINT [“sleep”]
sleep Try 'sleep --help' for more information.

CMD [“5”]
5

Command at Startup:

docker run ubuntu-sleeper 10


10

Command at Startup:

sleep2.0 ubuntu-sleeper 10
docker run --entrypoint sleep2.0 10

Command at Startup:
ubuntu-sleeper
docker run --name ubuntu-sleeper
ubuntu-sleeper ubuntu-sleeper pod-definition.yml
apiVersion: v1
[“10”]
docker run --name ubuntu-sleeper ubuntu-sleeper 10 kind: Pod
metadata:
name: ubuntu-sleeper-pod
spec:
containers:
- name:
image:
args:

kubectl create –f pod-definition.yml


pod-definition.yml
apiVersion: v1
kind: Pod
metadata:
FROM Ubuntu name: ubuntu-sleeper-pod
spec:
ENTRYPOINT [“sleep”] containers:
- name: ubuntu-sleeper
CMD [“5”] image: ubuntu-sleeper
args: [“10”]
command:

docker run --name ubuntu-sleeper \


[“sleep2.0”]
--entrypoint sleep2.0
ubuntu-sleeper 10

kubectl create –f pod-definition.yml


pod-definition.yml
apiVersion: v1
kind: Pod
metadata:
name: ubuntu-sleeper-pod
spec:
FROM Ubuntu containers:
- name: ubuntu-sleeper
image: ubuntu-sleeper

ENTRYPOINT [“sleep”] command:[“sleep2.0”]

CMD [“5”] args:[“10”]


89

ENVIRONMENT
VARIABLES
Environment Variables
app.py
import os
from flask import Flask

app = Flask(__name__)


color = "red"

@app.route("/")
def main():
print(color)
return render_template('hello.html', color=color)

if __name__ == "__main__":
app.run(host="0.0.0.0", port="8080")

python app.py
Environment
app.py
Variables
import os
from flask import Flask

app = Flask(__name__)


color = "red"
@app.route("/")
def main():
print(color)
return render_template('hello.html', color=color)

if __name__ == "__main__":
app.run(host="0.0.0.0", port="8080")
Environment Variables
app.py
import os
from flask import Flask

app = Flask(__name__)


os.environ.get('APP_COLOR')
color = "red"
@app.route("/")
def main():
print(color)
return render_template('hello.html', color=color)

if __name__ == "__main__":
app.run(host="0.0.0.0", port="8080")

export APP_COLOR=blue; python app.py


ENV Variables in Docker

app.py

import os
from flask import Flask

app = Flask(__name__)


os.environ.get('APP_COLOR')
color = "red"
@app.route("/")
def main():
print(color)
return render_template('hello.html', color=color)

if __name__ == "__main__":
app.run(host="0.0.0.0", port="8080")

docker run simple-webapp-color


-e APP_COLOR=blue
ENV Variables in Docker
docker run -e APP_COLOR=blue simple-webapp-color

docker run -e APP_COLOR=green simple-webapp-color

docker run -e APP_COLOR=pink simple-webapp-color


ENV Variables in Kubernetes
docker run -e APP_COLOR=pink simple-webapp-color

pod-definition.yaml
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 8080
env:
- name: APP_COLOR
value: pink
ENV Value Types
env:
- name: APP_COLOR 1 Plain Key Value
value: pink

env:
- name: APP_COLOR
valueFrom: 2 ConfigMap

configMapKeyRef:

env:
- name: APP_COLOR
valueFrom: 3 Secrets

secretKeyRef:
Course Objectives

Core Concepts

Configuration
ConfigMaps Secrets

SecurityContexts ServiceAccounts
Resource Requirements

Multi-Container Pods

Observability

Pod Design

Services & Networking

State Persistence
ConfigMaps
ConfigMap pod-definition.yaml
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 8080
env:
- name: APP_COLOR
value: blue
- name: APP_MODE
value: prod
ConfigMaps
ConfigMap pod-definition.yaml
APP_COLOR : blue
APP_COLOR: apiVersion: v1
APP_MODE:: prod kind: Pod
APP_MODE
metadata:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 8080
env:
envFrom:
- -configMapRef:
name:
value:
name: app-config

1 2
- name:
value:

Create ConfigMap Inject into Pod


Create ConfigMaps
ConfigMap
Imperative kubectl create configmap
APP_COLOR: blue
APP_MODE: prod

Declarative kubectl create –f

1
Create ConfigMap
Create ConfigMaps
ConfigMap
Imperative kubectl create configmap
APP_COLOR: blue <config-name> --from-literal=<key>=<value>
APP_MODE: prod

kubectl create configmap \


app-config --from-literal=APP_COLOR=blue \
--from-literal=APP_MOD=prod

kubectl create configmap

1
<config-name> --from-file=<path-to-file>

kubectl create configmap \


app-config --from-file=app_config.properties
Create ConfigMap
Create ConfigMaps
ConfigMap Declarative kubectl create –f

APP_COLOR: blue
APP_MODE: prod
config-map.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_COLOR: blue
APP_MODE: prod

1 kubectl create –f config-map.yaml

Create ConfigMap
Create ConfigMaps
app-config mysql-config redis-config
APP_COLOR: blue port: 3306 port: 6379
APP_MODE: prod max_allowed_packet: 128M rdb-compression: yes

1
Create ConfigMap
View ConfigMaps

kubectl get configmaps


NAME DATA AGE
app-config 2 3s

kubectl describe configmaps


Name: app-config
Namespace: default
Labels: <none>
Annotations: <none>

Data
====
APP_COLOR:
----
blue
APP_MODE:
----
prod
Events: <none>
ConfigMap in Pods
pod-definition.yaml config-map.yaml
apiVersion: v1 apiVersion: v1
kind: Pod kind: ConfigMap
metadata:
metadata:
name: simple-webapp-color
labels: name: app-config
name: simple-webapp-color data:
spec: APP_COLOR: blue
containers: APP_MODE: prod
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 8080
envFrom:
- configMapRef:
name: app-config

2
Inject into Pod
kubectl create –f pod-definition.yaml
ConfigMap in Pods
envFrom:
- configMapRef:
name: app-config ENV

env:
- name: APP_COLOR
valueFrom:
SINGLE ENV configMapKeyRef:
name: app-config
key: APP_COLOR

volumes:
- name: app-config-volume
configMap: VOLUME
name: app-config
107

Kubernetes
Secrets
Web-MySQL Application

app.py
import os
from flask import Flask

app = Flask(__name__)

@app.route("/")
def main():

mysql.connector.connect(host=‘mysql', database='mysql’,
user='root', password=‘paswrd')

return render_template('hello.html', color=fetchcolor())

if __name__ == "__main__":
app.run(host="0.0.0.0", port="8080")
Web-MySQL Application

app.py
import os
from flask import Flask

app = Flask(__name__)

@app.route("/")
def main():

mysql.connector.connect(host=‘mysql', database='mysql’,
user='root', password=‘paswrd')
return render_template('hello.html', color=fetchcolor())

if __name__ == "__main__":
app.run(host="0.0.0.0", port="8080")
Web-MySQL Application

app.py
import os
from flask import Flask

app = Flask(__name__) config-map.yaml


apiVersion: v1
@app.route("/") kind: ConfigMap
def main():
metadata:
mysql.connector.connect(host=‘mysql', database='mysql’, name: app-config
user='root', password=‘paswrd') data:
return render_template('hello.html', color=fetchcolor()) DB_Host: mysql
DB_User: root
if __name__ == "__main__":
DB_Password: paswrd
app.run(host="0.0.0.0", port="8080")
Secret
Secret
DB_Host: bXlzcWw=
mysql
DB_User: cm9vdA==
root
DB_Password: paswrd
cGFzd3Jk

Environment Variable
DB_Host: mysql
DB_User: root
DB_Password: paswrd

POD

1 2
Inject into Pod
Create Secret
Create Secrets
Secret
Imperative kubectl create secret generic
DB_Host: mysql
DB_User: root
DB_Password: paswrd

Declarative kubectl create –f

1
Create Secret
Create Secrets
Secret
Imperative kubectl create secret generic
DB_Host: mysql <secret-name> --from-literal=<key>=<value>
DB_User: root
DB_Password: paswrd

kubectl create secret generic \


app-secret --from-literal=DB_Host=mysql \
--from-literal=DB_User=root
--from-literal=DB_Password=paswrd

kubectl create secret generic

1
<secret-name> --from-file=<path-to-file>

kubectl create secret generic \


app-secret --from-file=app_secret.properties
Create Secret
Create Secrets
Secret Declarative kubectl create –f

DB_Host: mysql
DB_User: root
secret-data.yaml
DB_Password: paswrd
apiVersion: v1
kind: Secret
metadata:
name: app-secret
data:
DB_Host:
DB_Host: mysql
bXlzcWw=
DB_User:
DB_User: root
cm9vdA==
DB_Password:
DB_Password: paswrd
cGFzd3Jk

1
kubectl create –f secret-data.yaml
Create Secret
Encode Secrets
Secret Declarative kubectl create –f

DB_Host: mysql DB_Host: bXlzcWw=


DB_User: root DB_User: cm9vdA==
secret-data.yaml
DB_Password: paswrd DB_Password: cGFzd3Jk
apiVersion: v1
kind: Secret
metadata:
name: app-secret
echo –n ‘mysql’ | base64 data:
bXlzcWw= DB_Host: mysql
DB_User: root
echo –n ‘root’ | base64
DB_Password: paswrd

1
cm9vdA==

echo –n ‘paswrd’ | base64


kubectl create –f secret-data.yaml
cGFzd3JkCreate Secret
View Secrets

kubectl get secrets


NAME TYPE DATA AGE
app-secret Opaque 3 10m
default-token-mvtkv kubernetes.io/service-account-token 3 2h

kubectl describe secrets kubectl get secret app-secret –o yaml


Name: app-secret apiVersion: v1
Namespace: default data:
Labels: <none> DB_Host: bXlzcWw=
Annotations: <none> DB_Password: cGFzd3Jk
DB_User: cm9vdA==
Type: Opaque kind: Secret
metadata:
Data creationTimestamp: 2018-10-18T10:01:12Z
==== labels:
DB_Host: 10 bytes name: app-secret
DB_Password: 6 bytes name: app-secret
DB_User: 4 bytes namespace: default
uid: be96e989-d2bc-11e8-a545-080027931072
type: Opaque
Decode Secrets
Secret Declarative kubectl create –f

DB_Host: mysql DB_Host: bXlzcWw=


DB_User: root DB_User: cm9vdA==
secret-data.yaml
DB_Password: paswrd DB_Password: cGFzd3Jk
apiVersion: v1
kind: Secret
metadata:
name: app-secret
echo –n ‘bXlzcWw=’ | base64 --decode data:
mysql DB_Host: mysql
DB_User: root
echo –n ‘cm9vdA==’ | base64 --decode
DB_Password: paswrd

1
root

echo –n ‘cGFzd3Jk’ | base64 --decode


kubectl create –f secret-data.yaml
paswrd Create Secret
Secrets in Pods
pod-definition.yaml secret-data.yaml
apiVersion: v1 apiVersion: v1
kind: Pod kind: Secret
metadata:
name: simple-webapp-color
metadata:
labels: name: app-secret
name: simple-webapp-color data:
spec: DB_Host: bXlzcWw=
containers: DB_User: cm9vdA==
- name: simple-webapp-color
DB_Password: cGFzd3Jk
image: simple-webapp-color
ports:
- containerPort: 8080
envFrom:
- secretRef:
name: app-secret

2
Inject into Pod
kubectl create –f pod-definition.yaml
Secrets in Pods
envFrom:
- secretRef:
name: app-config ENV

env:
- name: DB_Password
valueFrom:
SINGLE ENV secretKeyRef:
name: app-secret
key: DB_Password

volumes:
- name: app-secret-volume
secret: VOLUME
secretName: app-secret
Secrets in Pods as Volumes
volumes:
- name: app-secret-volume
secret: VOLUME
secretName: app-secret

ls /opt/app-secret-volumes
DB_Host DB_Password DB_User

cat /opt/app-secret-volumes/DB_Password
paswrd

Inside the Container


121

Kubernetes
Multi-Container
PODs
MONOLITH
MICROSERVICES
MICROSERVICES
MICROSERVICES
MICROSERVICES
LOG Agent

WEB Server
LOG Agent

WEB Server
LOG Agent

WEB Server
Multi-Container PODs

POD POD
Multi-Container PODs
NETWORK
LIFECYCLE

POD

STORAGE
Create pod-definition.yaml
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp
labels:
name: simple-webapp
spec:
containers:
- name: simple-webapp
image: simple-webapp
ports:
- containerPort: 8080
- name: log-agent
image: log-agent

POD

You might also like