---
# System prepended metadata

title: Google Kubernetes Engine

---

# Google Kubernetes Engine

1. 創建GKE 叢集

```
gcloud container clusters create loadbalancedcluster --num-nodes=2
```
![](https://i.imgur.com/wIWXIAh.png)

2. 確認叢集
![](https://i.imgur.com/uPXi2ll.png)

3. 連線
![](https://i.imgur.com/ebAL3M9.png)

4. 先確認 kubectl context 是否為gke
```
 kubectl config get-contexts
 
 # 切換context
 kubectl config use-context gke_stable-reactor-326809_asia-east1-a_loadbalancedcluster
```
![](https://i.imgur.com/erAnxWg.png)


5. apply deployment 至 GKE
```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-first-deployment
  labels:
    k8s-app: glbc
    kubernetes.io/name: "GLBC"
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
spec:
  replicas: 3
  selector:
    matchLabels:
      app: k8s-net-pod
  template:
    metadata:
      labels:
        app: k8s-net-pod
    spec:
      containers:
      - name: web
        image: gcr.io/stable-reactor-326809/bx-test-project:ci-PROD-e2237fa
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        readinessProbe:
          httpGet:
            path: /host
            port: 80
          periodSeconds: 5
          timeoutSeconds: 60 
          successThreshold: 1
          failureThreshold: 3
        livenessProbe:
          httpGet:
            path: /host
            port: 80
          periodSeconds: 5
          timeoutSeconds: 60
          successThreshold: 1
          failureThreshold: 3          

```
:::    info
GLBC 為 GCE L7 load balancer controller
在ingress 建load balancer時, 會依照此label 設定health check 跟pod的 readinessProbe 相同
:::
```
 kubectl apply -f deployment.yaml
```

6. apply service 至 GKE

LoadBalancer
```
apiVersion: v1
kind: Service
metadata:
  name: my-first-service
spec:
  selector:
    app: k8s-net-pod
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 31000
  type: LoadBalancer
```

NodePort
```
apiVersion: v1
kind: Service
metadata:
  name: my-first-node-port-service
  labels:
    k8s-app: glbc
    kubernetes.io/name: "GLBCDefaultBackend"
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
spec:
  selector:
    app: k8s-net-pod
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 31001
  type: NodePort
```

:::info
service 的 Type
LoadBalancer 為自己有外部IP
NodePort則無 需要靠ingress轉導
:::

```
kubectl apply -f service.yaml
```

7. 若service Type為NodePort 因IP為內部IP, 需建ingress 幫助轉導至Service
```
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-first-ingress
spec:
  defaultBackend:
    service:
      name: my-first-node-port-service
      port:
        number: 80
  rules:
  - host: "k8singress.bingxiang1027test.com"
    http:
      paths:
      - path: /*
        pathType: ImplementationSpecific
        backend:
          service:
            name: my-first-node-port-service
            port:
              number: 80
```

:::info
ingress若無設定defaultBackend, 建立時 gke會自動建立defaultbackend
而自動建立的backend health check port 是自動生成, 而對應不到service的port 造成錯誤
:::


```
kubectl apply -f ingress.yaml
```

8. 建立完後查看 deployment、service、ingress 功能是否正常

## Deployment
![](https://i.imgur.com/BrIYKdd.png)

---
## Service-loadbalance
![](https://i.imgur.com/wBHBzZf.png)
![](https://i.imgur.com/CwNV9k7.png)

---
## Service-nodeport
![](https://i.imgur.com/L4ZU1ln.png)

## Ingress
![](https://i.imgur.com/FrBXNyk.png)
![](https://i.imgur.com/GL20SW0.png)
![](https://i.imgur.com/aMQqhiP.png)

---



