简介
通过Apisix反向代理Gitlab HTTPS请求,确保Gitlab能够安全使用
环境与版本
K8S: 1.24
Apisix: 2.15
Cert Manager: 1.10
Gitlab: 15.4.3-ee
参考资料
GitLab SSL配置官方说明: https://docs.gitlab.cn/omnibus/settings/ssl.html
Apisix TCP/UDP动态代理: https://apisix.apache.org/zh/docs/apisix/stream-proxy/
ApisixRoute 使用说明: https://apisix.apache.org/zh/docs/ingress-controller/concepts/apisix_route/
ApisixRoute 字段说明: https://apisix.apache.org/zh/docs/ingress-controller/references/apisix_route_v2/
需要注意的坑
如果用K8S部署的Gitlab、Apisix,并且添加了探针,此时需要注意删除探针配置,会导致Gitlab Pod一直判断启动失败报错。
Warning Unhealthy 14s (x3 over 74s) kubelet Startup probe failed: Get "https://10.244.0.46:80/": http: server gave HTTP response to HTTPS client
因为Gitlab虽然配置HTTPS的URL,但是请求走的还是HTTP,此时探针使用HTTP、HTTPS请求都会判断验证失败,导致探针一直判断启动失败,不断重启Pods。暂时没找到很好的解决方法暂时不管了。
Gitlab、Apisix部署
参考资料: K8S部署Gitlab
参考资料: K8S部署Apisix
参考资料: Apisix配置HTTPS,使用Cert Manager管理ACME免费证书
其中Apisix部署有点不一样多了2个参数,主要是为了进行Gitlab SSH端口转发
# `gateway.stream.enabled=true` 开启TCP/UDP 动态代理
# `gateway.stream.tcp={2222}` 开启tcp监听端口2222,用于后续转发到gitlab 2222端口
helm install --set gateway.tls.enabled=true --set gateway.http.containerPort=80 --set gateway.tls.containerPort=443 --set gateway.stream.enabled=true --set gateway.stream.tcp={2222} --set global.storageClass=nfs-local-k8s-2 --set admin.allow.ipList={0.0.0.0/0} --create-namespace --namespace apisix apisix ./apisix
Gitlab配置HTTPS域名
修改gitlab.rb配置文件,这里的修改主要是为了clone的展示效果,不会有实际HTTPS证书产生,避免Apisix域名转发和Gitlab显示不一致。
# 进入gitlab容器内
kubectl -n gitlab exec -it gitlab-ee-0 bash
# 修改配置文件
vi /etc/gitlab/gitlab.rb
...
external_url 'https://gitlab.ljdzsk.com' # http改成https,注意这里只是对外显示地址改为了https
nginx['listen_port'] = 80 # gitlab nginx对外暴露端口,
nginx['listen_https'] = false # 关闭https,配置之后使用的还是http协议,避免gitlab因为使用https自动访问Let’s Encrypt创建证书,因为是内网会导致创建失败gitlab无法启动。
gitlab_rails['gitlab_shell_ssh_port'] = 2222 # gitlab ssh对外暴露端口,这里只是git clone ssh命令的显示端口,容器内用的还是22
...
# 重启gitlab加载配置
gitlab-ctl reconfigure
配置Apisix Https并转发到Gitlab Web端口
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: gitlab
namespace: gitlab
annotations:
# 私有证书
#cert-manager.io/cluster-issuer: "selfsigned-cluster-issuer"
# cluster-issuer 使用这个注解
cert-manager.io/cluster-issuer: "letsencrypt-amce-cluster-issuer"
# issuer 使用这个注解
#cert-manager.io/issuer: "letsencrypt-amce-cluster-issuer"
# 重定向
k8s.apisix.apache.org/http-to-https: "true"
spec:
ingressClassName: apisix
tls:
- hosts:
- gitlab.ljdzsk.com # placing a host in the TLS config will determine what ends up in the cert's subjectAltNames
secretName: gitlab-tls # cert-manager will store the created certificate in this secret.
rules:
- host: "gitlab.ljdzsk.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: gitlab-ee-svc
port:
number: 80
配置Apisix TCP并转发到Gitlab ssh端口
Ingress只会提供HTTP、HTTPS的配置,Apisix TCP就要用到ApisixRoute
# 获取gitlab svc的ip、ssh端口,我这里是gitlab-ee-svc:22
kubectl -n gitlab get svc
# 创建部署文件
cat > gitlab_tcp_ssh_route.yaml <<EOF
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: gitlab-ssh-route
namespace: gitlab
spec:
stream:
- name: gitlab-ssh-route
protocol: TCP
match:
ingressPort: 2222
backend:
serviceName: gitlab-ee-svc
servicePort: 22
EOF
# 部署
kubectl apply -f gitlab_tcp_ssh_route.yaml
# 编辑apisix hostport配置添加端口: 2222
kubectl -n apisix edit deployments.apps apisix
...
- name: proxy-tcp-0
hostPort: 2222
containerPort: 2222
protocol: TCP
...