Kubernetes部署Nginx/Tomcat
[root@master tomcat]# kubectl get nodes
NAME STATUS AGE
node1 Ready 10d
node2 Ready 10d
部署nginx
需要三个配置文件:
[root@master nginx]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
[root@master nginx]# vim nginx-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-rc
spec:
replicas: 2
selector:
name: nginx-pod
template:
metadata:
labels:
name: nginx-pod
spec:
containers:
- name: nginx-pod
image: nginx
ports:
- containerPort: 80
[root@master nginx]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
[root@master nginx]#kubectl create -f nginx-pod.yaml
[root@master nginx]#kubectl describe pod nginx
[root@master nginx]#kubectl create -f nginx-rc.yaml
[root@master nginx]#kubectl create -f nginx-service.yaml
[root@master nginx]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-pod 1/1 Running 1 1d 10.255.95.2 node2
nginx-rc-351xd 1/1 Running 1 1d 10.255.1.3 node1
[root@master nginx]# kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 10d
nginx-service 10.254.208.231 <nodes> 80:30011/TCP 1d
[root@master nginx]# kubectl get rc
NAME DESIRED CURRENT READY AGE
nginx-rc 2 2 2 1d
访问nginx:
http://192.168.91.26:30011
http://192.168.91.27:30011
部署tomcat
[root@master tomcat]# vim tomcat-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-tomcat
spec:
replicas: 2
template:
metadata:
labels:
app: my-tomcat
spec:
containers:
- name: my-tomcat
image: docker.io/tomcat
ports:
- containerPort: 80
[root@master tomcat]# vim tomcat-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-tomcat
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
nodePort: 31111
selector:
app: my-tomcat
[root@master tomcat]# kubectl create -f tomcat-deployment.yaml
[root@master tomcat]# kubectl create -f tomcat-service.yaml
[root@master tomcat]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
my-tomcat 2 2 2 2 59m
[root@master tomcat]# kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 10d
my-tomcat 10.254.110.240 <nodes> 8080:31111/TCP 59m
nginx-service 10.254.208.231 <nodes> 80:30011/TCP 1d
[root@master tomcat]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
my-tomcat-464109745-ctmdf 1/1 Running 0 1h 10.255.1.5 node1
my-tomcat-464109745-wmxpz 1/1 Running 0 1h 10.255.95.4 node2
nginx-pod 1/1 Running 1 1d 10.255.95.2 node2
nginx-rc-351xd 1/1 Running 1 1d 10.255.1.3 node1
访问tomcat
http://192.168.91.26:31111
http://192.168.91.27:31111