資料

Docker入門

はてなリモートインターン2020 講義2「コンテナ」

はてなインターンシップ コンテナ

Kubernetes入門

はてなリモートインターン2020 講義3「Kubernetes」

はてなのインターンシップのKubernetes

Kubernetes応用

マイクロサービス

はてなのインターンシップのマイクロサービス

ストレージ

Cheat Sheet

image.png (171.2 kB)

Tips

GKE

Kubernetesデザインパターン

実装

テスト用Dockerイメージ

ホスト名を返す

  • https://github.com/adongy/hostname-docker
  • https://hub.docker.com/r/nginxdemos/hello/
  • https://registry.hub.docker.com/r/pmorjan/demo
  • https://github.com/paulbouwer/hello-kubernetes
  • https://github.com/brndnmtthws/nginx-echo-headers
  • https://github.com/mccutchen/go-httpbin

Kubernetes操作

チートシート: https://kubernetes.io/docs/reference/kubectl/cheatsheet/

リソースの操作

作成/更新

kubectl apply -f xxx.yaml

削除

kubectl delete -f xxx.yaml

ネームスペースの操作

新規作成

kubectl create namespace <namespace>

確認(一覧)

kubectl get ns

デフォルトネームスペースを変更

kubectl config set-context --current --namespace=dev-koyama

現在のネームスペースを取得

kubectl config view -o jsonpath='{.contexts[].context.namespace}' ; echo

コンテキストの操作

現在のコンテキスト

kubectl config current-context

利用できるコンテキスト一覧

kubectl config get-contexts

使用するコンテキストを設定

kubectl config use-context <CONTEXT_NAME>

クラスタの操作

現在の設定を確認

kubectl config get-contexts

設定を変更

kubectl config use-context docker-desktop

コンテナでコマンドを実行

podが複数コンテナの場合

kubectl exec <pod_name> --container <container_name> -- <command> 

podが単一コンテナの場合

kubectl exec <pod_name> -- <command> 

コンテナのシェルに入る

podが複数コンテナの場合

kubectl exec -it <pod_name> --container <container_name> -- /bin/bash

podが単一コンテナの場合

kubectl exec -it <pod_name> -- /bin/bash

レプリカ数の変更

kubectl scale --replicas=1 -f rs.yaml

デバッグ用コンテナを1つデプロイ

--rm をつけるとexitするとコンテナが消える.

kubectl run -it --rm ubuntu --image=ubuntu:18.04 /bin/bash
kubectl run -it --rm bb -n site2021 --image=busybox sh

Kubernetesサンプル設定ファイル

Deployment

https://github.com/MasayaAoyama/kubernetes-perfect-guide/blob/master/samples/chapter05/sample-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.12
          ports:
            - containerPort: 80

Service(Cluster IP)

https://github.com/MasayaAoyama/kubernetes-perfect-guide/blob/344391704dd78bbdd92dd5c0b2fd143a6633c374/samples/chapter06/sample-clusterip.yaml

apiVersion: v1
kind: Service
metadata:
  name: sample-clusterip
spec:
  type: ClusterIP
  ports:
    - name: "http-port"
      protocol: "TCP"
      port: 8080
      targetPort: 80
  selector:
    app: sample-app
curl http://<ip-address>:8080/

Service(NodePort)

https://github.com/MasayaAoyama/kubernetes-perfect-guide/blob/344391704dd78bbdd92dd5c0b2fd143a6633c374/samples/chapter06/sample-nodeport.yaml

apiVersion: v1
kind: Service
metadata:
  name: sample-nodeport
spec:
  type: NodePort
  ports:
    - name: "http-port"
      protocol: "TCP"
      port: 8080
      targetPort: 80
      nodePort: 30080
  selector:
    app: sample-app
curl http://<ip-address>:30080/