# 基本操作

當GKE Cluster建立起來之後，後續的操作基本上與K8S幾近相同，所有操作都是透過kubectl來使用，下面簡單介紹一下透過kubectl指令在GKE的環境上建立主機與服務。

## K8S的結構

GKE在架構上透過Pod來封裝一個以上的container作為部署的最小單位，並且透過deployment來封裝pod，讓pod本身可以被監控與執行scale或rolling update等動作，整體的概念如下圖所示：

![](/files/-LAi72-U3-E8kyN1HiuP)

* Pod封裝container作為部署的最小單位
* 透過deployment或是relicaset等方式封裝pod運作的細節，如份數等等資訊
* 最後透過service或是ingress連結對外的開放端口提供服務

而在這之中，GKE為管理整個K8S cluster的角色，透過GKE底層的instance group的操控，即可進一步做到底層資源的橫向擴展。

## 手動執行模式

kubectl提供指令"run"來手動執行單一個pod，可以作為一次性部署或是臨時操作使用。

### 建立Nginx服務為例

我們可以簡單的指定image名稱來啟動一個nginx起來...

```
kubectl run --image=nginx nginx-app --port=80 --env="DOMAIN=cluster"
```

仔細看，其實該nginx會被封裝到deployment中，而該deployment預設會以剛剛給定的image啟動一個pod

```
$ kubectl run --image=nginx nginx-app --port=80 --env="DOMAIN=cluster"
deployment "nginx-app" created

$ kubectl get deploy nginx-app
NAME        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-app   1         1         1            1           14s
```

因此跑起來的nginx-app，基本上可以透過deployment來操作scale out與rolling update等動作..

### 將服務掛載內部IP

到這個階段，啟動階段所掛載的port是container內部的port，如果想要讓外部可以存取您的nginx，可以透過expose的方式來掛載service到pod上...

```
kubectl expose deployment nginx-app --port=80 --name=nginx-http
```

操作無誤後，可以透過get service的方式來找到所有expose的設定...

```
kubectl get services
NAME         CLUSTER-IP      EXTERNAL-IP       PORT(S)    AGE
kubernetes   10.99.240.1     <none>            443/TCP    1d
nginx-http   10.99.255.30    <none>            80/TCP     1h
```

在這個時候，您可以直接登入該container所掛載的位置，並且透過docker ip來做存取，例如：

```
curl http://10.99.255.30:80
```

不過這樣的存取方式僅限於server內部使用，或是具備存取該node權限的服務使用。

### 對服務掛載LoadBalancer (外部IP)

如果要完全open您的服務供外部連線使用，需要透過LoadBalancer的參數指定來連線GKE的外部Network Load Balancer，才可以直接讓服務對外。

```
kubectl expose deployment nginx-app --type="LoadBalancer"
```

檢視服務掛載IP後：

```
kubectl get services
NAME         CLUSTER-IP      EXTERNAL-IP       PORT(S)    AGE
kubernetes   10.99.240.1     <none>            443/TCP    1d
nginx-app    10.99.249.119   130.211.254.181   80/TCP     2m
```

此時，service已經有外部的IP位置，可以透過外部IP來存取：

```
curl http://130.211.254.181:80
```

## 總結

透過剛剛的演練，大概說明了K8S的一些基本操作，包含：

* 如何啟動一個Deployment，包含使用指定的Image來建立
* 使用expose來串接service，串接外部Load Balancer

而K8S持續在演化與發展當中，請大家持續關注K8S帶來更好的部署概念！


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gdgcloud-taipei.gitbook.io/google-cloud-platform-in-practice/google-cloud-shang-de-yun-suan-fu-wu/container-engine/ji-ben-cao-zuo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
