spring-cloud-kubernetes之開發環境搭建

前言

爲了能讓spring cloud更好的部署在kubernetes中,爲此我決定將原來的spring cloud進行改造,改造爲spring cloud官方提供的spring-cloud-kubernetes

在技術選型時,對於要不要將原生的spring cloud改造爲spring-cloud-kubernetes可能也是許多人思考的一個問題,因爲畢竟spring-cloud-kubernetes的運行是需要依賴於k8s環境的,spring-cloud-kubernetes本地調試這個應該如何解決呢?

爲此,spring cloud官方爲了能讓用戶快速熟悉和體驗到spring-cloud-kubernetes的功能,也特意給出了demo,同時也在demo中對於上面開發環境應該怎樣解決做出瞭如下的說明:

To play with these examples, you can install locally Kubernetes & Docker using Minikube within a Virtual Machine managed by a hypervisor (Xhyve, Virtualbox or KVM) if your machine is not a native Unix operating system.

spring-cloud-kubernetes-demo說明
看上上面的官方說明後,我想對於spring-cloud-kubernetes本地調試應該如何解決我們應該也有了答案。在開發環境中安裝一個不佔用太多資源的minikube就行了

這裏我以目前常見的windows開發環境爲例,演示一下如何搭建一個spring-cloud-kubernetes的本地調試環境

minikube安裝

也可直接參考官方教程:https://kubernetes.io/docs/tasks/tools/install-minikube/

minikube也可以直接在windows中進行安裝,但是windows部分版本不支持,這裏爲了通用且不破壞物理機環境,建議採用直接安裝在虛擬機中

先提前安裝好vm,並安裝好一個linux,我這裏安裝的是centos,此部分比較簡單就不在這裏進行記錄了
centos-vm

提前準備

在安裝好了centos後,再進行如下的操作

關閉防火牆

systemctl stop firewalld && systemctl disable firewalld

關閉虛擬內存

swapoff -a

安裝docker

此部分可直接參考文章https://blog.csdn.net/puhaiyang/article/details/105738550中docker安裝部分即可

安裝minikube

爲了方便快捷採用官方文檔中的Install Minikube via direct download方法來安裝

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
&& chmod +x minikube

然後執行下面的兩個命令

sudo mkdir -p /usr/local/bin/

sudo install minikube /usr/local/bin/

最後啓動minikube就可以啦

在國內如果不能科學上網的話可以用阿里雲的鏡像來啓動minikube

minikube start --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --driver=none

如果啓動過程中有提示:
X Sorry, Kubernetes v1.18.0 requires conntrack to be installed in root’s path

那麼先把conntrack安裝一下:

yum install conntrack

然後再次運行上面那個minikube start的命令以啓動minikube

[root@localhost ~]#  minikube start --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --driver=none
* minikube v1.11.0 on Centos 7.7.1908
* Using the none driver based on existing profile
* Starting control plane node minikube in cluster minikube
* Restarting existing none bare metal machine for "minikube" ...
* OS release is CentOS Linux 7 (Core)
* Preparing Kubernetes v1.18.3 on Docker 18.06.3-ce ...
* Configuring local host environment ...
* 
! The 'none' driver is designed for experts who need to integrate with an existing VM
* Most users should use the newer 'docker' driver instead, which does not require root!
* For more information, see: https://minikube.sigs.k8s.io/docs/reference/drivers/none/
* 
! kubectl and minikube configuration will be stored in /root
! To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run:
* 
  - sudo mv /root/.kube /root/.minikube $HOME
  - sudo chown -R $USER $HOME/.kube $HOME/.minikube
* 
* This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
* Verifying Kubernetes components...
* Enabled addons: default-storageclass, storage-provisioner
* Done! kubectl is now configured to use "minikube"
[root@localhost ~]# 

安裝好minikube後驗證一下:

[root@localhost ~]# kubectl get nodes
NAME                    STATUS   ROLES    AGE    VERSION
localhost.localdomain   Ready    master   2m7s   v1.18.3
[root@localhost ~]# kubectl get pods -A
NAMESPACE     NAME                                            READY   STATUS    RESTARTS   AGE
kube-system   coredns-546565776c-2hmw6                        1/1     Running   0          2m12s
kube-system   coredns-546565776c-vm788                        1/1     Running   0          2m12s
kube-system   etcd-localhost.localdomain                      1/1     Running   0          2m14s
kube-system   kube-apiserver-localhost.localdomain            1/1     Running   0          2m14s
kube-system   kube-controller-manager-localhost.localdomain   1/1     Running   0          2m14s
kube-system   kube-proxy-jb6ls                                1/1     Running   0          2m12s
kube-system   kube-scheduler-localhost.localdomain            1/1     Running   0          2m14s
kube-system   storage-provisioner                             1/1     Running   0          2m18s
[root@localhost ~]# kubectl get svc -A
NAMESPACE     NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
default       kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP                  2m32s
kube-system   kube-dns     ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   2m31s
[root@localhost ~]# 

開發機器windows環境準備

安裝kubectl

因爲我們的開發環境是windows,所以爲了方便還是在windows上安裝一個kubectl,如果windows的kubectl都能用了,那麼spring-cloud-kubernetes的環境也就能用了

kubectl安裝方法的參考官網鏈接:
https://kubernetes.io/docs/tasks/tools/install-kubectl/

下載:
https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/windows/amd64/kubectl.exe

將kubectl.exe複製到某一目錄並配置環境變量,如我這裏是將它放到c盤下的Program Files目錄的

C:\Program Files\k8s\kubectl.exe

再將C:\Program Files\k8s添加到系統的path就ok了

輸入命令測試一下

C:\Users\DELL>kubectl version --client
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.0", GitCommit:"9e991415386e4cf155a24b1da15becaa390438d8", GitTreeState:"clean", BuildDate:"2020-03-25T14:58:59Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"windows/amd64"}

拷貝kube-config

拷貝minikube虛擬機上的kube-config文件到windows中當前用戶目錄的 .kube目錄下

[root@localhost .kube]# pwd
/root/.kube
[root@localhost .kube]# ll
total 12
drwxr-x---. 3 root root   23 Jul  1 22:56 cache
-rw-------. 1 root root  440 Jul  1 22:56 config
drwxr-x---. 3 root root 4096 Jul  1 23:08 http-cache
[root@localhost .kube]# cat config 
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /root/.minikube/ca.crt
    server: https://192.168.113.148:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: /root/.minikube/profiles/minikube/client.crt
    client-key: /root/.minikube/profiles/minikube/client.key
[root@localhost .kube]# 

將crt與key也複製到windows下,最終我這邊的kube-config文件內容如下:

apiVersion: v1
clusters:
- cluster:
    certificate-authority: C:\Users\DELL\.kube\ca.crt
    server: https://192.168.113.148:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: C:\Users\DELL\.kube\client.crt
    client-key: C:\Users\DELL\.kube\client.key

驗證windows下的kubectl是否能訪問minikube

C:\Users\DELL>cd ./.kube

C:\Users\DELL\.kube>dir
 驅動器 C 中的卷是 OS
 卷的序列號是 DA63-CEC0

 C:\Users\DELL\.kube 的目錄

2020/07/02  12:28    <DIR>          .
2020/07/02  12:28    <DIR>          ..
2020/07/02  12:20             1,082 ca.crt
2020/07/02  12:28    <DIR>          cache
2020/07/02  12:21             1,120 client.crt
2020/07/02  12:22             1,704 client.key
2020/07/02  12:22               433 config
2020/07/02  12:28    <DIR>          http-cache
               4 個文件          4,339 字節
               4 個目錄 71,635,701,760 可用字節

C:\Users\DELL\.kube>kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.0", GitCommit:"9e991415386e4cf155a24b1da15becaa390438d8", GitTreeState:"clean", BuildDate:"2020-03-25T14:58:59Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:43:34Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}

C:\Users\DELL\.kube>

在windows的控制檯中輸入kubectl version輸出了minikube的相關信息,說明kubectl在windows下是正確安裝且配置正確了的

驗證spirng-cloud-kubernetes的java程序

下載demo並導入到idea

官方的測試程序kubernetes-hello-world-example 總是會報一些奇奇怪怪的問題,想運行官方示例的話感覺有點麻煩

這裏我用的另一位大佬的demo,以驗證java程序能否正常訪問k8s,可直接參考文章:http://www.mydlq.club/article/33/

將這個大佬的demo代碼下載下來後導入到idea中運行即可,其demo地址爲:https://github.com/my-dlq/blog-example/tree/master/springcloud/springcloud-kubernetes/springcloud-kubernetes-discovery-demo
demo代碼導入到idea中

執行接口驗證環境

項目啓動好後在瀏覽器中訪問下面的接口:

http://localhost:8080/service

返回了:

[“kubernetes”,“kube-dns”]

其結果與控制檯的結果一致

C:\Users\DELL\.kube>kubectl get svc -A
NAMESPACE     NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
default       kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP                  96m
kube-system   kube-dns     ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   96m

C:\Users\DELL\.kube>

項目驗證結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章