在kubernetes集群中部署應(yīng)用黍特,可以使用kubectl run的方式
kubectl run nginx-pod --image=nginx:1.14.2
可以通過kubectl create deployment的方式創(chuàng)建
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
或者是采用執(zhí)行yaml文件的方式馒吴,yaml的格式為:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
執(zhí)行Kubectl apply的命令
[root@k8s-master k8s]# kubectl apply -f nginx.yaml
deployment.apps/nginx-deployment created
查看nginx-deployment
[root@k8s-master k8s]# kubectl describe deployment nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Fri, 17 Jul 2020 11:04:38 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.14.2
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-6b474476c4 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 29m deployment-controller Scaled up replica set nginx-deployment-6b474476c4 to 2
在master上查看pod
[root@k8s-master k8s]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-6b474476c4-nzzkj 1/1 Running 0 33m 10.244.58.196 k8s-node02 <none> <none>
nginx-deployment-6b474476c4-vhwqs 1/1 Running 0 33m 10.244.85.193 k8s-node01 <none> <none>
可以通過curl對(duì)應(yīng)的IP地址湾碎,查看目前的nginx已經(jīng)工作正常
[root@k8s-master k8s]# curl 10.244.58.196
<!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 >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
問題
現(xiàn)在可以再master節(jié)點(diǎn)和pod之間通過curl訪問缘回,使用的都是內(nèi)部的地址陷嘴,那么如何將這些服務(wù)暴露到kubernetes集群外面呢每聪?
可以通過kubectl expose的命令將這個(gè)deployment expose成為service练般,可以被外部訪問。
[root@k8s-master k8s]# kubectl expose deployment/nginx-deployment --type="NodePort" --port 80
service/nginx-deployment exposed
查看一下service
[root@k8s-master k8s]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 38h
nginx-deployment NodePort 10.1.223.127 <none> 80:30965/TCP 12s
此時(shí)可以在master上通過curl來訪問127.0.0.1上的nginx服務(wù)了
root@k8s-master k8s]# curl 127.0.0.1:30965
<!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 >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
再打開一個(gè)終端服爷,并不鏈接到虛擬機(jī)杜恰,直接訪問master的bridge網(wǎng)絡(luò)的IP地址+expose的端口,也可以訪問仍源。
JackZhangs-MacBook-Pro:~ jackzhang$ curl 192.168.31.95:30965
<!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 >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
通過本機(jī)的瀏覽器直接訪問也可以心褐,如果使用了代理需要設(shè)置不對(duì)此地址段使用代理
下一個(gè)問題
現(xiàn)在我們是有兩個(gè)POD提供nginx的服務(wù),那么他們之間的負(fù)載均衡是怎么做的呢笼踩?
是否需要什么特殊的設(shè)置呢逗爹?
是否從哪里可以看到每次訪問是哪個(gè)pod提供的服務(wù)呢?