9
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

Organization

[JAWS-UG KANSAI ハンズオン資料] Kubernetesを運用してみよう!企画#1 〜Kubernetes On EC2を構築してみよう〜

注意事項

手順

1. EC2を立ち上げる

  • keypairを作成する (chmod 600 xxx.pemを忘れずに)
  • EC2を3台立ち上げる (以下の設定以外は特に変更不要。わかりやすさを重視してVPCはdefault vpcを使う)
Key Value 補足
AMI Ubuntu 16.04 kubeadm推奨OS
Instance Type t3.medium kubeadmで2GB以上推奨
Security Group default 同SG間はフル解放されているため
  • 作成したEC2の1台のタグに Name:master を付与する
  • 残りのEC2の2台のダグに Name:worker を付与する
  • Security Group: defaultの22(SSH用),30080(Service公開用)を自分のIPだけ開放する

2. masterにsshしてkubeadmをセットアップする

dockerをインストールする

master
sudo su -
apt-get update && apt-get install -y docker.io=18.06.1-0ubuntu1.2~16.04.1
systemctl enable docker.service

kubeadmをインストールする

master
apt-get update && apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl

kubeadmをセットアップし、kubeconfigを設定する

master
# kubeadm init 実行後に出てくる kubeadm join コマンドを記録しておく
kubeadm init --pod-network-cidr=192.168.0.0/16

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

CNI(calico)をインストールする

master
kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml
kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml

3. workerにsshしてkubeadmをセットアップし、masterにjoinする

dockerをインストールする

worker
sudo su -
apt-get update && apt-get install -y docker.io=18.06.1-0ubuntu1.2~16.04.1
systemctl enable docker.service

kubeadmをインストールする

worker
apt-get update && apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl

masterにjoinする

worker
# masterで kubeadm init 実行時のjoinコマンドをコピペする
kubeadm join <ip>:6443 --token xxx123.aaaaaaaaaaaaaaaa --discovery-token-ca-cert-hash sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

4. masterでkubectlコマンドを操作する

master
$ kubectl get nodes
NAME               STATUS     ROLES    AGE     VERSION
ip-172-31-19-222   Ready      <none>   12s     v1.13.4
ip-172-31-25-223   NotReady   <none>   5s      v1.13.4
ip-172-31-26-53    Ready      master   3m16s   v1.13.4
master
$ kubectl get pod --all-namespaces
NAMESPACE     NAME                                      READY   STATUS    RESTARTS   AGE
kube-system   calico-node-2l4cb                         2/2     Running   0          53s
kube-system   calico-node-8tztv                         2/2     Running   0          59s
kube-system   calico-node-sxbcv                         2/2     Running   0          78s
kube-system   coredns-86c58d9df4-f7qbs                  1/1     Running   0          3m45s
kube-system   coredns-86c58d9df4-tpgh6                  1/1     Running   0          3m45s
kube-system   etcd-ip-172-31-26-53                      1/1     Running   0          2m46s
kube-system   kube-apiserver-ip-172-31-26-53            1/1     Running   0          2m56s
kube-system   kube-controller-manager-ip-172-31-26-53   1/1     Running   0          3m8s
kube-system   kube-proxy-kgmjn                          1/1     Running   0          53s
kube-system   kube-proxy-tcs8z                          1/1     Running   0          59s
kube-system   kube-proxy-xwhrg                          1/1     Running   0          3m45s
kube-system   kube-scheduler-ip-172-31-26-53            1/1     Running   0          3m2s
master
cat <<EOF > deployment.yaml 
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
EOF
kubectl apply -f deployment.yaml

cat <<EOF > service.yaml 
kind: Service
apiVersion: v1
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
  - protocol: "TCP"
    port: 8080
    targetPort: 80
    nodePort: 30080
EOF
kubectl apply -f service.yaml
master
$ kubectl get pod,deployment
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-deployment-76bf4969df-bcvd4   1/1     Running   0          48s
pod/nginx-deployment-76bf4969df-hcjsc   1/1     Running   0          48s

NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/nginx-deployment   2/2     2            2           48s

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
9
Help us understand the problem. What are the problem?