laravel6.x : ELB使用時にElastiCache(Redis)でセッション管理する

AWSのELBで冗長化する場合、TokenMismatchExceptionが発生しないよう、セッション管理をデフォルトのCookieからRedisに変更する。
ALBでStickinessを有効化する事で、Cookieのままでも機能的にはおよそ問題ないとも言えるが、
「インスタンスが死んだ場合にはセッションが切れる」「負荷分散が偏る場合が発生する」とのことで、
要件次第のところもあるが、基本的にはスティッキークッキーではなく、ElastiCacheでの管理に変更する。

### 1.Redis用のsubnet作成
Services->Database->ElastiCache->SubnetGroup
Name:${appName}-redis-subnet-group
Description:${appName}-redis-subnet-group
VPC:dev-vpc
Subnet:dev-subnet-private1(ap-northeast-1a)、dev-subnet-private2(ap-northeast-1c)

### 2.Redis用のSecurityGroup作成
name:redis-security-group
description:redis-security-group
VPC:dev-vpc

Edit Inbound Rule
Custom TCP, 6379port, PRDのSecurityGroup

### 3.Redisの作成
Name: ${appName}-redis
Engine version compatibility: 5.0.6
Port:6379
Parameter group:default.redis5.0
nodetype: t2.medium
replica:0
subnet group: ${appName}-redis-subnet-group
security group: redis-security-group
backup: null

### 4. Laravelでredisを使えるようにする
$ ssh ec2-user@*** -i ~/.ssh/***.pem
$ cd /var/www/${appName}

// predisを入れる為、メモリ拡張
$ sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
$ sudo /sbin/mkswap /var/swap.1
$ sudo /sbin/swapon /var/swap.1
$ free

$ sudo php composer.phar require predis/predis

$ sudo vi .env

CACHE_DRIVER=redis
SESSION_DRIVER=redis
REDIS_HOST=${redis endpoint}

$ php artisan config:clear
$ chown apache:apache /var/www/${appName}/storage/logs/laravel.log

で、動作確認すると、

please make sure the php redis extension is installed and enabled

何いいいいいいいいいいいいいい
「sudo yum install php-redis」でphp-redis入れろって事?

あ、
config/database/php
122行目
phpredis -> predis

'redis' => [

        'client' => env('REDIS_CLIENT', 'predis'),

再度動作確認

memcached

[vagrant@localhost public]$ yum list installed | grep memcached
[vagrant@localhost public]$ yum list installed | grep php
php.x86_64 5.6.40-12.el6.remi @remi-php56
php-cli.x86_64 5.6.40-12.el6.remi @remi-php56
php-common.x86_64 5.6.40-12.el6.remi @remi-php56
php-devel.x86_64 5.6.40-12.el6.remi @remi-php56
php-gd.x86_64 5.6.40-12.el6.remi @remi-php56
php-intl.x86_64 5.6.40-12.el6.remi @remi-php56
php-mbstring.x86_64 5.6.40-12.el6.remi @remi-php56
php-mysqlnd.x86_64 5.6.40-12.el6.remi @remi-php56
php-pdo.x86_64 5.6.40-12.el6.remi @remi-php56
php-pecl-jsonc.x86_64 1.3.10-2.el6.remi.5.6 @remi-php56
php-pecl-jsonc-devel.x86_64 1.3.10-2.el6.remi.5.6 @remi-php56
php-pecl-zip.x86_64 1.15.4-1.el6.remi.5.6 @remi-php56
php-xml.x86_64 5.6.40-12.el6.remi @remi-php56
[vagrant@localhost public]$ php -v
PHP 5.6.40 (cli) (built: Jul 30 2019 11:02:37)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
[vagrant@localhost public]$ sudo yum search php memcached
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
epel/metalink | 7.6 kB 00:00
* base: ftp.jaist.ac.jp
* epel: ftp.jaist.ac.jp
* extras: ftp.jaist.ac.jp
* remi-safe: ftp.riken.jp
* updates: ftp.jaist.ac.jp
base | 3.7 kB 00:00
epel | 5.3 kB 00:00
epel/primary_db | 6.1 MB 00:05
extras | 3.4 kB 00:00
mysql-connectors-community | 2.5 kB 00:00
mysql-tools-community | 2.5 kB 00:00
mysql56-community | 2.5 kB 00:00
nodesource | 2.5 kB 00:00
remi-safe | 3.0 kB 00:00
updates | 3.4 kB 00:00
============================================= N/S Matched: php, memcached ==============================================
php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php54-php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php54-php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php55-php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php55-php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php56-php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php56-php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php70-php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php70-php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php71-php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php71-php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php72-php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php72-php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php73-php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php73-php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php-ZendFramework-Cache-Backend-Libmemcached.noarch : Zend Framework libmemcache cache backend
php-ZendFramework-Cache-Backend-Memcached.noarch : Zend Framework memcache cache backend

Full name and summary matches only, use “search all” for everything.

ElastiCache

ElastiCache is a managed service related to AWS’s in-memory cache.
The cache engine can be selected from Redis and memcached.
There are units of nodes, shards, and clusters.

Node
– cache server
– the smallest unit of ElastiCache
– The performance(memory, CPU, etc.) varies depending on the selected node type.

Sharde
– A group that organizes nodes
– One shard has one primary node(read / write) and 0-5 read replicas (read only)

Cluster
– A group that organizes shards
– A cache engine can be selected for each cluster. Cluster mode can be enabled or disabled.

Amazon ElastiCache

Amazon ElastiCache is a web service that makes it easy to deploy and run server nodes compliant with Memcached or Redis protocols in the cloud. Amazon ElastiCache improves web application performance by enabling the retrieval of information from a fast managed in-memory system without relying entirely on slod disk-based databases. This service simplifies the management, monitoring, and operation of the in-memory environment and reduces the load so that engineering resources can be concentrated on application development. With Amazon ElastiCache, you can improve the loading and response times of user actions and queries, as well as reduce the costs associated with scaling web applications.

Amazon ElastiCacheを使おう

create an Amazon ElastiCache Redis node and connect to it from an Amazon EC2 instance

serviceからelasticacheを選ぶ

クラスターエンジン
redis
データベース、キャッシュ、メッセージブローカーとして使用されるインメモリデータ構造ストア

Memcached
動的なウェブアプリケーションの高速化に使用される高パフォーマンスの分散メモリオブジェクトキャッシュシステム。

redisを選択する。

node typeはcache.t2.micro
リプリケーションはnone

ほう

セキュリティグループ
nodeを6379にする

ec2 リモートデスクトップ接続

すげー