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

部署一个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

[root@k8s-node1 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命令行

[root@k8s-node1 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值