Kubernetes 系列6 asp.net core web api部署

前面已经部署了mysql pod, 这篇继续部署asp.net core web api的pod,并使用web api容器来连接mysql的容器获取数据。

 一. 构建镜像

  创建asp.net core web api项目(k8swebapi)

  定义web api接口,连接mysql,获取数据,测试正确

  vs 发布k8swebapi到文件夹webapipublish中

  创建dockerfile文件放入文件夹webapipublish中

  上传到云服务器(腾讯测试服务器)opt目录下,构建镜像,创建容器,api接口测试,如下所示,  也可参考:系列13 docker asp.net core部署

[root@k8s-host opt]# ls
containerd  kubectl-1.17.3-0.x86_64.rpm  rh  webapipublish
[root@k8s-host opt]# cd webapipublish
[root@k8s-host webapipublish]# docker build -t k8swebapi .
Sending build context to Docker daemon  17.98MB
Step 1/5 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
 ---> e7e3b238011c
Step 2/5 : WORKDIR /app
 ---> Running in 5294fa7329ca
Removing intermediate container 5294fa7329ca
 ---> 8721b62c02e8
Step 3/5 : EXPOSE 5000
 ---> Running in 77fb37914852
Removing intermediate container 77fb37914852
 ---> af5e3169d8c4
Step 4/5 : COPY . .
 ---> bee42661a7ad
Step 5/5 : ENTRYPOINT ["dotnet", "k8swebapi.dll", "--urls", "http://*:5000;http://*:5001"]
 ---> Running in 7b458b5a74c7
Removing intermediate container 7b458b5a74c7
 ---> e10cf664f9e6
Successfully built e10cf664f9e6
Successfully tagged k8swebapi:latest
[root@k8s-host webapipublish]#  docker run -d --rm -P  --name  k8swebapi  k8swebapi
0c1883c687364826dd420dfbebd29f13172e949c22535e71eabfac75c2ac44e2
[root@k8s-host webapipublish]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
0c1883c68736        k8swebapi           "dotnet k8swebapi.dl…"   12 seconds ago      Up 10 seconds       0.0.0.0:32776->5000/tcp   k8swebapi
fd7b5462fb65        ubuntu              "/bin/bash"              8 days ago          Up 8 days                                     db2
9fbad9a78ac8        ubuntu              "/bin/bash"              8 days ago          Up 8 days                                     dbdata
3ddee94cef3a        ubuntu:latest       "/bin/bash"              2 weeks ago         Up 2 weeks                                    quizzical_nash
[root@k8s-host webapipublish]# curl http://localhost:32776/api/v1/user/IndexList
[{"host":"%","user":"root"},{"host":"localhost","user":"mysql.infoschema"},{"host":"localhost","user":"mysql.session"},{"host":"localhost","user":"mysql.sys"},{"host":"localhost","user":"root"}][root@k8s-host webapipublish]# 

  测试成功,如上所示,测试服务器腾讯云已连接到华为生产服务器mysql pod,并获取到了user表数据。

二. 上传镜像

  当在测试服务器上,验证了镜像没有问题,需要上传到docker hub仓库中,没有账号的先创建账号(上传到docker hub好慢,最好自建镜像仓库,或国内的镜像仓库会好些)

[root@k8s-host webapipublish]# docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@k8s-host webapipublish]# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
k8swebapi                              latest              e10cf664f9e6        11 minutes ago      278MB
ubuntu                                 latest              1d622ef86b13        3 weeks ago         73.9MB
hello-world                            latest              bf756fb1ae65        4 months ago        13.3kB
hello-world                            latest              bf756fb1ae65        4 months ago        13.3kB
mcr.microsoft.com/dotnet/core/aspnet   2.2                 e7e3b238011c        4 months ago        261MB
[root@k8s-host webapipublish]# docker tag k8swebapi hushaoren/k8swebapi
[root@k8s-host webapipublish]# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
hushaoren/k8swebapi                    latest              e10cf664f9e6        12 minutes ago      278MB
k8swebapi                              latest              e10cf664f9e6        12 minutes ago      278MB
ubuntu                                 latest              1d622ef86b13        3 weeks ago         73.9MB
hello-world                            latest              bf756fb1ae65        4 months ago        13.3kB
hello-world                            latest              bf756fb1ae65        4 months ago        13.3kB
mcr.microsoft.com/dotnet/core/aspnet   2.2                 e7e3b238011c        4 months ago        261MB
[root@k8s-host webapipublish]# docker push hushaoren/k8swebapi
The push refers to repository [docker.io/hushaoren/k8swebapi]
5f7220a20951: Pushed 
aac3ea262576: Pushed 
579a8f1d6a12: Pushed 
15e45d99c926: Pushed 
0cf75cb98eb2: Pushed 
814c70fdae62: Pushed 
latest: digest: sha256:50db92dd729acddb446046053f113d171c36980da2f64243c48603b913ae0408 size: 1581
[root@k8s-host webapipublish]# 

 三. 创建webapi pod

  上面的构建镜像和上传镜像都是在腾讯云测试服务器上操作,下面在华为云服务器上操作: 创建k8swebapi-rc.yaml文件,放入华为云服务器的opt目录下,文件内容如下所示:

apiVersion: v1
kind: ReplicationController
metadata:
  name: k8swebapi
  labels:
    name: k8swebapi
spec:
  replicas: 2
  selector:
    name: k8swebapi
  template:
    metadata:
      labels: 
        name: k8swebapi
    spec:
      containers:
      - name: k8swebapi
        image: hushaoren/k8swebapi
        imagePullPolicy: Always
        ports:
        - containerPort: 5000
      tolerations:
      - key: "node-role.kubernetes.io/master"
        operator: "Equal"
        value: ""
        effect: "NoSchedule"
     

  下面代码步骤:

    1.登录:  docker login

    2.创建pod:    kubectl create -f k8swebapi-rc.yaml

    3.查看所有pod运行状态: kubectl get pod

    4.查看rc类型资源:kubectl get rc -o wide

    5.k8swebapi创建了二个pod,查看其中的一个pod详细信息(pod的status不为Running时可排查错误):kubectl describe pod k8swebapi-cgcrq

[root@k8s-host opt]# docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@k8s-host opt]# kubectl create -f k8swebapi-rc.yaml
replicationcontroller/k8swebapi created
[root@k8s-host opt]# kubectl get pod
NAME              READY   STATUS    RESTARTS   AGE
k8swebapi-cgcrq   1/1     Running   0          14s
k8swebapi-wkv29   1/1     Running   0          14s
mysql-rc-4cvzg    1/1     Running   0          4d4h
[root@k8s-host opt]# kubectl get rc -o wide
NAME        DESIRED   CURRENT   READY   AGE    CONTAINERS   IMAGES                SELECTOR
k8swebapi   2         2         2       47s    k8swebapi    hushaoren/k8swebapi   name=k8swebapi
mysql-rc    1         1         1       4d4h   mysql        mysql                 name=mysql-pod
[root@k8s-host opt]# kubectl describe pod k8swebapi-cgcrq
Name:         k8swebapi-cgcrq
Namespace:    default
Priority:     0
Node:         k8s-host/192.168.0.108
Start Time:   Wed, 20 May 2020 19:10:53 +0800
Labels:       name=k8swebapi
Annotations:  <none>
Status:       Running
IP:           172.17.0.7
IPs:
  IP:           172.17.0.7
Controlled By:  ReplicationController/k8swebapi
Containers:
  k8swebapi:
    Container ID:   docker://5b2c66e16adbfadba78f995ff31264df72a06c53e9c1328bb3ded69152fbe2a3
    Image:          hushaoren/k8swebapi
    Image ID:       docker-pullable://hushaoren/k8swebapi@sha256:50db92dd729acddb446046053f113d171c36980da2f64243c48603b913ae0408
    Port:           5000/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 20 May 2020 19:10:56 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-fj942 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-fj942:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-fj942
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node-role.kubernetes.io/master:NoSchedule
                 node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <unknown>  default-scheduler  Successfully assigned default/k8swebapi-cgcrq to k8s-host
  Normal  Pulling    81s        kubelet, k8s-host  Pulling image "hushaoren/k8swebapi"
  Normal  Pulled     78s        kubelet, k8s-host  Successfully pulled image "hushaoren/k8swebapi"
  Normal  Created    78s        kubelet, k8s-host  Created container k8swebapi
  Normal  Started    78s        kubelet, k8s-host  Started container k8swebapi

  能过查看docker ps 能看到创建了二个容器(每个容器对应一个pod),二个pod还可以轻松实现做负载均衡,如下所示:

[root@k8s-host opt]# docker ps
CONTAINER ID        IMAGE                                                           COMMAND                  CREATED             STATUS              PORTS               NAMES
f4751221efe1        hushaoren/k8swebapi                                             "dotnet k8swebapi.dl…"   5 minutes ago       Up 5 minutes                            k8s_k8swebapi_k8swebapi-wkv29_default_6587ab29-ff84-4740-8e17-f14d70d15bcc_0
5b2c66e16adb        hushaoren/k8swebapi                                             "dotnet k8swebapi.dl…"   5 minutes ago       Up 5 minutes                            k8s_k8swebapi_k8swebapi-cgcrq_default_e3562704-2e26-4294-8703-ceabef03796f_0

四.创建k8swebapi 的服务

    创建k8swebapi-svc.yaml文件,放入华为云服务器的opt目录下,文件内容如下所示:

apiVersion: v1
kind: Service
metadata:
  name: k8swebapi
spec:
  type: NodePort
  ports:
    - port: 5000
      nodePort: 30001
  selector:
    name: k8swebapi

  使用kubectl来创建

[root@k8s-host opt]# kubectl create -f k8swebapi-svc.yaml
service/k8swebapi created
[root@k8s-host opt]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
k8swebapi    NodePort    10.111.125.88    <none>        5000:30001/TCP   7s
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          9d
mysql-svc    NodePort    10.109.109.170   <none>        3306:30000/TCP   4d4h

   在华为云服务器,放开30001端口。通过上面可以看到,内网其它k8s的pod就可以使用10.111.125.88:5000来访问k8swebapi接口, 外网可以通过 云公网ip+30001来访问k8swebapi接口,如下所示:

[root@k8s-host opt]# curl http://121.xx.xxx.39:30001/api/v1/user/IndexList
[{"host":"%","user":"root"},{"host":"localhost","user":"mysql.infoschema"},{"host":"localhost","user":"mysql.session"},{"host":"localhost","user":"mysql.sys"},{"host":"localhost","user":"root"}][root@k8s-host opt]# 
[root@k8s-host opt]# curl http://10.111.125.88:5000/api/v1/user/IndexList
[{"host":"%","user":"root"},{"host":"localhost","user":"mysql.infoschema"},{"host":"localhost","user":"mysql.session"},{"host":"localhost","user":"mysql.sys"},{"host":"localhost","user":"root"}][root@k8s-host opt]# 

  这里的连接mysql是走的外网(云公网ip+30000)。 也可以改成内网(云私有ip+3306),mysql连接就走局域网。

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "dbconn": "server=121.xx.xxx.39;database=mysql;port=30000;uid=root;pwd=mysql"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值