LoginSignup
6
3

More than 3 years have passed since last update.

EC2インスタンス上でk3dをインストールしてサービス起動チェックまで

Last updated at Posted at 2019-09-18

k3d

  • Kubernetes
  • k3sは1ノード専門だけれど、そのk3sを複数ノードで起動してクラスタ化してくれる
  • Docker上で動き、1つのノードが1コンテナ扱いで起動される

環境

  • AWS EC2
  • Amazon Linux 2

Dockerが動いていれば、Macでもいける模様

前提条件

yum -y install docker
systemctl start docker

k3dインストール

curl -s https://raw.githubusercontent.com/rancher/k3d/master/install.sh | bash

kubectlインストール

curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
kubectl version

クラスタ作成

k3d create

環境変数

export KUBECONFIG=$(k3d get-kubeconfig)

Pod確認

kubectl get pods --all-namespaces

削除

k3d delete

nginxをデプロイしてみるテスト

nginx-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.5
        ports:
        - containerPort: 80

デプロイ

kubectl apply -f nginx-deployment.yaml

Podへ接続するためのサービスデプロイ

kubectl expose deployment/nginx --type="NodePort" --port 80
kubectl get service

拒否られる

[root@ip-172-31-47-223 ~]# curl -i http://localhost:30757
curl: (7) Failed to connect to localhost port 30757: Connection refused

再チャレンジ

公式ドキュメントをみるとIngressとNodePortでのデプロイ方法が説明されている

Ingress

k3d create --api-port 6550 --publish 8081:80 --workers 2
export KUBECONFIG="$(k3d get-kubeconfig --name='k3s-default')"
kubectl create deployment nginx --image=nginx
kubectl create service clusterip nginx --tcp=80:80
nginx-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx
  annotations:
    ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: nginx
          servicePort: 80
kubectl  apply -f nginx-ingress.yaml
curl localhost:8081/
k3d delete

NodePort

NodePortのPortへ直接アクセスしても接続できない。
下のコマンドの場合、NodePortのポートをローカルのポートと紐づけてあげるということをしているみたい。
よくよく考えてみると、ローカルではなくDocker上で動作をしているので、このような操作が必要なのかも。

k3d create --publish 8082:30080@k3d-k3s-default-worker-0 --workers 2

dockerコンテナの数は3つで、デフォルトサーバ1つとワーカー2つ

[root@ip-172-31-47-223 ~]# docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                     NAMES
da228d6d1786        rancher/k3s:v0.7.0   "/bin/k3s agent"         23 minutes ago      Up 23 minutes                                 k3d-k3s-default-worker-1
b68c0032047f        rancher/k3s:v0.7.0   "/bin/k3s agent"         23 minutes ago      Up 23 minutes       0.0.0.0:8082->30080/tcp   k3d-k3s-default-worker-0
f2209b7494ea        rancher/k3s:v0.7.0   "/bin/k3s server --h…"   23 minutes ago      Up 23 minutes       0.0.0.0:6443->6443/tcp    k3d-k3s-default-server
export KUBECONFIG="$(k3d get-kubeconfig --name='k3s-default')"
kubectl create deployment nginx --image=nginx
nginx-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  ports:
  - name: 80-80
    nodePort: 30080
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
kubectl apply -f nginx-nodeport.yaml
curl localhost:8082/
k3s delete

参考

6
3
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
6
3