Kubernetes1.18实践及踩坑(之四)部署一个(网页游戏)应用

 

1、编辑yml文件

  创建 game2048-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-game-deploy
  labels:
    app: game-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: game-pod
  template:
    metadata:
      labels:
        app: game-pod
    spec:
      containers:
      - name: game
        image: sdscello/2048:7
        ports:
        - containerPort: 80

---

kind: Service
apiVersion: v1
metadata:
  labels:
    app: game-svc
  name: my-game-svc
  namespace: default
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30180
  selector:
    app: game-pod

 注意port, targetPort, nodePort三个端口的区别,详见:https://blog.csdn.net/GuXiaoyan12/article/details/103751227

2、创建Pod和Service

[root@master01 app]# kubectl apply -f game2048-deploy.yaml

 中间下载Image, 需要一定时间。

 查看运行状态:

[root@master01 app]# kubectl get svc -A -o wide
NAMESPACE     NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE     SELECTOR
default       kubernetes       ClusterIP   10.10.0.1               443/TCP                  2d20h   
default       my-game-svc      NodePort    10.10.109.81            80:30180/TCP             12m     app=game-pod
kube-system   kube-dns         ClusterIP   10.10.0.10              53/UDP,53/TCP,9153/TCP   2d20h   k8s-app=kube-dns
[root@master01 app]# kubectl get pod -A -o wide
NAMESPACE     NAME                                          READY   STATUS    RESTARTS   AGE     IP                NODE                  NOMINATED NODE   READINESS GATES
default       my-game-deploy-698d6cd68f-77zz9               1/1     Running   0          13m     10.122.123.99     node01.huiyan.com                
default       my-game-deploy-698d6cd68f-pgrwm               1/1     Running   0          13m     10.122.123.100    node01.huiyan.com                
default       my-game-deploy-698d6cd68f-z7fjq               1/1     Running   0          13m     10.122.123.98     node01.huiyan.com                >
kube-system   calico-kube-controllers-555fc8cc5c-tf895      1/1     Running   3          2d20h   10.122.153.202    master01.huiyan.com              
kube-system   calico-node-8r2q9                             1/1     Running   3          2d20h   192.168.100.226   node01.huiyan.com                
kube-system   calico-node-ldq4m                             1/1     Running   3          2d20h   192.168.100.206   master01.huiyan.com              
kube-system   coredns-7ff77c879f-9lqhx                      1/1     Running   3          2d20h   10.122.153.204    master01.huiyan.com              
kube-system   coredns-7ff77c879f-gxbvv                      1/1     Running   3          2d20h   10.122.153.203    master01.huiyan.com              
kube-system   etcd-master01.huiyan.com                      1/1     Running   3          2d20h   192.168.100.206   master01.huiyan.com              
kube-system   kube-apiserver-master01.huiyan.com            1/1     Running   0          65m     192.168.100.206   master01.huiyan.com              
kube-system   kube-controller-manager-master01.huiyan.com   1/1     Running   0          64m     192.168.100.206   master01.huiyan.com              
kube-system   kube-proxy-r5kxx                              1/1     Running   0          63m     192.168.100.206   master01.huiyan.com              
kube-system   kube-proxy-sp8hm                              1/1     Running   1          64m     192.168.100.226   node01.huiyan.com                
kube-system   kube-scheduler-master01.huiyan.com            1/1     Running   0          64m     192.168.100.206   master01.huiyan.com              
[root@master01 app]# 

可以看到在节点node01上运行了三个实例。

可通过三种 地址+端口 访问:

ClusterIP + port : 10.10.109.81 : 80

PodIP + targetPort 10.122.123.98/99/100 : 80

NodeIP + NodePort 192.168.100.226:30180

前两种只能在集群内使用,后一种在集群外也可以作用。

访问页面展示如下:

Kubernetes1.18实践及踩坑(之四)部署一个(网页游戏)应用_第1张图片

3、在master结点上部署应用

从前面的输出可以看到,所有实例(Pod)都运行在node01节点上。因为默认master结点上是不部署Pod的。如果想充分利用master节点资源,可以执行:

[root@master01 app]# kubectl taint nodes --all node-role.kubernetes.io/master-
node/master01.huiyan.com untainted

然后修改game2048-deploy.yaml,将replicas 字段从3改成5,意是是增加两个实例。

[root@master01 app]# kubectl get pod -A -o wide
NAMESPACE     NAME                                          READY   STATUS    RESTARTS   AGE     IP                NODE                  NOMINATED NODE   READINESS GATES
default       my-game-deploy-698d6cd68f-77zz9               1/1     Running   0          3h37m   10.122.123.99     node01.huiyan.com                
default       my-game-deploy-698d6cd68f-cd4gw               1/1     Running   0          9m23s   10.122.153.211    master01.huiyan.com              
default       my-game-deploy-698d6cd68f-pgrwm               1/1     Running   0          3h37m   10.122.123.100    node01.huiyan.com                
default       my-game-deploy-698d6cd68f-t2wwg               1/1     Running   0          9m23s   10.122.153.212    master01.huiyan.com              
default       my-game-deploy-698d6cd68f-z7fjq               1/1     Running   0          3h37m   10.122.123.98     node01.huiyan.com                

可以看到,有两个pod部署到了master01节点。

 

你可能感兴趣的:(k8s,云计算,DevOps)