ためすう
Kubernetes の基本を学ぶ (スケーリング)
2019-10-31やったこと
Kubernetes のチュートリアルをやります。
Mac のローカル環境で試します。
調査
スケーリング
Deploymentをスケールアウトすると、新しいPodが作成され、使用可能なリソースを持つNodeにスケジュールされます。
チュートリアル
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 3h37m
$ kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment.apps/kubernetes-bootcamp scaled
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 4/4 4 4 3h37m
$ kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: app=kubernetes-bootcamp
Annotations: <none>
Selector: app=kubernetes-bootcamp
Type: NodePort
IP: 10.101.175.64
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30981/TCP
Endpoints: 172.17.0.10:8080,172.17.0.7:8080,172.17.0.8:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
$ export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
$ echo NODE_PORT=$NODE_PORT
NODE_PORT=30981
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-69fbc6f4cf-5d5dm | v=1
参考
Kubernetes の基本を学ぶ (Service)
2019-10-29やったこと
Kubernetes のチュートリアルをやります。
Mac のローカル環境で試します。
調査
Service
ワーカーのNodeが停止すると、そのNodeで実行されているPodも失われます。
各Podには固有のIPアドレスがありますが、それらのIPは、Serviceなしではクラスタの外部に公開されません。Serviceによって、アプリケーションはトラフィックを受信できるようになります。
チュートリアル
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d16h
$ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
service/kubernetes-bootcamp exposed
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d16h
kubernetes-bootcamp NodePort 10.101.175.64 <none> 8080:30981/TCP 18s
$ kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: app=kubernetes-bootcamp
Annotations: <none>
Selector: app=kubernetes-bootcamp
Type: NodePort
IP: 10.101.175.64
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30981/TCP
Endpoints: 172.17.0.2:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
$ export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
$ curl $(minikube ip):$NODE_PORT
$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME
$ kubectl label pod $POD_NAME app=v1 --overwrite
$ kubectl get pods -l app=v1
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-69fbc6f4cf-rfvs5 1/1 Running 0 3h32m
参考
Kubernetes の基本を学ぶ (Pod と Node)
2019-10-27やったこと
Kubernetes のチュートリアルをやります。
Mac のローカル環境で試します。
調査
Pod
- 共有ストレージ(ボリューム)
- ネットワーキング(クラスタに固有のIPアドレス)
- コンテナのイメージバージョンや使用するポートなどの、各コンテナをどう動かすかに関する情報
Node
Podは常にNode上で動作します。NodeはKubernetesではワーカーマシンであり、クラスタによって仮想、物理マシンのどちらであってもかまいません。
チュートリアル
$ kubectl describe pods
※ 長いので出力結果は省略
$ kubectl proxy
Starting to serve on 127.0.0.1:8001
$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
$ echo Name of the Pod: $POD_NAME
Name of the Pod: kubernetes-bootcamp-69fbc6f4cf-rfvs5
$ kubectl exec $POD_NAME env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=kubernetes-bootcamp-69fbc6f4cf-rfvs5
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
NPM_CONFIG_LOGLEVEL=info
NODE_VERSION=6.3.1
HOME=/root
コンテナに入る
$ kubectl exec -ti $POD_NAME bash
@コンテナ
# curl localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-69fbc6f4cf-rfvs5 | v=1
ログ確認
$ kubectl logs $POD_NAME
Kubernetes Bootcamp App Started At: 2019-10-27T06:22:01.080Z | Running On: kubernetes-bootcamp-69fbc6f4cf-rfvs5
Running On: kubernetes-bootcamp-69fbc6f4cf-rfvs5 | Total Requests: 1 | App Uptime: 2741.92 seconds | Log Time: 2019-10-27T07:07:43.000Z
参考
Kubernetes の基本を学ぶ (Deployment)
2019-10-27やったこと
Kubernetes のチュートリアルをやります。
Mac のローカル環境で試します。
調査
Deployment
Kubernetesにあなたのアプリケーションのインスタンスを作成し、更新する方法を指示します。
マシンの故障やメンテナンスに対処するためのセルフヒーリングの仕組みを提供しています。
Deploymentを作成するときは、アプリケーションのコンテナイメージと実行するレプリカの数を指定する必要があります。
チュートリアル
$ kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
deployment.apps/kubernetes-bootcamp created
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 0/1 1 0 11s
proxy を立ち上げる
$ kubectl proxy
Starting to serve on 127.0.0.1:8001
$ curl http://localhost:8001/version
{
"major": "1",
"minor": "16",
"gitVersion": "v1.16.0",
"gitCommit": "2bd9643cee5b3b3a5ecbd3af49d09018f0773c77",
"gitTreeState": "clean",
"buildDate": "2019-09-18T14:27:17Z",
"goVersion": "go1.12.9",
"compiler": "gc",
"platform": "linux/amd64"
}
pod name を表示する
$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
$ echo Name of the Pod: $POD_NAME
Name of the Pod: kubernetes-bootcamp-69fbc6f4cf-rfvs5
参考
Kubernetes の基本を学ぶ (クラスタの作成)
2019-10-27やったこと
Kubernetes のチュートリアルをやります。
Mac のローカル環境で試します。
調査
Kubernetesクラスタ
- マスターはクラスタの管理を担当します。
- Nodeは、Kubernetesクラスタのワーカーマシンとして機能するVMまたは物理マシンです
チュートリアル
minikube version 確認
$ minikube version
minikube version: v1.4.0
commit: 7969c25a98a018b94ea87d949350f3271e9d64b6
$ minikube start
🎉 minikube 1.5.0 is available! Download it: https://github.com/kubernetes/minikube/releases/tag/v1.5.0
💡 To disable this notice, run: 'minikube config set WantUpdateNotification false'
🙄 minikube v1.4.0 on Darwin 10.13.6
💡 Tip: Use 'minikube start -p <name>' to create a new cluster, or 'minikube delete' to delete this one.
🏃 Using the running virtualbox "minikube" VM ...
⌛ Waiting for the host to be provisioned ...
🐳 Preparing Kubernetes v1.16.0 on Docker 18.09.9 ...
🔄 Relaunching Kubernetes using kubeadm ...
⌛ Waiting for: apiserver proxy etcd scheduler controller dns
🏄 Done! kubectl is now configured to use "minikube"
kubectl version 確認
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.2", GitCommit:"c97fe5036ef3df2967d086711e6c0c405941e14b", GitTreeState:"clean", BuildDate:"2019-10-15T19:18:23Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.0", GitCommit:"2bd9643cee5b3b3a5ecbd3af49d09018f0773c77", GitTreeState:"clean", BuildDate:"2019-09-18T14:27:17Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
$ kubectl cluster-info
Kubernetes master is running at https://192.168.99.102:8443
KubeDNS is running at https://192.168.99.102:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready master 2d15h v1.16.0
参考
Minikube チュートリアルをやってみる
2019-10-25やったこと
Hello Minikube のチュートリアルをやります。
Mac のローカル環境で試します。
調査
Minicube クラスタ作成
$ minikube start
$ minikube dashboard
Deployment (Pod) 作成
$ kubectl create deployment hello-node --image=gcr.io/hello-minikube-zero-install/hello-node
deployment.apps/hello-node created
Deployment
Kubernetesの Deployment はPodの状態を確認し、Podのコンテナが停止した場合には再起動します。
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
hello-node 0/1 1 0 9s
pods
KubernetesのPod は、コンテナの管理やネットワーキングの目的でまとめられた、1つ以上のコンテナのグループです。
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-node-7676b5fb8d-shsvh 0/1 ContainerCreating 0 41s
Service 作成
コンテナを仮想ネットワークの外部からアクセスできるようにするため、サービスを作成する必要があります。
$ kubectl expose deployment hello-node --type=LoadBalancer --port=8080
service/hello-node exposed
確認
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-node LoadBalancer 10.109.94.113 <pending> 8080:32678/TCP 24s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 36m
ブラウザが開きます
$ minikube service hello-node
|-----------|------------|-------------|-----------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|------------|-------------|-----------------------------|
| default | hello-node | | http://192.168.99.102:32678 |
|-----------|------------|-------------|-----------------------------|
🎉 Opening kubernetes service default/hello-node in default browser...
おまけ
Katacoda はブラウザから Docker や Kubernetes を試すことが出来ます。
参考
Mac で kubectl と Minikube をインストール
2019-10-24やったこと
Mac で kubectl と Minikube をインストールします。
調査
kubectl インストール
$ curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl"
$ chmod +x ./kubectl
$ sudo mv ./kubectl /usr/local/bin/kubectl
バージョン確認
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.2", GitCommit:"c97fe5036ef3df2967d086711e6c0c405941e14b", GitTreeState:"clean", BuildDate:"2019-10-15T19:18:23Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"darwin/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?
Minikube インストール
$ brew cask install minikube
$ minikube version
minikube version: v1.4.0
commit: 7969c25a98a018b94ea87d949350f3271e9d64b6
参考
サーバーの仮想化
2019-10-22やったこと
サーバーの仮想化の概要を調べます。
調査
ホストOS型
ホストOSにゲストOSをインストールして利用します。
ソフトウェア例
- VirtualBox
- VMware
ハイパーバイザー型
コンピュータの仮想化技術のひとつである仮想機械(バーチャルマシン)を実現するための、制御プログラムである。仮想化モニタや仮想化OSと呼ばれることもある。
ソフトウェア例
- Hyper-V
- Linux KVM
コンテナ型
ゲストOSが不要で、単一のOSに対して、独立した環境を提供することが出来ます。
ソフトウェア例
- Docker
参考
GCP 周りの用語を整理する
2019-10-22やったこと
GCP (Google Cloud Platform) 周りの用語を整理します。
調査
GCP (Google Cloud Platform)
Google Cloud Platform(グーグル クラウド プラットフォーム、GCP)とは、Googleが提供しているクラウドコンピューティングサービスである。
GCE (Google Compute Engine)
Google Compute Engine は、Google のデータセンターとファイバー ネットワークで運用される仮想マシンを提供します。
GKE (Google Kubernetes Engine)
Google Kubernetes Engine は、Google のインフラストラクチャを使用して、コンテナ化されたアプリケーションのデプロイ、管理、スケーリングを行うマネージド環境を提供します。
GAE (Google App Engine)
Google App Engine は、Google のインフラの上でアプリケーションを作り、実行できるようにする PaaS ( Platform as a Service : サービスとしてのプラットフォーム)です。
参考
ssh-add を使う
2019-10-22やったこと
ssh-add について調べます。
確認環境
$ man ssh-add --help
man, version 1.6c
調査
man より抜粋
DESCRIPTION ssh-add adds private key identities to the authentication agent, ssh-agent(1). When run without arguments, it adds the files ~/.ssh/id_rsa, ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, and ~/.ssh/id_ed25519. After loading a private key, ssh-add will try to load corresponding certificate information from the filename obtained by appending -cert.pub to the name of the private key file. Alternative file names can be given on the command line.
ssh コマンドを使うとき、パスフレーズの入力をスキップ出来ます。
秘密鍵の登録
$ ssh-add ~/.ssh/id_rsa
Enter passphrase for /Users/hogehoge/.ssh/id_rsa:
Identity added: /Users/hogehoge/.ssh/id_rsa (/Users/hogehoge/.ssh/id_rsa)
$ ssh-add -l
2048 SHA256:xxxxxxxxxxx /Users/hogehoge/.ssh/id_rsa (RSA)
秘密鍵の削除
$ ssh-add -d ~/.ssh/id_rsa
Identity removed: /Users/hogehoge/.ssh/id_rsa (hogehoge@hogehoge-no-MBP)
$ ssh-add -l
The agent has no identities.
キーチェーンに保存する
-K When adding identities, each passphrase will also be stored in the user's keychain. When removing identities with -d, each passphrase will be
removed from it.