Kubernetes:應用部署、應用瞭解、應用公佈、應用伸縮,-image=ikubernetes/myapp:v1

k8s基礎命令:https://kubernetes.io/zh/docs/reference/kubectl/kubectl-cmds/

k8s基礎知識:https://kubernetes.io/zh/docs/tutorials/kubernetes-basics/

一,部署應用

kubectl create deployment my-test-ngx --image=nginx
deployment.apps/my-test-ngx created

二,瞭解應用

查看deployment

kubectl get deployment
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
my-test-ngx   1/1     1            1           103s

 

get deployment -o wide
NAME          READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES   SELECTOR
my-test-ngx   1/1     1            1           2m17s   nginx        nginx    app=my-test-ngx

查看pod

kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
my-test-ngx-77d994d88-srhzm   1/1     Running   0          39s

 

kubectl get pod -o wide
NAME                          READY   STATUS    RESTARTS   AGE     IP           NODE                   NOMINATED NODE   READINESS GATES
my-test-ngx-77d994d88-srhzm   1/1     Running   0          2m54s   10.244.0.7   master.oopxiajun.com   <none>           <none>

利用這個IP我們訪問下

curl 10.244.0.7
<!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>

本機瀏覽器也是可以訪問

 

三,公佈應用

kubectl create service clusterip my-test-ngx --tcp=8001:80
service/my-test-ngx created

這裏注意:

1:my-test-ngx 這個名字需要與svc 的名字一樣。

2:--tcp=8001:80 這裏8001是公佈對外的端口,80是容器內部端口

我看看svc詳細信息

kubectl describe svc/my-test-ngx
Name:              my-test-ngx
Namespace:         default
Labels:            app=my-test-ngx
Annotations:       <none>
Selector:          app=my-test-ngx
Type:              ClusterIP
IP:                10.105.139.80
Port:              8001-80  8001/TCP
TargetPort:        80/TCP
Endpoints:         10.244.0.7:80
Session Affinity:  None
Events:            <none>

可以看到IP:                10.105.139.80

curl 10.105.139.80:8001
<!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>

可以訪問到這個IP:port對應的容器信息

刪掉pod,看看有什麼不一樣。

#看看pod名
kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
my-test-ngx-77d994d88-srhzm   1/1     Running   0          28m
#刪掉pod
[root@master xiajun]# kubectl delete pod my-test-ngx-77d994d88-srhzm 
pod "my-test-ngx-77d994d88-srhzm" deleted

#再次看看pod
[root@master xiajun]# kubectl get pod
NAME                          READY   STATUS              RESTARTS   AGE
my-test-ngx-77d994d88-vsrwf   0/1     ContainerCreating   0          14s

#發現pod名字變了

pod刪掉後,又建了一個,pod名字變了,不僅僅是pod名字變了,容器的ip也有變化

kubectl describe svc/my-test-ngx
Name:              my-test-ngx
Namespace:         default
Labels:            app=my-test-ngx
Annotations:       <none>
Selector:          app=my-test-ngx
Type:              ClusterIP
IP:                10.105.139.80
Port:              8001-80  8001/TCP
TargetPort:        80/TCP
Endpoints:         10.244.0.8:80
Session Affinity:  None
Events:            <none>

前面我們看到:Endpoints:         10.244.0.7:80

這是我們看到:Endpoints:         10.244.0.8:80 

Name:              my-test-ngx
Namespace:         default
Labels:            app=my-test-ngx
Annotations:       <none>
Selector:          app=my-test-ngx
Type:              ClusterIP
IP:                10.105.139.80
Port:              8001-80  8001/TCP
TargetPort:        80/TCP
Endpoints:         10.244.0.14:80
Session Affinity:  None
Events:            <none>

但是IP:10.105.139.80 是沒變的。所以pod即使是被幹掉了,或者某個node的掛掉了,甚至是關機、重啓宿主機,我們也一樣是可以通過這個ip:port訪問的。

curl 10.105.139.80:8001
<!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>

很遺憾:這個地址也只能本機訪問,用域名也是無法訪問的。

curl my-test-ngx
curl: (6) Could not resolve host: my-test-ngx

用k8s本地的域名也是不行的。

curl my-test-ngx.default.svc.cluster.local.
curl: (7) Failed to connect to my-test-ngx.default.svc.cluster.local port 80: Connection refused

那我們怎麼才能讓應用公佈到外網呢?

這裏有一個叫kube-dns的svc,

kubectl get svc -n kube-system
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   30h

說明k8s有個dns服務器,其ip地址爲10.96.0.10

那麼我們是否可以將作爲我們宿主機的dns服務器呢?我們來試試。

cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain oopxiajun.com
nameserver 192.168.134.2

這兒我們看到了我們nameserver是192.168.134.2

我們改下這個nameserver

vim /etc/resolv.conf
# Generated by NetworkManager
search localdomain oopxiajun.com
nameserver 10.96.0.10

然後訪問

curl my-test-ngx.default.svc.cluster.local.
<!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>

瀏覽器中也可以了

這樣還是不夠的哦。

觀察下這個域名 my-test-ngx.default.svc.cluster.local

my-test-ngx:是service的名稱,

default:是命名空間

svc.cluster.local:是k8s固定的dns-domain格式

我們要將這個域名改爲我們自己定義的就需要在 kubeadm init 是 加入我們的參數  --service-dns-domain 

如果不加這個 --service-dns-domain  ,那麼默認的dns-domain就是svc.cluster.local.

這個點我們後面再來詳細研究。

四,伸縮引用

我們這兒舉例 就用 k8s 提供的一個鏡像 ikubernetes/myapp 

#部署
kubectl create deployment myapp-test --image=ikubernetes/myapp:v1
deployment.apps/myapp-test created

#公佈節點
kubectl create service clusterip  myapp-test --tcp=80:80 
service/myapp-test created

#訪問
[root@master xiajun]# curl myapp-test.default.svc.cluster.local.
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

#hostname.html 中就是 pod的名稱
curl myapp-test.default.svc.cluster.local./hostname.html
myapp-test-cc8865788-6s9xh

kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
my-test-ngx-77d994d88-f99lf   1/1     Running   0          36m
myapp-test-cc8865788-6s9xh    1/1     Running   0          2m16s

擴容

kubectl scale --replicas=3 deployment myapp-test
deployment.apps/myapp-test scaled

訪問

kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
my-test-ngx-77d994d88-f99lf   1/1     Running   0          43m
myapp-test-cc8865788-6s9xh    1/1     Running   0          9m2s
myapp-test-cc8865788-dw6nc    1/1     Running   0          33s
myapp-test-cc8865788-xw2mw    1/1     Running   0          33s
[root@master xiajun]# curl myapp-test.default.svc.cluster.local./hostname.html
myapp-test-cc8865788-6s9xh
[root@master xiajun]# curl myapp-test.default.svc.cluster.local./hostname.html
myapp-test-cc8865788-6s9xh
[root@master xiajun]# curl myapp-test.default.svc.cluster.local./hostname.html
myapp-test-cc8865788-6s9xh
[root@master xiajun]# curl myapp-test.default.svc.cluster.local./hostname.html
myapp-test-cc8865788-xw2mw
[root@master xiajun]# curl myapp-test.default.svc.cluster.local./hostname.html
myapp-test-cc8865788-dw6nc
[root@master xiajun]# curl myapp-test.default.svc.cluster.local./hostname.html
myapp-test-cc8865788-6s9xh
[root@master xiajun]# curl myapp-test.default.svc.cluster.local./hostname.html
myapp-test-cc8865788-dw6nc
[root@master xiajun]# curl myapp-test.default.svc.cluster.local./hostname.html
myapp-test-cc8865788-xw2mw
[root@master xiajun]# curl myapp-test.default.svc.cluster.local./hostname.html
myapp-test-cc8865788-6s9xh
[root@master xiajun]# curl myapp-test.default.svc.cluster.local./hostname.html
myapp-test-cc8865788-dw6nc

可以看到是隨機訪問了pod

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章