LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

0808-K8Sハンズオン環境と手順Part2(For WorkShop)

Last updated at Posted at 2019-08-07

K8SのServiceとIngressのハンズオン

nginxのClusterIpサービスを作成

■nginxのdeploymentの作成
名前:nginx
Dockerイメージ:--image=nginx:1.14

run:
> kubectl run nginx --image=nginx:1.14 --replicas=1 
> 

■nginxのClusterIpサービスを作成

run:
> kubectl expose deployment nginx --port=80 --target-port=80
> 

■curlテスト用のdeploymentを作成

run:
> kubectl run curl --image=tutum/curl --command -- sleep 360000
> 

■curlテスト

run:
> kubectl exec curl-XXXXXX-XXXX curl nginx
> 

※curl-XXXXXX-XXXXは、kubectl get podで実際のPod名を拾う

nginxのNodePortサービスを作成

■nginxのNodePortサービスを作成用のManifestベースを生成

run:
> kubectl create service nodeport nginx-np --tcp=80:80 --dry-run -oyaml
> 

■nginxのNodePortサービスを作成

apply:
cat <<EOF | kubectl apply -f -
★前の手順で生成したmanifestの「app: nginx-np」内容を「run: nginx」に置き換え、ここにコピー
EOF

■テスト
ブラウザー上に、NODE-IP:PORTでアクセス
NODE-IP:kubectl get node -owideで参照
PORT:kubectl get svc nginx-npで参照 30000台のもの

nginxのLoadBalancerサービスを作成(Optional Task)

■nginxのLoadBalancerサービスを作成用のManifestベースを生成

run:
> kubectl create service loadbalancer nginx-lb --tcp=80:80 --dry-run -oyaml
> 

■nginxのLoadBalancerサービスを作成

apply:
cat <<EOF | kubectl apply -f -
★前の手順で生成したmanifestの「app: nginx-np」内容を「run: nginx」に置き換え、ここにコピー
EOF

■テスト
ブラウザー上に、LB-URLでアクセス
LB-URL:kubectl get svc nginx-lbで参照

yahooのExternalNameサービスを作成(Optional Task)

■yahooのExternalNameサービスを作成

apply:
kubectl create service externalname ex-yahoo --external-name=yahoo.co.jp 
EOF

■curlテスト

run:
> kubectl exec curl-XXXXXX-XXXX curl ex-yahoo
> 

Ingressを作成(Optional Task)

■httpdのdeploymentの作成
名前:httpd
Dockerイメージ:--image=httpd

run:
> kubectl run httpd --image=httpd 
> 

■httpdのClusterIpサービスを作成

run:
> kubectl expose deployment httpd --port=80 --target-port=80
> 

■Ingressを作成

run:
cat <<EOF | kubectl apply -f -
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-test
spec:
  rules:
  - host: nginx.com
    http:
      paths:
      - backend:
          serviceName: nginx
          servicePort: 80
  - host: httpd.com
    http:
      paths:
      - backend:
          serviceName: httpd
          servicePort: 80
  - http:
      paths:
      - backend:
          serviceName: nginx
          servicePort: 80
EOF

■curlテスト(ローカル環境)
curl -H 'Host:nginx.com' NODE-IP
curl -H 'Host:httpd.com' NODE-IP
※NODE-IP:kubectl get node -owideで参照

Secretの作成と使用(Optional Task)

■作成
https://kubernetes.io/docs/concepts/configuration/secret/#creating-a-secret-manually
■使用
「Using Secrets as Files from a Pod」
https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-files-from-a-pod

「Using Secrets as Environment Variables」
https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables

0

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