2 minute read

Tags: , ,

  • section14-105, 106, 106



廢話不多說 充實自己比較實在!


Section Intro

  • Learning Service Types
  • Creating Service Types
  • Service DNS

Exposing Containers

  • kubectl expose create a service for existing pods
  • A service is a stable address for pod(s)
  • If we want to connect to pod(s), we need a service
  • CoreDNS allows use to resolve services by name
  • There are different types of services
    • ClusterIP
    • NodePort
    • LoadBalancer
    • ExternalName

Basic Service Types

  • ClusterIP (default) only good in the cluter

    • Single, internal virtual IP allocated
    • Only reachable from within cluster (nodes and pods)
    • Pods and reach service on apps port number
  • NodePort (outside cluster) outside the cluster to talk to servers

    • High port allocated on each node
    • Port is open on every node’s IP
    • Anyone can connect (if they reach node)
    • Other pods need to be updated to this port

These services (ClusterIP NodePort) are always available in k8s

  • LoadBalancer (mostly used in the cloud)

    • Controls a LB endpoint external to the cluster
    • Only available when infra provider give you a LB (AWS ELB, etc)
    • Create NodePort+ClusterIP services, tells LB to send to NodePort
  • ExternalName

    • Adds CNAME DNS reccord to CoreDNS only
    • Not used for Pods, but for giving pods a DNS name to use for somthing outside Kubernetes
  • Kubernets Ingress : We'll learn later


記得阿!! 今年第四季目標 在家 架一台 KKKKK

gogo~~ :heart:

Creating a ClusterIP Service

  • Open two shell windows so we can watch this
    • kubectl get pods -w -w keep watching!
  • In second window, lets start a simple http server using sample code

    • kubectl create deployment httpenv --image=bretfisher/httpenv
  • Scale it to 5 replicas

    • kubectl scale deployment/httpenv --replicas=5
  • Let’s create a ClusterIP service (default)

    • kubectl expose deployment/httpenv --port 8888
    • this will create in front of deployment

Inspecting ClusterIP Service

  • Look up what IP was allocated

    • kubectl get service

    •    PS E:\> kubectl get service
         httpenv ClusterIP <none> 888/TCP 35m
         kubernetes ClusterIP <none> 443/TCP 130d
  • Remember this IP is cluster internal only, how do we curl it ?

  • If you’re on Docker Desktop (Host OS is not container OS) -

    • kubectl run --generator run-pod/v1 tmp-shell --rm -it --image brefisher/netshoot -- bash

      • --generator type of template k8s v1.17 deprecated lol..
      • tmp-shell 自己對 template命的名字拉
      • ` – bash 告訴 run command stop looking options, bash` 就給哥跑 bash 就對了!
      • 靠! 上面都廢話了 直接

        • kubectl run tmp-shell --rm -it --image bretfisher/netshoot bash 進去

          我們創建的 service name 會 default 為 DNS name

          kubectl expose deployment/httpenv --port 8888

          curl httpevn:8888

             PS E:\> kubectl run tmp-shell --rm -it --image bretfisher/netshoot -- bash
             If you don't see a command prompt, try pressing enter.
             bash-5.0# curl httpenv:8888
          • pod name is hostname
          • and other k8s default info
  • If you’re on linux host

    • curl [ip of service]:8888

好颱風天 這樣就棒棒了