國際慣例炕檩,以helloworld工程為例斗蒋,從restful工程開發(fā),打包笛质,docker鏡像制作泉沾。Ingress+service+pod的方式發(fā)布服務(wù)的例子。
由于沒有遠(yuǎn)程鏡像服務(wù)器妇押,采用本地鏡像的方式加載服務(wù)跷究。
鏡像設(shè)置成本地策略:
? spec:
? ? ? containers:
? ? ? ? - name: **********
? ? ? ? ? image: **********
? ? ? ? ? imagePullPolicy: IfNotPresent
1.生成命名空間
在開發(fā)之前,我們先了解一下k8s的命名空間敲霍,命名空間可以幫助我們管理和隔離pod等組件俊马,增加權(quán)限租戶管理等操作。
以本文為例肩杈,創(chuàng)建一個(gè)名為dwayne的命名空間柴我,新建一個(gè)create-namespace.yaml文件,內(nèi)容如下
apiVersion: v1
kind: Namespace
metadata:
? name: dwayne
? labels:
? ? name: dwayne
執(zhí)行kubectl apply -f create-namespace.yaml
設(shè)置成默認(rèn)命名空間
kubectl config set-context $(kubectl config current-context) --namespace=dwayne
2.helloworld工程
工程很簡單锋恬,一個(gè)get接口/helloworld屯换,返回json
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AController {
????@RequestMapping(value = "helloworld", method = RequestMethod.GET)
????public Response hello() {
????????Response res = new Response();
????????res.setMsg("helloworld");
????????return res;
????}
}
打包成jar包,demo-0.0.1-SNAPSHOT.jar
3.生成docker鏡像
在節(jié)點(diǎn)服務(wù)器(所有節(jié)點(diǎn))上新建目錄作為工作目錄与学,將jar包上傳到節(jié)點(diǎn)服務(wù)器嘉抓,并在同一目錄下編輯Dockerfile:
FROM java:8
VOLUME /tmp
ADD demo-0.0.1-SNAPSHOT.jar /demo-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/demo-0.0.1-SNAPSHOT.jar"]
保存Dockerfile索守,退出,執(zhí)行docker build命令
docker build -t demo-hello-world-master .?
生成demo-hello-world-master抑片,先打個(gè)tag截汪,方便后續(xù)使用:
docker tag demo-hello-world-master dw/demo-hello-world-master
4.部署service
這里部署兩個(gè)helloworld服務(wù)實(shí)例(replicas: 2)蚓峦。部署在dwayne命名空間,yaml文件內(nèi)容如下:
apiVersion: v1
kind: Service
metadata:
? name: helloworld-master
? namespace: dwayne
spec:
? type: NodePort
? selector:
? ? app: helloworld
? ? release: master
? ports:
? ? ? - port: 7071
? ? ? ? targetPort: 17001
? ? ? ? nodePort: 30002
---
apiVersion: apps/v1
kind: Deployment
metadata:
? name: helloworld-master
? namespace: dwayne
spec:
? replicas: 2
? selector:
? ? matchLabels:
? ? ? app: helloworld
? ? ? release: master
? template:
? ? metadata:
? ? ? labels:
? ? ? ? app: helloworld
? ? ? ? release: master
? ? spec:
? ? ? containers:
? ? ? ? - name: demo-hello-world-master
? ? ? ? ? image: dw/demo-hello-world-master
? ? ? ? ? imagePullPolicy: IfNotPresent
? ? ? ? ? ports:
? ? ? ? ? ? - name: http
? ? ? ? ? ? ? containerPort: 17001
應(yīng)用之后霍转,查看pod,有兩個(gè)實(shí)例:
5.Ingress部署
service只設(shè)置了containerPort,沒有暴露外網(wǎng)接口均驶,本文使用ingress進(jìn)行服務(wù)端口暴露和服務(wù)負(fù)載
5.1Ingress基礎(chǔ)組件安裝
Ingress有多種實(shí)現(xiàn)枫虏,本文采用ingress-nginx實(shí)現(xiàn),執(zhí)行deploy.yaml
deploy.yaml
鏈接: https://caiyun.139.com/m/i?105CpmCT95tE3
提取碼:Ja0K
5.2設(shè)置服務(wù)負(fù)載
ingress.yaml
鏈接: https://caiyun.139.com/m/i?105CqQ7HZpuvc 提取碼:nG9h
文件中,指定了服務(wù)的hostname赞警,host: www.helloworld.com愧旦,訪問時(shí)需要綁定ip使用hostname訪問旁瘫,默認(rèn)是80端口: