k8s || service 练习

练习:


1.编写一个yaml文件mysql.yaml,启动一个MySQL的pod,创建一个服务service-MySQL,发布MySQL
  标准的方式,启动pod

2.编写一个yaml文件nginx.yaml,使用deployment控制器,启动一个nginx的pod,3个副本,然后创建一个服务service-nginx,发布nginx集群
  使用deployment控制器启动pod


[root@k8smaster service]# cat mysql.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: mysql
  labels:
    app.sc.io/name: scproxy
spec:
  containers:
  - name: mysql
    image: mysql:5.7.41
    imagePullPolicy: IfNotPresent
    ports:
      - containerPort: 3306
        name: mysql-service
    env:
      - name: MYSQL_ROOT_PASSWORD
        value: "123456"
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  type: NodePort
  selector:
    app.sc.io/name: scproxy
  ports:
  - name: name-of-service-port
    protocol: TCP
    port: 3306
    targetPort: mysql-service
    nodePort: 30001

[root@k8smaster service]# kubectl apply -f mysql.yaml 
pod/mysql created
service/mysql-service created

[root@k8smaster service]# kubectl get svc
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP          5d21h
mysql-service   NodePort    10.96.255.209   <none>        3306:30001/TCP   95s

[root@k8smaster service]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
mysql   1/1     Running   0          5s

[root@k8smaster service]# kubectl exec -it mysql -- 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 2
Server version: 5.7.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> 

[root@k8smaster service]# vim nginx-deployment.yaml 
[root@k8smaster service]# cat nginx-deployment.yaml 
# 定义nginx的Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
        - containerPort: 80
---
#定义nginx的Service
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-svc
  name: nginx-svc
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30002

[root@k8smaster service]# kubectl apply -f nginx-deployment.yaml 
deployment.apps/nginx unchanged
service/nginx-svc created

[root@k8smaster service]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
mysql                    1/1     Running   0          6m29s
nginx-7848d4b86f-49hxm   1/1     Running   0          35s
nginx-7848d4b86f-ph28r   1/1     Running   0          35s
nginx-7848d4b86f-qhfxn   1/1     Running   0          35s

[root@k8smaster service]# kubectl get svc
NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP          5d21h
mysql-service   NodePort    10.96.255.209    <none>        3306:30001/TCP   7m8s
nginx-svc       NodePort    10.103.195.224   <none>        80:30002/TCP     44s

访问:

http://192.168.102.136:30002/

http://192.168.102.137:30002/

http://192.168.102.138:30002/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩未零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值