前提:已安装好k8s集群
1.安装harbor
先在安装harbor的节点上安装docker-compose:curl -L "https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose (使用的daocloud的源),然后执行:chmod +x /usr/local/bin/docker-compose 即可,使用:docker-compose--version 进行检查。
下载harbor最新稳定的包:https://github.com/goharbor/harbor/releases/download/v2.3.3/harbor-offline-installer-v2.3.3.tgz ,在服务器上解压:tar zxvf harbor-offline-installerv2.3.3.tgz,进入harbor目录,复制一个harbor.yml文件,进行修改,将hostname修改为了服务器的ip(目前单节点先测通,用最简单的方式),然后将https相关内容注释调,然后便可执行install.sh,相当于拉取了一些镜像,创建了一些容器,最终启动了harbor。
因为没有使用https,所以还要修改/etc/docker/daemon.json,增加一个配置 "insecure-registries": ["192.168.56.100"](harbor服务器ip),然后重启docker(这个操作在之后k8s的工作节点也要执行!!),还要重启harbor,在harbor目录下执行:docker-compose up。 此时可访问服务器的80端口,进入harbor页面,默认账号:admin,密码:Harbor2345。 点击左侧菜单中的用户管理,新增一个用户。然后点击左侧的项目,可新建一个镜像仓库。
2.编写dockerfile,创建镜像
一个简单的springboot项目,打成jar(demo01.jar),上传到服务器,编写一个dockerfile(demo01-dockerfile):
FROM openjdk:8
ADD demo01.jar demo01.jar
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /demo01.jar" ]
然后创建镜像:docker build -t 192.168.56.100/zzn/demo01:v1 -f demo01-dockerfile .(tag名直接写harbor服务器ip地址,一步到位),然后在当前节点登录harbor:docker login 192.168.56.100 -u 用户名 -p 密码 ,最后将镜像推往harbor:docker push 192.168.56.100/zzn/demo01:v1
3.k8s部署服务
创建一个k8s的Secrets:
kubectl create secret docker-registry docker-harbor01 \
--docker-server=192.168.56.100 \
--docker-username=huyao \
--docker-password=Harbor12345 \
--docker-email=huyao@qq.com
这一步不做,docker login了也没用。最后就是编写k8s的yaml(demo01.yaml):
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-demo01
spec:
replicas: 3
selector:
matchLabels:
app: demo01
template:
metadata:
labels:
app: demo01
spec:
containers:
- name: demo01
image: 192.168.56.100/zzn/demo01:v1
imagePullSecrets:
- name: docker-harbor01
imagePullSecrets指定的是刚刚创建的secret 。然后执行yaml:kubectl apply -f demo01.yaml。检查:kubectl get pods -owide,发现3个pod都是running的状态了。可以创建service:kubectl expose deployment deploy-demo01 --port=微服务端口 --type=NodePort,然后使用:kubectl get svc,deploy -owide 进行查看NodePort的端口,访问各工作节点的指定端口看接口是否可正常访问
------------------------------------------------------------------------------------------------------------------------------------
部署网关和两个微服务,使用ingress对外暴露
1.编写三个springcloud微服务,一个gateway,一个订单,一个支付,订单通过feign访问支付,三个服务注册到宿主机的nacos上(对于k8s,我是初学者,但根据个人经验,我倾向于将有状态的服务还是部署在物理机上,如mysql,redis,nacos等)
2.为每个写dockerfile,生成镜像,上传至harbor
3.为每一个微服务编写k8s需要的yaml,订单的order.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-order
spec:
selector:
matchLabels:
app: order
template:
metadata:
labels:
app: order
spec:
containers:
- name: order
image: 192.168.56.100/zzn/k8s-order:v1
imagePullSecrets:
- name: docker-harbor01
支付的yaml基本和订单一致,关于网关的yaml,因为要向外暴露,并要对接ingress,所以还需要一个service,配置如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-gateway
spec:
selector:
matchLabels:
app: gateway
template:
metadata:
labels:
app: gateway
spec:
containers:
- name: gateway
image: 192.168.56.100/zzn/k8s-gateway:v1
imagePullSecrets:
- name: docker-harbor01
---
apiVersion: v1
kind: Service
metadata:
labels:
app: gateway-service
name: gateway-service
spec:
selector:
app: gateway
ports:
- port: 7001
protocol: TCP
targetPort: 6001
type: ClusterIP
注意:service配置下的selector对应的是pod的label!
4.应用三个微服务的yaml,生成deploy和网关的service
5.安装ingress:
wgethttps://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.47.0/deploy/static/provider/baremetal/deploy.yaml
#修改镜像: vi deploy.yaml #将image的值改为如下值:registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/ingress-nginx-controller:v0.46.0
#应用配置,安装ingress:kubectl apply -f deploy.yaml
# 检查安装的结果:kubectl get pod,svc -n ingress-nginx
6.编写针对网关的ingress的yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-host-bar
spec:
ingressClassName: nginx
rules:
- host: "hello.xiaoer.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: gateway-service
port:
number: 6001
查看ingress暴露的对应端口:kubectl get svc -n ingress-nginx
输出:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.96.25.30 <none> 80:32522/TCP,443:30615/TCP 25h
ingress-nginx-controller-admission ClusterIP 10.96.95.21 <none> 443/TCP 25h
7.测试,本机在host文件中添加网关的ingress中配置的域名,指向几个工作节点,在本机浏览器访问:http://hello.xiaoer.com:32522/order/test/pay 看是否正常返回数据
参考:https://www.yuque.com/leifengyang/oncloud/ctiwgo#gSYVF