Day 1 - Second Half
Workshop 2: Pods, Service, Deployment
Part 1 - Single Container
$ kubectl create -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/singlecontainer/webtest_pod.yml
$ kubectl get pods
$ kubectl create -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/singlecontainer/webtest_svc.yml
$ kubectl get svc
$ $ curl http://192.168.99.100:30467
<H1> Welcome Page from Container Python Lab </H1>Checkpoint Date/Time: Sun Jul 8 05:57:33 2018
$ kubectl describe pods webtest | more
$ kubectl describe svc webtest
$ kubectl logs webtest -c webtest
$ kubectl exec -it webtest -c webtest sh
$ kubectl delete -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/singlecontainer/webtest_pod.yml
$ kubectl delete -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/singlecontainer/webtest_svc.yml
Part 2 - Multiple Containers
Simple Architecture
More Robust Architecture
- TLS at nginx web proxy to reduce traffic overhead in the backend
- DB cache by In-Memory DB e.g. redis
- Scenario: Cinema's applications
- redis has an automatic expiration
APIs
- /init
- /insertuser
- /removeuser/<uid>
- /users/
Main Database
$ kubectl create -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/multicontainer/databasemodule_pod.yml
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
maindb 1/1 Running 0 2m
$ kubectl create -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/multicontainer/databasemodule_svc.yml
$ kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.96.0.1 <none> 443/TCP 3h
maindb 10.101.162.54 <none> 3306/TCP 6s
Web Service
$ kubectl create -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/multicontainer/webmodule_pod.yml
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
maindb 1/1 Running 0 2m
web 3/3 Running 0 50s
$ kubectl create -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/multicontainer/webmodule_svc.yml
$ kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.96.0.1 <none> 443/TCP 3h
maindb 10.101.162.54 <none> 3306/TCP 2m
web 10.106.153.242 <nodes> 5000:32500/TCP,80:30500/TCP 12s
Note: web pod has 3 containers
Pod YAML
apiVersion: "v1"
kind: Pod
metadata:
name: web
labels:
name: "web"
owner: "Praparn_L"
version: "1.0"
module: "web"
environment: "development"
spec:
containers:
- name: cachedb
image: labdocker/redis:latest
ports:
- containerPort: 6379
protocol: TCP
- name: webservice
image: labdocker/cluster:webservice
env:
- name: "REDIS_HOST"
value: "localhost"
ports:
- containerPort: 5000
protocol: TCP
- name: webcache
image: labdocker/cluster:webcache_kubernetes
ports:
- containerPort: 80
protocol: TCP
Service YAML
apiVersion: v1
kind: Service
metadata:
name: web
labels:
name: "web"
owner: "Praparn_L"
version: "1.0"
module: "Web"
environment: "development"
spec:
selector:
name: "web"
owner: "Praparn_L"
version: "1.0"
module: "web"
environment: "development"
type: NodePort
ports:
- port: 5000
name: webservice
targetPort: 5000
protocol: TCP
nodePort: 32500
- port: 80
name: webcache
targetPort: 80
protocol: TCP
nodePort: 30500
Test
$ export Server_IP=192.168.99.100
$ export Server_Port=30500
$ curl http://$Server_IP:$Server_Port/init
########### Database Create New Account Table Done ###########
$ curl -i -H "Content-Type: application/json" -X POST -d '{"uid": "1", "user":"Praparn Luangphoonlap", "descripe":"Slave"}' http://$Server_IP:$Server_Port/users/insertuser && \
curl -i -H "Content-Type: application/json" -X POST -d '{"uid": "2", "user":"Somchai Sunsukwan", "descripe":"Security Guard"}' http://$Server_IP:$Server_Port/users/insertuser && \
curl -i -H "Content-Type: application/json" -X POST -d '{"uid": "3", "user":"Sanyachan Panrudee", "descripe":"House Keeping"}' http://$Server_IP:$Server_Port/users/insertuser && \
curl -i -H "Content-Type: application/json" -X POST -d '{"uid": "4", "user":"Sakkan Yanyicharoen", "descripe":"Messenger"}' http://$Server_IP:$Server_Port/users/insertuser && \
curl -i -H "Content-Type: application/json" -X POST -d '{"uid": "5", "user":"Chatchai Moungang", "descripe":"Programmer"}' http://$Server_IP:$Server_Port/users/insertuser && \
curl -i -H "Content-Type: application/json" -X POST -d '{"uid": "6", "user":"Anusit Kannaphat", "descripe":"DevOps Manager"}' http://$Server_IP:$Server_Port/users/insertuser && \
curl -i -H "Content-Type: application/json" -X POST -d '{"uid": "7", "user":"Meelarp Maisanuk", "descripe":"System Engineer"}' http://$Server_IP:$Server_Port/users/insertuser && \
curl -i -H "Content-Type: application/json" -X POST -d '{"uid": "8", "user":"Pansa Bunsong", "descripe":"Secuirty Guard"}' http://$Server_IP:$Server_Port/users/insertuser && \
curl -i -H "Content-Type: application/json" -X POST -d '{"uid": "9", "user":"Wiphanee Wongsaisawan", "descripe":"Administrator"}' http://$Server_IP:$Server_Port/users/insertuser
$ curl http://$Server_IP:$Server_Port/users/1
Praparn Luangphoonlap(Database Direct)
$ curl http://$Server_IP:$Server_Port/users/1
Praparn Luangphoonlap(Database Cache)
$ curl http://$Server_IP:$Server_Port/users/4
Sakkan Yanyicharoen(Database Direct)
$ curl http://$Server_IP:$Server_Port/users/4
Sakkan Yanyicharoen(Database Cache)
$ curl http://$Server_IP:$Server_Port/users/removeuser/1 && \
curl http://$Server_IP:$Server_Port/users/removeuser/2 && \
curl http://$Server_IP:$Server_Port/users/removeuser/3 && \
curl http://$Server_IP:$Server_Port/users/removeuser/4
Web2
$ kubectl create -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/multicontainer/webmodule_pod2.yml
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
maindb 1/1 Running 0 21m
web 3/3 Running 0 19m
web2 3/3 Running 0 42s
$ curl http://$Server_IP:$Server_Port
<H1> Welcome Page from Container Python Lab (Set 2) </H1>Checkpoint Date/Time: Sun Jul 8 06:52:21 2018
$ curl http://$Server_IP:$Server_Port
<H1> Welcome Page from Container Python Lab </H1>Checkpoint Date/Time: Sun Jul 8 06:52:24 2018
$ kubectl delete pods web
pod "web" deleted
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
maindb 1/1 Running 0 23m
web2 3/3 Running 0 2m
$ curl http://$Server_IP:$Server_Port
<H1> Welcome Page from Container Python Lab (Set 2) </H1>Checkpoint Date/Time: Sun Jul 8 06:53:25 2018
At service, there's no load balance yet. The pods are competitive.
Cleanup
$ kubectl delete -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/multicontainer/webmodule_pod2.yml && \
kubectl delete -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/multicontainer/webmodule_svc.yml && \
kubectl delete -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/multicontainer/databasemodule_pod.yml && \
kubectl delete -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.2_Pods_Service_Deployment/multicontainer/databasemodule_svc.yml
Summary
Replication Controller
Daemon Set
- Make sure that Pods run on all (or some) node in cluster
- When node add to cluster. Pods will deploy automatic
- When node remove, GC will automatic remove Pod
- When remove Daemon Set, Pods will automatic remove
Replication Controller
-
Daemon Set and Replication Controller is work similar.
-
RC (Replication Controller) will response:
-
Create/Maintain Pods as ”Replication Controller” (Pods Farm)
-
Keep copy of Pods (Replicas) as design
-
Ensure that Pods is up and run with amount like design
-
If too much, It will kill some Pods
-
If too kill, It will create another replicas of Pods
-
-
Auto healing if some Pods crash with any reason
-
Maintenance on cluster-level not node level
-
Workshop 3: Replication Controller
$ kubectl create -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.3_Replication_Controller/webtest_rc.yml
$ kubectl get rc
NAME DESIRED CURRENT READY AGE
webtest 3 3 3 47s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
webtest-cn7zk 1/1 Running 0 52s
webtest-mzztp 1/1 Running 0 53s
webtest-wzwcj 1/1 Running 0 52s
$ kubectl create -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.3_Replication_Controller/webtest_svc.yml
service "webtest" created
$ kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.96.0.1 <none> 443/TCP 4h
webtest 10.100.156.224 <nodes> 5000:32500/TCP 43s
$ kubectl delete pods webtest-cn7zk
pod "webtest-cn7zk" deleted
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
webtest-cn7zk 0/1 Terminating 0 3m
webtest-lss7j 1/1 Running 0 24s
webtest-vzp8n 0/1 ContainerCreating 0 2s
webtest-wzwcj 1/1 Running 0 3m
$ kubectl scale --replicas=5 rc/webtest
replicationcontroller "webtest" scaled
$ kubectl get rc webtest
NAME DESIRED CURRENT READY AGE
webtest 5 5 3 5m
$ kubectl scale --replicas=5 -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.3_Replication_Controller/webtest_rc.yml
replicationcontroller "webtest" scaled
$ kubectl get rc webtest
NAME DESIRED CURRENT READY AGE
webtest 5 5 2 6m
$ kubectl describe rc webtest
$ kubectl delete -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.3_Replication_Controller/webtest_svc.yml
service "webtest" deleted
$ kubectl delete -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.3_Replication_Controller/webtest_rc.yml
replicationcontroller "webtest" deleted
Deployment and ReplicaSet
- What is deployment/RS?
- Deployment and RS (ReplicaSet) is set “next-generation of RC” by provide full function to maintain versioning of Pods in production (No downtime: On-the-fly)
- Update new version (Rollout)
- Revert old version (Rollback)
- Pause/Resume process
- Check status
- Deployment and RS (ReplicaSet) is set “next-generation of RC” by provide full function to maintain versioning of Pods in production (No downtime: On-the-fly)
-
ReplicaSet(RS) vs Replication Controller (RC)
-
RS is generation that evolution from RC with capability more dynamic
-
RC support label with method “Equality-based requirement”
RS support label with method “Equality-based requirement” and “Setbased
requirement”
-
-
Deployment Update Strategy
Workshop 4: Deployment
$ kubectl create -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.4_Deployment/webtest_deploy.yml --record
deployment "webtest" created
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
webtest 3 3 3 0 10s
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
webtest-77794cd4b-4g4ld 1/1 Running 0 24s environment=development,module=WebServer,name=web,owner=Praparn_L,pod-template-hash=333507806,version=1.0
webtest-77794cd4b-gt928 1/1 Running 0 24s environment=development,module=WebServer,name=web,owner=Praparn_L,pod-template-hash=333507806,version=1.0
webtest-77794cd4b-vcs5k 0/1 ContainerCreating 0 24s environment=development,module=WebServer,name=web,owner=Praparn_L,pod-template-hash=333507806,version=1.0
$ kubectl create -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.4_Deployment/webtest_svc.yml --record
service "webtest" created
$ kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.96.0.1 <none> 443/TCP 4h
webtest 10.105.57.113 <nodes> 5000:32500/TCP 7s
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
webtest-77794cd4b 3 3 3 1m
$ curl http://$Server_IP:32500
<H1> Welcome Page from Container Python Lab Web Version 1.00 </H1>Checkpoint Date/Time: Sun Jul 8 07:51:24 2018
$ kubectl describe deployment/webtest
$ kubectl set image deployment/webtest webtest=labdocker/cluster:webservicelite_v1.51rc
deployment "webtest" image updated
$ kubectl rollout status deployment/webtest
Waiting for rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for rollout to finish: 1 old replicas are pending termination...
Waiting for rollout to finish: 1 old replicas are pending termination...
Waiting for rollout to finish: 1 old replicas are pending termination...
deployment "webtest" successfully rolled out
$ kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
webtest 3 3 3 3 4m
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
webtest-67dc45c865 3 3 3 42s
webtest-77794cd4b 0 0 0 4m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
webtest-67dc45c865-7qxns 1/1 Running 0 36s
webtest-67dc45c865-8rss5 1/1 Running 0 50s
webtest-67dc45c865-f9xtl 1/1 Running 0 38s
$ curl http://$Server_IP:32500
<H1> Welcome Page from Container Python Lab Web Version 1.51 RC </H1>Checkpoint Date/Time: Sun Jul 8 07:53:42 2018
$ kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
webtest 3 3 3 3 6m
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
webtest-67dc45c865 0 0 0 2m
webtest-6886cb7964 3 3 3 33s
webtest-77794cd4b 0 0 0 6m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
webtest-6886cb7964-d27m2 1/1 Running 0 21s
webtest-6886cb7964-r8q9p 1/1 Running 0 24s
webtest-6886cb7964-vs5r9 1/1 Running 0 37s
$ curl http://$Server_IP:32500
<H1> Welcome Page from Container Python Lab Web Version 1.80 GA </H1>Checkpoint Date/Time: Sun Jul 8 07:55:46 2018
$ curl http://$Server_IP:32500
<H1> Welcome Page from Container Python Lab Web Version 1.80 GA </H1>Checkpoint Date/Time: Sun Jul 8 07:55:46 2018
$ kubectl rollout history deployment/webtest
deployments "webtest"
REVISION CHANGE-CAUSE
1 kubectl create --filename=https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.4_Deployment/webtest_deploy.yml --record=true
2 kubectl set image deployment/webtest webtest=labdocker/cluster:webservicelite_v1.51rc
3 kubectl set image deployment/webtest webtest=labdocker/cluster:webservicelite_v1.8ga
$ kubectl rollout undo deployment/webtest --to-revision=2
deployment "webtest" rolled back
$ kubectl rollout status deployment/webtest
Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for rollout to finish: 1 old replicas are pending termination...
Waiting for rollout to finish: 1 old replicas are pending termination...
deployment "webtest" successfully rolled out
$ curl http://$Server_IP:32500
<H1> Welcome Page from Container Python Lab Web Version 1.51 RC </H1>Checkpoint Date/Time: Sun Jul 8 07:57:12 2018
$ kubectl delete deployment/webtest
deployment "webtest" deleted
$ kubectl delete -f https://raw.githubusercontent.com/praparn/kubernetes_20180701/master/WorkShop_1.4_Deployment/webtest_svc.yml
service "webtest" deleted