*https://microk8s.io/
*https://kubernetes.io/
*https://kubernetes.io/docs/tutorials/kubernetes-basics/
==基本操作==
===Document===
*[https://kubernetes.io/docs/tutorials/ チュートリアル]
*[https://kubernetes.io/docs/reference/glossary/?fundamental=true 標準用語集]
===インストール===
====[[Mac]]にインストール====
</pre>
*Driverを[[VirtualBox]]に変更する場合、先に変更してから、install
*[https://www.typea.info/blog/index.php/2020/11/20/mac_multipass_microk8s_virtuabox_networibridge/ Virtual Boxでネットワークブリッジを作成する]
<pre>
$ microk8s install
</pre>
====[[Ubuntu]]にインストール====
<pre>
</pre>
====ステータスの確認====
*microk8s status でステータスの確認
*インストール中などは、--wait-ready で状況確認できる
|}
===マニフェスト===*[https://kubernetes.io/ja/docs/reference/ リファレンス]*[https://kubernetes.io/docs/reference/using-api/api-overview/ API概要]*[https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/ リファレンス v1.19] ===ダッシュボード===
*https://microk8s.io/docs/addon-dashboard
*https://github.com/kubernetes/dashboard/blob/master/docs/user/access-control/README.md
[https://www.typea.info/blog/index.php/2020/11/17/mac_microk8s_dashboard_etc/ ダッシュボード接続手順、トラブルシュート]
<pre>
$ microk8s enable dashboard dns
$ microk8s dashboard-proxy
</pre>
===[[Kubectl]]===
*MicroK8s は専用のバージョンの[[kubectl]]をバンドルしている。
*コマンドを監視と制御のために実行することができる。
</pre>
===Kubeconfig===
*https://microk8s.io/docs/working-with-kubectl
*https://qiita.com/imamura_sh/items/91208a9b30e701d1e7f2
</pre>
==操作[https://github.com/kubernetes/examples Kubernetesサンプル]====Up and Deploy=={{amazon|4873118409}}*https://github.com/kubernetes-up-and-running/kuard
===クラスタ===
----
*https://kubernetes.io/ja/docs/tutorials/kubernetes-basics/
*Kubernetesクラスターは以下の2種類のリソースで構成
</pre>
==Up and Deploy==
{{amazon|4873118409}}
*https://github.com/kubernetes-up-and-running/kuard
===Pod===
====作成====
</pre>
====execでコマンド実行====
*[[kubectl]] -it で対話実行
<pre>
$ kubectl exec -it pod/nginx-pod -- /bin/sh
#
</pre>
====コンテナローカル間でファイルコピー====
*ローカル のファイルを、Podにコピー
*もし、ゲストをゲストブックにアクセスさせたい場合、Frontend サービスを外部に見えるようにする必要がある。
*クライアントがコンテナクラスターの外部からサービスにリクエストするためには、NodePortサービスを公開することで可能となる
=====LoadBalancer経由で参照する=====
*type を LoadBalancerに変更して、apply
<pre>
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# comment or delete the following line if you want to use a LoadBalancer
#type: NodePort
# if your cluster supports it, uncomment the following to automatically create
# an external load-balanced IP for the frontend service.
type: LoadBalancer
ports:
- port: 80
selector:
app: guestbook
tier: frontend
</pre>
*URL(kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-service.yaml)ではなく、上記を変更した、ローカルファイルからapplyする。
*Macで[[Multipass]]上のため、事前にマウントを実施済み
<pre>
$ kubectl apply -f ./files/php_guest_book/frontend-service.yaml
</pre>
<pre>
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 36h
redis-master ClusterIP 10.152.183.90 <none> 6379/TCP 3h1m
redis-slave ClusterIP 10.152.183.62 <none> 6379/TCP 169m
frontend LoadBalancer 10.152.183.187 192.168.0.120 80:30334/TCP 3m10s
</pre>
[[File:microk8s_php_guestbook.png|400px]]
*ダッシュボード
[[File:microk8s_php_guestbook_dashboard.png|400px]]
===クリーンアップ===
<pre>
$ kubectl delete deployment -l app=redis
$ kubectl delete service -l app=redis
$ kubectl delete deployment -l app=guestbook
$ kubectl delete service -l app=guestbook
</pre>
==永続ボリューム==
*https://kubernetes.io/ja/docs/concepts/storage/persistent-volumes/
*ストレージを管理することはインスタンスを管理することとは全くの別物
*PersistentVolumeサブシステムは、ストレージが何から提供されているか、どのように消費されているかをユーザーと管理者から抽象化するAPIを提供
*Podは、PersistentVolumeClaimを通して「こういうspecのVolumeが欲しい」と要求を出すと、その要求に一番近いPersistentVolumeがmountされるという仕組み
*metadata.name、metadata.labelsと、spec.selector.matchLabels、spec.selector.matchExpressionsの値を合わせることで、PersistentVolumeとPersistentVolumeClaimのマッチングをより明示的に、期待したとおりにできる
*https://thinkit.co.jp/article/14195
===PersistentVolume (PV)===
*ストレージクラスを使って管理者もしくは動的にプロビジョニングされるクラスターのストレージの一部
*Nodeと同じようにクラスターリソースの一部
*PVはVolumeのようなボリュームプラグインですが、PVを使う個別のPodとは独立したライフサイクルを持っています
*このAPIオブジェクトはNFS、iSCSIやクラウドプロバイダー固有のストレージシステムの実装の詳細を捕捉します。
===PersistentVolumeClaim (PVC)===
*ユーザーによって要求されるストレージで、これはPodと似ています
*PodはNodeリソースを消費し、PVCはPVリソースを消費します
*Podは特定レベルのCPUとメモリーリソースを要求することができます。クレームは特定のサイズやアクセスモード(例えば、ReadWriteOnce、ReadOnlyMany、ReadWriteManyにマウントできます
*PersistentVolumeClaimはユーザーに抽象化されたストレージリソースの消費を許可する一方、ユーザーは色々な問題に対処するためにパフォーマンスといった様々なプロパティを持ったPersistentVolumeを必要とすることは一般的なことです。
*クラスター管理者はユーザーに様々なボリュームがどのように実装されているかを表に出すことなく、サイズやアクセスモードだけではない色々な点で異なった、様々なPersistentVolumeを提供できる必要があります。これらのニーズに応えるために StorageClass リソースがあります。
===チュートリアル===
*https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/
*クラスター管理者の場合、PersistentVolumeを物理ストレージに作成し、VolumeをPodに関連づける必要はない
*開発者、クラスタユーザーの場合、PersistentVolumeChlaimを作成すると、自動的に適したPersistentVolumeに結び付けられる
*Podを生成し、PersistentVolumeClaimを利用することで、ストレージを利用できる
====index.htmlをNodeの作成する====
<pre>
$ multipass sh microk8s-vm
ubuntu@microk8s-vm:~$ sudo mkdir /mnt/data
ubuntu@microk8s-vm:~$ sudo sh -c "echo 'Hello from Kubernetes storage' > /mnt/data/index.html"
</pre>
====PersistentVolume生成====
*hostPath PersistentVolume を作成する。KubernetesはhostPathをシングルノードクラスタの開発/テストでサポートする
*hostPath PersistentVolume は、ネットワークアタッチドストレージをエミュレートしたものとしてNodeのファイル、もしくはディレクトリを使用する
*製品版のクラスタでは、hostPathを使わない方が良い。クラスター管理者は、その代わりにネットワークリソース、Google Compute Engine 永続ディスクや、NFS 共有、Amazon Elastic Block Store Volumeを準備する
*クラスター管理者は動的プロビジョニングのために、StorageClassを利用する
<pre>
apiVersion: v1
kind: PersistentVolume
metadata:
name: task-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
</pre>
<pre>
$ kubectl apply -f https://k8s.io/examples/pods/storage/pv-volume.yaml
persistentvolume/task-pv-volume created
</pre>
<pre>
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
task-pv-volume 10Gi RWO Retain Available manual 41s
</pre>
====PersistentVolumeClaimの生成====
*PodはPersistentVolumeClaimを物理ストレージの要求に使用する
<pre>
$ kubectl apply -f https://k8s.io/examples/pods/storage/pv-claim.yaml
persistentvolumeclaim/task-pv-claim created
</pre>
<pre>
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
task-pv-volume 10Gi RWO Retain Bound default/task-pv-claim manual 44m
$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
task-pv-claim Bound task-pv-volume 10Gi RWO manual 77s
</pre>
====Podの生成====
*PersistentVolumeClaimをVolumeとして利用するPodの作成
<pre>
apiVersion: v1
kind: Pod
metadata:
name: task-pv-pod
spec:
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: task-pv-claim
containers:
- name: task-pv-container
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: task-pv-storage
</pre>
<pre>
$ kubectl apply -f https://k8s.io/examples/pods/storage/pv-pod.yaml
pod/task-pv-pod created
</pre>
<pre>
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
task-pv-pod 1/1 Running 0 2m5s
</pre>
*マウントしたVolume情報を確認
<pre>
kubectl exec -it pod/task-pv-pod -- /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# cat /usr/share/nginx/html/index.html
Hello from Kubernetes storage
</pre>
<pre>
# apt update
# apt install curl
# curl http://localhost
Hello from Kubernetes storage
</pre>
====クリーンアップ====
<pre>
$ kubectl delete pod task-pv-pod
$ kubectl delete pvc task-pv-claim
$ kubectl delete pv task-pv-volume
</pre>
==StatefulSet==
*https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/
===事前準備===
*[https://kubernetes.io/docs/concepts/workloads/pods/ Pods]
*[https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/ Cluster DNS]
*[https://kubernetes.io/docs/concepts/services-networking/service/#headless-services Headless Services]
*[https://kubernetes.io/docs/concepts/storage/persistent-volumes/ PersistentVolumes]
*[https://github.com/kubernetes/examples/tree/master/staging/persistent-volume-provisioning/ PersistentVolume Provisioning]
*[https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/ StatefulSets]
===StatefulSet作成===
*https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/application/web/web.yaml
*ダウンロードしてapply
<pre>
$ kubectl apply -f ./files/statefulset-basic/web.yaml
service/nginx created
statefulset.apps/web created
</pre>
<pre>
$ kubectl get services nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx ClusterIP None <none> 80/TCP 4m18s
$ kubectl get statefulset
NAME READY AGE
web 0/2 4m22s
</pre>
====PersistentVolumeClaimsエラーの対応====
<pre>
$ kubectl describe pods
Name: web-0
:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 40s (x8 over 9m33s) default-scheduler 0/1 nodes are available: 1 pod has unbound immediate PersistentVolumeClaims.
</pre>
*https://qiita.com/silverbirder/items/d3522237b28703a9adb6
*https://stackoverflow.com/questions/52668938/pod-has-unbound-persistentvolumeclaims
<pre>
$ multipass sh microk8s-vm
ubuntu@microk8s-vm:~$ sudo mkdir /mnt/data
</pre>
*[https://kubernetes.io/ja/docs/concepts/storage/persistent-volumes/ accessModes]
**ReadWriteOnce –ボリュームは単一のNodeで読み取り/書き込みとしてマウントできます
**ReadOnlyMany –ボリュームは多数のNodeで読み取り専用としてマウントできます
**ReadWriteMany –ボリュームは多数のNodeで読み取り/書き込みとしてマウントできます
<blockquote>
HostPath は、ReadWriteOnce しか選択できない
</blockquote>
<pre>
apiVersion: v1
kind: PersistentVolume
metadata:
name: web-pv
labels:
type: local
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
</pre>
<pre>
$ microk8s apply -f ./files/statefulset-basic/pv.yaml
persistentvolume/web-pv created
</pre>
===順序付けられた Pod 生成===
*StatefulSet n レプリカのために、Pod がデプロイされるときシーケンシャルに生成され順序づけられる(0...n-1)
<pre>
$ kubectl get pods -w -l app=nginx
NAME READY STATUS RESTARTS AGE
web-0 0/1 Pending 0 7m32s
web-0 0/1 Pending 0 20m
web-0 0/1 ContainerCreating 0 20m
web-0 0/1 ContainerCreating 0 20m
web-0 1/1 Running 0 20m
web-1 0/1 Pending 0 0s
web-1 0/1 Pending 0 0s
</pre>
===StatefulSetのpod===
*StatefulSetに含まれるPodは、一意な順序と安定したネットワークIDを持つ
*StetefulSetのPod は一意のIDを持つ
*このIDは、一意の順序付けられたインデックス、それぞれのpodがStatefulSetコントローラに結び付けられている
*Pod の名前は、<statefulset名>-<順序づけれれたindex> となる。
<pre>
$ kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
web-1 0/1 Pending 0 22h
web-0 0/1 Unknown 0 23h
</pre>
===Stable ネットワークIDを使用する ===
*それぞれのPod は順序付けられたインデックスに基づく安定したホスト名を持つ
*kubectl exec でそれぞれのpodにhostname コマンドを実行する
<pre>
$ for i in 0 1; do kubectl exec pod/web-$i hostname; done
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
web-0
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Error from server (BadRequest): pod web-1 does not have a host assigned
An error occurred when trying to execute 'sudo microk8s.kubectl exec pod/web-1 hostname' with 'multipass': returned exit code 1.
</pre>
<blockquote>
永続化Volumeが、hostPath指定の場合、1つのPodからしか接続できないため立ち上がらない。
</blockquote>
<pre>
$ kubectl describe pods web-1
:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 22h (x14 over 23h) default-scheduler 0/1 nodes are available: 1 pod has unbound immediate PersistentVolumeClaims.
</pre>
===[[Ubuntu NFS構成|NFS]]の構成===
*[[Ubuntu NFS構成]]
*[https://www.server-world.info/en/note?os=Ubuntu_20.04&p=microk8s&f=5 microk8s storage class]
*[https://qiita.com/Esfahan/items/7ad7695ea78c7630239a 外部ストレージをマウント]
*[https://qiita.com/Esfahan/items/68e2d97545091cb6d0ac NFS storage class]
*https://thinkit.co.jp/article/14195
*https://kubernetes.io/docs/concepts/storage/volumes/
*https://github.com/kubernetes/examples/tree/master/staging/volumes/nfs
[https://baremetal.jp/blog/2018/04/17/541/ NFS]
<pre>
apiVersion: v1
kind: PersistentVolume
metadata:
name: web-pv
labels:
type: local
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
storageClassName: slow
nfs:
server: 192.168.0.47
path: "/var/nfs/general"
</pre>
<pre>
$ kubectl describe pv
Name: web-pv
Labels: type=local
Annotations: <none>
Finalizers: [kubernetes.io/pv-protection]
StorageClass: slow
Status: Available
Claim:
Reclaim Policy: Retain
Access Modes: RWX
VolumeMode: Filesystem
Capacity: 5Gi
Node Affinity: <none>
Message:
Source:
Type: NFS (an NFS mount that lasts the lifetime of a pod)
Server: 192.168.0.47
Path: /var/nfs/general
ReadOnly: false
Events: <none>
</pre>
https://qiita.com/silverbirder/items/d3522237b28703a9adb6
<pre>
$ kubectl apply Type Reason Age From Message -f https--- ------ ---- ---- ------- Warning FailedScheduling 2s (x6 over 5m58s) default-scheduler 0/1 nodes are available://k8s.io/examples/application/guestbook/frontend-service1 pod has unbound immediate PersistentVolumeClaims.yaml
</pre>
===デプロイ===
Kubernetesにアプリケーションをデプロイするときは、
#マスターにアプリケーションコンテナを起動するように指示