k8s官方文檔實踐系列-Example:DeployingPHPGuestbookapplicationwithRedis

Start up the Redis Master 啟動Redis Master

Creating the Redis Master Deployment

The manifest file, included below, specifies a Deployment controller that runs a single replica Redis master Pod.

cat EOF << redis-master-deployment.yaml 
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: redis-master
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
      role: master
      tier: backend
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
        role: master
        tier: backend
    spec:
      containers:
      - name: master
        image: redis
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 6379

Query the list of Pods to verify that the Redis Master Pod is running:
查詢Pod列表央串,以驗證Redis主Pod正在運行

kubectl get pods

The response should be similar to this:
答復應類似于

NAME                            READY     STATUS    RESTARTS   AGE
  redis-master-1068406935-3lswp   1/1       Running   0          28s

Run the following command to view the logs from the Redis Master Pod:
運行以下命令從Redis主Pod查看日志:

kubectl logs -f POD-NAME

Creating the Redis Master Service

cat EOF <<< redis-master-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels:
    app: redis
    role: master
    tier: backend
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis
    role: master
    tier: backend

Apply the Redis Master Service from the following redis-master-service.yaml file:

kubectl apply -f redis-master-service.yaml

Query the list of Services to verify that the Redis Master Service is running:

kubectl get service

The response should be similar to this:

  NAME           TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
  kubernetes     ClusterIP   10.0.0.1     <none>        443/TCP    1m
  redis-master   ClusterIP   10.0.0.151   <none>        6379/TCP   8s

Note: This manifest file creates a Service named redis-master with a set of labels that match the labels previously defined, so the Service routes network traffic to the Redis master Pod.
此清單文件創(chuàng)建一個名為Redis -master的服務蹄咖,其中包含一組與前面定義的標簽相匹配的標簽颊亮,因此服務將網絡流量路由到Redis主Pod铆农。

Start up the Redis Slaves

Creating the Redis Slave Deployment

Deployments scale based off of the configurations set in the manifest file. In this case, the Deployment object specifies two replicas.

部署規(guī)模基于清單文件中設置的配置撬码。在本例中膘壶,部署對象指定兩個副本。

cat EOF <<<>application/guestbook/redis-slave-deployment.yaml
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: redis-slave
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
      role: slave
      tier: backend
  replicas: 2
  template:
    metadata:
      labels:
        app: redis
        role: slave
        tier: backend
    spec:
      containers:
      - name: slave
        image: redis
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
          # Using `GET_HOSTS_FROM=dns` requires your cluster to
          # provide a dns service. As of Kubernetes 1.3, DNS is a built-in
          # service launched automatically. However, if the cluster you are using
          # does not have a built-in DNS service, you can instead
          # access an environment variable to find the master
          # service's host. To do so, comment out the 'value: dns' line above, and
          # uncomment the line below:
          # value: env
        ports:
        - containerPort: 6379

Apply the Redis Slave Deployment from the redis-slave-deployment.yaml file:

kubectl apply -f https://k8s.io/examples/application/guestbook/redis-slave-deployment.yaml

Query the list of Pods to verify that the Redis Slave Pods are running:

kubectl get pods

The response should be similar to this:

  NAME                            READY     STATUS              RESTARTS   AGE
  redis-master-1068406935-3lswp   1/1       Running             0          1m
  redis-slave-2005841000-fpvqc    0/1       ContainerCreating   0          6s
  redis-slave-2005841000-phfv9    0/1       ContainerCreating   0          6s

Creating the Redis Slave Service
The guestbook application needs to communicate to Redis slaves to read data. To make the Redis slaves discoverable, you need to set up a Service. A Service provides transparent load balancing to a set of Pods.

application/guestbook/redis-slave-service.yaml Copy application/guestbook/redis-slave-service.yaml to clipboard
apiVersion: v1
kind: Service
metadata:
  name: redis-slave
  labels:
    app: redis
    role: slave
    tier: backend
spec:
  ports:
  - port: 6379
  selector:
    app: redis
    role: slave
    tier: backend
```Apply the Redis Slave Service from the following redis-slave-service.yaml file:

kubectl apply -f redis-slave-service.yaml

Query the list of Services to verify that the Redis slave service is running:

kubectl get services

The response should be similar to this:
響應內容大致如下

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 2m
redis-master ClusterIP 10.0.0.151 <none> 6379/TCP 1m
redis-slave ClusterIP 10.0.0.223 <none> 6379/TCP 6s

## Set up and Expose the Guestbook Frontend

### Creating the Guestbook Frontend Deployment

cat EOF <<< application/guestbook/frontend-deployment.yaml
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: frontend
labels:
app: guestbook
spec:
selector:
matchLabels:
app: guestbook
tier: frontend
replicas: 3
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google-samples/gb-frontend:v4
resources:
requests:
cpu: 100m
memory: 100Mi
env:
- name: GET_HOSTS_FROM
value: dns
# Using GET_HOSTS_FROM=dns requires your cluster to
# provide a dns service. As of Kubernetes 1.3, DNS is a built-in
# service launched automatically. However, if the cluster you are using
# does not have a built-in DNS service, you can instead
# access an environment variable to find the master
# service's host. To do so, comment out the 'value: dns' line above, and
# uncomment the line below:
# value: env
ports:
- containerPort: 80
EOF

Apply the frontend Deployment from the frontend-deployment.yaml file:

kubectl apply -f frontend-deployment.yaml

Query the list of Pods to verify that the three frontend replicas are running:

kubectl get pods -l app=guestbook -l tier=frontend

The response should be similar to this:

NAME READY STATUS RESTARTS AGE
frontend-3823415956-dsvc5 1/1 Running 0 54s
frontend-3823415956-k22zn 1/1 Running 0 54s
frontend-3823415956-w9gbt 1/1 Running 0 54s

### Creating the Frontend Service

The redis-slave and redis-master Services you applied are only accessible within the container cluster because the default type for a Service is ClusterIP.

 ClusterIP provides a single IP address for the set of Pods the Service is pointing to. This IP address is accessible only within the cluster.

您應用的redis-slave和redis-master服務只能在容器集群中訪問剂邮,因為服務的默認類型是ClusterIP摇幻。

ClusterIP為服務指向的一組pod提供一個IP地址,這個IP地址只能在集群中訪問挥萌。


If you want guests to be able to access your guestbook, you must configure the frontend Service to be externally visible, so a client can request the Service from outside the container cluster. 

Minikube can only expose Services through NodePort.

如果您希望客人能夠訪問您的來賓簿绰姻,您必須將前端服務配置為外部可見的,以便客戶端可以從容器集群外部請求服務引瀑。

Minikube只能通過NodePort公開服務狂芋。

本例使用LoadBalancer公開服務

Note: Some cloud providers, like Google Compute Engine or Google Kubernetes Engine, support external load balancers. If your cloud provider supports load balancers and you want to use it, simply delete or comment out type: NodePort, and uncomment type: LoadBalancer.

application/guestbook/frontend-service.yaml Copy application/guestbook/frontend-service.yaml to clipboard
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:

comment or delete the following line if you want to use a LoadBalancer

type: NodePort

if your cluster supports it, uncomment the following to automatically create

an external load-balanced IP for the frontend service.

type: LoadBalancer

ports:

  • port: 80
    selector:
    app: guestbook
    tier: frontend
Apply the frontend Service from the frontend-service.yaml file:

  kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-service.yaml
Query the list of Services to verify that the frontend Service is running:

kubectl get services


The response should be similar to this:

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend NodePort 10.0.0.112 <none> 80:31323/TCP 6s
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 4m
redis-master ClusterIP 10.0.0.151 <none> 6379/TCP 2m
redis-slave ClusterIP 10.0.0.223 <none> 6379/TCP 1m


Viewing the Frontend Service via NodePort
If you deployed this application to Minikube or a local cluster, you need to find the IP address to view your Guestbook.

Run the following command to get the IP address for the frontend Service.

minikube service frontend --url


The response should be similar to this:

http://192.168.99.100:31323


Copy the IP address, and load the page in your browser to view your guestbook.

Viewing the Frontend Service via LoadBalancer
If you deployed the frontend-service.yaml manifest with type: LoadBalancer you need to find the IP address to view your Guestbook.

Run the following command to get the IP address for the frontend Service.

kubectl get service frontend


The response should be similar to this:

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend ClusterIP 10.51.242.136 109.197.92.229 80:32372/TCP 1m



Copy the external IP address, and load the page in your browser to view your guestbook.

### Scale the Web Frontend
Scaling up or down is easy because your servers are defined as a Service that uses a Deployment controller.

Run the following command to scale up the number of frontend Pods:

kubectl scale deployment frontend --replicas=5



Query the list of Pods to verify the number of frontend Pods running:

kubectl get pods


The response should look similar to this:

NAME READY STATUS RESTARTS AGE
frontend-3823415956-70qj5 1/1 Running 0 5s
frontend-3823415956-dsvc5 1/1 Running 0 54m
frontend-3823415956-k22zn 1/1 Running 0 54m
frontend-3823415956-w9gbt 1/1 Running 0 54m
frontend-3823415956-x2pld 1/1 Running 0 5s
redis-master-1068406935-3lswp 1/1 Running 0 56m
redis-slave-2005841000-fpvqc 1/1 Running 0 55m
redis-slave-2005841000-phfv9 1/1 Running 0 55m



Run the following command to scale down the number of frontend Pods:

  kubectl scale deployment frontend --replicas=2
Query the list of Pods to verify the number of frontend Pods running:

kubectl get pods


The response should look similar to this:

NAME READY STATUS RESTARTS AGE
frontend-3823415956-k22zn 1/1 Running 0 1h
frontend-3823415956-w9gbt 1/1 Running 0 1h
redis-master-1068406935-3lswp 1/1 Running 0 1h
redis-slave-2005841000-fpvqc 1/1 Running 0 1h
redis-slave-2005841000-phfv9 1/1 Running 0 1h


### Cleaning up

Deleting the Deployments and Services also deletes any running Pods. Use labels to delete multiple resources with one command.

刪除部署和服務也會刪除任何正在運行的pod。使用標簽用一個命令刪除多個資源憨栽。

Run the following commands to delete all Pods, Deployments, and Services.

運行以下命令刪除所有pod帜矾、部署和服務辆影。

kubectl delete deployment -l app=redis
kubectl delete service -l app=redis
kubectl delete deployment -l app=guestbook
kubectl delete service -l app=guestbook


The responses should be:
響應應該是:

deployment.apps "redis-master" deleted
deployment.apps "redis-slave" deleted
service "redis-master" deleted
service "redis-slave" deleted
deployment.apps "frontend" deleted
service "frontend" deleted


Query the list of Pods to verify that no Pods are running:
查詢Pods列表,確認吊Pods沒有運行:


kubectl get pods


The response should be this:

No resources found.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末黍特,一起剝皮案震驚了整個濱河市蛙讥,隨后出現的幾起案子,更是在濱河造成了極大的恐慌灭衷,老刑警劉巖次慢,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異翔曲,居然都是意外死亡迫像,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門瞳遍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來闻妓,“玉大人,你說我怎么就攤上這事掠械∮衫拢” “怎么了?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵猾蒂,是天一觀的道長均唉。 經常有香客問我,道長肚菠,這世上最難降的妖魔是什么舔箭? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮蚊逢,結果婚禮上层扶,老公的妹妹穿的比我還像新娘。我一直安慰自己烙荷,他們只是感情好镜会,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著奢讨,像睡著了一般稚叹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上拿诸,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天,我揣著相機與錄音塞茅,去河邊找鬼亩码。 笑死,一個胖子當著我的面吹牛野瘦,可吹牛的內容都是我干的描沟。 我是一名探鬼主播飒泻,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼吏廉!你這毒婦竟也來了泞遗?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤席覆,失蹤者是張志新(化名)和其女友劉穎史辙,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體佩伤,經...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡聊倔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了生巡。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片耙蔑。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖孤荣,靈堂內的尸體忽然破棺而出甸陌,到底是詐尸還是另有隱情,我是刑警寧澤盐股,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布邀层,位于F島的核電站,受9級特大地震影響遂庄,放射性物質發(fā)生泄漏寥院。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一涛目、第九天 我趴在偏房一處隱蔽的房頂上張望秸谢。 院中可真熱鬧,春花似錦霹肝、人聲如沸估蹄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽臭蚁。三九已至,卻和暖如春讯赏,著一層夾襖步出監(jiān)牢的瞬間垮兑,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工漱挎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留系枪,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓磕谅,卻偏偏與公主長得像私爷,于是被迫代替她去往敵國和親雾棺。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內容