原创

k8s学习三:创建一个nginx服务

温馨提示:
本文最后更新于 2022年09月08日,已超过 568 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

部署nginx服务

创建nginx服务:

root@test02:/home/tioncico# kubectl create deployment nginx  --image=nginx:1.14-alpine
deployment.apps/nginx created
root@test02:/home/tioncico#

查看nginx pod状态:

root@test02:/home/tioncico# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7cbb8cd5d8-w9tn2   0/1     Pending   0          67s
root@test02:/home/tioncico#

可以发现nginx属于pending状态,说明发生了异常

部署服务出错排查

查看pod详情:

root@test02:/home/tioncico# kubectl describe pod nginx
Name:           nginx-7cbb8cd5d8-w9tn2
Namespace:      default
Priority:       0
Node:           <none>
Labels:         app=nginx
                pod-template-hash=7cbb8cd5d8
Annotations:    <none>
Status:         Pending
IP:             
IPs:            <none>
Controlled By:  ReplicaSet/nginx-7cbb8cd5d8
Containers:
  nginx:
    Image:        nginx:1.14-alpine
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-fzjmq (ro)
Conditions:
  Type           Status
  PodScheduled   False 
Volumes:
  kube-api-access-fzjmq:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason            Age                  From               Message
  ----     ------            ----                 ----               -------
  Warning  FailedScheduling  25s (x7 over 7m21s)  default-scheduler  0/1 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate.

可以看到最后的是 FailedScheduling  , 原因是 0/1 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate.

这个是因为当创建单机版的 k8s 时,这个时候 master 节点是默认不允许调度 pod 。

解决方案是增加节点或者把master标记为可调度即可:

kubectl taint nodes --all node-role.kubernetes.io/master-

再次查看pod状态:

root@test02:/home/tioncico# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7cbb8cd5d8-w9tn2   1/1     Running   0          11m
root@test02:/home/tioncico#

可看到已经可以运行

访问nginx

暴露nginx的端口:

 kubectl expose deploy nginx  --port=80 --target-port=80  --type=NodePort

查看pod当前的ip地址:

root@test02:/home/tioncico# kubectl get pod,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7cbb8cd5d8-w9tn2   1/1     Running   0          15m

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        17h
service/nginx        NodePort    10.96.119.144   <none>        80:32402/TCP   19s
root@test02:/home/tioncico#

这个时候,通过curl 127.0.0.1:32402 即可访问nginx,

root@test02:/home/tioncico# curl http://127.0.0.1:32402
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>

仙士可博客

正文到此结束
本文目录