[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

KubernetesのPod内で複数のコンテナが稼働している場合にkubectl execでログインする方法

インフラエンジニアの寺岡です。
今回はKubernetes関連のお話になります。

以下のURLにも記載されていますが
複数のコンテナが稼働しているPodに対してkubectl execを実行し
任意のコンテナに対してログインする方法をご紹介します。

https://kubernetes.io/ja/docs/tasks/debug-application-cluster/get-shell-running-container/

Podを起動する

まずは以下のマニフェスト通りのPodを立ち上げてみます。
デプロイメントコントローラを利用してNginxコンテナが稼働するPodを起動するシンプルなものです。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-web
  namespace: default
spec:
  selector:
    matchLabels:
      app: example-web
  replicas: 1
  template:
    metadata:
      labels:
        app: example-web
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 80

マニフェストを反映します。

$ Kubectl apply -f deployment_example_web.yaml

Podが立ち上がったか確認します。

$ kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
example-web-59fccdb6d4-slrkv   1/1     Running   0          13s

立ち上がりましたね。
このPod内で起動しているコンテナにログインする場合は

$ kubectl exec -it example-web-59fccdb6d4-slrkv bash
root@example-web-59fccdb6d4-slrkv:/# 

とすればログインできますね。
では次に少しマニフェストファイルを編集します。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-web
  namespace: default
spec:
  selector:
    matchLabels:
      app: example-web
  replicas: 1
  template:
    metadata:
      labels:
        app: example-web
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 80
      - name: php-fpm
        image: php:7.4-fpm
        imagePullPolicy: Always
        ports:
        - containerPort: 9000

PHP-FPMのコンテナも追加しました。
反映するコマンドは先ほどと同じなので省略しますが
Podを起動するとREADYの部分が2/2になります。

$ kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
example-web-85cd8dbd8d-24rx4   2/2     Running   0          69s

先ほどと同じkubectl execを実行してみます。

$ kubectl exec -it example-web-85cd8dbd8d-24rx4 bash
Defaulting container name to nginx.
Use 'kubectl describe pod/example-web-85cd8dbd8d-24rx4 -n default' to see all of the containers in this pod.

何やらメッセージが表示されました。
「デフォルトはnginxという名前のコンテナだから全てのコンテナの情報はkubectl describeで確認してね」という意味です。
つまり、PHP-FPMのコンテナにログインしたい場合は上記のコマンドは利用できません。
以下のように--containerオプションを利用することでログインできます。

$ kubectl exec -it example-web-85cd8dbd8d-24rx4 --container php-fpm bash
root@example-web-85cd8dbd8d-24rx4:/var/www/html# 

ログインできました!

まとめ

当たり前と言えばその通りですが備忘録として残しておきます。
Kubernetesもっと勉強しよ。。。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
3
読み込み中...
3 票, 平均: 1.00 / 13
9,059
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Yuki Teraoka

Joined Beyond in 2016 and is currently in his 6th year as an Infrastructure Engineer
MSP, where he troubleshoots failures while
also designing and building infrastructure using public clouds such as AWS.
Recently, I
have been working with Hashicorp tools such as Terraform and Packer as part of building container infrastructure such as Docker and Kubernetes and automating operations, and I
also play the role of an evangelist who speaks at external study groups and seminars.

・GitHub
https://github.com/nezumisannn

・Presentation history
https://github.com/nezumisannn/my-profile

・Presentation materials (SpeakerDeck)
https://speakerdeck.com/nezumisannn

・Certification:
AWS Certified Solutions Architect - Associate
Google Cloud Professional Cloud Architect