k8s harbor helm Ingress部署

644 阅读2分钟

1.harbor

下载与安装

# 下载
wget https://ghproxy.com/https://github.com/goharbor/harbor/releases/download/v2.5.3/harbor-offline-installer-v2.5.3.tgz
# 解压
tar xvf harbor-offline-installer-v2.5.3.tgz  -C /home/ && cd /home/harbor/

添加秘钥

# 创建秘钥和数据文件夹
mkdir -p /home/harbor/certs /home/harbor/data
# 生成秘钥
openssl req -newkey rsa:4096 -nodes -sha256 -keyout /home/harbor/certs/harbor.key -x509 -out /home/harbor/certs/harbor.crt -subj /C=CN/ST=BJ/L=BJ/O=DEVOPS/CN=harbor.jason.com -days 3650

修改配置

# 复制配置
cp /home/harbor/harbor.yml.tmpl /home/harbor/harbor.yml
# 修改配置
vi /home/harbor/harbor.yml

hostname = harbor.jason.com

把 
  certificate: /your/certificate/path
  private_key: /your/private/key/path
改成
  certificate: /home/harbor/certs/harbor.crt
  private_key: /home/harbor/certs/harbor.key

把  
data_volume: /data
改成
data_volume: /home/harbor/data

# 防止端口冲突
443 改成 4438
80 改成 7888


安装

./install.sh 

访问 端口 4438

https://服务器ip地址

修改 harbor.yml 调整账号
admin
Harbor12345  


创建项目 project
创建用户 test 并设置为管理员

测试docker上传

1. 修改docker 配置

添加 insecure-registries 里面对应的域名或ip

  "insecure-registries":[
    "harbor.jason.com"
  ],

也可以配置ip+端口

2.docker push

docker login https://harbor.jason.com
Username: test
Password: 
 
# 域名推送 
 docker tag nginx harbor.jason.com:444/project/nginx
 docker push harbor.jason.com:444/project/nginx
# 也可以使用ip
docker tag nginx 171.35.40.74:444/test/nginx
docker push 171.35.40.74:444/test/nginx


2.helm

helm等同于一个yml的全局包管理工具,同时支持滚动更新

1.部署

# 下载 安装
https://get.helm.sh/helm-v3.11.1-linux-amd64.tar.gz
tar zxvf helm-v3.2.1-linux-amd64.tar.gz
mv linux-amd64/helm /usr/bin/
# 设置
helm repo add stable http://mirror.azure.cn/kubernetes/chartshelm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts helm repo update
# 删除
helm repo remove aliyun

2.使用

# 搜索与安装
helm search repo weave
helm install ui stable/weave-scope
helm install my-nginx bitnami/nginx --version 10.2.1 
# 查看
helm list
helm status ui
# 修改配置 暴露端口 把 type:ClusterIP 改成 type:NodePort,保存后立马生效
kubectl edit svc ui-weave-scope
# 查看效果
kubectl get svc

3.chart

创建 设置

# 创建
 helm create testnginx
 rm -rf testnginx/templates/*

vi values.yaml
# 添加 name 属性
name: app-nginx

添加deployment

touch testnginx/templates/deploy.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
  name: {{ .Values.name  }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: web-nginx
          image: {{ .Values.image.repository  }}
          ports:
            - name: http
              containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
  name: svc-nginx
spec:
  ports:
    - name: http
      port: 80
      targetPort: 80
  selector:
    app: nginx
  type: NodePort

安装 + 运行

helm install test-nginx testnginx/

helm upgrade升级 release

# 修改配置
vi  testnginx/values.yaml
# 修改版本号
repository: nginx:alpine
# 启动更新
helm upgrade test-nginx testnginx/

回滚

# 查看
 helm history test-nginx
# 回滚指定版本
helm rollback test-nginx 1
# 卸载版本
helm uninstall test-nginx

3.Ingress

Ingress 主要解决使用ip+端口 对外访问主机复杂的问题

传统NodePort对外暴露方式

使用Service进行暴露的缺点

  • 所有部署的node都只能需要使用ip+端口暴露应用
  • 实际场景一般是不同域名对应不同应用

Ingress与其他概念关系逻辑

  • Ingress对应不同的域名
  • 不同域名对应不同service
  • service在对多个pod

使用

Ingress Controller

ingress-app.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-app-ingress2
spec:
  rules:
  - host: www.test.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx
          servicePort: 80

命令

# 下载 Ingress Controller
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.25.0/deploy/static/mandatory.yaml
# 启动
kubectl apply -f mandatory.yaml
kubectl get pod -n ingress-nginx -o wide
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get svc
# 启动服务
kubectl apply -f  ingress-app.yaml
kubectl get ingress -o wide
# 配置域名
vim /etc/hosts
192.168.0.74 www.test.com
curl www.test.com:31391