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