はじめてのISUCON9

暇なのでISUCON9のセットアップをAWSでやってみる。OSはUbuntu18.04を使用している。画像のデータ量がかなり多いのでDefault(8GB)ぐらいだと容量が足りなくなる。

### Create User

以後の操作は全て`isucon`で行う

```sh
sudo su
adduser isucon
gpasswd -a user_name sudo
sudo su - isucon
```

### Docker Install

初期データを生成するのにDockerを使っているので入れておく。

```sh

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get install docker-ce docker-ce-cli containerd.io
```

### Dependencies

雑貨的な。なんだかんだで必要な人たち。

```sh

sudo apt-get install build-essential unzip
sudo apt install mysql-server nginx
```

### Golang Install
Goも適当に入れておく。Versionは1.13を選択。

```sh

wget https://dl.google.com/go/go1.13.linux-amd64.tar.gz
tar -zxvf go1.13.linux-amd64.tar.gz && rm go1.13.linux-amd64.tar.gz
# Pathを通す
mkdir .go
echo 'export GOROOT=~/go' >> ~/.bashrc
echo 'export GOPATH=~/.go' >> ~/.bashrc
echo 'export PATH=~/go/bin:$PATH' >> ~/.bashrc
echo 'export PATH=~/.go/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
```

### Application Build

```sh

cd /home/isucon/
git clone https://github.com/isucon/isucon9-qualify.git isucari

# 初期データを生成。結構(5分程度?)時間がかかる。
cd /home/isucon/isucari/initial-data
sudo make

# アプリ用の画像データをダウンロード
cd /home/isucon/isucari/webapp/public
wget https://github.com/isucon/isucon9-qualify/releases/download/v2/initial.zip
unzip initial.zip && rm -rf upload
mv v3_initial_data upload

# ベンチ用の画像データをダウンロード
cd /home/isucon/isucari/initial-data
wget https://github.com/isucon/isucon9-qualify/releases/download/v2/bench1.zip
unzip bench1.zip && rm -rf images
mv v3_bench1 images

# MySQLに初期データを投入
cd /home/isucon/isucari/webapp/sql
cat 00_create_database.sql | sudo mysql
./init.sh

# Go Build
cd /home/isucon/isucari/webapp/go
make
#???
cd /home/isucon/isucari/
make
```

### Database Setting
INSTANCE_PUBLIC_IPは作成した自分のインスタンスのPublicIPAddressを入力。

```
sudo mysql
mysql> USE isucari;
mysql> INSERT INTO configs(name,val) VALUES("payment_service_url", "http://[INSTANCE_PUBLIC_IP]:5555");
mysql> INSERT INTO configs(name,val) VALUES("shipment_service_url", "http://[INSTANCE_PUBLIC_IP]:7000");
mysql> exit;
```

### Serviceとして登録

```
cd /etc/systemd/system/
sudo touch isucari.golang.service
sudo touch isucari.payment.service
sudo touch isucari.shipment.service

sudo systemctl enable isucari.golang
sudo systemctl enable isucari.payment
sudo systemctl enable isucari.shipment
```

```service:isucari.golang.service
[Unit]
Description = isucon9 qualifier main application in golang

[Service]
WorkingDirectory=/home/isucon/isucari/webapp/go/
# EnvironmentFile=/home/isucon/env.sh

ExecStart = /home/isucon/isucari/webapp/go/isucari

Restart   = always
Type      = simple
User      = isucon
Group     = isucon

[Install]
WantedBy = multi-user.target
```

```service:isucari.payment.service
[Unit]
Description = isucon9 qualifier sub application

[Service]
WorkingDirectory=/home/isucon/isucari/
ExecStart = /home/isucon/isucari/bin/payment

Restart   = always
Type      = simple
User      = isucon
Group     = isucon

[Install]
WantedBy = multi-user.target
```

```service:isucari.shipment.service
[Unit]
Description = isucon9 qualifier sub application

[Service]
WorkingDirectory=/home/isucon/isucari/
ExecStart = /home/isucon/isucari/bin/shipment

Restart   = always
Type      = simple
User      = isucon
Group     = isucon

[Install]
WantedBy = multi-user.target
```

Nginxは本番に参加していないのでIsucon7を参考に適当に用意する。

```
cd /etc/nginx/sites-available
sudo touch nginx.conf

cd /etc/nginx/sites-enabled
sudo unlink default
sudo ln -s ../sites-available/nginx.conf
sudo systemctl restart nginx
sudo systemctl enable nginx
```

```conf:nginx.conf

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        client_max_body_size 20M;

        location / {
                proxy_set_header Host $http_host;
                proxy_pass http://127.0.0.1:8000;
        }
}
```


## Setup

```sh

pbcopy < ~/.ssh/id_rsa.pub
mkdir /home/isucon/.ssh
touch /home/isucon/.ssh/authorized_keys
echo '[PUBLIC_KEY]' >> /home/isucon/.ssh/authorized_keys
```