LoginSignup
0
2

More than 5 years have passed since last update.

Kubernetes勉強 その1(環境構築)

Last updated at Posted at 2018-10-01

「Kubernetes完全ガイド」読んでお勉強開始。

勉強用の環境設定

本番ではまず使わないと思うけど、ローカルで試すために minikube をインストールする。
なお、 homebrew の入ったMacOSXを想定。

パッケージ情報のアップデート

$ brew update

VirtualBoxのインストール

$ breaw cask install virtualbox

Minikubeのインストール

$ brew cask install minikube

バージョン確認

$ minikube version
minikube version: v0.28.2

Kubernetesのインストール

minikube で使用できる kubernetes のバージョンを確認。

$ minikube get-k8s-versions

The following Kubernetes versions are available when using the localkube bootstrapper: 
    - v1.10.0
    - v1.9.4
    - v1.9.0
    - v1.8.0
    - v1.7.5
    ...

最新版を起動(今回は v1.10.0

$ minikube start

Starting local Kubernetes v1.10.0 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
Loading cached images from config file.

結構時間かかります☕️

kubectlによるワーカーの起動

カレントディレクトリに以下の内容のファイルを作る

sample-pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: sample-pod
spec:
  containers:
    - name: nginx-container
      image: nginx:1.12

起動する

$ kubectl apply -f sample-pod.yml 

pod "sample-pod" created

Podの状態を確認

$ kubectl get pods

NAME         READY     STATUS              RESTARTS   AGE
sample-pod   0/1       ContainerCreating   0          34s

Statusが ContainerCreating になっている。まだ作成中みたいだ。

数分後に再度チェック

$ kubectl get pods

NAME         READY     STATUS    RESTARTS   AGE
sample-pod   1/1       Running   0          1m

お、Statusが Running に変わった。起動したみたい。

続いて、Nginxのバージョンを 1.12 から 1.13 にあげる。
先ほどの sample-pod.yml を以下のように編集する.

sample-pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: sample-pod
spec:
  containers:
    - name: nginx-container
      image: nginx:1.13

podを停止せずにそのまま apply を実行。

$ kubectl apply -f sample-pod.yml

pod "sample-pod" configured

これでNginxが 1.12 から 1.13 になってる...はず。

リソースの削除

$ kubectl delete -f sample-pod.yml

pod "sample-pod" deleted

削除はあっという間なんだな。

複数のリソースを定義して起動する

ファイルはこんな感じ

sample2.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: order1-development
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
---
apiVersion: v1
kind: Service
metadata:
  name: order2-service
spec:
  type: LoadBalancer
  ports:
    - name: "http-port"
      protocol: "TCP"
      port: 8080
      targetPort: 80
  selector:
    app: sample-app

では起動してみよう。

$ kubectl apply -f sample2.yml

deployment.apps "order1-development" created
service "order2-service" created

状態チェック

まずはDeploymentの状態チェック

$ kubectl get deployment

NAME                 DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
order1-development   3         3         3            3           1m

ちゃんと3つ起動してる。
続いてService

$ kubectl get service

NAME             TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes       ClusterIP      10.96.0.1       <none>        443/TCP          26m
order2-service   LoadBalancer   10.111.39.199   <pending>     8080:30318/TCP   2m

10.111.39.1998080 ポートでロードバランサが起動している。

最後に全て削除して終了。

$ kubectl delete -f sample2.yml

deployment.apps "order1-development" deleted
service "order2-service" deleted
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2