欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

k8s学习(十三) 部署一个完整的应用

程序员文章站 2024-03-09 17:43:41
...

部署一个helloworld应用,引用mysql,使用健康检测、动态扩容、容器初始化等功能

1、helloworld-all.yaml文件:

---
 
apiVersion: extensions/v1beta1
 
kind: Deployment

metadata:
  name: mysql-deploy
  namespace: hello
  labels:
    app: mysql
spec:
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: 192.168.100.87:80/mysql-server:8.0.17
        ports:
        - containerPort: 3306
          name: dbport
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
#        - name: MYSQL_DATABASE
#          value: mysql
#        - name: MYSQL_USER
#          value: zqw
#        - name: MYSQL_PASSWORD
#          value: cc==%fQwy4i2
        volumeMounts:
        - name: db
          mountPath: /var/lib/mysql
      volumes: 
      - name: db
        hostPath:
          path: /var/lib/mysql2

---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: hello
spec:
  selector:
    app: mysql
  type: NodePort
  ports:
  - name: mysqlport
    protocol: TCP
    port: 3306
    nodePort: 32306
    targetPort: dbport

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: helloworld-all
  namespace: hello
  labels:
    app: helloworld-all
spec:
  revisionHistoryLimit: 10
  minReadySeconds: 5
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: helloworld-all
    spec:
      initContainers:
      - name: init-db
        image: 192.168.100.87:80/busybox
        command: ['sh', '-c', 'until nslookup mysql; do echo waiting for mysql service; sleep 2; done;']
      containers:
      - name: helloworld-all
        image: 192.168.100.87:80/helloworld:0.1.2
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          name: wdport
        env:
        - name: host
          value: mysql:3306
        - name: usr
          value: root
        - name: password
          value: "123456"
        livenessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 50
          periodSeconds: 3
        readinessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 50
          periodSeconds: 10
        resources:
          limits:
            cpu: 500m
            memory: 500Mi
          requests:
            cpu: 400m
            memory: 400Mi

---
apiVersion: v1
kind: Service
metadata:
  name: helloworld-all-service
  namespace: hello
spec:
  selector:
    app: helloworld-all
  type: NodePort
  ports:
  - name: helloworld-all-service-port
    protocol: TCP
    port: 8080
    nodePort: 32255
    targetPort: wdport

创建namespace hello

kubectl create namespace hello
kubectl apply -f helloword-all.yaml

2、修改mysql密码方式和允许远程访问

查看pod

[[email protected] k8s]# kubectl get pods -n hello
NAME                              READY   STATUS    RESTARTS   AGE
helloworld-all-59f85c5d7d-r6s5q   1/1     Running   0          4h53m
mysql-deploy-65b7878ff9-5sqk8     1/1     Running   0          4h46m

进入mysql命令行

[[email protected] k8s]# kubectl exec -it mysql-deploy-65b7878ff9-5sqk8    -n hello /bin/bash
bash-4.2# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.17 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

配置mysql

use mysql;
update user set host = '%' where user = 'root';
 ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'yourpassword';
flush privileges;

这样helloworld应用就可以访问到mysql啦,外部也可以通过暴露的service端口32306访问mysql啦

3、扩容

kubectl autoscale deployment helloworld-all --cpu-percent=10 --min=1 --max=10 -n hello