一个简单有效的kubernetes部署案例

时间:2022-07-14 14:50:06

  部署web-rc:web应用需要去获取redis注入的ip环境变量cluster_ip,以此来访问

[root@sdw1 files]# cat testweb-rc.yaml
kind: ReplicationController
metadata:
name: testweb
spec:
replicas: 1
selector:
app: testweb
template:
metadata:
labels:
app: testweb
spec:
containers:
- name: testweb
image: 10.10.18.19:5000/testweb #仓库镜像,或者其他本地镜像
ports:
- containerPort: 8080

  部署web-svc:注意label和selector对应

[root@sdw1 files]# cat testweb-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: testweb
spec:
type: NodePort
ports:
- port: 8080
nodePort: 31001
selector:
app: testweb

  部署redis-rc.yaml:

[root@sdw1 files]# cat redis-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: redis
spec:
replicas: 1
selector:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis
ports:
- containerPort: 6379

  部署redis-svc.yaml

[root@sdw1 files]# cat redis-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
type: NodePort
selector:
app: redis
clusterIP: 100.100.100.100
ports:
- name: "1"
port: 6379
protocol: TCP
targetPort: 6379
nodePort: 31009

  依次执行即可:

kubectl create -f testweb-rc.yaml
kubectl create -f testweb-svc.yaml
kubectl create -f redis-rc.yaml
kubectl create -f redis-svc.yaml

  此时查看pod应该都在正常运行了,web也可以访问redis.

  使用kubernetes前最好安装dashboard,看东西更方便,安装也很简单,也是创建三个资源:

  创建空间:

[root@sdw1 templates]# cat kube-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: kube-system

  创建rc:

[root@sdw1 templates]# cat kube-dashboard-rc.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
# Keep the name in sync with image version and
# gce/coreos/kube-manifests/addons/dashboard counterparts
name: kubernetes-dashboard-latest
namespace: kube-system
spec:
replicas: 1
template:
metadata:
labels:
k8s-app: kubernetes-dashboard
version: latest
kubernetes.io/cluster-service: "true"
spec:
containers:
- name: kubernetes-dashboard
image: docker.io/mritd/kubernetes-dashboard-amd64
resources:
# keep request = limit to keep this container in guaranteed class
limits:
cpu: 100m
memory: 50Mi
requests:
cpu: 100m
memory: 50Mi
ports:
- containerPort: 9090
args:
- --apiserver-host=http://master:8080
livenessProbe:
httpGet:
path: /
port: 9090
initialDelaySeconds: 30
timeoutSeconds: 30

  创建svc:

[root@sdw1 templates]# cat kube-dashboard-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: kubernetes-dashboard
namespace: kube-system
labels:
k8s-app: kubernetes-dashboard
kubernetes.io/cluster-service: "true"
spec:
selector:
k8s-app: kubernetes-dashboard
ports:
- port: 80
targetPort: 9090

  依次执行:

kubectl create -f kube-namespace.yaml
kubectl create -f kube-dashboard-rc.yaml
kubectl create -f kube-dashboard-svc.yaml

  完