際際滷

際際滷Share a Scribd company logo
Kubernetes in Action
chapter-2
Sunggon Song
Docker れ(Mac)
Install Docker for Mac
 To download Docker for Mac, head to Docker Hub.
 https://download.docker.com/mac/stable/Docker.dmg
 Double-click Docker.dmg to open the installer, then drag Moby the whale to the
Applications folder.
 Double-click Docker.app in the Applications folder to start Docker.
Hello World 貉企 ろ
$ docker run busybox echo "Hello World"
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
57c14dd66db0: Pull complete
Digest: sha256:7964ad52e396a6e045c39b5a44438424ac52e12e4d5a25d94895f2058cb863a0
Status: Downloaded newer image for busybox:latest
Hello World
$docker run busybox echo "Hello World"
Hello World
螳 Node.js app 
$ cat <<'EOF' > app.js
const http = require('http');
const os = require('os');
console.log("Kubia server starting...");
var handler = function(request, response) {
console.log("Received request from " + request.connection.remoteAddress);
response.writeHead(200);
response.end("You've hit " + os.hostname() + "n");
};
var www = http.createServer(handler);
www.listen(8080);
EOF
Node.js app ろ
$ node app.js
Kubia server starting...
Received request from ::1
$ curl http://localhost:8080/
You've hit users-MacBook-Pro.local
Docker 企語襯  Dockerfile 
$ cat <<'EOF' > Dockerfile
FROM node:7
ADD app.js /app.js
CMD node app.js
EOF
貉企 企語 觜蠍
$ docker build -t kubia .
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM node:7
---> d9aed20b68a4
Step 2/3 : ADD app.js /app.js
---> Using cache
---> fb87dfeb507d
Step 3/3 : ENTRYPOINT ["node", "app.js"]
---> Using cache
---> 368abcc4bef2
Successfully built 368abcc4bef2
Successfully tagged kubia:latest
企語 layer 危危蠍
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kubia latest 368abcc4bef2 3 days ago 660MB
busybox latest 3a093384ac30 13 days ago 1.2MB
node 7 d9aed20b68a4 17 months ago 660MB
貉企 企語 ろ蠍
$ docker run --name kubia-container -p
8080:8080 -d kubia
 --name : 貉企 企 讌
 -d : backgroud(detach mode)襦 ろ
 -p 8080:8080 : 貉企  8080() 語ろ ろ 
8080() 覦碁
ろ譴 貉企 
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
831a05a16037 kubia "node app.js" 3 days ago
Up 8 seconds 0.0.0.0:8080->8080/tcp kubia-container
$ curl localhost:8080
You've hit 831a05a16037
貉企 誤 覲 誤蠍
$ docker inspect kubia-container

$ docker inspect kubia-container |egrep "Port|8080"
"PortBindings": {
"8080/tcp": [
"HostPort": "8080"
"PublishAllPorts": false,
"ExposedPorts": {
"8080/tcp": {}
"Ports": {
"8080/tcp": [
"HostPort": "8080"
ろ 譴 貉企 企 覲願鍵
$ docker exec -it kubia-container bash
-i, --interactive Keep STDIN open even if not attached
-t, --tty Allocate a pseudo-TTY
root@831a05a16037:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 11:44 ? 00:00:00 node app.js
root 30 0 1 11:55 pts/0 00:00:00 bash
root 35 30 0 11:55 pts/0 00:00:00 ps -ef
root@831a05a16037:/# exit
ろ 譴 貉企 企 覲願鍵
$ docker exec -it kubia-container ps aux |
grep node
root 1 0.0 1.2 614432 26452 ? ssl 11:44 0:00 node app.js
$ docker exec -it kubia-container ls /
app.js boot etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
貉企 譬襭 覦 
$ docker stop kubia-container
$ docker ps
$ docker ps -a
$ docker rm kubia-container
企語 讌ろ碁Μ 貉企 企語 襴蠍
$ docker tag kubia sunggonsong/kubia
 企語 蠏(Tag) 豢螳
$ docker login
 docker hub login
$ docker push sunggonsong/kubia
 讌ろ碁Μ 企語 襴蠍
るジ ろ 企語 ろ蠍
$ docker run -p 8080:8080 -d sunggonsong/kubia
Unable to find image 'sunggonsong/kubia:latest' locally
latest: Pulling from sunggonsong/kubia
ad74af05f5a2: Downloading [==============================================> ]
48.49MB/52.61MB
2b032b8bbe8b: Download complete
a9a5b35f6ead: Downloading [===============================================> ]
41.42MB/43.23MB
3245b5a1c52c: Downloading [========================> ]
64.55MB/131.9MB
afa075743392: Waiting
9fb9f21641cd: Waiting
3f40ad2666bc: Waiting
49c0ed396b49: Waiting
9a3e1e0242fc: Waiting
Kubernates 企ろ れ
Minikube襦 讌
Minikubeれ(Mac)
$ brew cask install minikube
Minikubeれ(Ubuntu 18.04)
$ sudo apt-get update
$ sudo apt-get install apt-transport-https
$ sudo apt-get upgrade
 update & upgrade system
$ sudo apt install virtualbox virtualbox-ext-pack
 install virtualbox
$ wget
https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube-linux-amd64 --no-check-certificate
sudo mv minikube-linux-amd64 /usr/local/bin/minikube
$ minikube version
 download & install minikube
Kubectl れ(Ubuntu 18.04)
$ curl -s
https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo
apt-key add -
$ echo "deb http://apt.kubernetes.io/ kubernetes-xenial main"
| sudo tee /etc/apt/sources.list.d/kubernetes.list
$ sudo apt update
$ sudo apt -y install kubectl
$ kubectl version -o json
Minikube襦 朱碁 Kubernetes cluster

$ minikube start
 Start minikube cluster
cluster 覲 
$ kubectl cluster-info
Kubernetes master is running at https://192.168.99.101:8443
KubeDNS is running at
https://192.168.99.101:8443/api/v1/namespaces/kube-system/services/kube-dns:dns
/proxy
Using a hosted Kubernetes cluster with
Google Kubernetes Engine
...
Creating a Kubernetes clusterwith three nodes-
using GKE
$ gcloud container clusters create kubia --num-nodes 3 --machine-type f1-micro
cluster nodes 覈襦朱 cluster  
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready master 2d v1.12.4
螳豌伎 碁  
$ kubectl describe node minikube
Name: minikube
Roles: master
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
...
alias 貉るЖ 殊 completion 蠍
$ alias k=kubectl
$ source <(kubectl completion zsh)
Or
$ source <(kubectl completion bash)
$ source <(kubectl completion zsh | sed s/kubectl/k/g)
Node.js app 覦壱
$ kubectl run kubia --image=sunggonsong/kubia --port=8080
--generator=run/v1
replicationcontroller "kubia" created
Listing Pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubia-k99gg 1/1 Running 0 16m
Understanding what happened behind the scenes
Deleting deployment
$ kubectl delete deployment kubia
Deleting RC
$ kubectl delete rc kubia
Service 螳豌 
$ kubectl expose rc kubia --type=LoadBalancer --name kubia-http
service "kubia-http" exposed
Listing services
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d
kubia-http LoadBalancer 10.107.37.211 <pending> 8080:30701/TCP 1m
External IP襯 牛 觜 ′語
$ minikube service kubia-http
Opening kubernetes service default/kubia-http in default browser
 觜れ ′語ろ   IP 碁ゼ 詞
$ curl http://192.168.99.101:30701/
You've hit kubia-k99gg
ReplicationController, Pod, Service螳  覦
危
Kubectl run 牛伎 rc螻,
RC pod襯 蟯襴螻(覲旧, 暑  豌 ),
碁 accessる podれ 觜る 語
Pod 貉企 危
 貎覯ろ一 ろ 譯殊 蟲煙 Pod
 Pod  襷 貉企襯 
 貉企 伎 危襴貅伎 襦語 譟伎
 Pod 螻 private IP 語ろ 企 螳
ReplicationController  危
 Pod襯 覲旧 覲旧襯 讌讌 朱  Pod襯 
 Pod螳 暑覃 豌 Pod襯 襦
觜り  伎
觜り  伎 危
Pod 殊企. (IP  殊)
蟲郁 Pod襯 蟇磯, Pod螳  碁 蟇磯 .
RC螳 る Pod襯 襦 Pod襦 豌危螻 蠏碁覃 蠏  IP
殊  .
觜り 焔覃 螻IP襯 至螻 觜
覈伎 蟆一 覲讌 .
Application  れ朱
$ kubectl get replicationcontrollers
NAME DESIRED CURRENT READY AGE
kubia 1 1 1 33m
$ kubectl scale rc kubia --replicas=3
replicationcontroller "kubia" scaled
$ k get rc
NAME DESIRED CURRENT READY AGE
kubia 3 3 2 35m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubia-57zlr 1/1 Running 0 1m
kubia-k99gg 1/1 Running 0 36m
kubia-sl82g 1/1 Running 0 1m
觜 豌  覈  豌讌 
$ curl http://192.168.99.101:30701/
You've hit kubia-57zlr
$ curl http://192.168.99.101:30701/
You've hit kubia-k99gg
$ curl http://192.168.99.101:30701/
You've hit kubia-k99gg
$ curl http://192.168.99.101:30701/
You've hit kubia-sl82g
ろ 襦  螳
Pods襯 危 IP 碁 蠍
$ k get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
kubia-57zlr 1/1 Running 0 9m 172.17.0.5 minikube
kubia-k99gg 1/1 Running 0 43m 172.17.0.7 minikube
kubia-sl82g 1/1 Running 0 9m 172.17.0.6 minikube
kubectl describe襯 伎 Pod碁 伎
$ kubectl describe pod kubia-57zlr
Name: kubia-57zlr
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: minikube/10.0.2.15
Start Time: Mon, 14 Jan 2019 22:24:05 +0900
Labels: run=kubia
Annotations: <none>
Status: Running
IP: 172.17.0.5
Kubernetes dashboard 螳
$ kubectl cluster-info | grep dashboard
$ minikube dashboard
Opening
http://127.0.0.1:62471/api/v1/namespaces/kube-system/services/http:kubernetes-dashboard:/prox
y/ in your default browser...
Kubernetes dashboard
Referece
https://livebook.manning.com/#!/book/kub
ernetes-in-action/chapter-2/24
https://docs.docker.com/docker-for-mac/in
stall/

More Related Content

K8s in action02

  • 2. Docker れ(Mac) Install Docker for Mac To download Docker for Mac, head to Docker Hub. https://download.docker.com/mac/stable/Docker.dmg Double-click Docker.dmg to open the installer, then drag Moby the whale to the Applications folder. Double-click Docker.app in the Applications folder to start Docker.
  • 3. Hello World 貉企 ろ $ docker run busybox echo "Hello World" Unable to find image 'busybox:latest' locally latest: Pulling from library/busybox 57c14dd66db0: Pull complete Digest: sha256:7964ad52e396a6e045c39b5a44438424ac52e12e4d5a25d94895f2058cb863a0 Status: Downloaded newer image for busybox:latest Hello World $docker run busybox echo "Hello World" Hello World
  • 4. 螳 Node.js app $ cat <<'EOF' > app.js const http = require('http'); const os = require('os'); console.log("Kubia server starting..."); var handler = function(request, response) { console.log("Received request from " + request.connection.remoteAddress); response.writeHead(200); response.end("You've hit " + os.hostname() + "n"); }; var www = http.createServer(handler); www.listen(8080); EOF
  • 5. Node.js app ろ $ node app.js Kubia server starting... Received request from ::1 $ curl http://localhost:8080/ You've hit users-MacBook-Pro.local
  • 6. Docker 企語襯 Dockerfile $ cat <<'EOF' > Dockerfile FROM node:7 ADD app.js /app.js CMD node app.js EOF
  • 7. 貉企 企語 觜蠍 $ docker build -t kubia . Sending build context to Docker daemon 3.072kB Step 1/3 : FROM node:7 ---> d9aed20b68a4 Step 2/3 : ADD app.js /app.js ---> Using cache ---> fb87dfeb507d Step 3/3 : ENTRYPOINT ["node", "app.js"] ---> Using cache ---> 368abcc4bef2 Successfully built 368abcc4bef2 Successfully tagged kubia:latest
  • 8. 企語 layer 危危蠍 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE kubia latest 368abcc4bef2 3 days ago 660MB busybox latest 3a093384ac30 13 days ago 1.2MB node 7 d9aed20b68a4 17 months ago 660MB
  • 9. 貉企 企語 ろ蠍 $ docker run --name kubia-container -p 8080:8080 -d kubia --name : 貉企 企 讌 -d : backgroud(detach mode)襦 ろ -p 8080:8080 : 貉企 8080() 語ろ ろ 8080() 覦碁
  • 10. ろ譴 貉企 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 831a05a16037 kubia "node app.js" 3 days ago Up 8 seconds 0.0.0.0:8080->8080/tcp kubia-container $ curl localhost:8080 You've hit 831a05a16037
  • 11. 貉企 誤 覲 誤蠍 $ docker inspect kubia-container $ docker inspect kubia-container |egrep "Port|8080" "PortBindings": { "8080/tcp": [ "HostPort": "8080" "PublishAllPorts": false, "ExposedPorts": { "8080/tcp": {} "Ports": { "8080/tcp": [ "HostPort": "8080"
  • 12. ろ 譴 貉企 企 覲願鍵 $ docker exec -it kubia-container bash -i, --interactive Keep STDIN open even if not attached -t, --tty Allocate a pseudo-TTY root@831a05a16037:/# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 11:44 ? 00:00:00 node app.js root 30 0 1 11:55 pts/0 00:00:00 bash root 35 30 0 11:55 pts/0 00:00:00 ps -ef root@831a05a16037:/# exit
  • 13. ろ 譴 貉企 企 覲願鍵 $ docker exec -it kubia-container ps aux | grep node root 1 0.0 1.2 614432 26452 ? ssl 11:44 0:00 node app.js $ docker exec -it kubia-container ls / app.js boot etc lib media opt root sbin sys usr bin dev home lib64 mnt proc run srv tmp var
  • 14. 貉企 譬襭 覦 $ docker stop kubia-container $ docker ps $ docker ps -a $ docker rm kubia-container
  • 15. 企語 讌ろ碁Μ 貉企 企語 襴蠍 $ docker tag kubia sunggonsong/kubia 企語 蠏(Tag) 豢螳 $ docker login docker hub login $ docker push sunggonsong/kubia 讌ろ碁Μ 企語 襴蠍
  • 16. るジ ろ 企語 ろ蠍 $ docker run -p 8080:8080 -d sunggonsong/kubia Unable to find image 'sunggonsong/kubia:latest' locally latest: Pulling from sunggonsong/kubia ad74af05f5a2: Downloading [==============================================> ] 48.49MB/52.61MB 2b032b8bbe8b: Download complete a9a5b35f6ead: Downloading [===============================================> ] 41.42MB/43.23MB 3245b5a1c52c: Downloading [========================> ] 64.55MB/131.9MB afa075743392: Waiting 9fb9f21641cd: Waiting 3f40ad2666bc: Waiting 49c0ed396b49: Waiting 9a3e1e0242fc: Waiting
  • 18. Minikubeれ(Mac) $ brew cask install minikube
  • 19. Minikubeれ(Ubuntu 18.04) $ sudo apt-get update $ sudo apt-get install apt-transport-https $ sudo apt-get upgrade update & upgrade system $ sudo apt install virtualbox virtualbox-ext-pack install virtualbox $ wget https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 chmod +x minikube-linux-amd64 --no-check-certificate sudo mv minikube-linux-amd64 /usr/local/bin/minikube $ minikube version download & install minikube
  • 20. Kubectl れ(Ubuntu 18.04) $ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - $ echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list $ sudo apt update $ sudo apt -y install kubectl $ kubectl version -o json
  • 21. Minikube襦 朱碁 Kubernetes cluster $ minikube start Start minikube cluster
  • 22. cluster 覲 $ kubectl cluster-info Kubernetes master is running at https://192.168.99.101:8443 KubeDNS is running at https://192.168.99.101:8443/api/v1/namespaces/kube-system/services/kube-dns:dns /proxy
  • 23. Using a hosted Kubernetes cluster with Google Kubernetes Engine ...
  • 24. Creating a Kubernetes clusterwith three nodes- using GKE $ gcloud container clusters create kubia --num-nodes 3 --machine-type f1-micro
  • 25. cluster nodes 覈襦朱 cluster $ kubectl get nodes NAME STATUS ROLES AGE VERSION minikube Ready master 2d v1.12.4
  • 26. 螳豌伎 碁 $ kubectl describe node minikube Name: minikube Roles: master Labels: beta.kubernetes.io/arch=amd64 beta.kubernetes.io/os=linux ...
  • 27. alias 貉るЖ 殊 completion 蠍 $ alias k=kubectl $ source <(kubectl completion zsh) Or $ source <(kubectl completion bash) $ source <(kubectl completion zsh | sed s/kubectl/k/g)
  • 28. Node.js app 覦壱 $ kubectl run kubia --image=sunggonsong/kubia --port=8080 --generator=run/v1 replicationcontroller "kubia" created
  • 29. Listing Pods $ kubectl get pods NAME READY STATUS RESTARTS AGE kubia-k99gg 1/1 Running 0 16m
  • 30. Understanding what happened behind the scenes
  • 31. Deleting deployment $ kubectl delete deployment kubia
  • 32. Deleting RC $ kubectl delete rc kubia
  • 33. Service 螳豌 $ kubectl expose rc kubia --type=LoadBalancer --name kubia-http service "kubia-http" exposed
  • 34. Listing services $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d kubia-http LoadBalancer 10.107.37.211 <pending> 8080:30701/TCP 1m
  • 35. External IP襯 牛 觜 ′語 $ minikube service kubia-http Opening kubernetes service default/kubia-http in default browser 觜れ ′語ろ IP 碁ゼ 詞 $ curl http://192.168.99.101:30701/ You've hit kubia-k99gg
  • 36. ReplicationController, Pod, Service螳 覦 危 Kubectl run 牛伎 rc螻, RC pod襯 蟯襴螻(覲旧, 暑 豌 ), 碁 accessる podれ 觜る 語
  • 37. Pod 貉企 危 貎覯ろ一 ろ 譯殊 蟲煙 Pod Pod 襷 貉企襯 貉企 伎 危襴貅伎 襦語 譟伎 Pod 螻 private IP 語ろ 企 螳
  • 38. ReplicationController 危 Pod襯 覲旧 覲旧襯 讌讌 朱 Pod襯 Pod螳 暑覃 豌 Pod襯 襦
  • 40. 觜り 伎 危 Pod 殊企. (IP 殊) 蟲郁 Pod襯 蟇磯, Pod螳 碁 蟇磯 . RC螳 る Pod襯 襦 Pod襦 豌危螻 蠏碁覃 蠏 IP 殊 . 觜り 焔覃 螻IP襯 至螻 觜 覈伎 蟆一 覲讌 .
  • 41. Application れ朱 $ kubectl get replicationcontrollers NAME DESIRED CURRENT READY AGE kubia 1 1 1 33m $ kubectl scale rc kubia --replicas=3 replicationcontroller "kubia" scaled $ k get rc NAME DESIRED CURRENT READY AGE kubia 3 3 2 35m $ kubectl get pods NAME READY STATUS RESTARTS AGE kubia-57zlr 1/1 Running 0 1m kubia-k99gg 1/1 Running 0 36m kubia-sl82g 1/1 Running 0 1m
  • 42. 觜 豌 覈 豌讌 $ curl http://192.168.99.101:30701/ You've hit kubia-57zlr $ curl http://192.168.99.101:30701/ You've hit kubia-k99gg $ curl http://192.168.99.101:30701/ You've hit kubia-k99gg $ curl http://192.168.99.101:30701/ You've hit kubia-sl82g
  • 43. ろ 襦
  • 44. Pods襯 危 IP 碁 蠍 $ k get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE kubia-57zlr 1/1 Running 0 9m 172.17.0.5 minikube kubia-k99gg 1/1 Running 0 43m 172.17.0.7 minikube kubia-sl82g 1/1 Running 0 9m 172.17.0.6 minikube
  • 45. kubectl describe襯 伎 Pod碁 伎 $ kubectl describe pod kubia-57zlr Name: kubia-57zlr Namespace: default Priority: 0 PriorityClassName: <none> Node: minikube/10.0.2.15 Start Time: Mon, 14 Jan 2019 22:24:05 +0900 Labels: run=kubia Annotations: <none> Status: Running IP: 172.17.0.5
  • 46. Kubernetes dashboard 螳 $ kubectl cluster-info | grep dashboard $ minikube dashboard Opening http://127.0.0.1:62471/api/v1/namespaces/kube-system/services/http:kubernetes-dashboard:/prox y/ in your default browser...