2021了,還不會Kubernetes嗎系列文章(四)——實戰篇

1.項目倉庫

k8s4-1

2.添加一個節點

  • 增長一個 node2 的節點

2 佈署 MSYQL

2.1 設置污點

  • Node2 節點機器只用於部署 MySQL 服務
  • 能夠給節點加污點,只用來佈署 MySQL 服務
  • node1 增長 webserver 的污點
  • node2 增長 mysql 的污點
kubectl taint nodes node1 webserver:NoSchedule
kubectl taint nodes node2 mysql=true:NoSchedule
複製代碼

2.2 建立數據目錄

  • 在本地建立 MYSQL 數據文件夾而後掛載進MySQL容器
  • 以方便 MySQL 數據能夠持久化
  • 在 node2 上建立 mysql 數據文件夾
  • 此文件夾要爲,否則啓動 MYSQL 會失敗
mkdir /var/lib/mysql
複製代碼
  • 將 root 密碼存入 secret 內保存前端

    kubectl create secret generic mysql-auth --from-literal=username=root  --from-literal=password=root
    複製代碼

vi deployment-cicd-mysql.yamlnode

apiVersion: apps/v1
kind: Deployment
metadata:
  name: cicd-mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cicd-mysql
  template:
    metadata:
      labels:
        app: cicd-mysql
    spec:
      tolerations:
      - key: "mysql"
        operator: "Equal"
        value: "true"
        effect: "NoSchedule"
      containers:
      - name: cicd-mysql
        image: mysql:5.7
        imagePullPolicy: IfNotPresent
        args:
        - "--ignore-db-dir=lost+found"
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysql-data
          mountPath: "/var/lib/mysql"
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-auth
              key: password
      volumes:
      - name: mysql-data
        hostPath:
          path: /var/lib/mysql
          type: Directory
[root@master project]# kubectl apply -f deployment-cicd-mysql.yaml
deployment.apps/cicd-mysql created

//查看容器內的日誌 方便查看報錯
kubectl get pods
kubectl describe pods cicd-mysql-bcb77c759-bdrd8
kubectl logs cicd-mysql-6cbd4f95-g64hh
複製代碼

vi service-cicd-mysql.yamlmysql

apiVersion: v1
kind: Service
metadata:
  name: service-cicd-mysql
spec:
  selector:
    app: cicd-mysql
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306
  type: NodePort
複製代碼
  • 讓配置文件生效c++

    kubectl apply -f service-cicd-mysql.yaml
    複製代碼
  • 鏈接數據庫初始化數據git

  • -h 爲任意節點的公網或內網 IPweb

mysql -h172.31.178.169 -P32636 -uroot -proot
mysql -h118.190.156.138 -P32636 -uroot -proot
create database cicd;
use cicd;
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `name` varchar(255) NOT NULL COMMENT '姓名',
  `age` int(11) NOT NULL COMMENT '年齡',
  `sex` varchar(255) NOT NULL COMMENT '性別;1男 2女',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
複製代碼

3 佈署後端

3.1 新建 jenkins 項目

  • cicd-backend
  • 設置 git 源碼地址
  • 配置 git 私鑰
  • 配置 DOCKER_LOGIN_USERNAME 和 DOCKER_LOGIN_PASSWORD

3.2 添加構建佈署

#!/bin/bash
time=$(date "+%Y%m%d%H%M%S")
npm install --registry=https://registry.npm.taobao.org
docker build -t 115.28.139.92:8082/cicd-backend:$time .
docker login -u $DOCKER_LOGIN_USERNAME -p $DOCKER_LOGIN_PASSWORD 115.28.139.92:8082
docker push 115.28.139.92:8082/cicd-backend:$time
複製代碼

3.3 配置信息

3.3.1 數據庫地址

vi mysql.config.yamlsql

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
data:
  host: "service-cicd-mysql"
  port: "3306"
  database: "cicd"
kubectl apply -f  mysql.config.yaml
複製代碼

3.3.2 數據庫帳號

vi mysql-auth.yamldocker

apiVersion: v1
kind: Secret
metadata:
  name: mysql-auth
stringData:
  username: root
  password: root
type: Opaque
kubectl apply -f  mysql.config.yaml
複製代碼

3.3.3 私有倉庫認證

kubectl create secret docker-registry private-registry \
--docker-username=admin \
--docker-password=admin123 \
--docker-email=admin@example.org \
--docker-server=115.28.139.92:8082
複製代碼

3.3.4 後臺 Deployment

vi cicd-backend.yamlshell

apiVersion: apps/v1
kind: Deployment
metadata:
  name: cicd-backend
spec:
  selector:
    matchLabels:
      app: cicd-backend
  replicas: 1
  template:
    metadata:
      labels:
        app: cicd-backend
    spec:
      imagePullSecrets:
      - name: private-registry
      containers:
      - name: cicd-backend
        imagePullPolicy: Always
        image: "115.28.139.92:8082/cicd-backend:20210321202052"
        ports:
        - containerPort: 7001
        env:
        - name: MYSQL_HOST
          valueFrom:
            configMapKeyRef:
              name: mysql-config
              key: host
        - name: MYSQL_PORT
          valueFrom:
            configMapKeyRef:
              name: mysql-config
              key: port
        - name: MYSQL_DATABASE
          valueFrom:
            configMapKeyRef:
              name: mysql-config
              key: database
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
             name: mysql-auth
             key: username
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
             name: mysql-auth
             key: password
kubectl apply -f  cicd-backend.yaml
複製代碼

3.3.5 後臺 Service

  • vi service-cicd-backend.yaml
apiVersion: v1
kind: Service
metadata:
  name: service-cicd-backend
spec:
  selector:
    app: cicd-backend
  ports:
  - protocol: TCP
    port: 7001
    targetPort: 7001
  type: NodePort
kubectl apply -f  service-cicd-backend.yaml
curl http://172.31.178.169:31300/user/list
複製代碼

4 佈署前端

4.1 安裝編譯器

yum -y install gcc gcc-c++ kernel-devel
複製代碼

4.1 新建 jenkins 項目

  • cicd-frontend
  • 設置 git 源碼地址
  • 配置 git 私鑰
  • 配置 DOCKER_LOGIN_USERNAME 和 DOCKER_LOGIN_PASSWORD

4.1 配置構建步驟

#!/bin/sh -l
time=$(date "+%Y%m%d%H%M%S")
npm install --registry=https://registry.npm.taobao.org
npm run build
docker build -t 115.28.139.92:8082/cicd-frontend:$time .
docker login -u $DOCKER_LOGIN_USERNAME -p $DOCKER_LOGIN_PASSWORD 115.28.139.92:8082
docker push 115.28.139.92:8082/cicd-frontend:$time
複製代碼

4.2 配置構建步驟

vi cicd-frontend.yaml數據庫

apiVersion: apps/v1
kind: Deployment
metadata:
  name: cicd-frontend
spec:
  selector:
    matchLabels:
      app: cicd-frontend
  replicas: 1
  template:
    metadata:
      labels:
        app: cicd-frontend
    spec:
      imagePullSecrets:
      - name: private-registry
      containers:
      - name: cicd-frontend
        image: 115.28.139.92:8082/cicd-frontend:20210321204724
kubectl apply -f  cicd-frontend.yaml
複製代碼

vi service-cicd-frontend.yaml

apiVersion: v1
kind: Service
metadata:
  name: service-cicd-frontend
spec:
  selector:
    app: cicd-frontend
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: NodePort
kubectl apply -f  service-cicd-frontend.yaml
kubectl get svc
http://118.190.156.138:31753/
複製代碼

5.集成 jenkins

5.1 添加全局配置文件

  • 系統管理=>Managed files=>Add a new Config=>Custom file
  • Name 設置爲 k8s-config
  • 把 master 上的~/.kube/config拷貝到 Content 中

5.2 安裝 kubectl

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
        http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubectl
複製代碼

5.3 綁定配置文件

  • 打開項目配置
  • 選擇綁定=>Provide Configuration files=>Target 選擇k8s-config=>Target 輸入k8s-config.yaml

5.4 shell

  • 使用 kubectl set image 命令快速設置鏡像地址版本
  • 格式爲:kubectl set image deployment/[deployment 名稱] [容器名稱]=[鏡像版本]
#!/bin/bash
time=$(date "+%Y%m%d%H%M%S")
npm install --registry=https://registry.npm.taobao.org
docker build -t 115.28.139.92:8082/cicd-backend:$time .
docker login -u $DOCKER_LOGIN_USERNAME -p $DOCKER_LOGIN_PASSWORD 115.28.139.92:8082
docker push 115.28.139.92:8082/cicd-backend:$time
+kubectl --kubeconfig=k8s-config.yaml set image deployment/cicd-backend cicd-backend=115.28.139.92:8082/cicd-backend:$time
複製代碼

deployment.apps/cicd-backend image updated表示更新成功

6.推送觸發構建

6.1 安裝插件

  • publish over ssh(方便操做遠程的服務器)
  • gitee
  • Last Changes(可視化查看 git 文件變化)

6.2 構建觸發器

  • Gitee webhook 觸發構建,並記錄webhook URL地址
  • 生成 Gitee WebHook 密碼

6.3 配置 WebHooks

  • 打開項目的 WebHooks 管理頁面
  • 配置webhookURLWebHook 密碼

6.參考

  • 強行刪除 pod

    kubectl delete pod  cicd-mysql-84795bc9d7-fpjmp       --force --grace-period=0
    複製代碼
相關文章
相關標籤/搜索