一、集群创建pod
上节我们搭建好了两节点集群,这一节我们来创建pod,实现一个简单的Web示例。
1.在部署示例之前先拉取镜像
docker pull mysql:5.5.59
docker pull kubeguide/tomcat-app:v1

2.启动mysql服务
1)首先为mysql创建一个RC的yaml文件:mysql-rc.yaml

vim mysql-rc.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
      app: mysql
  template:
      metadata:
        labels:
          app: mysql
      spec:
        containers:
        - name: mysql
          image: mysql:5.5.59
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: "123456" 

2)创建好mysql-rc.yaml文件后,执行命令部署
执行命令:
kubectl create -f mysql-rc.yaml
查看状态:
kubectl get rc
kubectl get pods

3)docker ps 查看

4)创建Service服务

vim mysql-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql

5)部署并查看状态
执行命令:
kubectl create -f mysql-service.yaml
查看状态:
kubectl get svc

3.启动tomcat应用
1)创建RC文件myweb-rc.yaml

vim myweb-rc.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 2
  selector:
    app: myweb
  template:
   metadata:
     labels:
       app: myweb
   spec:
     containers:
       - name: myweb
         image: kubeguide/tomcat-app:v1
         ports:
         - containerPort: 8080

2)部署并查看状态
执行命令:
kubectl create -f myweb-rc.yaml
查看状态:
kubectl get pods

3)创建service

vim myweb-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 30001
  selector:
    app: myweb

4)部署并查看状态
执行命令:
kubectl create -f myweb-svc.yaml

查看状态:
kubectl get service

5)查看整体状态

[root@master01 tomcat]# kubectl get pods --all-namespaces -o wide
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE   IP                NODE       NOMINATED NODE   READINESS GATES
default                mysql-b5fgf                                  1/1     Running   0          99s   192.168.196.145   node01     <none>           <none>
default                myweb-8vjzt                                  1/1     Running   0          3m    192.168.196.143   node01     <none>           <none>
default                myweb-tvkf5                                  1/1     Running   0          3m    192.168.196.144   node01     <none>           <none>
kube-system            calico-kube-controllers-555fc8cc5c-b6pvw     1/1     Running   3          13d   192.168.241.80    master01   <none>           <none>
kube-system            calico-node-bhbrt                            1/1     Running   2          7d    192.168.1.113     node01     <none>           <none>
kube-system            calico-node-lgttw                            1/1     Running   2          7d    192.168.1.112     master01   <none>           <none>
kube-system            coredns-66bff467f8-bfkd7                     1/1     Running   5          13d   192.168.241.76    master01   <none>           <none>
kube-system            coredns-66bff467f8-qc6cw                     1/1     Running   5          13d   192.168.241.78    master01   <none>           <none>
kube-system            etcd-master01                                1/1     Running   60         13d   192.168.1.112     master01   <none>           <none>
kube-system            kube-apiserver-master01                      1/1     Running   61         13d   192.168.1.112     master01   <none>           <none>
kube-system            kube-controller-manager-master01             1/1     Running   5          13d   192.168.1.112     master01   <none>           <none>
kube-system            kube-proxy-dqtvt                             1/1     Running   4          13d   192.168.1.112     master01   <none>           <none>
kube-system            kube-proxy-t986s                             1/1     Running   2          13d   192.168.1.113     node01     <none>           <none>
kube-system            kube-scheduler-master01                      1/1     Running   5          13d   192.168.1.112     master01   <none>           <none>
kubernetes-dashboard   dashboard-metrics-scraper-6b4884c9d5-lb9tv   1/1     Running   2          7d    192.168.241.77    master01   <none>           <none>
kubernetes-dashboard   kubernetes-dashboard-5fd6f47c48-fk56r        1/1     Running   2          7d    192.168.241.79    master01   <none>           <none>

4.通过浏览器访问
打开浏览器,输入IP:30001/demo

192.168.1.112:30001/demo
在这里插入图片描述点击Add添加
在这里插入图片描述提交后查看结果:
在这里插入图片描述到这里一个基础的示例就演示完成,下一节我们部署dashboard。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐