Spiga

k8s生产部署(十一):netcore应用部署

2021-04-02 19:15:08

1、打包镜像

docker build -t zq-apigateway:1.0.0 ./zq-apigateway/.

2、Helm Create

在master节点上创建一个文件夹,如deploy

cd deploy
helm create zq-apigateway

将生成的文件下载到本地进行编辑

3、编辑deployment.yaml

spec/template/spec/containers,imagePullPolicy后

      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: ASPNETCORE_HOSTINGSTARTUPASSEMBLIES
              valueFrom:
                configMapKeyRef:
                  name: zq-env-config
                  key: skywalking_hostingStartup
            - name: SkyWalking__Transport__gRPC__Servers
              valueFrom:
                configMapKeyRef:
                  name: zq-env-config
                  key: skywalking_servers
            - name: exceptionless__ServerUrl
              valueFrom:
                configMapKeyRef:
                  name: zq-env-config
                  key: exceptionless_serverUrl
            - name: ASPNETCORE_USE_Forwarded_Headers
              value: "true"
            - name: ASPNETCORE_USE_PathBase
              value: "true"
          volumeMounts:
            #替换appsetting.json,每个版本保存一个配置
            - mountPath: "/app/appsettings.json"
              name: appsettings
              subPath: appsettings-{{.Chart.AppVersion}}.json
            #要映射的目录
            - name: logs
              mountPath: /app/logs
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
            - name: http2
              containerPort: 81
              protocol: TCP
            - name: https
              containerPort: 443
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /live
              port: http
          readinessProbe:
            httpGet:
              path: /ready
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      volumes:
        - name: appsettings
          configMap:
              name: zq-apigateway-config
        - name: logs
          hostPath:
              path: "/nfs/k8s/zq-apigateway/logs"
              type: DirectoryOrCreate

3、在service.yaml中放开网络访问端口

apiVersion: v1
kind: Service
metadata:
  name: {{ include "zq-apigateway.fullname" . }}
  labels:
    {{- include "zq-apigateway.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
    - port: 81
      targetPort: http2
      protocol: TCP
      name: http2
    - port: 443
      targetPort: https
      protocol: TCP
      name: https
  selector:
    {{- include "zq-apigateway.selectorLabels" . | nindent 4 }}

5、修改版本号

编辑Chart.yaml

version: 1.0.0
appVersion: 1.0.0

6、修改values.yaml

修改镜像名称

  repository: harbor.qhfinance.com/library/zq-apigateway
  # 拉取策略:IfNotPresent: 本地有则使用本地镜像,不拉取; Always:总是拉去; Never: 只使用本地镜像,从不拉取
  pullPolicy: IfNotPresent   

关闭角色创建

serviceAccount:
  # 指定是否应该创建服务帐户
  create: false
  # 要使用的服务帐户的名称
  # 如果不设置and create为true,则使用fullname模板生成名称
  name: ""

设置service网络为nodePort

service:
  type: NodePort   
  port: 80

7、执行

kubectl create configmap zq-env-config --from-env-file=env.txt  -n qjy-public -o yaml --dry-run | kubectl apply -f - 
kubectl create configmap zq-apigateway-config --from-file=zq-apigateway-configs  -n qjy-public -o yaml --dry-run | kubectl apply -f -

#创建
helm install qjy-zq-apigateway ./charts/zq-apigateway -n qjy-public

#更新
helm upgrade qjy-zq-apigateway ./charts/zq-apigateway -n qjy-public

#删除
helm uninstall qjy-zq-sys-api -n qjy-public

附录

  1. env.txt文件内容
skywalking_hostingStartup=SkyAPM.Agent.AspNetCore
skywalking_servers=skywalking-oap.qjy-apm.svc.cluster.local:11800
exceptionless_serverUrl=http://exceptionless-api.qjy-ex.svc.cluster.local
  1. zq-apigateway-configs为文件夹,里面放置各个版本的配置文件,我们这里发布的1.0.0的版本,所以里面会有一个appsettings-1.0.0.json的配置文件。
  2. 需要一个ingress来访问
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: zq-apigateway-ingress
  namespace: qjy-public
  annotations:
    nginx.ingress.kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: "/"
spec:
  tls:
    - hosts:
        - k8s.qhfinance.com
      secretName: k8s-secret
  rules:
  - host: k8s.qhfinance.com
    http:
      paths:
      - path: /
        backend:
          serviceName: qjy-zq-apigateway
          servicePort: 80
kubectl apply -f zq-apigateway-ingress.yaml -n qjy-public
  1. 配置https证书

在腾讯云申请证书后执行

kubectl create secret tls k8s-secret -n qjy-public --key k8s.qhfinance.com.key --cert k8s.qhfinance.com.pem