Skip to content

Instantly share code, notes, and snippets.

@dfang
Last active October 15, 2020 16:37
Show Gist options
  • Save dfang/5ad5337b6de409c7921f8641258b69a5 to your computer and use it in GitHub Desktop.
Save dfang/5ad5337b6de409c7921f8641258b69a5 to your computer and use it in GitHub Desktop.
expose services on k8s via nginx-ingress-controller, traefik, ambassador

httpbin, whoami, qotm (quote of the moment) 服务特别适合用来作为测试

kubectl create namespace httpbin
kubectl create deploy httpbin --image=citizenstig/httpbin --port=8000 -n httpbin
kubectl expose deployment httpbin --name=httpbin -n httpbin --port=80 --target-port=8000

kubectl create ns whoami
kubectl create deploy whoami --image=containous/whoami --port=80 -n whoami --replicas=3
kubectl expose deployment whoami --name=whoami -n whoami --port=80 --target-port=80

kubectl create ns qotm
kubectl create deploy qotm --image=datawire/qotm:1.7 --port=5000 -n qotm --replicas=3
kubectl expose deployment qotm --name=qotm -n qotm --port=80 --target-port=5000

// https://github.com/kubernetes-up-and-running/kuard
kubectl run --restart=Never --image=gcr.io/kuar-demo/kuard-amd64:blue kuard
kubectl port-forward kuard 8080:8080

tips:

  1. docker for mac stable channel 比较新的版本支持kubernetes single node cluster了。也可试试edge channel.

  2. 较新版本的docker for mac 或 minikube支持 type=LoadBalancer类型 的service了(通过vpnkit), minikube tunnel也支持. 因此mac中不需要 MetalLB的方案,除非线上vps的microk8s

  3. mac 中 k3d 可以创建multi-nodes kubernetes cluster. 非常方便本地开发测试

k3d version
k3d version v1.7.0
k3s version v1.17.3-k3s1

tips: k3s 1.17 里自带的traefik是 1.xx版本的 非2.0的 

# 会自动安装traefik 1.x
k3d create --name="demo" --workers="2" --publish "80:80" --publish "443:443" --api-port=16443

# --server-arg "--no-deploy=traefik" 禁止自动安装traefik 1.x, 然后通过helm 3 安装 traefik 2.x
k3d create --name k3s-traefik-v2 --workers 2 --publish "80:80" --publish "443:443" --server-arg "--no-deploy=traefik"
helm install traefik traefik/traefik --set dashboard.ingressRoute=true
  1. 需要添加相关的记录到 /etc/hosts
@dfang
Copy link
Author

dfang commented May 19, 2020

Contour as ingress controller

https://blog.heptio.com/making-it-easy-to-use-envoy-as-a-kubernetes-load-balancer-dde82959f171

kubectl apply -f - << EOF
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: httpbin
  namespace: httpbin
spec:
  rules:
    - host: httpbin.example.io
      http:
        paths:
          - backend:
              serviceName: httpbin
              servicePort: 8000
            path: /
EOF

HTTPProxy

Note: IngressRoute is deprecated and will be removed after Contour 1.0 ships in November.

IngressRoute renamed to HTTPProxy

kubectl apply -f - << EOF
apiVersion: projectcontour.io/v1
kind: HTTPProxy
metadata:
  name: httpbin
  namespace: httpbin
spec:
  virtualhost:
    fqdn: httpbin.example.io
  routes:
    - conditions:
      - prefix: /
      services:
        - name: httpbin
          port: 8000
EOF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment