首页 > 编程语言 > 详细

spring-cloud-kubernetes开发环境搭建

时间:2020-11-13 14:49:57      阅读:71      评论:0      收藏:0      [点我收藏+]

参考的网址:https://puhaiyang.blog.csdn.net/article/details/107082958

服务端k8s环境:

提前准备

在安装好了centos后,再进行如下的操作

关闭防火墙

systemctl stop firewalld && systemctl disable firewalld

关闭虚拟内存

swapoff -a

1、安装docker

# step 1: 安装必要的一些系统工具

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

# Step 2: 添加软件源信息

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# Step 3: 更新并安装 Docker-CE

sudo yum makecache fast

sudo yum -y install docker-ce

# Step 4: 开启Docker服务

sudo service docker start

 

2、安装kubectl

先用浏览器访问 https://storage.googleapis.com/kubernetes-release/release/stable.txt

这个地址,就会显示最新稳定版的版本号。这里的得到的是v1.15.1

使用那个版本号下载kubectl:

wget "https://storage.googleapis.com/kubernetes-release/release/v1.15.1/bin/linux/amd64/kubectl" -O "/usr/local/bin/kubectl" 

或者你还是借助浏览器下载直接访问https://storage.googleapis.com/kubernetes-release/release/v1.15.1/bin/linux/amd64/kubectl这个地址,

在虚拟机好像覆盖不了,一般都是直接浏览器下载的。

cp kubectl /usr/local/bin/

chmod +x /usr/local/bin/kubectl

 

3、安装minikube

minikube官网地址:https://minikube.sigs.k8s.io/docs/start/

安装前提:

1)         2个或更多CPU

2)         2GB的可用内存

3)         20GB的可用磁盘空间

4)         网络连接

5)         容器或虚拟机管理器,例如:Docker,Hyperkit,Hyper-V,KVM,Parallels,Podman,VirtualBox或VMWare

为了方便快捷采用官方文档中的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 --memory=8192

 上述minikube启动命令中的--memory=8192是指定minikube的内存,最小2048,按实际情况修改。不加好像启动报错。

如果启动过程中有提示:

X Sorry, Kubernetes v1.18.0 requires conntrack to be installed in root’s path

那么先把conntrack安装一下:

yum install conntrack

然后再次运行上面那个minikube start的命令以启动minikube。注意:服务器要2核。

[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 ~]# 

  

kubectl get nodes

 

[root@kube20 yaml]# kubectl get node
NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   8d    v1.15.0
[root@kube20 yaml]# 

  

开发机器windows环境准备

1、安装kubectl

kubectl安装方法的参考官网链接:

https://kubernetes.io/docs/tasks/tools/install-kubectl/

下载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"}

  

2、拷贝kube-config

拷贝minikube虚拟机上的/root/.kube 目录下的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下,文件路径按config中地址找。 最终我这边的kube-config文件内容如下:

注意写路径的地方,按windows实际路径修改。

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

执行接口验证环境

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

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>

  

 

spring-cloud-kubernetes开发环境搭建

原文:https://www.cnblogs.com/nuomm/p/13968887.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!