spring-cloud-kubernetes之开发环境搭建

相关文章目录

  • spring-cloud-kubernetes之开发环境搭建
  • spring cloud项目改造为spring-cloud-kubernetes项目
  • 使用istio对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之开发环境搭建_第1张图片
看上上面的官方说明后,我想对于spring-cloud-kubernetes本地调试应该如何解决我们应该也有了答案。在开发环境中安装一个不占用太多资源的minikube就行了

这里我以目前常见的windows开发环境为例,演示一下如何搭建一个spring-cloud-kubernetes的本地调试环境

minikube安装

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

minikube也可以直接在windows中进行安装,但是windows部分版本不支持,这里为了通用且不破坏物理机环境,建议采用直接安装在虚拟机中

先提前安装好vm,并安装好一个linux,我这里安装的是centos,此部分比较简单就不在这里进行记录了
spring-cloud-kubernetes之开发环境搭建_第2张图片

提前准备

在安装好了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            443/TCP                  2m32s
kube-system   kube-dns     ClusterIP   10.96.0.10           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              .
2020/07/02  12:28              ..
2020/07/02  12:20             1,082 ca.crt
2020/07/02  12:28              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              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
spring-cloud-kubernetes之开发环境搭建_第3张图片

执行接口验证环境

项目启动好后在浏览器中访问下面的接口:

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            443/TCP                  96m
kube-system   kube-dns     ClusterIP   10.96.0.10           53/UDP,53/TCP,9153/TCP   96m

C:\Users\DELL\.kube>

spring-cloud-kubernetes之开发环境搭建_第4张图片

你可能感兴趣的:(javaweb入门学习,spring,cloud,kubernetes,minikube,本地调试,虚拟机minikube)