kubernetes 应用快速入门

增删改查

使用kubectl run 来增加一个pod,使用nginx的镜像,开放80端口,副本数为 1

[root@master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-deploy created

查看

在第一次查看时,因为nginx镜像还需要时间拖拽下来,看到的是还在创建中

[root@master ~]# kubectl get pods
NAME                            READY   STATUS              RESTARTS   AGE
nginx-deploy-7689897d8d-hkl8n   0/1     ContainerCreating   0          8s

使用 -o wide 来查看详细信息

[root@master ~]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE     IP           NODE     NOMINATED NODE   READINESS GATES
nginx-deploy-7689897d8d-hkl8n   1/1     Running   0          2m37s   10.244.1.2   node01   <none>           <none>
当镜像拖拽完成后,pod就被创建成功了。

能看到详细信息中,该pod所对应的容器呗创建在node01 上,并且ip地址是:10.244.1.2,目前只能在Kubernetes节点上才能访问,跳过之外是不能访问的。

[root@master ~]# curl 10.244.1.2
<!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>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

service创建

事实上,上述的访问是不可取的,虽然可以访问,但容器的ip可能随时都会被修改或者其他情况导致该ip不可被访问。

此时就需要使用到 service 固定访问端点。使用 kubectl expose 来创建。

使用说明如下:

Usage:
  kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name]
[--name=name] [--external-ip=external-ip-of-service] [--type=type] [options]

注意: --target-port 表示容器上的端口,--name 表示service的名称,而 --port 表示service暴露的端口

--type=type 类型有: ClusterIP, NodePort, LoadBalancer, or ExternalName. Default is 'ClusterIP'.

下面开始创建:

[root@master ~]# kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP
service/nginx exposed

查看

[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.1.0.1      <none>        443/TCP   25h
nginx        ClusterIP   10.1.196.89   <none>        80/TCP    24s

这里的访问,直接访问刚刚创建的nginx service所对应的ip

[root@master ~]# curl 10.1.196.89
<!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>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

这里需要用到coreDNS,来解析对应的service,这样外部即可访问。要配置coreDNS的域名解析才可。

测试其他pod通过series访问nginx

在master上查看kube-dns 信息

[root@master ~]# kubectl get service -n kube-system
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.1.0.10    <none>        53/UDP,53/TCP,9153/TCP   25h

创建一个pod,并使用 -it 参数进入到容器中。

[root@master ~]# kubectl run -i -t busybox --image=busybox --restart=Never
If you don't see a command prompt, try pressing enter.
/ # 

查看 resolv.conf 文件中DNS

/ # cat /etc/resolv.conf
nameserver 10.1.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
/ # 

尝试解析nginx service

/ # nslookup nginx
Server:         10.1.0.10
Address:        10.1.0.10:53

** server can't find nginx.default.svc.cluster.local: NXDOMAIN

*** Can't find nginx.svc.cluster.local: No answer
*** Can't find nginx.cluster.local: No answer
*** Can't find nginx.default.svc.cluster.local: No answer
*** Can't find nginx.svc.cluster.local: No answer
*** Can't find nginx.cluster.local: No answer

/ # nslookup nginx.default.svc.cluster.local
Server:         10.1.0.10
Address:        10.1.0.10:53


*** Can't find nginx.default.svc.cluster.local: No answer

可以看到可以解析,尝试访问:

/ # wget -O - -q http://nginx
<!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>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
/ # 

可以直接在busybox中访问 刚刚创建的nginx,没问题。

测试手动变更nginx对应的pod的ip

手动删除一下nginx的pod,这样,ip就会变更,然后在busybox中使用相同的service端点看能否访问。

[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox 1/1 Running 0 2m12s 10.244.2.2 node02 <none> <none>
nginx-deploy-7689897d8d-hkl8n 1/1 Running 0 4m5s 10.244.1.2 node01 <none> <none>

[root@master ~]# kubectl delete pod nginx-deploy-7689897d8d-hkl8n
pod "nginx-deploy-7689897d8d-hkl8n" deleted
[root@master ~]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE     IP           NODE     NOMINATED NODE   READINESS GATES
busybox                         1/1     Running     0          5m28s   10.244.2.2   node02   <none>           <none>
nginx-deploy-7689897d8d-mdkhx   1/1     Running   0          24s     10.244.2.3   node02   <none>           <none>

可以看到,nginx-deploy 的ip已经变更,下面测试访问依然可以。

pod和service之间的关系

查看 nginx 的service 对应的详细信息;

[root@master ~]# wget -O - -q http://nginx
[root@master ~]# kubectl describe svc nginx
Name:              nginx
Namespace:         default
Labels:            run=nginx-deploy
Annotations:       <none>
Selector:          run=nginx-deploy
Type:              ClusterIP
IP:                10.1.196.89
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.2.3:80
Session Affinity:  None
Events:            <none>

查看pod标签

[root@master ~]#  kubectl get pods --show-labels
NAME                            READY   STATUS    RESTARTS   AGE     LABELS
busybox                         1/1     Running     0          7m56s   run=busybox
nginx-deploy-7689897d8d-mdkhx   1/1     Running   0          2m52s   pod-template-hash=7689897d8d,run=nginx-deploy

这里可以看出,因为nginx-deply的pod中,有nginx-deploy标签,而nginx的service的Selector选择的正式nginx-deploy标签,所以会被选中。

这就是pod和service之间的关系

service调度测试

创建一个两个副本的pod

[root@master ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/myapp created
[root@master ~]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE     IP           NODE     NOMINATED NODE   READINESS GATES
busybox                         0/1     Running     0          9m16s   10.244.2.2   node02   <none>           <none>
myapp-84cd4b7f95-45zrk          1/1     Running   0          24s     10.244.2.4   node02   <none>           <none>
myapp-84cd4b7f95-ws6qw          1/1     Running   0          24s     10.244.1.3   node01   <none>           <none>
nginx-deploy-7689897d8d-mdkhx   1/1     Running   0          4m12s   10.244.2.3   node02   <none>           <none>

在busybox中直接访问myapp

/ # wget -O - -q http://10.244.1.4
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
/ # wget -O - -q http://10.244.1.4/hostname.html
myapp-84cd4b7f95-ws6qw
/ # wget -O - -q http://10.244.2.3
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
/ # wget -O - -q http://10.244.2.3/hostname.html
myapp-84cd4b7f95-45zrk

创建myapp对应的service

posted @ 2020-02-20 20:28  星火撩原  阅读(197)  评论(0编辑  收藏  举报