Consul を試す

Consul を試すメモ。

コンポーネント バージョン 備考
eksctl 0.80.0
Kubernetes バージョン 1.21
プラットフォームのバージョン eks.4
Consul CLI v1.11.2
Consul チャート 0.35.0

クラスターの作成

1.21 のクラスターをノードなしで作成する。

CLUSTER_NAME="consul"
cat << EOF > cluster.yaml
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: ${CLUSTER_NAME}
  region: ap-northeast-1
  version: "1.21"
vpc:
  cidr: "10.0.0.0/16"

availabilityZones:
  - ap-northeast-1a
  - ap-northeast-1c

cloudWatch:
  clusterLogging:
    enableTypes: ["*"]

iam:
  withOIDC: true
EOF
eksctl create cluster -f cluster.yaml

ノードを作成する。

cat << EOF > managed-ng-1.yaml
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: ${CLUSTER_NAME}
  region: ap-northeast-1

managedNodeGroups:
  - name: managed-ng-1
    minSize: 2
    maxSize: 10
    desiredCapacity: 2
    privateNetworking: true
    iam:
      attachPolicyARNs:
        - arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy
        - arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly
        - arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
EOF
eksctl create nodegroup -f managed-ng-1.yaml

Admin ロールにも権限をつけておく。

USER_NAME="Admin:{{SessionName}}"
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
ROLE_ARN="arn:aws:iam::${AWS_ACCOUNT_ID}:role/Admin"
eksctl create iamidentitymapping --cluster ${CLUSTER_NAME} --arn ${ROLE_ARN} --username ${USER_NAME} --group system:masters

Consul のデプロイ

Consul CLI をインストールする。

brew tap hashicorp/tap
brew install hashicorp/tap/consul

確認する。

$ consul version
Consul v1.11.2
Revision 37c7d06b
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

チュートリアルリポジトリをクローンする。

git clone https://github.com/hashicorp/learn-consul-kubernetes.git
cd learn-consul-kubernetes/service-mesh/deploy

チャートリポジトリを追加する。

helm repo add hashicorp https://helm.releases.hashicorp.com
helm repo update

チャートを確認する。

$ helm search repo hashicorp/consul
NAME                    CHART VERSION   APP VERSION     DESCRIPTION
hashicorp/consul        0.39.0          1.11.1          Official HashiCorp Consul Chart

valuse ファイルを確認する。

global:
  name: consul
  datacenter: dc1
  image: hashicorp/consul:1.10.3
  imageEnvoy: envoyproxy/envoy:v1.18.4
  imageK8S: hashicorp/consul-k8s-control-plane:0.34.1
  metrics:
    enabled: true
    enableAgentMetrics: true
server:
  replicas: 1
ui:
  enabled: true
connectInject:
  enabled: true
  default: true
controller:
  enabled: true
prometheus:
  enabled: true
grafana:
  enabled: true

Consul をデプロイする。

$ helm install -f config.yaml consul hashicorp/consul --create-namespace -n consul --version "0.35.0"
NAME: consul
LAST DEPLOYED: Tue Jan 25 08:21:47 2022
NAMESPACE: consul
STATUS: deployed
REVISION: 1
NOTES:
Thank you for installing HashiCorp Consul!

Now that you have deployed Consul, you should look over the docs on using
Consul with Kubernetes available here:

https://www.consul.io/docs/platform/k8s/index.html


Your release is named consul.

To learn more about the release, run:

  $ helm status consul
  $ helm get all consul

Pod を確認する。

$ k -n consul get po
NAME                                                          READY   STATUS    RESTARTS   AGE
consul-265gh                                                  1/1     Running   0          66s
consul-6x524                                                  1/1     Running   0          66s
consul-connect-injector-webhook-deployment-7586fcf6f8-9vrg4   1/1     Running   0          66s
consul-connect-injector-webhook-deployment-7586fcf6f8-kh79n   1/1     Running   0          66s
consul-controller-5b98b9b584-6p2wv                            1/1     Running   0          66s
consul-server-0                                               1/1     Running   0          66s
consul-webhook-cert-manager-c59995c7c-2pv2c                   1/1     Running   0          66s
prometheus-server-5cbddcc44b-zgtph                            2/2     Running   0          25s

Consul Server は StatefulSet で、PV を使っている。各ノードに DeamonSet のノードもいる。

$ k get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                STORAGECLASS   REASON   AGE
pvc-6114c28a-c49e-4626-a542-a48ac6433a01   10Gi       RWO            Delete           Bound    consul/data-consul-consul-server-0   gp2                     74s
$ k -n consul get pvc
NAME                          STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-consul-consul-server-0   Bound    pvc-6114c28a-c49e-4626-a542-a48ac6433a01   10Gi       RWO            gp2            89s

UI にアクセスするためにポートフォワードする。http://localhost:18500

kubectl --namespace consul port-forward service/consul-ui 18500:80 --address 0.0.0.0

http://localhost:18500 にアクセスする。

f:id:sotoiwa:20220125165203p:plain

CLI は Consul Server 上で実行できる。

$ kubectl -n consul exec -it consul-server-0 -- consul members
Node                                             Address            Status  Type    Build   Protocol  DC   Segment
consul-server-0                                  10.0.114.112:8301  alive   server  1.10.3  2         dc1  <all>
ip-10-0-103-225.ap-northeast-1.compute.internal  10.0.103.144:8301  alive   client  1.10.3  2         dc1  <default>
ip-10-0-94-2.ap-northeast-1.compute.internal     10.0.93.160:8301   alive   client  1.10.3  2         dc1  <default>

Consul Server の 8500 へのポートフォワードをすれば、ローカルマシン上の CLI も実行できる。

kubectl --namespace consul port-forward service/consul-server 8500:8500
$ consul members
Node                                             Address            Status  Type    Build   Protocol  DC   Partition  Segment
consul-server-0                                  10.0.114.112:8301  alive   server  1.10.3  2         dc1  default    <all>
ip-10-0-103-225.ap-northeast-1.compute.internal  10.0.103.144:8301  alive   client  1.10.3  2         dc1  default    <default>
ip-10-0-94-2.ap-northeast-1.compute.internal     10.0.93.160:8301   alive   client  1.10.3  2         dc1  default    <default>

デモアプリのデプロイ

デモアプリをデプロイする。

$ kubectl apply -f hashicups/
service/frontend created
serviceaccount/frontend created
servicedefaults.consul.hashicorp.com/frontend created
configmap/nginx-configmap created
deployment.apps/frontend created
service/postgres created
serviceaccount/postgres created
servicedefaults.consul.hashicorp.com/postgres created
deployment.apps/postgres created
service/product-api created
serviceaccount/product-api created
servicedefaults.consul.hashicorp.com/product-api created
configmap/db-configmap created
deployment.apps/product-api created
service/public-api created
serviceaccount/public-api created
servicedefaults.consul.hashicorp.com/public-api created
deployment.apps/public-api created

Pod を確認する。

$ k get po -L consul.hashicorp.com/connect-inject-status
NAME                           READY   STATUS    RESTARTS   AGE   CONNECT-INJECT-STATUS
frontend-98cb6859b-v7b7g       2/2     Running   0          69s   injected
postgres-6ccb6d9968-7zc8m      2/2     Running   0          69s   injected
product-api-6798bc4b4d-nvsdn   2/2     Running   0          69s   injected
public-api-5bdf986897-nmcbw    2/2     Running   0          69s   injected

デモアプリを探検

アプリにアクセスするためポートフォワードする。

kubectl port-forward service/frontend 18080:80 --address 0.0.0.0

http://localhost:18080/ でアプリにアクセスする。

f:id:sotoiwa:20220125165222p:plain

ダッシュボードではサービスが確認できる。

f:id:sotoiwa:20220125165237p:plain

サービスを選択すると、トポロジーが確認できる。

f:id:sotoiwa:20220125165251p:plain

Consul の CRD を見てみる。

$ k get crd
NAME                                         CREATED AT
eniconfigs.crd.k8s.amazonaws.com             2022-01-24T22:18:02Z
ingressgateways.consul.hashicorp.com         2022-01-24T23:21:52Z
meshes.consul.hashicorp.com                  2022-01-24T23:21:52Z
proxydefaults.consul.hashicorp.com           2022-01-24T23:21:52Z
securitygrouppolicies.vpcresources.k8s.aws   2022-01-24T22:18:05Z
servicedefaults.consul.hashicorp.com         2022-01-24T23:21:52Z
serviceintentions.consul.hashicorp.com       2022-01-24T23:21:52Z
serviceresolvers.consul.hashicorp.com        2022-01-24T23:21:52Z
servicerouters.consul.hashicorp.com          2022-01-24T23:21:52Z
servicesplitters.consul.hashicorp.com        2022-01-24T23:21:52Z
terminatinggateways.consul.hashicorp.com     2022-01-24T23:21:52Z