在微服務(wù)領(lǐng)域雹顺,spring cloud 與 kubernetes 一直被拿來(lái)做比較,但實(shí)際上,這兩者有著不同得定義咧擂。
- spring cloud : 基于 spring boot 得微服務(wù)解決方案窟社,有著完整得微服務(wù)生態(tài)體系券勺。
- kubernetes : 用于跨多個(gè)主機(jī)管理容器化應(yīng)用程序,為應(yīng)用程序的部署灿里,維護(hù)和擴(kuò)展提供基本機(jī)制关炼。提供對(duì)容器得管理機(jī)制。
兩者并非是相同匣吊、互斥得技術(shù)儒拂,相反,兩者都是微服務(wù)領(lǐng)域各自部分里屬于頂尖色鸳,如果兩者能結(jié)合起來(lái)社痛,那肯定會(huì)給開(kāi)發(fā)、維護(hù)帶來(lái)無(wú)數(shù)得好處與優(yōu)勢(shì)缕碎。
spring cloud kubernetes 就是這樣一個(gè)項(xiàng)目褥影,使用 kubernetes 作為服務(wù)得運(yùn)行容器,kubernetes 將代替注冊(cè)中心咏雌、配置中心得職責(zé)凡怎,并提供kubernetes其他得功能校焦。
spring cloud kubernetes gitHub 地址:spring-cloud-kubernetes
現(xiàn)在開(kāi)始學(xué)習(xí) spring cloud kubernetes 與 kubernetes ,編寫第一個(gè) hello world 項(xiàng)目。
Hello world
spring cloud kubernetes hello world 項(xiàng)目
按照官方文檔例子统倒,先建立一個(gè) spring boot 項(xiàng)目寨典,然后在 pom.xml 中加入以下依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes</artifactId>
<version>${latest.version}</version>
</dependency>
目前 spring cloud kubernetes 公開(kāi)版本只有 0.3.0.RELEASE、0.2.0.RELEASE房匆,該例子使用 0.3.0.RELEASE 版本耸成。
在 Application 中加入以下注解:
@SpringBootApplication
@EnableDiscoveryClient
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
編寫一個(gè)HelloWorldController:
@RestController
public class HelloWorldController {
private static final Log log = LogFactory.getLog(HelloWorldController.class);
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/")
public String hello() {
return "Hello World";
}
@RequestMapping("/services")
public List<String> services() {
return this.discoveryClient.getServices();
}
}
注入得 DiscoveryClient 是基于 kubernetes 容器得實(shí)現(xiàn),/services 接口 會(huì)返回 kubernetes 中所有得service名稱浴鸿。
在 application.perproties 中加入以下屬性:
spring.application.name=hello_world
這個(gè)名稱非常重要井氢,如果想在其他服務(wù)使用服務(wù)名稱來(lái)訪問(wèn)服務(wù)得話,如:http://hello_world/services 岳链,該名稱必須要與 kubernetes services 得名稱相同花竞。
到此, hello_world 項(xiàng)目已經(jīng)完成掸哑,非常簡(jiǎn)單得項(xiàng)目约急,接下來(lái)需要將服務(wù)運(yùn)行在 kubernetes 上。
kubernetes
首先需要安裝 kubernetes 容器苗分,并將其運(yùn)行起來(lái)厌蔽,可以選擇 kubernetes 集群或者用于本地調(diào)試開(kāi)發(fā)得 minikube 上,具體可看 kubernetes-安裝文檔
本例子使用的是基于docker的kubernetes集群摔癣。
環(huán)境安裝好之后奴饮,首先要?jiǎng)?chuàng)建出hello_world的docker的運(yùn)行鏡像,創(chuàng)建 Dockerfile 文件供填,并寫入以下內(nèi)容:
FROM openjdk:8-jdk-alpine
VOLUME /var/demo/
COPY hello_world-1.0.0.jar hello_world.jar
ENV JAVA_OPTS=""
ENTRYPOINT exec java -jar /hello_world.jar --debug
將 Dockerfile 與 打包出來(lái)的 jar 包都復(fù)制到 /var/demo/hello_world 目錄中拐云,并執(zhí)行 Docker 命令創(chuàng)建 image:
docker build --file=Dockerfile --tag=hello_word:latest --rm=true .
執(zhí)行成功應(yīng)返回 Successfully 標(biāo)識(shí),并執(zhí)行 docker images 命令近她,應(yīng)能看到該鏡像:
注意:如果是 kubernetes 集群的話叉瘩,應(yīng)該在集群每一部節(jié)點(diǎn)上都執(zhí)行上面的過(guò)程,如果不想則需要配置 Docker 的本地倉(cāng)庫(kù)粘捎。
docker image 配置完畢之后薇缅,則開(kāi)始配置kubernetes運(yùn)行該image。
1攒磨、創(chuàng)建 deployment
使用以下命令來(lái)創(chuàng)建一個(gè) deployment :
kubectl run hello-word --image=hello_word:latest --port=8080 --image-pull-policy=Never
--image:指定 hello_word 鏡像泳桦,版本為 latest 。
--port:指定為 8080 端口娩缰,因?yàn)?spring boot 默認(rèn)得端口為 8080灸撰。
--image-pull-policy=Never:不從Docker 公共服務(wù)器中拉去鏡像,而是從本地拉取。
使用 kubectl get deployments 命令來(lái)查看是否成功:
再使用 kubectl get pods 來(lái)查看容器是否運(yùn)行成功:
當(dāng) status 為 Running 得時(shí)候浮毯,表示正常運(yùn)行完疫,可以使用 kubectl logs [name] 來(lái)查看日志信息。
2债蓝、創(chuàng)建service
當(dāng)deployment創(chuàng)建成功時(shí)壳鹤,只代表了里面運(yùn)行了這個(gè)容器,需要對(duì)這個(gè)容器進(jìn)行定義 service 饰迹,才能被外界訪問(wèn)芳誓。
使用以下命令來(lái)定義一個(gè) service :
kubectl expose deployment hello-world --type=NodePort
--type=NodePort:使集群外部的服務(wù)可用,將對(duì)應(yīng)IP:端口得請(qǐng)求映射到8080端口上啊鸭。
expose 命令將使端口隨機(jī)分配锹淌,默認(rèn)范圍是30000-32767,如果需要指定端口莉掂,需要使用配置文件得形式進(jìn)行啟動(dòng)葛圃。
使用 kubectl get services 命令來(lái)查看是否成功:
可以看到,上圖顯示 type = NodePort 憎妙,集群IP 為 10.99.24.253,端口為:32433 曲楚,到現(xiàn)在厘唾,服務(wù)已經(jīng)運(yùn)行在 kubernetes 上了。
但如果使用 10.99.24.253:32433 進(jìn)行訪問(wèn)接口龙誊,是訪問(wèn)不到得抚垃,因?yàn)?kubernetes 只有一個(gè)主IP可以對(duì)外訪問(wèn),10.99.24.253得IP是 kubernetes 內(nèi)部IP趟大,獲取主IP命令如下:
kubectl cluster-info
可以看到鹤树,我這邊得IP為:172.31.176.30
那位訪問(wèn)接口就為: http://172.31.176.30:32433,分別訪問(wèn) / 與 /service 接口如下:
在訪問(wèn) /services 得時(shí)候逊朽,可以會(huì)得到以下錯(cuò)誤:
{
"timestamp":"2018-12-05T08:18:31.503+0000",
"status":500,
"error":"Internal Server Error",
"message":"Failure executing: GET at: https://kubernetes.default.svc/api/v1/namespaces/default/services. Message: Forbidden!Configured service account doesn't have access. Service account may have been revoked. services is forbidden: User "system:serviceaccount:default:default" cannot list resource "services" in API group "" in the namespace "default".",
"path":"/services"
}
這表示了當(dāng)前執(zhí)行用戶沒(méi)有訪問(wèn)API得權(quán)限罕伯,具體可以看
kubernetes account
kubernetes role
如果不想看,可使用下面得命令進(jìn)行全部用戶賦予管理員權(quán)限:
kubectl create clusterrolebinding permissive-binding \
--clusterrole=cluster-admin \
--user=admin \
--user=kubelet \
--group=system:serviceaccounts
到此叽讳,使用spring cloud kubernetes 得 hello world 就結(jié)束了追他。