LoginSignup
13

More than 5 years have passed since last update.

KubernetesをCentOS7 1台(AWS EC2)へインストール

Last updated at Posted at 2016-02-07

kubernetesをAWS EC2のCentOS7にインストールしてみた。
1台でMaster/Minionを兼ねるシンプルなパターン。
※AWS用のセットアップスクリプトが提供されている(http://kubernetes.io/v1.1/docs/getting-started-guides/aws.html) が、ここでは手動でトライ。

参考URL

1.AWS EC2でCentOS7インスタンス起動

CentOS7インスタンスは、AWS Marketplaceから指定する
https://www.youtube.com/watch?v=_tWa5cIhO-k

CentOS7インスタンスは、ユーザ名がec2-userではなくcentosになっているので注意。
t2-micro1の場合メモリ不足の可能性があるため、t2-largeにしてみた。

2.dockerをインストール

sudo yum update -y

sudo yum install -y docker

sudoなしでcentosユーザがdockerコマンドを実行できるようにする設定

sudo usermod -a -G docker centos

3.kubernetes/etcdをインストール

(1)yumリポジトリを追加
※yum -y install kubernetesでインストールできた場合は不要

sudo /etc/yum.repos.d/\_virt7-testing.repo

name=virt7-testing
baseurl=http://cbs.centos.org/repos/virt7-common-testing/x86_64/os/
gpgcheck=0

(2)kubernetesをインストール
sudo yum -y install --enablerepo=virt7-testing kubernetes

(3)etcdをインストール
sudo yum -y install etcd

または

sudo yum install http://cbs.centos.org/kojifiles/packages/etcd/2.0.9/1.el7/x86_64/etcd-2.0.9-1.el7.x86_64.rpm

(4)/etc/hostsを編集
sudo vi /etc/hosts
以下を追加(IPはEC2のプライベートIP)
XX.XX.XX.XX centos-master

(5)/etc/kubernetes/configを編集

# kubernetes system config
#
# The following values are used to configure various aspects of all
# kubernetes services, including
#
#   kube-apiserver.service
#   kube-controller-manager.service
#   kube-scheduler.service
#   kubelet.service
#   kube-proxy.service
# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers=http://centos-master:4001"

# logging to stderr means we get it in the systemd journal
KUBE_LOGTOSTDERR="--logtostderr=true"

# journal message level, 0 is debug
KUBE_LOG_LEVEL="--v=0"

# Should this cluster be allowed to run privileged docker containers
KUBE_ALLOW_PRIV="--allow_privileged=false"

# How the controller-manager, scheduler, and proxy find the apiserver
KUBE_MASTER="--master=http://127.0.0.1:8080"

(6)/etc/kubernetes/apiserver を編集

###
# kubernetes system config
#
# The following values are used to configure the kube-apiserver
#

# The address on the local server to listen to.
KUBE_API_ADDRESS="--address=0.0.0.0"

# The port on the local server to listen on.
KUBE_API_PORT="--port=8080"

# How the replication controller and scheduler find the kube-apiserver
KUBE_MASTER="--master=http://centos-master:8080"

# Port minions listen on
KUBELET_PORT="--kubelet_port=10250"

# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd_servers=http://127.0.0.1:2379"

# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"

# default admission control policies
#KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"
KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,ResourceQuota"

# Add your own!
KUBE_API_ARGS=""

(7)/etc/kubernetes/kubelet を編集

###
# kubernetes kubelet (minion) config

# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
#KUBELET_ADDRESS="--address=127.0.0.1"
KUBELET_ADDRESS="--address=0.0.0.0"

# The port for the info server to serve on
KUBELET_PORT="--port=10250"

# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname_override=centos-master"

# location of the api-server
KUBELET_API_SERVER="--api_servers=http://centos-master:8080"

# Add your own!
KUBELET_ARGS=""

(8)/etc/etcd/etcd.conf を編集

ETCD_LISTEN_CLIENT_URLS="http://localhost:2379"

(9)~/.kube/config を編集
以下のコマンドを実行(または~/.kube/configを直接編集)
kubectl get nodesで設定確認もできる

kubectl config set-credentials myself --username=centos --password=<適当なパスワード>
kubectl config set-cluster local-server --server=http://centos-master:8080
kubectl config set-context default-context --cluster=local-server --user=centos
kubectl config use-context default-context
kubectl config set contexts.default-context.namespace default

4.kubernetes(および関連コンポーネント)を起動

(1)master起動用スクリプト

vi start_kuber_master_service.sh

#!/bin/sh

for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do 
    systemctl restart $SERVICES
    systemctl enable $SERVICES
    systemctl status $SERVICES 
done

sudo sh start_kuber_master_service.sh

(2)minion起動用スクリプト

vi start_kuber_minion_service.sh

#!/bin/sh


for SERVICES in kube-proxy kubelet docker; do 
    systemctl restart $SERVICES
    systemctl enable $SERVICES
    systemctl status $SERVICES 
done

sudo sh start_kuber_minion_service.sh

5.動作確認

kubectl get nodes

NAME            LABELS                                 STATUS
centos-master   kubernetes.io/hostname=centos-master   Ready

podを立てる

参考)http://qiita.com/Arturias/items/62499b961b5d7375f608

vi pod-nginx.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
    - name: nginx-container
      image: nginx
      ports:
      - containerPort: 80

kubectl create -f pod-nginx.yaml

$ kubectl get pod
NAME        READY     STATUS    RESTARTS   AGE
nginx-pod   1/1       Running   0          6m

[トラブルシューティング]


kubectlコマンド実行時に以下のようなエラーがでたら、config系の設定に誤りがあるか、kube-apiserverなどがただしく起動できていない。

error: couldn't read version from server: Get http://localhost:8080/api: dial tcp 127.0.0.1:8080: connection refused


kubectl create -f XXX.yamlでpod作成時に、
retry after the token is automatically created and added to the service account
というエラーが出た場合は、/etc/kubernetes/apiserverKUBE_ADMISSION_CONTROLからservice accountを削除し、apiserverを再起動する。


Node StatusがNotReadyのままの場合、podをcreateしても、get podのStatusがPendingのままとなってしまう。原因の1つとして、kubeletが正しく起動できていない。
/etc/hostsの設定が正しいかなど要確認。

$ kubectl get nodes
NAME            LABELS                                 STATUS
centos-master   kubernetes.io/hostname=centos-master   NotReady

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
13