Kubernetes(K8S)配置域名訪問 Ingress Controller (Traefik)

搭建的一個最新比較熱門的小遊戲合成西瓜html

須要安裝好k8s集羣以及docker,若是還沒部署k8s能夠看以下文檔:我是用的k8s版本爲1.16
http://www.javashuo.com/article/p-dkdetoal-ve.htmlnode

由於要將製做的鏡像上傳harbor因此沒安裝harbor也能夠參考:
https://blog.51cto.com/13555423/2621233
將合成西瓜代碼下載下來,連接1爲github地址,連接2爲下載地址:
https://github.com/bullhe4d/bigwatermelon
https://github.com/bullhe4d/bigwatermelon/archive/main.zip
注:1.20版本k8s個別api已經棄用若是你的版本爲1.20那將沒法部署成功nginx

先yum安裝一個nginx,做爲反向代理,在不加端口的狀況下訪問git

cat <<EOF >  /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF
yum install nginx

將下載好的代碼放到bigwatermelon目錄下解壓github

mkdir bigwatermelon
cd bigwatermelon
unzip bigwatermelon-main.zip
rm -rf bigwatermelon-main.zip

下載好後建立個目錄寫一個Dockerfile文件,我是用的nginx做爲基礎鏡像web

vi Dockerfile
FROM nginx
COPY ./* /usr/share/nginx/html/
WORKDIR /usr/share/nginx/html/
RUN chown -R daemon:daemon /usr/share/nginx/html/ &&\
chmod -R 755 /usr/share/nginx/html/

用docker build製做鏡像docker

docker build -t nginx-test:v4 .

製做好鏡像後上傳到harbor上面方便node節點進行pulljson

鏡像作好後用traefik做爲負載均衡:vim

vim nginx-app.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-demo
  labels:
    app: test-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test-demo
  template:
    metadata:
      labels:
        app: test-demo
    spec:
      containers:
        - name: test-demo-app
          image: nginx-test:v4
          imagePullPolicy: Never
          ports:
            - containerPort: 80
          resources:
            requests:
              cpu: "1000m"
              memory: "1Gi"
            limits:
              cpu: "2000m"
              memory: "2Gi"
---
apiVersion: v1
kind: Service
metadata:
  name: test-demo-service
spec:
  selector:
    app: test-demo
#  type: NodePort
  ports:
    - name: web
      port: 80
      protocol: TCP
**kubectl apply -f nginx-app.yaml**

啓動後查看服務狀態centos

kubectl get pods -o wide

建立路由

vim traefik-Route.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: ingre***outes.traefik.containo.us

spec:
  group: traefik.containo.us
  version: v1alpha1
  names:
    kind: Ingre***oute
    plural: ingre***outes
    singular: ingre***oute
  scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: middlewares.traefik.containo.us

spec:
  group: traefik.containo.us
  version: v1alpha1
  names:
    kind: Middleware
    plural: middlewares
    singular: middleware
  scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: ingre***outetcps.traefik.containo.us

spec:
  group: traefik.containo.us
  version: v1alpha1
  names:
    kind: Ingre***outeTCP
    plural: ingre***outetcps
    singular: ingre***outetcp
  scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: ingre***outeudps.traefik.containo.us

spec:
  group: traefik.containo.us
  version: v1alpha1
  names:
    kind: Ingre***outeUDP
    plural: ingre***outeudps
    singular: ingre***outeudp
  scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: tlsoptions.traefik.containo.us

spec:
  group: traefik.containo.us
  version: v1alpha1
  names:
    kind: TLSOption
    plural: tlsoptions
    singular: tlsoption
  scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: tlsstores.traefik.containo.us

spec:
  group: traefik.containo.us
  version: v1alpha1
  names:
    kind: TLSStore
    plural: tlsstores
    singular: tlsstore
  scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: traefikservices.traefik.containo.us

spec:
  group: traefik.containo.us
  version: v1alpha1
  names:
    kind: TraefikService
    plural: traefikservices
    singular: traefikservice
  scope: Namespaced

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: serverstransports.traefik.containo.us

spec:
  group: traefik.containo.us
  version: v1alpha1
  names:
    kind: ServersTransport
    plural: serverstransports
    singular: serverstransport
  scope: Namespaced

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: traefik-ingress-controller

rules:
  - apiGroups:
      - ""
    resources:
      - services
      - endpoints
      - secrets
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - extensions
      - networking.k8s.io
    resources:
      - ingresses
      - ingressclasses
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - extensions
    resources:
      - ingresses/status
    verbs:
      - update
  - apiGroups:
      - traefik.containo.us
    resources:
      - middlewares
      - ingre***outes
      - traefikservices
      - ingre***outetcps
      - ingre***outeudps
      - tlsoptions
      - tlsstores
      - serverstransports
    verbs:
      - get
      - list
      - watch

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: traefik-ingress-controller

roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: traefik-ingress-controller
subjects:
  - kind: ServiceAccount
    name: traefik-ingress-controller
    namespace: default
kubectl apply -f traefik-Route.yaml

建立service

vim traefik-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: traefik

spec:
  type: NodePort
  ports:
    - protocol: TCP
      name: web
      port: 8000
    - protocol: TCP
      name: admin
      port: 8080
    - protocol: TCP
      name: websecure
      port: 4443
  selector:
    app: traefik
kubectl apply -f traefik-svc.yaml

建立delployment

vim traefik-delployment.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  namespace: default
  name: traefik-ingress-controller

---
kind: Deployment
apiVersion: apps/v1
metadata:
  namespace: default
  name: traefik
  labels:
    app: traefik

spec:
  replicas: 1
  selector:
    matchLabels:
      app: traefik
  template:
    metadata:
      labels:
        app: traefik
    spec:
      serviceAccountName: traefik-ingress-controller
      containers:
        - name: traefik
          image: traefik:v2.4
          args:
            - --api.insecure
            - --accesslog
            - --entrypoints.web.Address=:8000
            - --entrypoints.websecure.Address=:4443
            - --providers.kubernetescrd
            - --certificatesresolvers.myresolver.acme.tlschallenge
            - --certificatesresolvers.myresolver.acme.email=foo@you.com
            - --certificatesresolvers.myresolver.acme.storage=acme.json
            # Please note that this is the staging Let's Encrypt server.
            # Once you get things working, you should remove that whole line altogether.
            - --certificatesresolvers.myresolver.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory
          ports:
            - name: web
              containerPort: 8000
            - name: websecure
              containerPort: 4443
            - name: admin
              containerPort: 8080
kubectl apply -f traefik-delployment.yaml

traefik搭建完畢,查看pod是否運行,以及服務

kubectl get pods -o wide

kubectl get svc -o wide

看到8080映射的端口也就是traefik的端口,IP加端口便可看到traefik管理界面

將域名和service服務關聯

vim traefik-host.yaml
apiVersion: traefik.containo.us/v1alpha1
kind: Ingre***oute
metadata:
  name: test-demo-service-ingress-route
  namespace: default
spec:
  entryPoints:
    - web
  routes:
    - match: Host(`test.bigwatermelon.com`)
      kind: Rule
      services:
        - name: test-demo-service
          port: 80
kubectl apply -f traefik-host.yaml

使用nginx代理ingress-controller的8000端口,127.0.0.1:後面的端口是8000經過nodeport映射到本地的端口

vi /etc/nginx/conf.d/test-bitwater.conf
server {
        listen 80;
        server_name test.bigwatermelon.com;

        location / {
                proxy_pass      http://127.0.0.1:31472;
                proxy_set_header Host $host;
        }
}
systemctl start nginx
nginx -s reload

在 /etc/hosts中添加
192.168.254.1 test.bigwatermelon.com

由於是內網地址因此想要訪問還須要在hosts中添加
C:\Windows\System32\drivers\etc\hosts
192.168.254.1 test.bigwatermelon.com
最後訪問域名展現以下:

Kubernetes(K8S)配置域名訪問 Ingress Controller (Traefik)

相關文章
相關標籤/搜索